mirror of
				https://github.com/tc39/test262.git
				synced 2025-10-31 19:53:50 +01:00 
			
		
		
		
	Move compareArray into assert.js (#4592)
This commit is contained in:
		
							parent
							
								
									2e7412547d
								
							
						
					
					
						commit
						06dfddc4eb
					
				| @ -290,6 +290,7 @@ Function | Purpose | ||||
| `assert.sameValue(actual, expected, message)` | throw a new Test262Error instance if the first two arguments are not [the same value](https://tc39.github.io/ecma262/#sec-samevalue); accepts an optional string message explaining the scenario and what should have happened | ||||
| `assert.notSameValue(actual, unexpected, message)` | throw a new Test262Error instance if the first two arguments are [the same value](https://tc39.github.io/ecma262/#sec-samevalue); accepts an optional string message explaining the scenario and what should have happened | ||||
| `assert.throws(expectedErrorConstructor, fn, message)` | throw a new Test262Error instance if the provided function does not throw an error or if the constructor of the value thrown does not match the provided constructor; accepts an optional string message explaining the scenario and what should have happened | ||||
| `assert.compareArray(actual, expected, message)` | throw a new Test262Error instance if the first two arguments have differing `length` or there is an array index less than that length at which their respective elements are not [the same value](https://tc39.github.io/ecma262/#sec-samevalue); accepts an optional string message explaining the scenario and what should have happened | ||||
| `$DONOTEVALUATE()` | throw an exception if the code gets evaluated. This may only be used in [negative test cases for parsing errors](#handling-errors-and-negative-test-cases). | ||||
| `throw "Test262: This statement should not be evaluated.";` | throw an exception if the code gets evaluated. Use this if the test file has the `raw` flag and it's a negative test case for parsing error. | ||||
| 
 | ||||
|  | ||||
| @ -101,6 +101,45 @@ assert.throws = function (expectedErrorConstructor, func, message) { | ||||
|   throw new Test262Error(message); | ||||
| }; | ||||
| 
 | ||||
| function isPrimitive(value) { | ||||
|   return !value || (typeof value !== 'object' && typeof value !== 'function'); | ||||
| } | ||||
| 
 | ||||
| assert.compareArray = function (actual, expected, message) { | ||||
|   message = message === undefined ? '' : message; | ||||
| 
 | ||||
|   if (typeof message === 'symbol') { | ||||
|     message = message.toString(); | ||||
|   } | ||||
| 
 | ||||
|   if (isPrimitive(actual)) { | ||||
|     assert(false, `Actual argument [${actual}] shouldn't be primitive. ${message}`); | ||||
|   } else if (isPrimitive(expected)) { | ||||
|     assert(false, `Expected argument [${expected}] shouldn't be primitive. ${message}`); | ||||
|   } | ||||
|   var result = compareArray(actual, expected); | ||||
|   if (result) return; | ||||
| 
 | ||||
|   var format = compareArray.format; | ||||
|   assert(false, `Actual ${format(actual)} and expected ${format(expected)} should have the same contents. ${message}`); | ||||
| }; | ||||
| 
 | ||||
| function compareArray(a, b) { | ||||
|   if (b.length !== a.length) { | ||||
|     return false; | ||||
|   } | ||||
|   for (var i = 0; i < a.length; i++) { | ||||
|     if (!assert._isSameValue(b[i], a[i])) { | ||||
|       return false; | ||||
|     } | ||||
|   } | ||||
|   return true; | ||||
| } | ||||
| 
 | ||||
| compareArray.format = function (arrayLike) { | ||||
|   return `[${Array.prototype.map.call(arrayLike, String).join(', ')}]`; | ||||
| }; | ||||
| 
 | ||||
| assert._formatIdentityFreeValue = function formatIdentityFreeValue(value) { | ||||
|   switch (value === null ? 'null' : typeof value) { | ||||
|     case 'string': | ||||
|  | ||||
| @ -2,49 +2,6 @@ | ||||
| // This code is governed by the BSD license found in the LICENSE file.
 | ||||
| /*--- | ||||
| description: | | ||||
|     Compare the contents of two arrays | ||||
|     Deprecated now that compareArray is defined in assert.js. | ||||
| defines: [compareArray] | ||||
| ---*/ | ||||
| 
 | ||||
| function compareArray(a, b) { | ||||
|   if (b.length !== a.length) { | ||||
|     return false; | ||||
|   } | ||||
| 
 | ||||
|   for (var i = 0; i < a.length; i++) { | ||||
|     if (!compareArray.isSameValue(b[i], a[i])) { | ||||
|       return false; | ||||
|     } | ||||
|   } | ||||
|   return true; | ||||
| } | ||||
| 
 | ||||
| compareArray.isSameValue = function(a, b) { | ||||
|   if (a === 0 && b === 0) return 1 / a === 1 / b; | ||||
|   if (a !== a && b !== b) return true; | ||||
| 
 | ||||
|   return a === b; | ||||
| }; | ||||
| 
 | ||||
| compareArray.format = function(arrayLike) { | ||||
|   return `[${[].map.call(arrayLike, String).join(', ')}]`; | ||||
| }; | ||||
| 
 | ||||
| assert.compareArray = function(actual, expected, message) { | ||||
|   message  = message === undefined ? '' : message; | ||||
| 
 | ||||
|   if (typeof message === 'symbol') { | ||||
|     message = message.toString(); | ||||
|   } | ||||
| 
 | ||||
|   assert(actual != null, `Actual argument shouldn't be nullish. ${message}`); | ||||
|   assert(expected != null, `Expected argument shouldn't be nullish. ${message}`); | ||||
|   var format = compareArray.format; | ||||
|   var result = compareArray(actual, expected); | ||||
| 
 | ||||
|   // The following prevents actual and expected from being iterated and evaluated
 | ||||
|   // more than once unless absolutely necessary.
 | ||||
|   if (!result) { | ||||
|     assert(false, `Actual ${format(actual)} and expected ${format(expected)} should have the same contents. ${message}`); | ||||
|   } | ||||
| }; | ||||
|  | ||||
| @ -20,9 +20,9 @@ function assertThrows(func, errorMessage) { | ||||
|     assert(caught, `Expected ${func} to throw, but it didn't.`); | ||||
| } | ||||
| 
 | ||||
| assertThrows(() => assert.compareArray(), "Actual argument shouldn't be nullish. "); | ||||
| assertThrows(() => assert.compareArray(null, []), "Actual argument shouldn't be nullish. "); | ||||
| assertThrows(() => assert.compareArray(null, [], "foo"), "Actual argument shouldn't be nullish. foo"); | ||||
| assertThrows(() => assert.compareArray(), "Actual argument [undefined] shouldn't be primitive. "); | ||||
| assertThrows(() => assert.compareArray(null, []), "Actual argument [null] shouldn't be primitive. "); | ||||
| assertThrows(() => assert.compareArray(null, [], "foo"), "Actual argument [null] shouldn't be primitive. foo"); | ||||
| 
 | ||||
| assertThrows(() => assert.compareArray([]), "Expected argument shouldn't be nullish. "); | ||||
| assertThrows(() => assert.compareArray([], undefined, "foo"), "Expected argument shouldn't be nullish. foo"); | ||||
| assertThrows(() => assert.compareArray([]), "Expected argument [undefined] shouldn't be primitive. "); | ||||
| assertThrows(() => assert.compareArray([], undefined, "foo"), "Expected argument [undefined] shouldn't be primitive. foo"); | ||||
|  | ||||
| @ -4,25 +4,24 @@ | ||||
| /*--- | ||||
| description: Test replace function with duplicate names in alteration. | ||||
| features: [regexp-duplicate-named-groups] | ||||
| includes: [compareArray.js] | ||||
| ---*/ | ||||
| 
 | ||||
| assert.compareArray( | ||||
| assert.sameValue( | ||||
|     '2xyy', 'xxyy'.replace(/(?:(?:(?<a>x)|(?<a>y))\k<a>)/, '2$<a>')); | ||||
| assert.compareArray( | ||||
| assert.sameValue( | ||||
|     'x2zyyxxy', | ||||
|     'xzzyyxxy'.replace( | ||||
|         /(?:(?:(?<a>x)|(?<a>y)|(a)|(?<b>b)|(?<a>z))\k<a>)/, '2$<a>')); | ||||
| assert.compareArray( | ||||
| assert.sameValue( | ||||
|     '2x(x,)yy', 'xxyy'.replace(/(?:(?:(?<a>x)|(?<a>y))\k<a>)/, '2$<a>($1,$2)')); | ||||
| assert.compareArray( | ||||
| assert.sameValue( | ||||
|     'x2z(,,,,z)yyxxy', | ||||
|     'xzzyyxxy'.replace( | ||||
|         /(?:(?:(?<a>x)|(?<a>y)|(a)|(?<b>b)|(?<a>z))\k<a>)/, | ||||
|         '2$<a>($1,$2,$3,$4,$5)')); | ||||
| assert.compareArray( | ||||
| assert.sameValue( | ||||
|     '2x2y', 'xxyy'.replace(/(?:(?:(?<a>x)|(?<a>y))\k<a>)/g, '2$<a>')); | ||||
| assert.compareArray( | ||||
| assert.sameValue( | ||||
|     'x2z2y2xy', | ||||
|     'xzzyyxxy'.replace( | ||||
|         /(?:(?:(?<a>x)|(?<a>y)|(a)|(?<b>b)|(?<a>z))\k<a>)/g, '2$<a>')); | ||||
|  | ||||
| @ -4,10 +4,9 @@ | ||||
| /*--- | ||||
| description: Test search function with duplicate names in alteration. | ||||
| features: [regexp-duplicate-named-groups] | ||||
| includes: [compareArray.js] | ||||
| ---*/ | ||||
| 
 | ||||
| assert.compareArray(3, 'abcxyz'.search(/(?<a>x)|(?<a>y)/)); | ||||
| assert.compareArray(3, 'abcxyz'.search(/(?<a>y)|(?<a>x)/)); | ||||
| assert.compareArray(1, 'aybcxyz'.search(/(?<a>x)|(?<a>y)/)); | ||||
| assert.compareArray(1, 'aybcxyz'.search(/(?<a>y)|(?<a>x)/)); | ||||
| assert.sameValue(3, 'abcxyz'.search(/(?<a>x)|(?<a>y)/)); | ||||
| assert.sameValue(3, 'abcxyz'.search(/(?<a>y)|(?<a>x)/)); | ||||
| assert.sameValue(1, 'aybcxyz'.search(/(?<a>x)|(?<a>y)/)); | ||||
| assert.sameValue(1, 'aybcxyz'.search(/(?<a>y)|(?<a>x)/)); | ||||
|  | ||||
| @ -21,19 +21,19 @@ assert.compareArray("\uD83D\uDC38\uD83D\uDC39X\uD83D\uDC3A".match(/\uD83D\uDC38| | ||||
| // ==== String.prototype.replace ====
 | ||||
| 
 | ||||
| // empty string replacement (optimized)
 | ||||
| assert.compareArray("\uD83D\uDC38\uD83D\uDC39X\uD83D\uDC3A".replace(/\uD83D|X|/gu, ""), | ||||
| assert.sameValue("\uD83D\uDC38\uD83D\uDC39X\uD83D\uDC3A".replace(/\uD83D|X|/gu, ""), | ||||
|               "\uD83D\uDC38\uD83D\uDC39\uD83D\uDC3A"); | ||||
| assert.compareArray("\uD83D\uDC38\uD83D\uDC39X\uD83D\uDC3A".replace(/\uDC38|X|/gu, ""), | ||||
| assert.sameValue("\uD83D\uDC38\uD83D\uDC39X\uD83D\uDC3A".replace(/\uDC38|X|/gu, ""), | ||||
|               "\uD83D\uDC38\uD83D\uDC39\uD83D\uDC3A"); | ||||
| assert.compareArray("\uD83D\uDC38\uD83D\uDC39X\uD83D\uDC3A".replace(/\uD83D\uDC38|X|/gu, ""), | ||||
| assert.sameValue("\uD83D\uDC38\uD83D\uDC39X\uD83D\uDC3A".replace(/\uD83D\uDC38|X|/gu, ""), | ||||
|               "\uD83D\uDC39\uD83D\uDC3A"); | ||||
| 
 | ||||
| // non-empty string replacement
 | ||||
| assert.compareArray("\uD83D\uDC38\uD83D\uDC39X\uD83D\uDC3A".replace(/\uD83D|X|/gu, "x"), | ||||
| assert.sameValue("\uD83D\uDC38\uD83D\uDC39X\uD83D\uDC3A".replace(/\uD83D|X|/gu, "x"), | ||||
|               "x\uD83D\uDC38x\uD83D\uDC39xx\uD83D\uDC3Ax"); | ||||
| assert.compareArray("\uD83D\uDC38\uD83D\uDC39X\uD83D\uDC3A".replace(/\uDC38|X|/gu, "x"), | ||||
| assert.sameValue("\uD83D\uDC38\uD83D\uDC39X\uD83D\uDC3A".replace(/\uDC38|X|/gu, "x"), | ||||
|               "x\uD83D\uDC38x\uD83D\uDC39xx\uD83D\uDC3Ax"); | ||||
| assert.compareArray("\uD83D\uDC38\uD83D\uDC39X\uD83D\uDC3A".replace(/\uD83D\uDC38|X|/gu, "x"), | ||||
| assert.sameValue("\uD83D\uDC38\uD83D\uDC39X\uD83D\uDC3A".replace(/\uD83D\uDC38|X|/gu, "x"), | ||||
|               "xx\uD83D\uDC39xx\uD83D\uDC3Ax"); | ||||
| 
 | ||||
| // ==== String.prototype.split ====
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user