Skip to content

Commit

Permalink
handle valueOf in tcompare::same
Browse files Browse the repository at this point in the history
PR-URL: #970
Credit: @RonaldZielaznicki
Close: #970
Reviewed-by: @isaacs
  • Loading branch information
Ronald M Zielaznicki authored and isaacs committed Nov 15, 2023
1 parent dd4f233 commit 7505ccd
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/asserts/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@ t.test('same, notSame', t => {
t.ok(a.notSame({ a: 1 }, { a: 1, b: 2 }))
t.ok(a.notSame({ a: 1 }, { a: 1, b: 2 }))
t.ok(a.notSame({ a: 1, b: 2 }, { b: 2 }))
class Numberish {
foo: number
valueOf() { return 1 }
constructor() {
this.foo = Math.random()
}
}
t.ok(a.same(1, new Numberish()))
t.ok(a.same(new Numberish(), 1))
t.ok(a.same(new Numberish(), new Numberish()))
t.end()
})

Expand Down
12 changes: 10 additions & 2 deletions src/tcompare/src/same.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ export class Same extends Format {
? true
: a === b
? true
: a instanceof Date && b instanceof Date
? a.getTime() === b.getTime()
: typeof a?.valueOf === 'function' &&
typeof b?.valueOf === 'function' &&
a.valueOf() === b.valueOf()
? true
: typeof a?.valueOf === 'function' && a.valueOf() === b
? true
: typeof b?.valueOf === 'function' && b.valueOf() === a
? true
: a === null || b === null
? a == b
: a !== a
Expand All @@ -125,8 +135,6 @@ export class Same extends Format {
? false
: Buffer.isBuffer(a) && Buffer.isBuffer(b)
? a.equals(b)
: a instanceof Date && b instanceof Date
? a.getTime() === b.getTime()
: a instanceof RegExp && b instanceof RegExp
? this.regexpSame(a, b)
: 'COMPLEX' // might still be a deeper mismatch, of course
Expand Down
24 changes: 24 additions & 0 deletions src/tcompare/tap-snapshots/test/same.tsx.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -1236,3 +1236,27 @@ exports[`test/same.tsx > TAP > undefined is the same as itself > must match snap
exports[`test/same.tsx > TAP > undefined is the same as itself > must match snapshot 3`] = `
`

exports[`test/same.tsx > TAP > valueOf > must match snapshot 1`] = `
`

exports[`test/same.tsx > TAP > valueOf > must match snapshot 2`] = `
`

exports[`test/same.tsx > TAP > valueOf > must match snapshot 3`] = `
`

exports[`test/same.tsx > TAP > valueOf > must match snapshot 4`] = `
`

exports[`test/same.tsx > TAP > valueOf > must match snapshot 5`] = `
`

exports[`test/same.tsx > TAP > valueOf > must match snapshot 6`] = `
`
29 changes: 29 additions & 0 deletions src/tcompare/test/same.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -794,3 +794,32 @@ t.test('react', t => {
})
t.end()
})

t.test('valueOf', t => {
class Numberish {
foo: number
valueOf() {
return 1
}
constructor() {
this.foo = Math.random()
}
}
t.ok(same(t, 1, new Numberish()), 'num to ish')
t.ok(same(t, new Numberish(), 1), 'ish to num')
t.ok(same(t, new Numberish(), new Numberish()), 'ish to ish')
const data = Object.create(null)
class DataWrap {
foo: number
constructor() {
this.foo = Math.random()
}
valueOf() {
return data
}
}
t.ok(same(t, data, new DataWrap()), 'data to wrap')
t.ok(same(t, new DataWrap(), data), 'wrap to data')
t.ok(same(t, new DataWrap(), new DataWrap()), 'wrap to wrap')
t.end()
})

0 comments on commit 7505ccd

Please sign in to comment.