Skip to content

Commit

Permalink
Sync test of Temporal.Calendar.p*.fields to 1750 (#3188)
Browse files Browse the repository at this point in the history
* 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 <Ms2ger@gmail.com>

* Update test/built-ins/Temporal/Calendar/prototype/fields/long-input.js

Co-authored-by: Ms2ger <Ms2ger@gmail.com>

* Update test/built-ins/Temporal/Calendar/prototype/fields/repeated-throw.js

Co-authored-by: Ms2ger <Ms2ger@gmail.com>

* Update test/built-ins/Temporal/Calendar/prototype/fields/reverse.js

Co-authored-by: Ms2ger <Ms2ger@gmail.com>

* Update test/built-ins/Temporal/Calendar/prototype/fields/reverse.js

Co-authored-by: Ms2ger <Ms2ger@gmail.com>

* Remove loop

* Update test/built-ins/Temporal/Calendar/prototype/fields/long-input.js

Co-authored-by: Ms2ger <Ms2ger@gmail.com>

* 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 <Ms2ger@gmail.com>
  • Loading branch information
FrankYFTang and Ms2ger authored Oct 6, 2021
1 parent d5ac0c3 commit 61339fd
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 14 deletions.
44 changes: 30 additions & 14 deletions test/built-ins/Temporal/Calendar/prototype/fields/long-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Original file line number Diff line number Diff line change
@@ -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");
});
42 changes: 42 additions & 0 deletions test/built-ins/Temporal/Calendar/prototype/fields/reverse.js
Original file line number Diff line number Diff line change
@@ -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');

0 comments on commit 61339fd

Please sign in to comment.