diff --git a/test/built-ins/Temporal/PlainDate/prototype/equals/argument-object-invalid.js b/test/built-ins/Temporal/PlainDate/prototype/equals/argument-object-invalid.js new file mode 100644 index 0000000000..5755940fdc --- /dev/null +++ b/test/built-ins/Temporal/PlainDate/prototype/equals/argument-object-invalid.js @@ -0,0 +1,20 @@ +// Copyright (C) 2020 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.plaindate.prototype.equals +description: Appropriate error thrown when object argument is invalid +features: [Temporal] +---*/ + +const instance = Temporal.PlainDate.from({ year: 2000, month: 5, day: 2 }); + +// End up in ISODateFromFields with missing fields +assert.throws(TypeError, () => instance.equals({}), "plain object"); +assert.throws(TypeError, () => instance.equals({ year: 1972, month: 7 }), "only year, month"); +assert.throws(TypeError, () => instance.equals({ year: 1972, month: 7 }), "only year, month"); +assert.throws(TypeError, () => instance.equals({ year: 1972, day: 7 }), "only year, day"); +assert.throws(TypeError, () => instance.equals(Temporal.PlainDate), "Temporal.PlainDate"); + +// Tries to get fields with an invalid receiver +assert.throws(TypeError, () => instance.equals(Temporal.PlainDate.prototype), "Temporal.PlainDate.prototype"); diff --git a/test/built-ins/Temporal/PlainDate/prototype/equals/argument-object-valid.js b/test/built-ins/Temporal/PlainDate/prototype/equals/argument-object-valid.js new file mode 100644 index 0000000000..c87e6925d9 --- /dev/null +++ b/test/built-ins/Temporal/PlainDate/prototype/equals/argument-object-valid.js @@ -0,0 +1,16 @@ +// Copyright (C) 2020 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.plaindate.prototype.equals +description: equals with a valid property bag +features: [Temporal] +---*/ + +const instance = new Temporal.PlainDate(2000, 5, 2); +assert.sameValue(instance.equals({ year: 2000, month: 5, day: 2 }), true, "same date"); +assert.sameValue(instance.equals({ year: 2000, month: 5, day: 4 }), false, "different date"); + +const calendar = { toString() { return "a" } }; +assert.sameValue(instance.withCalendar(calendar).equals({ year: 2000, month: 5, day: 2 }), + false, "different calendar"); diff --git a/test/built-ins/Temporal/PlainDate/prototype/equals/argument-string.js b/test/built-ins/Temporal/PlainDate/prototype/equals/argument-string.js new file mode 100644 index 0000000000..85e28ca874 --- /dev/null +++ b/test/built-ins/Temporal/PlainDate/prototype/equals/argument-string.js @@ -0,0 +1,15 @@ +// Copyright (C) 2021 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.plaindate.prototype.equals +description: equals with a valid string +features: [Temporal] +---*/ + +const instance = new Temporal.PlainDate(2000, 5, 2); +assert.sameValue(instance.equals("2000-05-02"), true, "same date"); +assert.sameValue(instance.equals("2000-05-04"), false, "different date"); + +const calendar = { toString() { return "a" } }; +assert.sameValue(instance.withCalendar(calendar).equals("2000-05-02"), false, "different calendar"); diff --git a/test/built-ins/Temporal/PlainDate/prototype/equals/argument-wrong-type.js b/test/built-ins/Temporal/PlainDate/prototype/equals/argument-wrong-type.js index 2e8088d767..2f61141c08 100644 --- a/test/built-ins/Temporal/PlainDate/prototype/equals/argument-wrong-type.js +++ b/test/built-ins/Temporal/PlainDate/prototype/equals/argument-wrong-type.js @@ -15,6 +15,3 @@ assert.throws(RangeError, () => instance.equals(true), "true"); assert.throws(RangeError, () => instance.equals(""), "empty string"); assert.throws(TypeError, () => instance.equals(Symbol()), "symbol"); assert.throws(RangeError, () => instance.equals(1), "1"); -assert.throws(TypeError, () => instance.equals({}), "plain object"); -assert.throws(TypeError, () => instance.equals(Temporal.PlainDate), "Temporal.PlainDate"); -assert.throws(TypeError, () => instance.equals(Temporal.PlainDate.prototype), "Temporal.PlainDate.prototype"); diff --git a/test/built-ins/Temporal/PlainDate/prototype/equals/basic.js b/test/built-ins/Temporal/PlainDate/prototype/equals/basic.js new file mode 100644 index 0000000000..3bb19205ba --- /dev/null +++ b/test/built-ins/Temporal/PlainDate/prototype/equals/basic.js @@ -0,0 +1,15 @@ +// Copyright (C) 2021 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.plaindate.protoype.equals +description: basic tests +features: [Temporal] +---*/ + +const date1 = new Temporal.PlainDate(1976, 11, 18); +const date2 = new Temporal.PlainDate(1914, 2, 23); +const date3 = new Temporal.PlainDate(1914, 2, 23); +assert.sameValue(date1.equals(date2), false, "different dates"); +assert.sameValue(date2.equals(date3), true, "same dates"); +assert.sameValue(date2.equals(date2), true, "same objects"); diff --git a/test/built-ins/Temporal/PlainDate/prototype/equals/calendar-call-different.js b/test/built-ins/Temporal/PlainDate/prototype/equals/calendar-call-different.js new file mode 100644 index 0000000000..b1e30dd710 --- /dev/null +++ b/test/built-ins/Temporal/PlainDate/prototype/equals/calendar-call-different.js @@ -0,0 +1,30 @@ +// Copyright (C) 2021 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.plaindate.protoype.equals +description: test if the calendar is compared +features: [Temporal] +---*/ + +class CalendarTraceToString extends Temporal.Calendar { + constructor(id) { + super("iso8601"); + this.id_ = id; + this.calls = 0; + } + toString() { + ++this.calls; + return this.id_; + } +}; + +const calendar1 = new CalendarTraceToString("a"); +const date1 = new Temporal.PlainDate(1914, 2, 23, calendar1); + +const calendar2 = new CalendarTraceToString("b"); +const date2 = new Temporal.PlainDate(1914, 2, 23, calendar2); + +assert.sameValue(date1.equals(date2), false, "different calendars"); +assert.sameValue(calendar1.calls, 1, "calendar1 toString() calls"); +assert.sameValue(calendar2.calls, 1, "calendar2 toString() calls"); diff --git a/test/built-ins/Temporal/PlainDate/prototype/equals/calendar-call-same.js b/test/built-ins/Temporal/PlainDate/prototype/equals/calendar-call-same.js new file mode 100644 index 0000000000..2fea62c77e --- /dev/null +++ b/test/built-ins/Temporal/PlainDate/prototype/equals/calendar-call-same.js @@ -0,0 +1,30 @@ +// Copyright (C) 2021 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.plaindate.protoype.equals +description: test if the calendar is compared +features: [Temporal] +---*/ + +class CalendarTraceToString extends Temporal.Calendar { + constructor(id) { + super("iso8601"); + this.id_ = id; + this.calls = 0; + } + toString() { + ++this.calls; + return this.id_; + } +}; + +const calendar1 = new CalendarTraceToString("a"); +const date1 = new Temporal.PlainDate(1914, 2, 23, calendar1); + +const calendar2 = new CalendarTraceToString("a"); +const date2 = new Temporal.PlainDate(1914, 2, 23, calendar2); + +assert.sameValue(date1.equals(date2), true, "same calendar id"); +assert.sameValue(calendar1.calls, 1, "calendar1 toString() calls"); +assert.sameValue(calendar2.calls, 1, "calendar2 toString() calls"); diff --git a/test/built-ins/Temporal/PlainDate/prototype/equals/calendar-no-call.js b/test/built-ins/Temporal/PlainDate/prototype/equals/calendar-no-call.js new file mode 100644 index 0000000000..95f485fa49 --- /dev/null +++ b/test/built-ins/Temporal/PlainDate/prototype/equals/calendar-no-call.js @@ -0,0 +1,30 @@ +// Copyright (C) 2021 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.plaindate.protoype.equals +description: test if the calendar is compared +features: [Temporal] +---*/ + +class CalendarTraceToString extends Temporal.Calendar { + constructor(id) { + super("iso8601"); + this.id_ = id; + this.calls = 0; + } + toString() { + ++this.calls; + return this.id_; + } +}; + +const calendar1 = new CalendarTraceToString("a"); +const date1 = new Temporal.PlainDate(1914, 2, 23, calendar1); + +const calendar2 = new CalendarTraceToString("b"); +const date2 = new Temporal.PlainDate(1914, 2, 22, calendar2); + +assert.sameValue(date1.equals(date2), false, "different ISO dates"); +assert.sameValue(calendar1.calls, 0, "calendar1 toString() calls"); +assert.sameValue(calendar2.calls, 0, "calendar2 toString() calls");