new.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. var failed = 0;
  6. function runtest(name, func, throwException)
  7. {
  8. try
  9. {
  10. func();
  11. if (throwException)
  12. {
  13. WScript.Echo(name + ": Test failed, unexpected no exception thrown");
  14. failed++;
  15. }
  16. else
  17. {
  18. WScript.Echo(name + ": Test passed, expected no exception thrown");
  19. }
  20. }
  21. catch (e)
  22. {
  23. if (!throwException || (e.name != "TypeError" && e.name != "ReferenceError"))
  24. {
  25. WScript.Echo(name + ": test failed, unexpected " + e.name + "-" + e.message);
  26. failed++;
  27. }
  28. else
  29. {
  30. WScript.Echo(name + ": Test passed, expected " + e.name + "-" + e.message);
  31. }
  32. }
  33. }
  34. function assert(cond)
  35. {
  36. if (!cond)
  37. {
  38. throw new Error("AssertFailed");
  39. }
  40. }
  41. //-------------------------------------------------------------
  42. // Test 1 - throw, new integer value
  43. //-------------------------------------------------------------
  44. function test1()
  45. {
  46. var i = 1;
  47. new i;
  48. }
  49. //-------------------------------------------------------------
  50. // Test 2 - throw, new null constant
  51. //-------------------------------------------------------------
  52. function test2()
  53. {
  54. new null();
  55. }
  56. //-------------------------------------------------------------
  57. // Test 3 - throw, new int constant
  58. //-------------------------------------------------------------
  59. function test3()
  60. {
  61. new 1();
  62. }
  63. //-------------------------------------------------------------
  64. // Test 4 - success, plain old new Object()
  65. //-------------------------------------------------------------
  66. function test4()
  67. {
  68. var o = new Object();
  69. }
  70. //-------------------------------------------------------------
  71. // Test 5 - throw, new object reference
  72. //-------------------------------------------------------------
  73. function test5()
  74. {
  75. var o = new Object();
  76. new o;
  77. }
  78. //-------------------------------------------------------------
  79. // Test 6 - throw, new undefined "Blah"
  80. //-------------------------------------------------------------
  81. function test6()
  82. {
  83. new Blah();
  84. }
  85. //-------------------------------------------------------------
  86. // Test 7 - throw, new object reference
  87. //-------------------------------------------------------------
  88. function test7()
  89. {
  90. var o = new Object();
  91. new o;
  92. }
  93. //-------------------------------------------------------------
  94. // Test 8 - success, new function with prototype and field init
  95. //-------------------------------------------------------------
  96. function ClassProto()
  97. {
  98. this.hello = "yay"
  99. }
  100. ClassProto.prototype.func = function()
  101. {
  102. return 3;
  103. }
  104. function test8()
  105. {
  106. var o = new ClassProto();
  107. assert(o.constructor == ClassProto);
  108. assert(o.hello == "yay");
  109. assert(o.func() == 3);
  110. }
  111. //-------------------------------------------------
  112. // Test 9 - success, new plain function without prototype
  113. //-------------------------------------------------
  114. function PlainFunction()
  115. {
  116. this.hello = "yay2";
  117. }
  118. function test9()
  119. {
  120. var o = new PlainFunction();
  121. assert(o.constructor == PlainFunction);
  122. assert(o.hello == "yay2");
  123. }
  124. runtest("test1", test1, true);
  125. runtest("test2", test2, true);
  126. runtest("test3", test3, true);
  127. runtest("test4", test4, false);
  128. runtest("test5", test5, true);
  129. runtest("test6", test6, true);
  130. runtest("test7", test7, true);
  131. runtest("test8", test8, false);
  132. runtest("test9", test9, false);
  133. if (failed == 0)
  134. {
  135. WScript.Echo("Passed");
  136. }