Add tests for PlainDate#equals().

This commit is contained in:
Ms2ger 2021-12-23 17:48:26 +01:00 committed by Rick Waldron
parent 0855621088
commit 78b8f7945c
8 changed files with 156 additions and 3 deletions

View File

@ -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");

View File

@ -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");

View File

@ -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");

View File

@ -15,6 +15,3 @@ assert.throws(RangeError, () => instance.equals(true), "true");
assert.throws(RangeError, () => instance.equals(""), "empty string"); assert.throws(RangeError, () => instance.equals(""), "empty string");
assert.throws(TypeError, () => instance.equals(Symbol()), "symbol"); assert.throws(TypeError, () => instance.equals(Symbol()), "symbol");
assert.throws(RangeError, () => instance.equals(1), "1"); 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");

View File

@ -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");

View File

@ -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");

View File

@ -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");

View File

@ -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");