basics.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  6. const tests = [
  7. {
  8. name : "Simple",
  9. body() {
  10. var f = false;
  11. var t = true;
  12. assert.isTrue(!f, "!f should be true");
  13. assert.isTrue(!!!f, "!!!f should be true");
  14. assert.isTrue(t, "t should be true");
  15. assert.isTrue(!!t, "!!t should be true");
  16. }
  17. },
  18. {
  19. name : "Equals",
  20. body() {
  21. var a = [true, false, new Boolean(true), new Boolean(false)];
  22. var b = [true, false, new Boolean(true), new Boolean(false), -1, 0, 1, 2, 1.0, 1.1, 0.0, +0, -0,
  23. null, undefined, new Object(), "", "abc", "-1", "0", "1", "2", "true", "false", "t", "f",
  24. "True", "False", " 1.00 ", " 1. ", " +1.0 ", new Number(0), new Number(1)];
  25. var results = [true, false, true, false, false, false, true, false, true, false,
  26. false, false, false, false, false, false, false, false, false, false, true,
  27. false, false, false, false, false, false, false, true, true, true, false,
  28. true, false, true, false, true, false, true, false, false, false, false,
  29. true, true, true, false, false, false, true, false, false, true, false,
  30. false, false, false, false, false, false, false, false, false, false, true,
  31. false, true, false, false, false, false, false, true, false, true, false,
  32. false, false, false, false, false, false, false, false, false, false, true,
  33. false, false, false, false, false, false, false, true, true, true, false,
  34. false, false, true, false, false, false, true, false, false, false, false,
  35. true, true, true, false, false, false, true, false, false, true, false,
  36. false, false, false, false, false, false, false, false, false, false, false,
  37. false];
  38. for (var i = 0, k = 0; i < a.length; i++)
  39. {
  40. for (var j = 0; j < b.length; j++, k++)
  41. {
  42. assert.areEqual(a[i] == b[j], results[k], `${a[i]} == ${b[j]} should evaluate to ${results[k]}`);
  43. }
  44. }
  45. }
  46. },
  47. {
  48. name : "Wrapped Object",
  49. body() {
  50. var f = new Boolean(false);
  51. assert.isTrue(f == false, "new Boolean(false) should == false");
  52. assert.isTrue(f !== false, "new Boolean(false) should !== false");
  53. assert.isTrue(!f == false, "!(new Boolean(false)) should == false");
  54. }
  55. },
  56. {
  57. name : "Boolean values generated with ! outside of a conditional",
  58. body() {
  59. var q = new Object();
  60. var tests = [-0.5, -1, 1, 2, 3, new Object(), q, [4,5,6], "blah", 'c', true];
  61. for (var x in tests) {
  62. assert.isFalse(!tests[x], `!${tests[x]} should evaluate to false`);
  63. }
  64. assert.isTrue(!0 && !false, "!0 && !false should evaluate to true");
  65. }
  66. },
  67. {
  68. name : "Value producing comparisons",
  69. body() {
  70. var lhs = [1, 2, 2, -1, 1, 0, 0, 0x70000000, 0];
  71. var rhs = [2, 1, 2, 2, -2, 0, 0.1, 0, 0x70000000];
  72. var results = [
  73. [true, true, false, false, false, true, false, true],
  74. [false, false, true, true, false, true, false, true],
  75. [false, true, false, true, true, false, true, false],
  76. [true, true, false, false, false, true, false, true],
  77. [false, false, true, true, false, true, false, true],
  78. [false, true, false, true, true, false, true, false],
  79. [true, true, false, false, false, true, false, true],
  80. [false, false, true, true, false, true, false, true],
  81. [true, true, false, false, false, true, false, true]
  82. ];
  83. for (var i = 0; i < 9; ++i)
  84. {
  85. assert.areEqual(lhs[i] < rhs[i], results[i][0], `Expected ${lhs[i]} < ${rhs[i]} to equal ${results[i][0]}`);
  86. assert.areEqual(lhs[i] <= rhs[i], results[i][1], `Expected ${lhs[i]} <= ${rhs[i]} to equal ${results[i][1]}`);
  87. assert.areEqual(lhs[i] > rhs[i], results[i][2], `Expected ${lhs[i]} > ${rhs[i]} to equal ${results[i][2]}`);
  88. assert.areEqual(lhs[i] >= rhs[i], results[i][3], `Expected ${lhs[i]} >= ${rhs[i]} to equal ${results[i][3]}`);
  89. assert.areEqual(lhs[i] == rhs[i], results[i][4], `Expected ${lhs[i]} == ${rhs[i]} to equal ${results[i][4]}`);
  90. assert.areEqual(lhs[i] != rhs[i], results[i][5], `Expected ${lhs[i]} != ${rhs[i]} to equal ${results[i][5]}`);
  91. assert.areEqual(lhs[i] === rhs[i], results[i][6], `Expected ${lhs[i]} === ${rhs[i]} to equal ${results[i][6]}`);
  92. assert.areEqual(lhs[i] !== rhs[i], results[i][7], `Expected ${lhs[i]} !== ${rhs[i]} to equal ${results[i][7]}`);
  93. }
  94. }
  95. },
  96. {
  97. name: "Assignment to a property on a boolean without a setter in sloppy mode should be ignored",
  98. body: function ()
  99. {
  100. var flag = true;
  101. flag.a = 12;
  102. assert.areEqual(undefined, flag.a);
  103. }
  104. },
  105. {
  106. name: "Assignment to a property on a boolean without a setter in strict mode should throw an error",
  107. body: function ()
  108. {
  109. var flag = true;
  110. assert.throws(function() { "use strict"; flag.a = 1; }, TypeError, "Assigning to a property of a number should throw a TypeError.", "Assignment to read-only properties is not allowed in strict mode");
  111. }
  112. },
  113. {
  114. name: "Assignment to a property on a boolean without a setter in sloppy mode should be ignored",
  115. body: function ()
  116. {
  117. var flag = true;
  118. flag['a'] = 12;
  119. assert.areEqual(undefined, flag.a);
  120. }
  121. },
  122. {
  123. name: "Assignment to a property on a boolean without a setter in strict mode should throw an error",
  124. body: function ()
  125. {
  126. var flag = true;
  127. assert.throws(function() { "use strict"; flag['a'] = 1; }, TypeError, "Assigning to a property of a number should throw a TypeError.", "Assignment to read-only properties is not allowed in strict mode");
  128. }
  129. },
  130. {
  131. name: "Assignment to an index on a boolean without a setter in sloppy mode should be ignored",
  132. body: function ()
  133. {
  134. var flag = true;
  135. flag[66] = 12;
  136. assert.areEqual(undefined, flag.a);
  137. }
  138. },
  139. {
  140. name: "Assignment to an index on a boolean without a setter in strict mode should throw an error",
  141. body: function ()
  142. {
  143. var flag = true;
  144. assert.throws(function() { "use strict"; flag[66] = 1; }, TypeError, "Assigning to a property of a number should throw a TypeError.", "Assignment to read-only properties is not allowed in strict mode");
  145. }
  146. }
  147. ];
  148. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });