From 61339fd294087b22baa8edb306c83c972e1dbe51 Mon Sep 17 00:00:00 2001 From: Frank Yung-Fong Tang Date: Tue, 5 Oct 2021 20:03:07 -0700 Subject: [PATCH] Sync test of Temporal.Calendar.p*.fields to 1750 (#3188) * Sync test of Temporal.Calendar.p*.fields to 1750 https://github.com/tc39/proposal-temporal/pull * add more test * add more tests for T*.Calendar.p*.fields * Update test/built-ins/Temporal/Calendar/prototype/fields/long-input.js Co-authored-by: Ms2ger * Update test/built-ins/Temporal/Calendar/prototype/fields/long-input.js Co-authored-by: Ms2ger * Update test/built-ins/Temporal/Calendar/prototype/fields/repeated-throw.js Co-authored-by: Ms2ger * Update test/built-ins/Temporal/Calendar/prototype/fields/reverse.js Co-authored-by: Ms2ger * Update test/built-ins/Temporal/Calendar/prototype/fields/reverse.js Co-authored-by: Ms2ger * Remove loop * Update test/built-ins/Temporal/Calendar/prototype/fields/long-input.js Co-authored-by: Ms2ger * Update long-input.js * Update repeated-throw.js * Update reverse.js * ensure the implementation check the content make sure the validation does not happen after the looping the generator * add test to check all valid field value Co-authored-by: Ms2ger --- .../Calendar/prototype/fields/long-input.js | 44 ++++++++++----- .../prototype/fields/repeated-throw.js | 56 +++++++++++++++++++ .../Calendar/prototype/fields/reverse.js | 42 ++++++++++++++ 3 files changed, 128 insertions(+), 14 deletions(-) create mode 100644 test/built-ins/Temporal/Calendar/prototype/fields/repeated-throw.js create mode 100644 test/built-ins/Temporal/Calendar/prototype/fields/reverse.js diff --git a/test/built-ins/Temporal/Calendar/prototype/fields/long-input.js b/test/built-ins/Temporal/Calendar/prototype/fields/long-input.js index 03c7314d7de..9ed52dc9f71 100644 --- a/test/built-ins/Temporal/Calendar/prototype/fields/long-input.js +++ b/test/built-ins/Temporal/Calendar/prototype/fields/long-input.js @@ -4,28 +4,44 @@ /*--- esid: sec-temporal.calendar.prototype.fields description: > - Temporal.Calendar.prototype.fields will take iterable of any size and any string - and return Array of the same content. + Temporal.Calendar.prototype.fields will throw when its input iterable yields an + invalid field. info: | ## 12.4.21 Temporal.Calendar.prototype.fields ( fields ) 1. Let calendar be the this value. 2. Perform ? RequireInternalSlot(calendar, [[InitializedTemporalCalendar]]). - 3. Assert: calendar.[[Identifier]] is "iso8601". - 4. Let fieldNames be ? IterableToListOfType(fields, « String »). - 5. Return ! CreateArrayFromList(fieldNames). + 4. Let iteratorRecord be ? Getiterator(fields, sync). + 5. Let fieldNames be a new empty List. + 6. Let next be true. + 7. Repeat, while next is not false, + a. Set next to ? IteratorStep(iteratorRecord). + b. If next is not false, then + i. Let nextValue be ? IteratorValue(next). + iv. If nextValue is not one of "year", "month", "monthCode", "day", "hour", "minute", "second", "millisecond", "microsecond", "nanosecond", then + 1. Let completion be ThrowCompletion(a newly created RangeError object). + 2. Return ? IteratorClose(iteratorRecord, completion). features: [Symbol, Symbol.iterator, Temporal, computed-property-names, generators] -includes: [compareArray.js] ---*/ let cal = new Temporal.Calendar("iso8601") +let i = 0; const fields = { *[Symbol.iterator]() { - let i = 0; - while (i++ < 1000001) { - yield "garbage " + i; - } + // The first three are valid values + yield "year"; + i++; + yield "month"; + i++; + yield "monthCode"; + i++; + // The fourth one is wrong and should throw after the next line. + yield "garbage"; + // The following three lines should not be reached if the implemention + // correctly check the previous line. + i++; + yield "hour"; + i++; } } -assert( - compareArray(cal.fields(fields), Array.from(fields)), - 'compareArray(cal.fields(fields), Array.from(fields)) must return true' -); +assert.throws(RangeError, () => cal.fields(fields), "Garbage content"); +// stop after the third one. +assert.sameValue(i, 3); diff --git a/test/built-ins/Temporal/Calendar/prototype/fields/repeated-throw.js b/test/built-ins/Temporal/Calendar/prototype/fields/repeated-throw.js new file mode 100644 index 00000000000..97d9ba22d4d --- /dev/null +++ b/test/built-ins/Temporal/Calendar/prototype/fields/repeated-throw.js @@ -0,0 +1,56 @@ +// Copyright (C) 2021 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.calendar.prototype.fields +description: > + Temporal.Calendar.prototype.fields will throw if its input iterable yields + the same value twice. +info: | + ## 12.4.21 Temporal.Calendar.prototype.fields ( fields ) + 1. Let calendar be the this value. + 2. Perform ? RequireInternalSlot(calendar, [[InitializedTemporalCalendar]]). + 4. Let iteratorRecord be ? Getiterator(fields, sync). + 5. Let fieldNames be a new empty List. + 6. Let next be true. + 7. Repeat, while next is not false, + a. Set next to ? IteratorStep(iteratorRecord). + b. If next is not false, then + i. Let nextValue be ? IteratorValue(next). + iii. If fieldNames contains nextValue, then + 1. Let completion be ThrowCompletion(a newly created RangeError object). + 2. Return ? IteratorClose(iteratorRecord, completion). +features: [Symbol, Symbol.iterator, Temporal, computed-property-names, generators] +---*/ +let cal = new Temporal.Calendar("iso8601") +let i = 0; +const fields = { + *[Symbol.iterator]() { + yield "month"; + i++; + yield "year"; + i++; + yield "year"; + i++; + } +} +assert.throws( + RangeError, () => cal.fields(fields), "repeated valid value should throw"); +assert.sameValue(i, 2, "Should stop at 2"); + +// Test all valid value will throw while repeate +[ "nanosecond", "microsecond", "millisecond", "second", + "minute", "hour", "day", "monthCode", "month", "year" ].forEach((f) => { + i = 0; + const fields2 = { + *[Symbol.iterator]() { + yield f; + i++; + yield f; + i++; + } + } + assert.throws( + RangeError, () => cal.fields(fields2), "repeated valid value should throw"); + assert.sameValue(i, 1, "Should stop at 1"); +}); diff --git a/test/built-ins/Temporal/Calendar/prototype/fields/reverse.js b/test/built-ins/Temporal/Calendar/prototype/fields/reverse.js new file mode 100644 index 00000000000..e0e099ac8c0 --- /dev/null +++ b/test/built-ins/Temporal/Calendar/prototype/fields/reverse.js @@ -0,0 +1,42 @@ +// Copyright (C) 2021 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.calendar.prototype.fields +description: > + Temporal.Calendar.prototype.fields will return the iterable in array if all + input are valid regardless of it's order. +info: | + ## 12.4.21 Temporal.Calendar.prototype.fields ( fields ) + 1. Let calendar be the this value. + 2. Perform ? RequireInternalSlot(calendar, [[InitializedTemporalCalendar]]). + 4. Let iteratorRecord be ? Getiterator(fields, sync). + 5. Let fieldNames be a new empty List. + 6. Let next be true. + 7. Repeat, while next is not false, + a. Set next to ? IteratorStep(iteratorRecord). + b. If next is not false, then + i. Let nextValue be ? IteratorValue(next). + iv. If nextValue is not one of "year", "month", "monthCode", "day", "hour", "minute", "second", "millisecond", "microsecond", "nanosecond", then + 1. Let completion be ThrowCompletion(a newly created RangeError object). + 2. Return ? IteratorClose(iteratorRecord, completion). +features: [Symbol, Symbol.iterator, Temporal, computed-property-names, generators] +includes: [compareArray.js] +---*/ +let cal = new Temporal.Calendar("iso8601") +const fields = { + *[Symbol.iterator]() { + yield "nanosecond"; + yield "microsecond"; + yield "millisecond"; + yield "second"; + yield "minute"; + yield "hour"; + yield "day"; + yield "monthCode"; + yield "month"; + yield "year"; + } +} +assert.compareArray(cal.fields(fields), Array.from(fields), + 'valid fields should be supported even if they are in reversed order of the spec');