Skip to content

Commit fdbc13b

Browse files
committed
feat: Add equality testers setup function for comparing algots primitives
1 parent 6a7dacd commit fdbc13b

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/set-up.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { internal } from '@algorandfoundation/algorand-typescript'
2+
3+
type Tester = (this: TesterContext, a: unknown, b: unknown, customTesters: Array<Tester>) => boolean | undefined
4+
interface TesterContext {
5+
equals: (a: unknown, b: unknown, customTesters?: Array<Tester>, strictCheck?: boolean) => boolean
6+
}
7+
interface ExpectObj {
8+
addEqualityTesters: (testers: Array<Tester>) => void
9+
}
10+
11+
function addEqualityTesters(expectObj: ExpectObj) {
12+
expectObj.addEqualityTesters([
13+
function (this: TesterContext, subject, test, customTesters): boolean | undefined {
14+
const subjectIsPrimitive = subject instanceof internal.primitives.AlgoTsPrimitiveCls
15+
const testIsPrimitive = test instanceof internal.primitives.AlgoTsPrimitiveCls
16+
if (subjectIsPrimitive && testIsPrimitive) {
17+
return this.equals(subject.valueOf(), test.valueOf(), customTesters)
18+
} else if (subjectIsPrimitive === testIsPrimitive) {
19+
// Neither is primitive
20+
return undefined
21+
} else {
22+
// One is primitive, one is not
23+
return false
24+
}
25+
},
26+
])
27+
}
28+
29+
export function setUpTests({ expect }: { expect: ExpectObj }) {
30+
addEqualityTesters(expect)
31+
}

vitest.setup.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { expect } from 'vitest'
2+
import { setUpTests } from './src/set-up'
3+
4+
export function setup() {
5+
setUpTests({ expect })
6+
}

0 commit comments

Comments
 (0)