From 097f436e6efdf7fe50879482ed573f69d77e3571 Mon Sep 17 00:00:00 2001 From: Simon Pieters Date: Thu, 7 Mar 2013 16:49:25 +0100 Subject: [PATCH] add greater/less than (or equals) assertions --- apisample.htm | 30 +++++++++++++++++++ testharness.js | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) diff --git a/apisample.htm b/apisample.htm index 1ee4610b89c565..5e8896f970930b 100644 --- a/apisample.htm +++ b/apisample.htm @@ -73,6 +73,36 @@

Sample HTML5 API Tests

} 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"} diff --git a/testharness.js b/testharness.js index 5a2cd2aed5937f..68df8c6231359d 100644 --- a/testharness.js +++ b/testharness.js @@ -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/ * @@ -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)