Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create tests around math order of operations for zonedDateTimeFns() #90

Open
kolton-musgrove opened this issue Jan 31, 2022 · 0 comments
Labels
enhancement New feature or request

Comments

@kolton-musgrove
Copy link
Collaborator

describe('math order of operations and options', () => {
    const breakoutUnits = (op: any, zdt: Iso.ZonedDateTime, d: any, options: object) =>
      zdt[op]({ years: d.years }, options)
        [op]({ months: d.months }, options)
        [op]({ weeks: d.weeks }, options)
        [op]({ days: d.days }, options)
        [op](
          {
            hours: d.hours,
            minutes: d.minutes,
            seconds: d.seconds,
            milliseconds: d.milliseconds
          },

          options
        )

    it('order of operations: add / none', () => {
      const zdt = zonedDateTimeFns.from('2020-01-31T00:00-08:00[America/Los_Angeles]')
      const d = durationFns.from({ months: 1, days: 1 })
      const options = undefined
      const result = zonedDateTimeFns.add(zdt, d, options)
      assert.equal(result.toString(), '2020-03-01T00:00:00-08:00[America/Los_Angeles]')
      assert.equal(breakoutUnits('add', zdt, d, options).toString(), result.toString())
    })
    it('order of operations: add / constrain', () => {
      const zdt = zonedDateTimeFns.from('2020-01-31T00:00-08:00[America/Los_Angeles]')
      const d = durationFns.from({ months: 1, days: 1 })
      const options = { overflow: 'constrain' }
      const result = zonedDateTimeFns.add(zdt, d, options)
      assert.equal(result.toString(), '2020-03-01T00:00:00-08:00[America/Los_Angeles]')
      assert.equal(breakoutUnits('add', zdt, d, options).toString(), result.toString())
    })
    it('order of operations: add / reject', () => {
      const zdt = zonedDateTimeFns.from('2020-01-31T00:00-08:00[America/Los_Angeles]')
      const d = durationFns.from({ months: 1, days: 1 })
      const options = { overflow: 'reject' }
      assert.throws(() => zonedDateTimeFns.add(zdt, d, options), RangeError)
    })
    it('order of operations: subtract / none', () => {
      const zdt = zonedDateTimeFns.from('2020-03-31T00:00-07:00[America/Los_Angeles]')
      const d = durationFns.from({ months: 1, days: 1 })
      const options = undefined
      const result = zonedDateTimeFns.subtract(zdt, d, options)
      assert.equal(result.toString(), '2020-02-28T00:00:00-08:00[America/Los_Angeles]')
      assert.equal(breakoutUnits('subtract', zdt, d, options).toString(), result.toString())
    })
    it('order of operations: subtract / constrain', () => {
      const zdt = zonedDateTimeFns.from('2020-03-31T00:00-07:00[America/Los_Angeles]')
      const d = durationFns.from({ months: 1, days: 1 })
      const options = { overflow: 'constrain' }
      const result = zonedDateTimeFns.subtract(zdt, d, options)
      assert.equal(result.toString(), '2020-02-28T00:00:00-08:00[America/Los_Angeles]')
      assert.equal(breakoutUnits('subtract', zdt, d, options).toString(), result.toString())
    })
    it('order of operations: subtract / reject', () => {
      const zdt = zonedDateTimeFns.from('2020-03-31T00:00-07:00[America/Los_Angeles]')
      const d = durationFns.from({ months: 1, days: 1 })
      const options = { overflow: 'reject' }
      assert.throws(() => zonedDateTimeFns.subtract(zdt, d, options), RangeError)
    })
  })

The temporal proposal has tests around the order of operations, but they make use of the fact that the hour, minute, second, etc. fields are directly accessible on their objects which makes it simple to access and test them with different math operators. We should create tests around this.

@kolton-musgrove kolton-musgrove added the enhancement New feature or request label Jan 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant