ObjLitGetSetParseOnly.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. function write(value)
  6. {
  7. WScript.Echo(value);
  8. }
  9. function RunTest(testCase, testCount)
  10. {
  11. var testFunction = testCase[0];
  12. var testScenario = testCase[1];
  13. testScenario = " (test " + testCount + "): " + testScenario;
  14. write(testScenario);
  15. try
  16. {
  17. var result = testFunction();
  18. if (result == true)
  19. {
  20. write("PASS");
  21. }
  22. }
  23. catch (e)
  24. {
  25. var resultString = "FAILED" + testScenario;
  26. write(resultString + " :: " + e.message);
  27. }
  28. }
  29. function RunAllTests(){
  30. for(var i = 0; i < testList.length; ++i){
  31. RunTest(testList[i], i + 1);
  32. }
  33. }
  34. var testList = [
  35. [Test1, "Object literal duplicate set/get property"],
  36. [Test2, "Object literal set must have exactly one argument"],
  37. ];
  38. // Utility functions
  39. function Verify(expression, actualValue, expectedValue)
  40. {
  41. if (!expectedValue) {
  42. if (actualValue) {
  43. write("Success: " + expression);
  44. } else {
  45. write("Failed: " + expression);
  46. }
  47. return actualValue;
  48. } else if (expectedValue != actualValue) {
  49. write("Failed: Expected " + expression + " = " + expectedValue + ", got " + actualValue);
  50. return false;
  51. }
  52. write("Success: Expected " + expression + " = " + expectedValue + ", got " + actualValue);
  53. return true;
  54. }
  55. //Tests
  56. write("ES 5 Object Literal grammar: parsing-only tests");
  57. // Test: Object literal duplicate set\get property. See ECMAScript 11.1.5.
  58. function Test1(args)
  59. {
  60. // duplicate property
  61. // This causes error only in the case when strict code mode is enabled. Strict mode is not implemented yet.
  62. var isOk = true;
  63. try
  64. {
  65. eval("function f() { var o = { foo: 1, foo: 2 }; }");
  66. }
  67. catch (ex)
  68. {
  69. isOk = Verify("duplicate data properties in object literal should not throw SyntaxError in non-strict mode", false);
  70. }
  71. // property and get
  72. try
  73. {
  74. eval("function f() { var o = { foo: 1, get foo() {return this.value;} }; }");
  75. }
  76. catch(ex)
  77. {
  78. isOk = Verify("data property and get with same name in object literal should not throw SyntaxError", false);
  79. }
  80. // get and property
  81. try
  82. {
  83. eval("function f() { var o = { get foo() {return this.value;}, foo: 1 }; }");
  84. }
  85. catch(ex)
  86. {
  87. isOk = Verify("get and data property with same name in object literal should not throw SyntaxError", false);
  88. }
  89. // property and set
  90. try
  91. {
  92. eval("function f() { var o = { foo: 1, set foo(arg) { this.value = arg;} }; }");
  93. }
  94. catch(ex)
  95. {
  96. isOk = Verify("data property and set with same name in object literal should not throw SyntaxError", false);
  97. }
  98. // set and property
  99. try
  100. {
  101. eval("function f() { var o = { set foo(arg) { this.value = arg;}, foo: 1 }; }");
  102. }
  103. catch(ex)
  104. {
  105. isOk = Verify("set and data property with same name in object literal should not throw SyntaxError", false);
  106. }
  107. try //duplicate get
  108. {
  109. eval("function f() { var o = {get foo(){return this.value;}, get foo(){return this.value*2;}}; }");
  110. }
  111. catch(ex)
  112. {
  113. isOk = Verify("duplicate get in object literal should not throw SyntaxError", false);
  114. }
  115. try //duplicate set
  116. {
  117. eval("function f() { var o = {set foo(args){this.value = args*2;}, set foo(args){ this.value = args*args}}; }");
  118. }
  119. catch(ex)
  120. {
  121. isOk = Verify("duplicate set in object literal should not throw SyntaxError", false);
  122. }
  123. try //duplicate get after get and set
  124. {
  125. eval("function f() { var o = { get foo(){ return this.value; }, set foo(arg){ this.value = arg; }, get foo(){ return this.value; } }; }");
  126. }
  127. catch(ex)
  128. {
  129. isOk = Verify("duplicate get after get and set in object literal should not throw SyntaxError", false);
  130. }
  131. try //duplicate set after set and get
  132. {
  133. eval("function f() { var o = { set foo(arg){ this.value = arg; }, get foo(){ return this.value; }, set foo(arg1){ this.value = arg1; } } }");
  134. }
  135. catch(ex)
  136. {
  137. isOk = Verify("duplicate set after get and set in object literal should not throw SyntaxError", false);
  138. }
  139. return isOk;
  140. }
  141. // Object literal set must have exactly one argument
  142. function Test2()
  143. {
  144. var exception;
  145. exception = null;
  146. try // setter with no arguments
  147. {
  148. eval("function f() { var o = { set foo() { this.value = 0; } } }");
  149. }
  150. catch (ex)
  151. {
  152. exception = ex;
  153. }
  154. if(!Verify("set with no arguments in object literal throws SyntaxError", true, exception instanceof SyntaxError)) return false;
  155. exception = null;
  156. try // setter with two arguments
  157. {
  158. eval("function f() { var o = { set foo(arg1, arg2) { this.value = 0; } } }");
  159. }
  160. catch (ex)
  161. {
  162. exception = ex;
  163. }
  164. if(!Verify("set with two arguments in object literal throws SyntaxError", true, exception instanceof SyntaxError)) return false;
  165. return true;
  166. }
  167. RunAllTests();