diff options
author | Luke Wilde <lukew@serenityos.org> | 2021-11-29 22:30:46 +0000 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-11-29 22:56:35 +0000 |
commit | ac8c3919cb964dc8e97ecb04fe8149c9fe1469f8 (patch) | |
tree | f1da65e4437342cfe9b9bb9a78e220b91682bed3 | |
parent | acf8729a81c744dbe183d3660bc3b332cd1c3aa6 (diff) | |
download | serenity-ac8c3919cb964dc8e97ecb04fe8149c9fe1469f8.zip |
LibJS: Implement Temporal.Duration.prototype.add
3 files changed, 111 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp index fe6ffcd734..6f862b4dcc 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp @@ -46,6 +46,7 @@ void DurationPrototype::initialize(GlobalObject& global_object) define_native_function(vm.names.with, with, 1, attr); define_native_function(vm.names.negated, negated, 0, attr); define_native_function(vm.names.abs, abs, 0, attr); + define_native_function(vm.names.add, add, 1, attr); define_native_function(vm.names.round, round, 1, attr); define_native_function(vm.names.total, total, 1, attr); define_native_function(vm.names.toString, to_string, 0, attr); @@ -289,6 +290,29 @@ JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::abs) return TRY(create_temporal_duration(global_object, fabs(duration->years()), fabs(duration->months()), fabs(duration->weeks()), fabs(duration->days()), fabs(duration->hours()), fabs(duration->minutes()), fabs(duration->seconds()), fabs(duration->milliseconds()), fabs(duration->microseconds()), fabs(duration->nanoseconds()))); } +// 7.3.18 Temporal.Duration.prototype.add ( other [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.add +JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::add) +{ + // 1. Let duration be the this value. + // 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]). + auto* duration = TRY(typed_this_object(global_object)); + + // 3. Set other to ? ToLimitedTemporalDuration(other, ยซ ยป). + auto other = TRY(to_limited_temporal_duration(global_object, vm.argument(0), {})); + + // 4. Set options to ? GetOptionsObject(options). + auto* options = TRY(get_options_object(global_object, vm.argument(1))); + + // 5. Let relativeTo be ? ToRelativeTemporalObject(options). + auto relative_to = TRY(to_relative_temporal_object(global_object, *options)); + + // 6. Let result be ? AddDuration(duration.[[Years]], duration.[[Months]], duration.[[Weeks]], duration.[[Days]], duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]], duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]], other.[[Years]], other.[[Months]], other.[[Weeks]], other.[[Days]], other.[[Hours]], other.[[Minutes]], other.[[Seconds]], other.[[Milliseconds]], other.[[Microseconds]], other.[[Nanoseconds]], relativeTo). + auto result = TRY(add_duration(global_object, duration->years(), duration->months(), duration->weeks(), duration->days(), duration->hours(), duration->minutes(), duration->seconds(), duration->milliseconds(), duration->microseconds(), duration->nanoseconds(), other.years, other.months, other.weeks, other.days, other.hours, other.minutes, other.seconds, other.milliseconds, other.microseconds, other.nanoseconds, relative_to)); + + // 7. Return ? CreateTemporalDuration(result.[[Years]], result.[[Months]], result.[[Weeks]], result.[[Days]], result.[[Hours]], result.[[Minutes]], result.[[Seconds]], result.[[Milliseconds]], result.[[Microseconds]], result.[[Nanoseconds]]). + return TRY(create_temporal_duration(global_object, result.years, result.months, result.weeks, result.days, result.hours, result.minutes, result.seconds, result.milliseconds, result.microseconds, result.nanoseconds)); +} + // 7.3.20 Temporal.Duration.prototype.round ( roundTo ), https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.round JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::round) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h index 94d2304cf7..ece335210a 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h @@ -35,6 +35,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(with); JS_DECLARE_NATIVE_FUNCTION(negated); JS_DECLARE_NATIVE_FUNCTION(abs); + JS_DECLARE_NATIVE_FUNCTION(add); JS_DECLARE_NATIVE_FUNCTION(round); JS_DECLARE_NATIVE_FUNCTION(total); JS_DECLARE_NATIVE_FUNCTION(to_string); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.add.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.add.js new file mode 100644 index 0000000000..b6a50d14d8 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.add.js @@ -0,0 +1,86 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Temporal.Duration.prototype.add).toHaveLength(1); + }); + + function checkCommonResults(durationResult) { + expect(durationResult.years).toBe(0); + expect(durationResult.months).toBe(0); + expect(durationResult.weeks).toBe(0); + expect(durationResult.days).toBe(3); + expect(durationResult.hours).toBe(3); + expect(durationResult.minutes).toBe(3); + expect(durationResult.seconds).toBe(3); + expect(durationResult.milliseconds).toBe(3); + expect(durationResult.microseconds).toBe(3); + expect(durationResult.nanoseconds).toBe(3); + } + + test("basic functionality", () => { + const duration = new Temporal.Duration(0, 0, 0, 2, 2, 2, 2, 2, 2, 2); + const oneDuration = new Temporal.Duration(0, 0, 0, 1, 1, 1, 1, 1, 1, 1); + const durationResult = duration.add(oneDuration); + + checkCommonResults(durationResult); + }); + + test("from duration-like", () => { + const duration = new Temporal.Duration(0, 0, 0, 2, 2, 2, 2, 2, 2, 2); + const oneDuration = { + days: 1, + hours: 1, + minutes: 1, + seconds: 1, + milliseconds: 1, + microseconds: 1, + nanoseconds: 1, + }; + const durationResult = duration.add(oneDuration); + + checkCommonResults(durationResult); + }); + + test("from string", () => { + const duration = new Temporal.Duration(0, 0, 0, 2, 2, 2, 2, 2, 2, 2); + const oneDuration = "P1DT1H1M1.001001001S"; + const durationResult = duration.add(oneDuration); + + checkCommonResults(durationResult); + }); +}); + +describe("errors", () => { + test("this value must be a Temporal.Duration object", () => { + expect(() => { + Temporal.Duration.prototype.add.call("foo"); + }).toThrowWithMessage(TypeError, "Not an object of type Temporal.Duration"); + }); + + test("relativeTo is required when duration has calendar units", () => { + const yearDuration = new Temporal.Duration(1); + const monthDuration = new Temporal.Duration(0, 1); + const weekDuration = new Temporal.Duration(0, 0, 1); + const durationToAdd = { seconds: 1 }; + + expect(() => { + yearDuration.add(durationToAdd); + }).toThrowWithMessage( + RangeError, + "A starting point is required for balancing year, month or week" + ); + + expect(() => { + monthDuration.add(durationToAdd); + }).toThrowWithMessage( + RangeError, + "A starting point is required for balancing year, month or week" + ); + + expect(() => { + weekDuration.add(durationToAdd); + }).toThrowWithMessage( + RangeError, + "A starting point is required for balancing year, month or week" + ); + }); +}); |