Skip to content

Commit 94b86b1

Browse files
committed
Reduce duplication in order-of-operations tests
Build expected AO calls for expectedOpsForPrimitiveOptions and reuse property bag observers after clearing state. Thanks to @ptomato for the suggestions.
1 parent 6ee0c4a commit 94b86b1

33 files changed

Lines changed: 156 additions & 1345 deletions

test/built-ins/Temporal/Duration/compare/order-of-operations.js

Lines changed: 4 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ includes: [compareArray.js, temporalHelpers.js]
88
features: [Temporal]
99
---*/
1010

11-
const expected = [
11+
const expectedOpsForPrimitiveOptions = [
1212
// ToTemporalDuration on first argument
1313
"get one.days",
1414
"get one.days.valueOf",
@@ -71,9 +71,11 @@ const expected = [
7171
"get two.years",
7272
"get two.years.valueOf",
7373
"call two.years.valueOf",
74+
];
75+
const expected = expectedOpsForPrimitiveOptions.concat([
7476
// ToRelativeTemporalObject
7577
"get options.relativeTo",
76-
];
78+
]);
7779
const actual = [];
7880

7981
// basic order of observable operations with no relativeTo
@@ -85,71 +87,6 @@ Temporal.Duration.compare(
8587
assert.compareArray(actual, expected, "order of operations");
8688
actual.splice(0); // clear
8789

88-
const expectedOpsForPrimitiveOptions = [
89-
// ToTemporalDuration on first argument
90-
"get one.days",
91-
"get one.days.valueOf",
92-
"call one.days.valueOf",
93-
"get one.hours",
94-
"get one.hours.valueOf",
95-
"call one.hours.valueOf",
96-
"get one.microseconds",
97-
"get one.microseconds.valueOf",
98-
"call one.microseconds.valueOf",
99-
"get one.milliseconds",
100-
"get one.milliseconds.valueOf",
101-
"call one.milliseconds.valueOf",
102-
"get one.minutes",
103-
"get one.minutes.valueOf",
104-
"call one.minutes.valueOf",
105-
"get one.months",
106-
"get one.months.valueOf",
107-
"call one.months.valueOf",
108-
"get one.nanoseconds",
109-
"get one.nanoseconds.valueOf",
110-
"call one.nanoseconds.valueOf",
111-
"get one.seconds",
112-
"get one.seconds.valueOf",
113-
"call one.seconds.valueOf",
114-
"get one.weeks",
115-
"get one.weeks.valueOf",
116-
"call one.weeks.valueOf",
117-
"get one.years",
118-
"get one.years.valueOf",
119-
"call one.years.valueOf",
120-
// ToTemporalDuration on second argument
121-
"get two.days",
122-
"get two.days.valueOf",
123-
"call two.days.valueOf",
124-
"get two.hours",
125-
"get two.hours.valueOf",
126-
"call two.hours.valueOf",
127-
"get two.microseconds",
128-
"get two.microseconds.valueOf",
129-
"call two.microseconds.valueOf",
130-
"get two.milliseconds",
131-
"get two.milliseconds.valueOf",
132-
"call two.milliseconds.valueOf",
133-
"get two.minutes",
134-
"get two.minutes.valueOf",
135-
"call two.minutes.valueOf",
136-
"get two.months",
137-
"get two.months.valueOf",
138-
"call two.months.valueOf",
139-
"get two.nanoseconds",
140-
"get two.nanoseconds.valueOf",
141-
"call two.nanoseconds.valueOf",
142-
"get two.seconds",
143-
"get two.seconds.valueOf",
144-
"call two.seconds.valueOf",
145-
"get two.weeks",
146-
"get two.weeks.valueOf",
147-
"call two.weeks.valueOf",
148-
"get two.years",
149-
"get two.years.valueOf",
150-
"call two.years.valueOf",
151-
];
152-
15390
assert.throws(TypeError, () => Temporal.Duration.compare(
15491
createDurationPropertyBagObserver("one", 0, 0, 0, 7),
15592
createDurationPropertyBagObserver("two", 0, 0, 0, 6),

test/built-ins/Temporal/Instant/prototype/since/order-of-operations.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ includes: [compareArray.js, temporalHelpers.js]
88
features: [Temporal]
99
---*/
1010

11-
const expected = [
11+
const expectedOpsForPrimitiveOptions = [
1212
"get other.toString",
1313
"call other.toString",
14+
];
15+
const expected = expectedOpsForPrimitiveOptions.concat([
1416
"get options.largestUnit",
1517
"get options.largestUnit.toString",
1618
"call options.largestUnit.toString",
@@ -23,7 +25,7 @@ const expected = [
2325
"get options.smallestUnit",
2426
"get options.smallestUnit.toString",
2527
"call options.smallestUnit.toString",
26-
];
28+
]);
2729
const actual = [];
2830

2931
const instance = new Temporal.Instant(1_000_000_000_000_000_000n);
@@ -46,11 +48,6 @@ assert.compareArray(actual, expected, "order of operations with identical instan
4648

4749
actual.splice(0); // clear
4850

49-
const expectedOpsForPrimitiveOptions = [
50-
"get other.toString",
51-
"call other.toString",
52-
];
53-
5451
assert.throws(TypeError, () => instance.since(TemporalHelpers.toPrimitiveObserver(actual, "1970-01-01T00:00Z", "other"), null));
5552
assert.compareArray(actual, expectedOpsForPrimitiveOptions,
5653
"other instant is converted before TypeError is thrown for primitive options");

test/built-ins/Temporal/Instant/prototype/until/order-of-operations.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ includes: [compareArray.js, temporalHelpers.js]
88
features: [Temporal]
99
---*/
1010

11-
const expected = [
11+
const expectedOpsForPrimitiveOptions = [
1212
"get other.toString",
1313
"call other.toString",
14+
];
15+
const expected = expectedOpsForPrimitiveOptions.concat([
1416
"get options.largestUnit",
1517
"get options.largestUnit.toString",
1618
"call options.largestUnit.toString",
@@ -23,7 +25,7 @@ const expected = [
2325
"get options.smallestUnit",
2426
"get options.smallestUnit.toString",
2527
"call options.smallestUnit.toString",
26-
];
28+
]);
2729
const actual = [];
2830

2931
const instance = new Temporal.Instant(1_000_000_000_000_000_000n);
@@ -46,11 +48,6 @@ assert.compareArray(actual, expected, "order of operations with identical instan
4648

4749
actual.splice(0); // clear
4850

49-
const expectedOpsForPrimitiveOptions = [
50-
"get other.toString",
51-
"call other.toString",
52-
];
53-
5451
assert.throws(TypeError, () => instance.until(TemporalHelpers.toPrimitiveObserver(actual, "1970-01-01T00:00Z", "other"), null));
5552
assert.compareArray(actual, expectedOpsForPrimitiveOptions,
5653
"other instant is converted before TypeError is thrown for primitive options");

test/built-ins/Temporal/PlainDate/from/order-of-operations.js

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const expectedOptionsReading = [
1414
"call options.overflow.toString",
1515
];
1616

17-
const expected = [
17+
const expectedOpsForPrimitiveOptions = [
1818
"get fields.calendar",
1919
"get fields.day",
2020
"get fields.day.valueOf",
@@ -28,7 +28,8 @@ const expected = [
2828
"get fields.year",
2929
"get fields.year.valueOf",
3030
"call fields.year.valueOf",
31-
].concat(expectedOptionsReading);
31+
];
32+
const expected = expectedOpsForPrimitiveOptions.concat(expectedOptionsReading);
3233
const actual = [];
3334

3435
const fields = TemporalHelpers.propertyBagObserver(actual, {
@@ -69,32 +70,7 @@ assert.compareArray(actual, expectedOptionsReading, "order of operations when pa
6970

7071
actual.splice(0);
7172

72-
const expectedOpsForPrimitiveOptions = [
73-
"get fields.calendar",
74-
"get fields.day",
75-
"get fields.day.valueOf",
76-
"call fields.day.valueOf",
77-
"get fields.month",
78-
"get fields.month.valueOf",
79-
"call fields.month.valueOf",
80-
"get fields.monthCode",
81-
"get fields.monthCode.toString",
82-
"call fields.monthCode.toString",
83-
"get fields.year",
84-
"get fields.year.valueOf",
85-
"call fields.year.valueOf",
86-
];
87-
88-
// Non-integer values to ensure valueOf is called
89-
const fields2 = TemporalHelpers.propertyBagObserver(actual, {
90-
year: 1.7,
91-
month: 1.7,
92-
monthCode: "M01",
93-
day: 1.7,
94-
calendar: "iso8601",
95-
}, "fields", ["calendar"]);
96-
97-
assert.throws(TypeError, () => Temporal.PlainDate.from(fields2, null));
73+
assert.throws(TypeError, () => Temporal.PlainDate.from(fields, null));
9874
assert.compareArray(actual, expectedOpsForPrimitiveOptions,
9975
"item fields are read before TypeError is thrown for primitive options");
10076

test/built-ins/Temporal/PlainDate/prototype/add/order-of-operations.js

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ includes: [compareArray.js, temporalHelpers.js]
88
features: [Temporal]
99
---*/
1010

11-
const expected = [
11+
const expectedOpsForPrimitiveOptions = [
1212
// ToTemporalDurationRecord
1313
"get fields.days",
1414
"get fields.days.valueOf",
@@ -40,10 +40,12 @@ const expected = [
4040
"get fields.years",
4141
"get fields.years.valueOf",
4242
"call fields.years.valueOf",
43+
];
44+
const expected = expectedOpsForPrimitiveOptions.concat([
4345
"get options.overflow",
4446
"get options.overflow.toString",
4547
"call options.overflow.toString",
46-
];
48+
]);
4749
const actual = [];
4850

4951
const instance = new Temporal.PlainDate(2000, 5, 2, "iso8601");
@@ -70,54 +72,7 @@ assert.compareArray(actual, expected, "order of operations");
7072

7173
actual.splice(0); // clear
7274

73-
const expectedOpsForPrimitiveOptions = [
74-
// ToTemporalDurationRecord
75-
"get fields.days",
76-
"get fields.days.valueOf",
77-
"call fields.days.valueOf",
78-
"get fields.hours",
79-
"get fields.hours.valueOf",
80-
"call fields.hours.valueOf",
81-
"get fields.microseconds",
82-
"get fields.microseconds.valueOf",
83-
"call fields.microseconds.valueOf",
84-
"get fields.milliseconds",
85-
"get fields.milliseconds.valueOf",
86-
"call fields.milliseconds.valueOf",
87-
"get fields.minutes",
88-
"get fields.minutes.valueOf",
89-
"call fields.minutes.valueOf",
90-
"get fields.months",
91-
"get fields.months.valueOf",
92-
"call fields.months.valueOf",
93-
"get fields.nanoseconds",
94-
"get fields.nanoseconds.valueOf",
95-
"call fields.nanoseconds.valueOf",
96-
"get fields.seconds",
97-
"get fields.seconds.valueOf",
98-
"call fields.seconds.valueOf",
99-
"get fields.weeks",
100-
"get fields.weeks.valueOf",
101-
"call fields.weeks.valueOf",
102-
"get fields.years",
103-
"get fields.years.valueOf",
104-
"call fields.years.valueOf",
105-
];
106-
107-
const fields2 = TemporalHelpers.propertyBagObserver(actual, {
108-
years: 1,
109-
months: 1,
110-
weeks: 1,
111-
days: 1,
112-
hours: 1,
113-
minutes: 1,
114-
seconds: 1,
115-
milliseconds: 1,
116-
microseconds: 1,
117-
nanoseconds: 1,
118-
}, "fields");
119-
120-
assert.throws(TypeError, () => instance.add(fields2, null));
75+
assert.throws(TypeError, () => instance.add(fields, null));
12176
assert.compareArray(actual, expectedOpsForPrimitiveOptions,
12277
"duration fields are read before TypeError is thrown for primitive options");
12378

test/built-ins/Temporal/PlainDate/prototype/since/order-of-operations.js

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ includes: [compareArray.js, temporalHelpers.js]
88
features: [Temporal]
99
---*/
1010

11-
const expected = [
11+
const expectedOpsForPrimitiveOptions = [
1212
// ToTemporalDate
1313
"get other.calendar",
1414
"get other.day",
@@ -23,6 +23,8 @@ const expected = [
2323
"get other.year",
2424
"get other.year.valueOf",
2525
"call other.year.valueOf",
26+
];
27+
const expected = expectedOpsForPrimitiveOptions.concat([
2628
// GetDifferenceSettings
2729
"get options.largestUnit",
2830
"get options.largestUnit.toString",
@@ -36,7 +38,7 @@ const expected = [
3638
"get options.smallestUnit",
3739
"get options.smallestUnit.toString",
3840
"call options.smallestUnit.toString",
39-
];
41+
]);
4042
const actual = [];
4143

4244
const instance = new Temporal.PlainDate(2000, 5, 2, "iso8601");
@@ -66,32 +68,7 @@ instance.since(otherDatePropertyBag, createOptionsObserver({ largestUnit: "years
6668
assert.compareArray(actual, expected, "order of operations");
6769
actual.splice(0); // clear
6870

69-
const expectedOpsForPrimitiveOptions = [
70-
// ToTemporalDate
71-
"get other.calendar",
72-
"get other.day",
73-
"get other.day.valueOf",
74-
"call other.day.valueOf",
75-
"get other.month",
76-
"get other.month.valueOf",
77-
"call other.month.valueOf",
78-
"get other.monthCode",
79-
"get other.monthCode.toString",
80-
"call other.monthCode.toString",
81-
"get other.year",
82-
"get other.year.valueOf",
83-
"call other.year.valueOf",
84-
];
85-
86-
const otherDatePropertyBag2 = TemporalHelpers.propertyBagObserver(actual, {
87-
year: 2001,
88-
month: 6,
89-
monthCode: "M06",
90-
day: 2,
91-
calendar: "iso8601",
92-
}, "other", ["calendar"]);
93-
94-
assert.throws(TypeError, () => instance.since(otherDatePropertyBag2, null));
71+
assert.throws(TypeError, () => instance.since(otherDatePropertyBag, null));
9572
assert.compareArray(actual, expectedOpsForPrimitiveOptions,
9673
"other date fields are read before TypeError is thrown for primitive options");
9774
actual.splice(0); // clear

0 commit comments

Comments
 (0)