Skip to content

Commit

Permalink
add greater/less than (or equals) assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
zcorpan committed Mar 7, 2013
1 parent dad4f54 commit 097f436
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
30 changes: 30 additions & 0 deletions apisample.htm
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,36 @@ <h1>Sample HTML5 API Tests</h1>
}
test(basicAssertObjectEquals, "basic assert_object_equals test");

function basicAssertApproxEquals()
{
assert_approx_equals(10, 11, 1, "10 is approximately (+/- 1) 11")
}
test(basicAssertApproxEquals, "basic assert_approx_equals test");

function basicAssertLessThan()
{
assert_less_than(10, 11, "10 is less than 11")
}
test(basicAssertApproxEquals, "basic assert_less_than test");

function basicAssertGreaterThan()
{
assert_greater_than(10, 11, "10 is not greater than 11");
}
test(basicAssertGreaterThan, "assert_greater_than expected to fail");

function basicAssertGreaterThanEqual()
{
assert_greater_than_equal(10, 10, "10 is greater than or equal to 10")
}
test(basicAssertGreaterThanEqual, "basic assert_greater_than_equal test");

function basicAssertLessThanEqual()
{
assert_greater_than_equal('10', 10, "'10' is not a number")
}
test(basicAssertLessThanEqual, "assert_less_than_equal expected to fail");

function testAssertInherits() {
var A = function(){this.a = "a"}
A.prototype = {b:"b"}
Expand Down
80 changes: 80 additions & 0 deletions testharness.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,18 @@ policies and contribution forms [3].
* assert_approx_equals(actual, expected, epsilon, description)
* asserts that /actual/ is a number within +/- /epsilon/ of /expected/
*
* assert_less_than(actual, expected, description)
* asserts that /actual/ is a number less than /expected/
*
* assert_greater_than(actual, expected, description)
* asserts that /actual/ is a number greater than /expected/
*
* assert_less_than_equal(actual, expected, description)
* asserts that /actual/ is a number less than or equal to /expected/
*
* assert_greater_than_equal(actual, expected, description)
* asserts that /actual/ is a number greater than or equal to /expected/
*
* assert_regexp_match(actual, expected, description)
* asserts that /actual/ matches the regexp /expected/
*
Expand Down Expand Up @@ -745,6 +757,74 @@ policies and contribution forms [3].
};
expose(assert_approx_equals, "assert_approx_equals");

function assert_less_than(actual, expected, description)
{
/*
* Test if a primitive number is less than another
*/
assert(typeof actual === "number",
"assert_less_than", description,
"expected a number but got a ${type_actual}",
{type_actual:typeof actual});

assert(actual < expected,
"assert_less_than", description,
"expected a number less than ${expected} but got ${actual}",
{expected:expected, actual:actual});
};
expose(assert_less_than, "assert_less_than");

function assert_greater_than(actual, expected, description)
{
/*
* Test if a primitive number is greater than another
*/
assert(typeof actual === "number",
"assert_greater_than", description,
"expected a number but got a ${type_actual}",
{type_actual:typeof actual});

assert(actual > expected,
"assert_greater_than", description,
"expected a number greater than ${expected} but got ${actual}",
{expected:expected, actual:actual});
};
expose(assert_greater_than, "assert_greater_than");

function assert_less_than_equal(actual, expected, description)
{
/*
* Test if a primitive number is less than or equal to another
*/
assert(typeof actual === "number",
"assert_less_than_equal", description,
"expected a number but got a ${type_actual}",
{type_actual:typeof actual});

assert(actual <= expected,
"assert_less_than", description,
"expected a number less than or equal to ${expected} but got ${actual}",
{expected:expected, actual:actual});
};
expose(assert_less_than_equal, "assert_less_than_equal");

function assert_greater_than_equal(actual, expected, description)
{
/*
* Test if a primitive number is greater than or equal to another
*/
assert(typeof actual === "number",
"assert_greater_than_equal", description,
"expected a number but got a ${type_actual}",
{type_actual:typeof actual});

assert(actual >= expected,
"assert_greater_than_equal", description,
"expected a number greater than or equal to ${expected} but got ${actual}",
{expected:expected, actual:actual});
};
expose(assert_greater_than_equal, "assert_greater_than_equal");

function assert_regexp_match(actual, expected, description) {
/*
* Test if a string (actual) matches a regexp (expected)
Expand Down

0 comments on commit 097f436

Please sign in to comment.