Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.03 KB | None | 0 0
  1. // Copyright (C) 2017 Ecma International. All rights reserved.
  2. // This code is governed by the BSD license found in the LICENSE file.
  3. /*---
  4. description: |
  5. Collection of assertion functions used throughout test262
  6. ---*/
  7.  
  8. function assert(mustBeTrue, message) {
  9. if (mustBeTrue === true) {
  10. return;
  11. }
  12.  
  13. if (message === undefined) {
  14. message = 'Expected true but got ' + String(mustBeTrue);
  15. }
  16. $ERROR(message);
  17. }
  18.  
  19. assert._isSameValue = function (a, b) {
  20. if (a === b) {
  21. // Handle +/-0 vs. -/+0
  22. return a !== 0 || 1 / a === 1 / b;
  23. }
  24.  
  25. // Handle NaN vs. NaN
  26. return a !== a && b !== b;
  27. };
  28.  
  29. assert.sameValue = function (actual, expected, message) {
  30. if (assert._isSameValue(actual, expected)) {
  31. return;
  32. }
  33.  
  34. if (message === undefined) {
  35. message = '';
  36. } else {
  37. message += ' ';
  38. }
  39.  
  40. message += 'Expected SameValue(«' + String(actual) + '», «' + String(expected) + '») to be true';
  41.  
  42. $ERROR(message);
  43. };
  44.  
  45. assert.notSameValue = function (actual, unexpected, message) {
  46. if (!assert._isSameValue(actual, unexpected)) {
  47. return;
  48. }
  49.  
  50. if (message === undefined) {
  51. message += ' ';
  52. }
  53.  
  54. message += 'Expected SameValue(«' + String(actual) + '», «' + String(unexpected) + '») to be false';
  55.  
  56. $ERROR(message);
  57. };
  58.  
  59. assert.throws = function (expectedErrorConstructor, func, message) {
  60. if (typeof func !== "function") {
  61. $ERROR('assert.throws requires two arguments: the error constructor ' +
  62. 'and a function to run');
  63. return;
  64. }
  65. if (message === undefined) {
  66. message = '';
  67. } else {
  68. message += ' ';
  69. }
  70.  
  71. try {
  72. func();
  73. } catch (thrown) {
  74. if (typeof thrown !== 'object' || thrown === null) {
  75. message += 'Thrown value was not an object!';
  76. $ERROR(message);
  77. } else if (thrown.constructor !== expectedErrorConstructor) {
  78. message += 'Expected a ' + expectedErrorConstructor.name + ' but got a ' + thrown.constructor.name;
  79. $ERROR(message);
  80. }
  81. return;
  82. }
  83.  
  84. message += 'Expected a ' + expectedErrorConstructor.name + ' to be thrown but no exception was thrown at all';
  85. $ERROR(message);
  86. };
  87. // Copyright (C) 2017 Ecma International. All rights reserved.
  88. // This code is governed by the BSD license found in the LICENSE file.
  89. /*---
  90. description: |
  91. Collection of functions used to safely verify the correctness of
  92. property descriptors.
  93. ---*/
  94.  
  95. function verifyProperty(obj, name, desc, options) {
  96. assert(
  97. arguments.length > 2,
  98. 'verifyProperty should receive at least 3 arguments: obj, name, and descriptor'
  99. );
  100.  
  101. var originalDesc = Object.getOwnPropertyDescriptor(obj, name);
  102. var nameStr = String(name);
  103.  
  104. // Allows checking for undefined descriptor if it's explicitly given.
  105. if (desc === undefined) {
  106. assert.sameValue(
  107. originalDesc,
  108. undefined,
  109. "obj['" + nameStr + "'] descriptor should be undefined"
  110. );
  111.  
  112. // desc and originalDesc are both undefined, problem solved;
  113. return true;
  114. }
  115.  
  116. assert(
  117. Object.prototype.hasOwnProperty.call(obj, name),
  118. "obj should have an own property " + nameStr
  119. );
  120.  
  121. assert.notSameValue(
  122. desc,
  123. null,
  124. "The desc argument should be an object or undefined, null"
  125. );
  126.  
  127. assert.sameValue(
  128. typeof desc,
  129. "object",
  130. "The desc argument should be an object or undefined, " + String(desc)
  131. );
  132.  
  133. var failures = [];
  134.  
  135. if (Object.prototype.hasOwnProperty.call(desc, 'value')) {
  136. if (desc.value !== originalDesc.value) {
  137. failures.push("descriptor value should be " + desc.value);
  138. }
  139. }
  140.  
  141. if (Object.prototype.hasOwnProperty.call(desc, 'enumerable')) {
  142. if (desc.enumerable !== originalDesc.enumerable ||
  143. desc.enumerable !== isEnumerable(obj, name)) {
  144. failures.push('descriptor should ' + (desc.enumerable ? '' : 'not ') + 'be enumerable');
  145. }
  146. }
  147.  
  148. if (Object.prototype.hasOwnProperty.call(desc, 'writable')) {
  149. if (desc.writable !== originalDesc.writable ||
  150. desc.writable !== isWritable(obj, name)) {
  151. failures.push('descriptor should ' + (desc.writable ? '' : 'not ') + 'be writable');
  152. }
  153. }
  154.  
  155. if (Object.prototype.hasOwnProperty.call(desc, 'configurable')) {
  156. if (desc.configurable !== originalDesc.configurable ||
  157. desc.configurable !== isConfigurable(obj, name)) {
  158. failures.push('descriptor should ' + (desc.configurable ? '' : 'not ') + 'be configurable');
  159. }
  160. }
  161.  
  162. assert(!failures.length, failures.join('; '));
  163.  
  164. if (options && options.restore) {
  165. Object.defineProperty(obj, name, originalDesc);
  166. }
  167.  
  168. return true;
  169. }
  170.  
  171. function isConfigurable(obj, name) {
  172. try {
  173. delete obj[name];
  174. } catch (e) {
  175. if (!(e instanceof TypeError)) {
  176. $ERROR("Expected TypeError, got " + e);
  177. }
  178. }
  179. return !Object.prototype.hasOwnProperty.call(obj, name);
  180. }
  181.  
  182. function isEnumerable(obj, name) {
  183. var stringCheck = false;
  184.  
  185. if (typeof name === "string") {
  186. for (var x in obj) {
  187. if (x === name) {
  188. stringCheck = true;
  189. break;
  190. }
  191. }
  192. } else {
  193. // skip it if name is not string, works for Symbol names.
  194. stringCheck = true;
  195. }
  196.  
  197. return stringCheck &&
  198. Object.prototype.hasOwnProperty.call(obj, name) &&
  199. Object.prototype.propertyIsEnumerable.call(obj, name);
  200. }
  201.  
  202. function isEqualTo(obj, name, expectedValue) {
  203. var actualValue = obj[name];
  204.  
  205. return assert._isSameValue(actualValue, expectedValue);
  206. }
  207.  
  208. function isWritable(obj, name, verifyProp, value) {
  209. var newValue = value || "unlikelyValue";
  210. var hadValue = Object.prototype.hasOwnProperty.call(obj, name);
  211. var oldValue = obj[name];
  212. var writeSucceeded;
  213.  
  214. try {
  215. obj[name] = newValue;
  216. } catch (e) {
  217. if (!(e instanceof TypeError)) {
  218. $ERROR("Expected TypeError, got " + e);
  219. }
  220. }
  221.  
  222. writeSucceeded = isEqualTo(obj, verifyProp || name, newValue);
  223.  
  224. // Revert the change only if it was successful (in other cases, reverting
  225. // is unnecessary and may trigger exceptions for certain property
  226. // configurations)
  227. if (writeSucceeded) {
  228. if (hadValue) {
  229. obj[name] = oldValue;
  230. } else {
  231. delete obj[name];
  232. }
  233. }
  234.  
  235. return writeSucceeded;
  236. }
  237.  
  238. function verifyEqualTo(obj, name, value) {
  239. if (!isEqualTo(obj, name, value)) {
  240. $ERROR("Expected obj[" + String(name) + "] to equal " + value +
  241. ", actually " + obj[name]);
  242. }
  243. }
  244.  
  245. function verifyWritable(obj, name, verifyProp, value) {
  246. if (!verifyProp) {
  247. assert(Object.getOwnPropertyDescriptor(obj, name).writable,
  248. "Expected obj[" + String(name) + "] to have writable:true.");
  249. }
  250. if (!isWritable(obj, name, verifyProp, value)) {
  251. $ERROR("Expected obj[" + String(name) + "] to be writable, but was not.");
  252. }
  253. }
  254.  
  255. function verifyNotWritable(obj, name, verifyProp, value) {
  256. if (!verifyProp) {
  257. assert(!Object.getOwnPropertyDescriptor(obj, name).writable,
  258. "Expected obj[" + String(name) + "] to have writable:false.");
  259. }
  260. if (isWritable(obj, name, verifyProp)) {
  261. $ERROR("Expected obj[" + String(name) + "] NOT to be writable, but was.");
  262. }
  263. }
  264.  
  265. function verifyEnumerable(obj, name) {
  266. assert(Object.getOwnPropertyDescriptor(obj, name).enumerable,
  267. "Expected obj[" + String(name) + "] to have enumerable:true.");
  268. if (!isEnumerable(obj, name)) {
  269. $ERROR("Expected obj[" + String(name) + "] to be enumerable, but was not.");
  270. }
  271. }
  272.  
  273. function verifyNotEnumerable(obj, name) {
  274. assert(!Object.getOwnPropertyDescriptor(obj, name).enumerable,
  275. "Expected obj[" + String(name) + "] to have enumerable:false.");
  276. if (isEnumerable(obj, name)) {
  277. $ERROR("Expected obj[" + String(name) + "] NOT to be enumerable, but was.");
  278. }
  279. }
  280.  
  281. function verifyConfigurable(obj, name) {
  282. assert(Object.getOwnPropertyDescriptor(obj, name).configurable,
  283. "Expected obj[" + String(name) + "] to have configurable:true.");
  284. if (!isConfigurable(obj, name)) {
  285. $ERROR("Expected obj[" + String(name) + "] to be configurable, but was not.");
  286. }
  287. }
  288.  
  289. function verifyNotConfigurable(obj, name) {
  290. assert(!Object.getOwnPropertyDescriptor(obj, name).configurable,
  291. "Expected obj[" + String(name) + "] to have configurable:false.");
  292. if (isConfigurable(obj, name)) {
  293. $ERROR("Expected obj[" + String(name) + "] NOT to be configurable, but was.");
  294. }
  295. }
  296. // Copyright (c) 2012 Ecma International. All rights reserved.
  297. // This code is governed by the BSD license found in the LICENSE file.
  298. /*---
  299. description: |
  300. Provides both:
  301.  
  302. - An error class to avoid false positives when testing for thrown exceptions
  303. - A function to explicitly throw an exception using the Test262Error class
  304. ---*/
  305.  
  306.  
  307. function Test262Error(message) {
  308. this.message = message || "";
  309. }
  310.  
  311. Test262Error.prototype.toString = function () {
  312. return "Test262Error: " + this.message;
  313. };
  314.  
  315. var $ERROR;
  316. $ERROR = function $ERROR(message) {
  317. throw new Test262Error(message);
  318. };
  319. // Copyright (C) 2015 the V8 project authors. All rights reserved.
  320. // This code is governed by the BSD license found in the LICENSE file.
  321. /*---
  322. esid: sec-array.prototype.fill
  323. es6id: 22.1.3.6
  324. description: >
  325. Return abrupt from ToLength(Get(O, "length")).
  326. info: |
  327. 22.1.3.6 Array.prototype.fill (value [ , start [ , end ] ] )
  328.  
  329. 1. Let O be ToObject(this value).
  330. 2. ReturnIfAbrupt(O).
  331. 3. Let len be ToLength(Get(O, "length")).
  332. 4. ReturnIfAbrupt(len).
  333. ---*/
  334.  
  335. var o1 = {};
  336.  
  337. Object.defineProperty(o1, 'length', {
  338. get: function() {
  339. throw new Test262Error();
  340. },
  341. configurable: true
  342. });
  343. assert.throws(Test262Error, function() {
  344. [].fill.call(o1, 1);
  345. });
  346.  
  347. var o2 = {
  348. length: {
  349. valueOf: function() {
  350. throw new Test262Error();
  351. }
  352. }
  353. };
  354. assert.throws(Test262Error, function() {
  355. [].fill.call(o2, 1);
  356. });
  357. // Copyright (C) 2015 the V8 project authors. All rights reserved.
  358. // This code is governed by the BSD license found in the LICENSE file.
  359. /*---
  360. esid: sec-array.prototype.fill
  361. es6id: 22.1.3.6
  362. description: >
  363. Return abrupt from ToLength(Get(O, "length")) where length is a Symbol.
  364. info: |
  365. 22.1.3.6 Array.prototype.fill (value [ , start [ , end ] ] )
  366.  
  367. 1. Let O be ToObject(this value).
  368. 2. ReturnIfAbrupt(O).
  369. 3. Let len be ToLength(Get(O, "length")).
  370. 4. ReturnIfAbrupt(len).
  371. features: [Symbol]
  372. ---*/
  373.  
  374. var o = {};
  375.  
  376. o.length = Symbol(1);
  377.  
  378. // value argument is given to avoid false positives
  379. assert.throws(TypeError, function() {
  380. [].fill.call(o, 1);
  381. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement