|
| 1 | +import { assertMatch, biguint, BigUint, Bytes, match, Uint64 } from '@algorandfoundation/algorand-typescript' |
| 2 | +import { TestExecutionContext } from '@algorandfoundation/algorand-typescript-testing' |
| 3 | +import { afterEach, describe, expect, test } from 'vitest' |
| 4 | +import { MAX_UINT512, MAX_UINT64 } from '../src/constants' |
| 5 | +import { StrImpl } from '../src/impl/encoded-types' |
| 6 | +describe('match', () => { |
| 7 | + const ctx = new TestExecutionContext() |
| 8 | + |
| 9 | + afterEach(async () => { |
| 10 | + ctx.reset() |
| 11 | + }) |
| 12 | + |
| 13 | + const numericTestData = [ |
| 14 | + { subject: 1, test: 1, expected: true }, |
| 15 | + { subject: 0, test: 1, expected: false }, |
| 16 | + { subject: 1, test: 0, expected: false }, |
| 17 | + { subject: 1, test: Uint64(1), expected: true }, |
| 18 | + { subject: Uint64(1), test: Uint64(1), expected: true }, |
| 19 | + { subject: Uint64(1), test: 1, expected: true }, |
| 20 | + { subject: 42, test: MAX_UINT64, expected: false }, |
| 21 | + { subject: Uint64(MAX_UINT64), test: Uint64(42), expected: false }, |
| 22 | + { subject: BigUint(1), test: 1n, expected: true }, |
| 23 | + { subject: 1n, test: BigUint(1), expected: true }, |
| 24 | + { subject: BigUint(1), test: BigUint(1), expected: true }, |
| 25 | + { subject: 42n, test: MAX_UINT512, expected: false }, |
| 26 | + { subject: BigUint(MAX_UINT512), test: BigUint(42n), expected: false }, |
| 27 | + { subject: { a: BigUint(MAX_UINT512) }, test: { a: { lessThan: MAX_UINT512 } }, expected: false }, |
| 28 | + { subject: { a: BigUint(MAX_UINT512) }, test: { a: { lessThanEq: MAX_UINT64 } }, expected: false }, |
| 29 | + { subject: { a: MAX_UINT64 }, test: { a: { lessThan: BigUint(MAX_UINT512) } }, expected: true }, |
| 30 | + { subject: { a: MAX_UINT512 }, test: { a: { lessThanEq: BigUint(MAX_UINT512) } }, expected: true }, |
| 31 | + { subject: { a: BigUint(MAX_UINT512) }, test: { a: { greaterThan: MAX_UINT512 } }, expected: false }, |
| 32 | + { subject: { a: BigUint(MAX_UINT64) }, test: { a: { greaterThanEq: MAX_UINT512 } }, expected: false }, |
| 33 | + { subject: { a: MAX_UINT512 }, test: { a: { greaterThan: BigUint(MAX_UINT64) } }, expected: true }, |
| 34 | + { subject: { a: MAX_UINT512 }, test: { a: { greaterThanEq: BigUint(MAX_UINT512) } }, expected: true }, |
| 35 | + { |
| 36 | + subject: { a: MAX_UINT512 }, |
| 37 | + test: { a: { between: [BigUint(MAX_UINT64), BigUint(MAX_UINT512)] as [biguint, biguint] } }, |
| 38 | + expected: true, |
| 39 | + }, |
| 40 | + { |
| 41 | + subject: { a: MAX_UINT64 }, |
| 42 | + test: { a: { between: [BigUint(MAX_UINT64), BigUint(MAX_UINT512)] as [biguint, biguint] } }, |
| 43 | + expected: true, |
| 44 | + }, |
| 45 | + { subject: { a: 42 }, test: { a: { between: [BigUint(MAX_UINT64), BigUint(MAX_UINT512)] as [biguint, biguint] } }, expected: false }, |
| 46 | + ] |
| 47 | + |
| 48 | + const account1 = ctx.any.account() |
| 49 | + const sameAccount = ctx.ledger.getAccount(account1) |
| 50 | + const differentAccount = ctx.any.account() |
| 51 | + |
| 52 | + const app1 = ctx.any.application() |
| 53 | + const sameApp = ctx.ledger.getApplication(app1.id) |
| 54 | + const differentApp = ctx.any.application() |
| 55 | + |
| 56 | + const asset1 = ctx.any.asset() |
| 57 | + const sameAsset = ctx.ledger.getAsset(asset1.id) |
| 58 | + const differentAsset = ctx.any.application() |
| 59 | + |
| 60 | + const arc4Str1 = ctx.any.arc4.str(10) |
| 61 | + const sameArc4Str = new StrImpl((arc4Str1 as StrImpl).typeInfo, arc4Str1.native) |
| 62 | + const differentArc4Str = ctx.any.arc4.str(10) |
| 63 | + |
| 64 | + const testData = [ |
| 65 | + { subject: '', test: '', expected: true }, |
| 66 | + { subject: 'hello', test: 'hello', expected: true }, |
| 67 | + { subject: 'hello', test: 'world', expected: false }, |
| 68 | + { subject: '', test: 'world', expected: false }, |
| 69 | + { subject: Bytes(), test: Bytes(), expected: true }, |
| 70 | + { subject: Bytes('hello'), test: Bytes('hello'), expected: true }, |
| 71 | + { subject: Bytes('hello'), test: Bytes('world'), expected: false }, |
| 72 | + { subject: Bytes(''), test: Bytes('world'), expected: false }, |
| 73 | + { subject: account1, test: account1, expected: true }, |
| 74 | + { subject: account1, test: sameAccount, expected: true }, |
| 75 | + { subject: account1, test: differentAccount, expected: false }, |
| 76 | + { subject: app1, test: app1, expected: true }, |
| 77 | + { subject: app1, test: sameApp, expected: true }, |
| 78 | + { subject: app1, test: differentApp, expected: false }, |
| 79 | + { subject: asset1, test: asset1, expected: true }, |
| 80 | + { subject: asset1, test: sameAsset, expected: true }, |
| 81 | + { subject: asset1, test: differentAsset, expected: false }, |
| 82 | + { subject: arc4Str1, test: arc4Str1, expected: true }, |
| 83 | + { subject: arc4Str1, test: sameArc4Str, expected: true }, |
| 84 | + { subject: arc4Str1, test: differentArc4Str, expected: false }, |
| 85 | + { subject: { a: 'hello', b: 42, c: arc4Str1 }, test: { a: 'hello', b: { lessThanEq: 42 }, c: sameArc4Str }, expected: true }, |
| 86 | + { subject: { a: 'hello', b: 42, c: arc4Str1 }, test: { c: sameArc4Str }, expected: true }, |
| 87 | + { subject: { a: 'hello', b: 42, c: arc4Str1 }, test: { c: differentArc4Str }, expected: false }, |
| 88 | + { subject: ['hello', 42, arc4Str1], test: ['hello', { lessThanEq: 42 }, sameArc4Str], expected: true }, |
| 89 | + { subject: ['hello', 42, arc4Str1], test: ['hello'], expected: true }, |
| 90 | + { subject: ['hello', 42, arc4Str1], test: ['world'], expected: false }, |
| 91 | + ] |
| 92 | + |
| 93 | + test.each(numericTestData)('should be able to match numeric data %s', (data) => { |
| 94 | + const { subject, test, expected } = data |
| 95 | + expect(match(subject, test)).toBe(expected) |
| 96 | + }) |
| 97 | + |
| 98 | + test.each(testData)('should be able to match %s', (data) => { |
| 99 | + const { subject, test, expected } = data |
| 100 | + expect(match(subject, test)).toBe(expected) |
| 101 | + }) |
| 102 | + |
| 103 | + test.each(numericTestData.filter((x) => x.expected))('should be able to assert match numeric data %s', (data) => { |
| 104 | + const { subject, test, expected } = data |
| 105 | + expect(assertMatch(subject, test)).toBe(expected) |
| 106 | + }) |
| 107 | + |
| 108 | + test.each(testData.filter((x) => x.expected))('should be able to assert match %s', (data) => { |
| 109 | + const { subject, test, expected } = data |
| 110 | + expect(match(subject, test)).toBe(expected) |
| 111 | + }) |
| 112 | + |
| 113 | + test.each(numericTestData.filter((x) => !x.expected))('should throw exception when assert match fails for numeric data %s', (data) => { |
| 114 | + const { subject, test } = data |
| 115 | + expect(() => assertMatch(subject, test)).toThrow('Assertion failed') |
| 116 | + }) |
| 117 | + |
| 118 | + test.each(testData.filter((x) => !x.expected))('should throw exception when assert match fails %s', (data) => { |
| 119 | + const { subject, test } = data |
| 120 | + expect(() => assertMatch(subject, test)).toThrow('Assertion failed') |
| 121 | + }) |
| 122 | +}) |
0 commit comments