David Mark wrote:
> Garrett Smith wrote:
>> David Mark wrote:
>>> Thomas 'PointedEars' Lahn wrote:
>>>> David Mark wrote:
>>>>
>>>>> Garrett Smith wrote:
>>>>>> Thomas 'PointedEars' Lahn wrote:
>>>>>>> var reFeaturedMethod = new RegExp('^(function|object)$', 'i
>> [...]
>>
>>>> But AISB the `!!' does not really save anything as in a boolean
>>>> context the return value would be subject to type conversion anyway.
>>> That's true. But I prefer to have the function return booleans only.
>>>
>> Having gthe function return boolean provides clear expectations to the
>> caller. WIth an "is" method, the caller should be able to expect a
>> boolean value.
>>
>> This expectation could be clearly defined by a unit test. I might write
>> it like this:
>>
>> "testIsHostMethod - contains" : function(){
>> var actualResult = isHostMethod(document.body.contains);
>> Assert.isTrue(actualResult);
>> }
>
> isHostMethod(document.body, 'contains')
>
Right.
> But I don't see that as a good unit test for this method as it will fail
> if that host method is missing. I would prefer to simply test that the
> result is a boolean.
>
>> That isHostMethod returning something other than false would end up
>> failing that test. By always returning boolean value, the expectation is
>> simpler.
>
> Perhaps I am reading your test wrong. Did you mean something like
> isBoolean (and returns something other than true/false?)
>
No, that makes sense, but it is not what I meant.
I was going for testing under known conditions, that the method does
what is expected it will do.
A "contains" method will be absent in some implementations, so no good
as test.
A valid test might be to set up a case where the result is known or
assumed to be true or false. For example, if the test case assumes that
the environment has a `document.getElementById` that is potentially
callable and that `document.title` would never be callable:
testIsHostMethodWithDomCoreMethod : function() {
Assert.isTrue(isHostMethod(document, "getElementById"));
}
testIsHostMethodWithNonCallableObject : function() {
Assert.isFalse(isHostMethod(document, "title"));
}
The expected outcome could also be dynamic. For example:
"testIsHostMethod - element.contains()" : function() {
// First determine if calling something either works or doesn't.
var expectedCallability = (function(){
try {
document.body.contains(document.body);
return true;
} catch(ex) {
return false;
}
})();
Assert.areSame(expectedCallability ,
isHostMethod(document.body, "contains");
}
The only problem with this is that testing a property such as
`document.forms` or `document.images` will be true, but not always callable.
[...]
--
Garrett
comp.lang.javascript FAQ:
http://jibbering.com/faq/