File indexing completed on 2024-04-28 15:28:39

0001 // Utility functions for JS test suite
0002 // (c) 2001 Harri Porten <porten@kde.org>
0003 // (c) 2020 froglogic GmbH <contact@froglogic.com>
0004 
0005 function testPassed(msg)
0006 {
0007   debug("PASS. " + msg);
0008 }
0009 
0010 function testFailed(msg)
0011 {
0012   debug("FAIL. " + msg);
0013   if (this.regtest)
0014     regtest.reportResult(false, msg);  
0015 }
0016 
0017 function description(s)
0018 {
0019     debug(s);
0020 }
0021 
0022 function evalError(arg, ex)
0023 {
0024     //return "eval('" + arg + "') => " + ex.toString();
0025     return ex.toString() + " [eval('" + arg + "')]";
0026 }
0027 
0028 function shouldBe(_a, _b)
0029 {
0030   if (typeof _a != "string" || typeof _b != "string")
0031     debug("WARN: shouldBe() expects string arguments");
0032   var _av, _bv;
0033   try {
0034     _av = eval(_a);
0035   } catch (e) {
0036     _av = evalError(_a, e);
0037   }
0038   try {
0039     _bv = eval(_b);
0040   } catch (e) {
0041     _bv = evalError(_b, e);
0042   }
0043 
0044   if (_av === _bv)
0045     testPassed(_a + " is " + _b);
0046   else
0047     testFailed(_a + " should be " + _bv + ". Was " + _av);
0048 }
0049 
0050 function shouldBeTrue(_a) { shouldBe(_a, "true"); }
0051 
0052 function shouldBeFalse(_a) { shouldBe(_a, "false"); }
0053 
0054 function shouldBeUndefined(_a)
0055 {
0056   var _av;
0057   try {
0058     _av = eval(_a);
0059   } catch (e) {
0060     _av = evalError(_a, e);
0061   }
0062 
0063   if (typeof _av == "undefined")
0064     testPassed(_a + " is undefined.");
0065   else
0066     testFailed(_a + " should be undefined. Was " + _av);
0067 }
0068 
0069 function shouldThrow(_a, _e)
0070 {
0071   var exception;
0072   var _av;
0073   try {
0074      _av = eval(_a);
0075   } catch (e) {
0076      exception = e;
0077   }
0078 
0079   var _ev;
0080   if (_e)
0081       _ev =  eval(_e);
0082 
0083   if (exception) {
0084     if (typeof _e == "undefined" || exception == _ev)
0085       testPassed(_a + " threw exception " + exception + ".");
0086     else
0087       testFailed(_a + " should throw exception " + _ev + ". Threw exception " + exception + ".");
0088   } else if (typeof _av == "undefined")
0089     testFailed(_a + " should throw exception " + _e + ". Was undefined.");
0090   else
0091     testFailed(_a + " should throw exception " + _e + ". Was " + _av + ".");
0092 }
0093 
0094 function shouldExcept(error, func, msg)
0095 {
0096     var exception;
0097     try {
0098         func();
0099     } catch (e) {
0100         exception = e;
0101     }
0102     if (exception) {
0103         if (exception instanceof error) {
0104             test.pass(msg + ": got expected exception " + error.name);
0105         } else {
0106             test.fail(msg + ": got wrong exception. Name: " + error.name);
0107         }
0108     } else {
0109         test.fail(msg + ": function did not throw an exception");
0110     }
0111 }