- Assertions for values
- Assertions for the type of the value
- Assertions for the raising exception
- Assertions for amount of time required
- Assertions for the number of performed assertions
- Assertions for regular expression
- More...
- UxU Home
- How to write testcases for UxU?
- Testing Greasemonkey scripts by UxU
- Helper methods for testcases
- How to write testcases with mocks?
- How to control UxU from remote?
- Command line options
Assertions for values
- void
assert.equals(in Object aExpected, in Object aActual, [in String aMessage])alias:assertEqual() -
Compares the expected and the actual, and
AssertionFailedexception raises if they are not equal unexpectedly. They are compared by the==operator. This works for primitives, objects, DOM nodes, and arrays.assert.equals('expectedValue', object.property); assert.equals($('node'), object.returnsNode()); // this compares each element in both arrays. assert.equals([$('node1'), $('node2'), $('node2')], object.returnsNodesArray()); // this compares each value of properties of both objects. assert.equals({ name : 'foo', value : 'bar' }, object.getResult()); - void
assert.strictlyEquals(in Object aExpected, in Object aActual, [in String aMessage])alias:assertStrictlyEqual() -
This is a strict version of
assert.equals(). Values are compared by the===operator.var value = 'expectedValue'; assert.strictlyEquals('expectedValue', value); // success value = new String('expectedValue'); assert.strictlyEquals('expectedValue', value); // failure - void
assert.notEquals(in Object aExpected, in Object aActual, [in String aMessage])alias:assertNotEqual() -
Opposite of
assert.equals(). Compares the expected and the actual, andAssertionFailedexception raises if they are unexpectedly equal.assert.notEquals('true', node.getAttribute('disabled')); assert.notEquals(object.firstItem, object.lastItem); - void
assert.notStrictlyEquals(in Object aExpected, in Object aActual, [in String aMessage])alias:assertNotStrictlyEqual() -
Opposite of
assert.strictlyEquals(). Values are compared by the!==operator.var value = '10'; assert.notStrictlyEquals(10, value); // success assert.notStrictlyEquals(10, parseInt(value)); // failure - void
assert.contains(in Object aElementShouldBeContained, in Object aElementsOrRange, [in String aMessage])alias:assertContain() -
Asserts that the second argument contains the first argument.
AssertionFailedexception raises if the second doesn't contain the first.You can specify a nsISelection, a DOM Range, a DOM Node, or an array as the second argument. Otherwise this method converts the object to a string. See specific examples:
second argument first argument assertion nsISelection DOM Range The range of the nsISelection completely wraps the first argument range. (Note: This doesn't assert that one of ranges of the nsISelection equals the first argument. This only asserts that one of them wraps the first argument.) DOM Node The range of nsISelection completely wraps the node. DOM Range DOM Range The second argument range completely wraps the first argument range. DOM Node The range completely wraps the node. DOM Node DOM Range The whole range of the node's sub tree wraps the first argument range. DOM Node The first argument node is a descendant of the second argument node. Array Any object The object is an element of the array. Otherwise The stringized version of the second argument object contains the stringized version of the first argument object. (Note: This is case sensitive.) assert.contains('- Firefox', window.document.title); var terms = ['extracted', 'extended', 'expected']; assert.contains('expected', terms); // success assert.contains(doc.body.firstChild, doc.body); // success var range = doc.createRange(); range.selectNode(doc.body); assert.contains(doc.body, range); // success assert.contains(doc.body.firstChild, range); // success assert.contains(doc.documentElement, range); // failure - void
assert.notContains(in Object aElementShouldNotBeContained, in Object aElementsOrRange, [in String aMessage])alias:assertNotContain() -
Opposite of
assert.contains().assert.notContains('error', object.getResultsArray()); assert.notContains('out of selection', content.getSelection()); - void
assert.contained(in Object aElementsOrRangeShouldContain, in Object aElement, [in String aMessage])alias:assertContained() -
Asserts that the second argument is contained in the first argument.
AssertionFailedexception raises if the first doesn't contain the second.You can specify a nsISelection, a DOM Range, a DOM Node, or an array as the first argument. Otherwise this method converts the object to a string. See
assert.contains()also.assert.contained('Start - Firefox', myAddon.brandName); var terms = ['extracted', 'extended', 'expected']; assert.contained(terms, myAddon.lastTerm); assert.contained(doc.body, doc.body.firstChild); // success var range = doc.createRange(); range.selectNode(doc.body); assert.contained(range, doc.body.firstChild); // success assert.contained(range, doc.documentElement); // failure - void
assert.notContained(in Object aElementsOrRangeShouldNotContain, in Object aElement, [in String aMessage])alias:assertNotContaied() -
Opposite of
assert.contained().assert.notContained('error,failure', myAddon.lastStatus); assert.notContained(content.getSelection(), myAddon.getNextNodeOfSelection()); - void
assert.isTrue(in aExpression, [in String aMessage])alias:assert.true(),assert(),assertTrue() -
If the expression equals to
false(ex. 0, "", false, null, or undefined) unexpectedly, thenAssertionFailedexception raise.checkbox.setAttribute('checked', true); assert.isTrue(checkbox.checked); - void
assert.isFalse(in aExpression, [in String aMessage])alias:assert.false(),assertFalse() -
If the expression equals to
trueunexpectedly, thenAssertionFailedexception raises.checkbox.removeAttribute('checked'); assert.isTrue(checkbox.checked); - void
assert.inDelta(in Number aExpected, in Number aActual, in Number aDelta, [in String aMessage])alias:assertInDelta() -
AssertionFailedexception raises if the actual value is out of the specified range. In other words, this fails when "aActual < aExpected - aDelta || aActual > aExpected + aDelta" is true.var array = '0,1,2,3,4,5,6,7,8,9'.split(','); // extract about half elements at random var extracted = array.filter(function(aItem) { return Math.random() > 0.5; }); // assert the number of extracted items, // margin of error is plus or minus 2 assert.inDelta(5, extracted.length, 2); - void
assert.difference(in Fuction aGetter, in Number aExpectedDifference, in Function aTestTask, [in Object aScope], [in String aMessage])alias:assertDifference() -
TBD
TBD - void
assert.noDifference(in Fuction aGetter, in Function aTestTask, [in Object aScope], [in String aMessage])alias:assertNoDifference() -
TBD
TBD - void
assert.compare(in Number aExpected, in String aOperator, in Number aActual, [in String aMessage])alias:assertCompare() -
AssertionFailedexception raises if the result of comparison aExpected and aActual by aOperator is false. Available operators are==,===,!=,!==,<,<=,>, and>=. Otherwise raises an errorassert.compare(100, '<=', func1()); assert.compare(100, '>', func2());
Note: special cases of "equals" assertions
assert.equals(), assert.notEquals(), assert.strictlyEquals(), assert.notStrictlyEquals(), and assert.compare() work specially for the following types:
Date-
Date-times are compared.
Array-
Lengthes and all elements are compared.
Object(object literals, hashes, instances of custom classes)-
Names and values of all properties are compared.
Assertions for the type of the value
- void
assert.isBoolean(in aExpression, [in String aMessage])alias:assert.boolean(),assertBoolean() -
AssertionFailedexception raises if it is not a boolean value unexpectedly.assert.isBoolean(checkbox.checked); assert.isBoolean(checkbox.disabled); assert.isBoolean(utility.getPref('some.boolean.pref')); - void
assert.isNotBoolean(in aExpression, [in String aMessage])alias:assert.notBoolean(),assertNotBoolean() -
AssertionFailedexception raises if it is a boolean value unexpectedly.assert.isNotBoolean(notInitializedObject.booleanProperty); - void
assert.isString(in aExpression, [in String aMessage])alias:assert.string(),assertString() -
AssertionFailedexception raises if it is not a string value unexpectedly.assert.isString(input.value); assert.isString(element.getAttribute('undefined-attribute')); assert.isString(utility.getPref('some.string.pref')); - void
assert.isNotString(in aExpression, [in String aMessage])alias:assert.notString(),assertNotString() -
AssertionFailedexception raises if it is a string value unexpectedly.assert.isNotString(notInitializedObject.stringProperty); - void
assert.isNumber(in aExpression, [in String aMessage])alias:assert.number(),assertNumber() -
AssertionFailedexception raises if it is not a numeric value unexpectedly.assert.isNumber(httpClient.errorCode); assert.isNumber(utility.getPref('some.integer.pref')); - void
assert.isNotNumber(in aExpression, [in String aMessage])alias:assert.notNumber(),assertNotNumber() -
AssertionFailedexception raises if it is a numeric value unexpectedly.assert.isNotString(notInitializedObject.intProperty); - void
assert.isFunction(in aExpression, [in String aMessage])alias:assert.function(),assertFunction() -
AssertionFailedexception raises if it is not a function unexpectedly.assert.isFunction(element.hasAttribute); assert.isFunction(instance.inheritedMethod); - void
assert.isNotFunction(in aExpression, [in String aMessage])alias:assert.notFunction(),assertNotFunction() -
AssertionFailedexception raises if it is a function unexpectedly.assert.isNotFunction(instanceOfSuperClass.methodOfSubClass); - void
assert.isDefined(in aExpression, [in String aMessage])alias:assert.defined(),assertDefined() -
AssertionFailedexception raises if it is undefined unexpectedly.assert.isDefined(instance.inheritedProperty); assert.isDefined(Ci.myICustomInterface); - void
assert.isUndefined(in aExpression, [in String aMessage])alias:assert.undefined(),assertUndefined() -
AssertionFailedexception raises if it is defined unexpectedly.assert.isUndefined(checkbox.valid); assert.isUndefined(Ci.myIOldCustomInterface); - void
assert.isNull(in aExpression, [in String aMessage])alias:assert.null(),assertNull() -
AssertionFailedexception raises if it is not null unexpectedly.assert.isNull(element.nodeValue); assert.isNull(utility.getPref('some.undefined.pref')); - void
assert.isNotNull(in aExpression, [in String aMessage])alias:assert.notNull(),assertNotNull() -
AssertionFailedexception raises if it is null unexpectedly.assert.isNotNull(element.getAttribute('undefined-attribute')); assert.isNotNull(utility.getPref('some.defined.pref')); - void
assert.isInstanceOf(in Object aExpectedClass, in aObject aInstance, [in String aMessage])alias:assert.instanceOf(),assert.instanceof(),assert.instance(),assertInstanceOf(),assertInstanceof(),assertInstance() -
TBD
TBD - void
assert.inplementsInterface(in aInterface, in aObject, [in String aMessage])alias:assert.inplementInterface(),assertInplementsInterface(),assertInplementInterface() -
Asserts an object inplements an XPCOM interface. You can specify any interface by the reference (like
Components.interfaces.nsI*) or the name of the interface (like"nsI*).AssertionFailedexception raises if the object doesn't implement the specified interface.assert.implementsInterface(Ci.nsIDOMWindow, getWindow()); assert.implementsInterface('nsIDOMWindow', getWindow()); assert.implementsInterface(Ci.nsIDOMRange, getWindow()); // this will fail
Assertions for the raising exception
- Object
assert.raises(in Exception aExpected, in Function aTestTask, [in Object aScope], [in String aMessage])alias:assert.throws(),assertRaise(),assertThrow() -
This runs the function in the scope specified as the third argument or the current scope. If no exception or unexpected one raises from the operation, then
AssertionFailedexception raises.loader.INVALID_URI_ERROR = 'invalid uri'; loader.open = function(aURI) { if (aURI) this.uri = aURI; if (!/^https?:/.test(this.uri)) throw new Error(this.INVALID_URI_ERROR); window.open(this.uri); }; loader.uri = 'mailto:test@example.com'; assert.raises( loader.INVALID_URI_ERROR, loader.open, loader ); assert.raises( loader.INVALID_URI_ERROR, function() { loader.open('ftp://ftp.example.com'); }, {} );This assertion successes when one of followings equals to the expected: raised exception itself, the constructor function, its
nameproperty, itsmessageproperty, itsresultproperty, or its properties specified by a hash. Valid samples:assert.raises('error', function() { throw 'error'; }); assert.raises('error', function() { throw new Error('error'); }); assert.raises('SyntaxError', function() { eval('{'); }); assert.raises(SyntaxError, function() { eval('{'); }); assert.raises(Components.results.NS_NOINTERFACE, function() { window.QueryInterface(Ci.nsIDOMDocument); }); assert.raises('NS_NOINTERFACE', function() { window.QueryInterface(Ci.nsIDOMDocument); }); assert.raises({ expected : true, actual : false }, function() { var e = new Error(); e.expected = true; e.actual = false; throw e; });Moreover, this assertion returns an object for waiting. You can test features which require to be asserted after delay.
yield Do(assert.raises( loader.INVALID_URI_ERROR, function() { loader.open('http://www.example.com'); yield 500; loader.open('ftp://ftp.example.com'); }, {} )); - Object
assert.notRaises(in Exception aExpected, in Function aTestTask, in Object aScope, [in String aMessage])alias:assert.notThrows(),assertNotRaise(),assertNotThrow() -
Opposite of
assert.raises(). If the specified exception raises from the operation, thenAssertionFailedexception raises. This assertion compares the actual exception with the expected, by the method same algorithm of assert.raise().loader.INVALID_URI_ERROR = 'invalid uri'; loader.open = function(aURI) { if (aURI) this.uri = aURI; if (!/^https?:/.test(this.uri)) throw new Error(this.INVALID_URI_ERROR); window.open(this.uri); }; loader.uri = 'http://www.example.com'; assert.notRaises( loader.INVALID_URI_ERROR, loader.open, loader ); assert.notRaises( loader.INVALID_URI_ERROR, function() { loader.open('https://secure.example.com'); }, {} );Moreover, this assertion returns an object for waiting. You can test features which require to be asserted after delay.
yield Do(assert.notRaises( loader.INVALID_URI_ERROR, function() { loader.open('http://www1.example.com'); yield 500; loader.open('http://www2.example.com'); }, {} ));
Assertions for amount of time required
- Object
assert.finishesWithin(in Number aExpectedTime, in Function aTestTask, in Object aScope, [in String aMessage])alias:assert.finishWithin(),assertFinishWithin() -
This runs the function in the scope specified as the third argument or the current scope. If it uses more than the expected time (in milliseconds), then
AssertionFailedexception raises.function functionContainsLongLoop() { for (var i = 0, maxi = 5000; i < maxi; i++) { process(data[i]); } } assert.finishesWithin( 5 * 1000, // 5sec. functionContainsLongLoop, {} );Moreover, this assertion returns an object for waiting. You can test features which require to be asserted after delay.
var manager = { tasks : [], func : loadAndParse, timer : null, start : function() { this.timer = window.setInterval(function(aSelf) { if (!aSelf.tasks.length) { window.clearInterval(aSelf.timer); return; } aSelf.func(aSelf.tasks.shift()); }, 10, this); } }; manager.tasks.push('http://www.example.com/'); manager.tasks.push('http://www.example.jp/'); manager.tasks.push('http://www.example.net/'); utils.wait(assert.finishesWithin( 10 * 1000, // 10sec. function() { manager.start(); while (manager.tasks.length) { utils.wait(100); } }, {} )); - Object
assert.finishesOver(in Number aExpectedTime, in Function aTestTask, in Object aScope, [in String aMessage])alias:assert.finishOver(),assertFinishOver(),assert.notFinishesWithin(),assert.notFinishWithin(),assertNotFinishWithin() -
This runs the function in the scope specified as the third argument or the current scope. If it uses less than the expected time (in milliseconds), then
AssertionFailedexception raises.function doWait(aTimeout) { ... } assert.finishesOver( 5 * 1000, // 5sec. function() { doWait(5); } );Moreover, this assertion returns an object for waiting. You can test features which require to be asserted after delay.
Assertions for the number of performed assertions
- Object
assert.assertionsCountEquals(in Number aExpectedCount, in Function aTestTask, in Object aScope, [in String aMessage])alias:assert.assertionsCountEqual(),assertAssertionsCountEqual() -
This is the local version of assertion by
assertionsproperty.This runs the function in the scope specified as the third argument or the current scope. If more or less assertions are performed than the expected, then
AssertionFailedexception raises.assert.assertionsCountEquals( 2, function() { assert.isNotNull(value); assert.isFunction(value); }, {} );Moreover, this assertion returns an object for waiting. You can test features which require to be asserted after delay.
utils.wait(assert.assertionsCountEquals( 10, function() { assert.isNotNull(value); utils.wait(500); assert.isFunction(value); }, {} )); - Object
assert.assertionsMinCount(in Number aExpectedMinCount, in Function aTestTask, in Object aScope, [in String aMessage])alias:assert.assertionsMinCount(),assertAssertionsMinCount() -
This is the local version of assertion by
minAssertionsproperty.This runs the function in the scope specified as the third argument or the current scope. If less assertions are performed than the expected, then
AssertionFailedexception raises.assert.assertionsMinCount( 2, function() { myCustomAssertion(value); // includes 2 or more assertions }, {} );Moreover, this assertion returns an object for waiting. You can test features which require to be asserted after delay.
utils.wait(assert.assertionsCountEquals( 10, function() { utils.wait(myCustomAssertion(value)); }, {} )); - Object
assert.assertionsMaxCount(in Number aExpectedMaxCount, in Function aTestTask, in Object aScope, [in String aMessage])alias:assert.assertionsMaxCount(),assertAssertionsMaxCount() -
This is the local version of assertion by
maxAssertionsproperty.This runs the function in the scope specified as the third argument or the current scope. If more assertions are performed than the expected, then
AssertionFailedexception raises.assert.assertionsMinCount( 2, function() { myCustomAssertion(value); // includes 2 or less assertions }, {} );Moreover, this assertion returns an object for waiting. You can test features which require to be asserted after delay.
utils.wait(assert.assertionsCountEquals( 10, function() { utils.wait(myCustomAssertion(value)); }, {} ));
Assertions for regular expression
- void
assert.matches(in RegExp aExpected, in String aActual, [in String aMessage])alias:assertMatch() -
AssertionFailedexception raises if the string doesn't match to the regular expression.assert.matches(/^(success|skip)$/i, module.result); assert.matches(/https?:\/\//, module.message); - void
assert.notMatches(in RegExp aUnexpected, in String aActual, [in String aMessage])alias:assertNotMatch() -
Opposite of
assert.matches().assert.notMatches(/^(failure|error)$/i, module.result); assert.notMatches(/(ftp|nntp):/, module.message); - void
assert.pattern(in String aExpected, in RegExp aActual, [in String aMessage])alias:assertPattern() -
This is for regular expression generators. If the regular expression doesn't match to the string,
AssertionFailedexception raises.var regexp = module.mailAddressRegExp; assert.pattern('test@example.com', regexp); assert.pattern('My Name <test@example.com>', regexp); - void
assert.notPattern(in String aUnexpected, in RegExp aActual, [in String aMessage])alias:assertNotPattern() -
Opposite of
assert.pattern().var regexp = module.mailAddressRegExp; assert.notPattern('foobar@', regexp); assert.notPattern('My Name (test at example.com)', regexp);
More...
You can specify a custom message for any assertion. The message will be shown in the report if the assertion is failed, so you can output details of the situation which the test fails on.
Custom assertions can be defined like as:
assert.isOK = function(aActualValue) {
var expected = 'OK';
assert.equals(expected, aActualValue);
}