forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LibJS: Implement Temporal.Duration.prototype.with()
- Loading branch information
Showing
7 changed files
with
253 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -362,6 +362,7 @@ namespace JS { | |
P(values) \ | ||
P(warn) \ | ||
P(weeks) \ | ||
P(with) \ | ||
P(writable) \ | ||
P(years) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
Userland/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.with.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
const DURATION_PROPERTIES = [ | ||
"years", | ||
"months", | ||
"weeks", | ||
"days", | ||
"hours", | ||
"minutes", | ||
"seconds", | ||
"milliseconds", | ||
"microseconds", | ||
"nanoseconds", | ||
]; | ||
|
||
describe("correct behavior", () => { | ||
test("length is 1", () => { | ||
expect(Temporal.Duration.prototype.with).toHaveLength(1); | ||
}); | ||
|
||
test("basic functionality", () => { | ||
const duration = new Temporal.Duration(1, 2, 3).with({ years: 4, foo: 5, weeks: 6 }); | ||
expect(duration.years).toBe(4); | ||
expect(duration.months).toBe(2); | ||
expect(duration.weeks).toBe(6); | ||
}); | ||
|
||
test("each property is looked up from the object", () => { | ||
for (const property of DURATION_PROPERTIES) { | ||
const duration = new Temporal.Duration().with({ [property]: 1 }); | ||
expect(duration[property]).toBe(1); | ||
} | ||
}); | ||
|
||
test("each property is coerced to number", () => { | ||
for (const property of DURATION_PROPERTIES) { | ||
const duration = new Temporal.Duration().with({ [property]: "1" }); | ||
expect(duration[property]).toBe(1); | ||
} | ||
}); | ||
}); | ||
|
||
test("errors", () => { | ||
test("this value must be a Temporal.Duration object", () => { | ||
expect(() => { | ||
Temporal.Duration.prototype.with.call("foo"); | ||
}).toThrowWithMessage(TypeError, "Not a Temporal.Duration"); | ||
}); | ||
|
||
test("argument is not an object", () => { | ||
expect(() => { | ||
new Temporal.Duration().with("foo"); | ||
}).toThrowWithMessage(TypeError, "foo is not an object"); | ||
expect(() => { | ||
new Temporal.Duration().with(42); | ||
}).toThrowWithMessage(TypeError, "42 is not an object"); | ||
}); | ||
|
||
test("argument is an invalid duration-like object", () => { | ||
expect(() => { | ||
new Temporal.Duration().with({}); | ||
}).toThrowWithMessage(TypeError, "Invalid duration-like object"); | ||
expect(() => { | ||
new Temporal.Duration().with({ foo: 1, bar: 2 }); | ||
}).toThrowWithMessage(TypeError, "Invalid duration-like object"); | ||
}); | ||
|
||
test("error when coercing property to number", () => { | ||
for (const property of DURATION_PROPERTIES) { | ||
expect(() => { | ||
new Temporal.Duration().with({ | ||
[property]: { | ||
valueOf() { | ||
throw new Error(); | ||
}, | ||
}, | ||
}); | ||
}).toThrow(Error); | ||
} | ||
}); | ||
|
||
test("invalid duration value", () => { | ||
for (const property of DURATION_PROPERTIES) { | ||
expect(() => { | ||
new Temporal.Duration().with({ [property]: Infinity }); | ||
}).toThrowWithMessage(RangeError, "Invalid duration"); | ||
} | ||
}); | ||
}); |