Home | History | Annotate | Download | only in resources
      1 function description(msg)
      2 {
      3     print(msg);
      4     print("\nOn success, you will see a series of \"PASS\" messages, followed by \"TEST COMPLETE\".\n");
      5     print();
      6 }
      7 
      8 function debug(msg)
      9 {
     10     print(msg);
     11 }
     12 
     13 function escapeString(text)
     14 {
     15     return text.replace(/\0/g, "");
     16 }
     17 
     18 function testPassed(msg)
     19 {
     20     print("PASS", escapeString(msg));
     21 }
     22 
     23 function testFailed(msg)
     24 {
     25     print("FAIL", escapeString(msg));
     26 }
     27 
     28 function areArraysEqual(_a, _b)
     29 {
     30     if (_a.length !== _b.length)
     31         return false;
     32     for (var i = 0; i < _a.length; i++)
     33         if (_a[i] !== _b[i])
     34             return false;
     35     return true;
     36 }
     37 
     38 function isMinusZero(n)
     39 {
     40     // the only way to tell 0 from -0 in JS is the fact that 1/-0 is
     41     // -Infinity instead of Infinity
     42     return n === 0 && 1/n < 0;
     43 }
     44 
     45 function isResultCorrect(_actual, _expected)
     46 {
     47     if (_expected === 0)
     48         return _actual === _expected && (1/_actual) === (1/_expected);
     49     if (_actual === _expected)
     50         return true;
     51     if (typeof(_expected) == "number" && isNaN(_expected))
     52         return typeof(_actual) == "number" && isNaN(_actual);
     53     if (Object.prototype.toString.call(_expected) == Object.prototype.toString.call([]))
     54         return areArraysEqual(_actual, _expected);
     55     return false;
     56 }
     57 
     58 function stringify(v)
     59 {
     60     if (v === 0 && 1/v < 0)
     61         return "-0";
     62     else return "" + v;
     63 }
     64 
     65 function shouldBe(_a, _b)
     66 {
     67   if (typeof _a != "string" || typeof _b != "string")
     68     debug("WARN: shouldBe() expects string arguments");
     69   var exception;
     70   var _av;
     71   try {
     72      _av = eval(_a);
     73   } catch (e) {
     74      exception = e;
     75   }
     76   var _bv = eval(_b);
     77 
     78   if (exception)
     79     testFailed(_a + " should be " + _bv + ". Threw exception " + exception);
     80   else if (isResultCorrect(_av, _bv))
     81     testPassed(_a + " is " + _b);
     82   else if (typeof(_av) == typeof(_bv))
     83     testFailed(_a + " should be " + _bv + ". Was " + stringify(_av) + ".");
     84   else
     85     testFailed(_a + " should be " + _bv + " (of type " + typeof _bv + "). Was " + _av + " (of type " + typeof _av + ").");
     86 }
     87 
     88 function shouldBeTrue(_a) { shouldBe(_a, "true"); }
     89 function shouldBeFalse(_a) { shouldBe(_a, "false"); }
     90 function shouldBeNaN(_a) { shouldBe(_a, "NaN"); }
     91 function shouldBeNull(_a) { shouldBe(_a, "null"); }
     92 
     93 function shouldBeEqualToString(a, b)
     94 {
     95   var unevaledString = '"' + b.replace(/"/g, "\"") + '"';
     96   shouldBe(a, unevaledString);
     97 }
     98 
     99 function shouldBeUndefined(_a)
    100 {
    101   var exception;
    102   var _av;
    103   try {
    104      _av = eval(_a);
    105   } catch (e) {
    106      exception = e;
    107   }
    108 
    109   if (exception)
    110     testFailed(_a + " should be undefined. Threw exception " + exception);
    111   else if (typeof _av == "undefined")
    112     testPassed(_a + " is undefined.");
    113   else
    114     testFailed(_a + " should be undefined. Was " + _av);
    115 }
    116 
    117 
    118 function shouldThrow(_a, _e)
    119 {
    120   var exception;
    121   var _av;
    122   try {
    123      _av = eval(_a);
    124   } catch (e) {
    125      exception = e;
    126   }
    127 
    128   var _ev;
    129   if (_e)
    130       _ev =  eval(_e);
    131 
    132   if (exception) {
    133     if (typeof _e == "undefined" || exception == _ev)
    134       testPassed(_a + " threw exception " + exception + ".");
    135     else
    136       testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Threw exception " + exception + ".");
    137   } else if (typeof _av == "undefined")
    138     testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Was undefined.");
    139   else
    140     testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Was " + _av + ".");
    141 }
    142