diff --git a/jest.config.ts b/jest.config.ts index 9a411ce..a31af38 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -5,5 +5,4 @@ export default { transform: { "^.+.tsx?$": ["ts-jest", { tsconfig: 'test/tsconfig.json' }], }, - setupFilesAfterEnv: ["./test/util.ts"], } as Config; diff --git a/package.json b/package.json index f4dea58..bd0c7b9 100644 --- a/package.json +++ b/package.json @@ -7,8 +7,9 @@ "scripts": { "build": "rm -rf build && tsc", "run": "yarn build && node build/index.js", - "debug": "yarn build && node --inspect-brk build/index.js", + "run:debug": "yarn build && node --inspect-brk build/index.js", "test": "jest", + "test:debug": "node --inspect-brk node_modules/.bin/jest --runInBand", "lint": "eslint", "lint:fix": "eslint --fix" }, diff --git a/test/bool.spec.ts b/test/bool.spec.ts index 292a592..ce1e127 100644 --- a/test/bool.spec.ts +++ b/test/bool.spec.ts @@ -1,4 +1,5 @@ import { describe, expect, it } from '@jest/globals'; +import { undef } from '../src/core'; import { toBoolean as $, False, True, fromBoolean as _, and, eq, iif, not, or, xor } from '../src/bool'; describe('Bool', () => { @@ -24,113 +25,105 @@ describe('Bool', () => { describe('iif', () => { it('returns true argument', () => { - expect(iif).toDeferEvaluationOf(False, expect.skipped(True), False); expect($(iif._(True)._(True)._(False))).toBe(true); }); it('returns false argument', () => { - expect(iif).toDeferEvaluationOf(True, True, expect.skipped(False)); expect($(iif._(False)._(True)._(False))).toBe(false); }); + + it('ignores other argument', () => { + expect($(iif._(True)._(True)._(undef))).toBe(true); + expect($(iif._(False)._(undef)._(False))).toBe(false); + }); }); describe('eq', () => { it('False eq False is True', () => { - expect(eq).toDeferEvaluationOf(False, False); expect($(eq._(False)._(False))).toBe(true); }); it('False eq True is False', () => { - expect(eq).toDeferEvaluationOf(False, True); expect($(eq._(False)._(True))).toBe(false); }); it('True eq False is False', () => { - expect(eq).toDeferEvaluationOf(True, False); expect($(eq._(True)._(False))).toBe(false); }); it('True eq True is True', () => { - expect(eq).toDeferEvaluationOf(True, True); expect($(eq._(True)._(True))).toBe(true); }); }); describe('not', () => { it('not False is True', () => { - expect(not).toDeferEvaluationOf(False); expect($(not._(False))).toBe(true); - }); it('not True is False', () => { - expect(not).toDeferEvaluationOf(True); expect($(not._(True))).toBe(false); }); }); describe('and', () => { it('False and False is False', () => { - expect(and).toDeferEvaluationOf(False, expect.skipped(False)); expect($(and._(False)._(False))).toBe(false); }); it('False and True is False', () => { - expect(and).toDeferEvaluationOf(False, expect.skipped(True)); expect($(and._(False)._(True))).toBe(false); }); it('True and False is False', () => { - expect(and).toDeferEvaluationOf(True, False); expect($(and._(True)._(False))).toBe(false); }); it('True and True is True', () => { - expect(and).toDeferEvaluationOf(True, True); expect($(and._(True)._(True))).toBe(true); }); + + it('False and ignores other argument', () => { + expect($(and._(False)._(undef))).toBe(false); + }); }); describe('or', () => { it('False or False is False', () => { - expect(or).toDeferEvaluationOf(False, False); expect($(or._(False)._(False))).toBe(false); }); it('False or True is True', () => { - expect(or).toDeferEvaluationOf(False, True); expect($(or._(False)._(True))).toBe(true); }); it('True or False is True', () => { - expect(or).toDeferEvaluationOf(True, expect.skipped(False)); expect($(or._(True)._(False))).toBe(true); }); it('True or True is True', () => { - expect(or).toDeferEvaluationOf(True, expect.skipped(True)); expect($(or._(True)._(True))).toBe(true); }); + + it('True or ignores other argument', () => { + expect($(or._(True)._(undef))).toBe(true); + }); }); describe('xor', () => { it('False xor False is False', () => { - expect(xor).toDeferEvaluationOf(False, False); expect($(xor._(False)._(False))).toBe(false); }); it('False xor True is True', () => { - expect(xor).toDeferEvaluationOf(False, True); expect($(xor._(False)._(True))).toBe(true); }); it('True xor False is True', () => { - expect(xor).toDeferEvaluationOf(True, False); expect($(xor._(True)._(False))).toBe(true); }); it('True xor True is False', () => { - expect(xor).toDeferEvaluationOf(True, True); expect($(xor._(True)._(True))).toBe(false); }); }); diff --git a/test/char.spec.ts b/test/char.spec.ts index a477093..dd70d44 100644 --- a/test/char.spec.ts +++ b/test/char.spec.ts @@ -42,22 +42,18 @@ describe('Char', () => { describe('eq', () => { it('"0" eq "0" is True', () => { - expect(eq).toDeferEvaluationOf(_('0'), _('0')); expect($b(eq._(_('0'))._(_('0')))).toBe(true); }); it('"0" eq "a" is False', () => { - expect(eq).toDeferEvaluationOf(_('0'), _('a')); expect($b(eq._(_('0'))._(_('a')))).toBe(false); }); it('"a" eq "a" is True', () => { - expect(eq).toDeferEvaluationOf(_('a'), _('a')); expect($b(eq._(_('a'))._(_('a')))).toBe(true); }); it('"a" eq "z" is True', () => { - expect(eq).toDeferEvaluationOf(_('a'), _('z')); expect($b(eq._(_('a'))._(_('z')))).toBe(false); }); }); diff --git a/test/core.spec.ts b/test/core.spec.ts index 4f28893..a003f30 100644 --- a/test/core.spec.ts +++ b/test/core.spec.ts @@ -1,16 +1,18 @@ import { describe, expect, it } from '@jest/globals'; -import { _, cnst, fromNative, id, toNative } from '../src/core'; +import { _, cnst, fromNative, id, toNative, undef } from '../src/core'; describe('id', () => { it('returns first argument', () => { - expect(id).toDeferEvaluationOf('first'); expect(toNative(id._(fromNative('first')))).toBe('first'); }); }); describe('cnst', () => { it('returns first argument after receiving second', () => { - expect(cnst).toDeferEvaluationOf('first', expect.skipped(fromNative('second'))); expect(toNative(cnst._(fromNative('first'))._(fromNative('second')))).toBe('first'); }); + + it('ignores second argument', () => { + expect(toNative(cnst._(fromNative('first'))._(undef))).toBe('first'); + }); }); diff --git a/test/list.spec.ts b/test/list.spec.ts index b290a74..8d0346c 100644 --- a/test/list.spec.ts +++ b/test/list.spec.ts @@ -38,17 +38,14 @@ describe('List', () => { describe('cons', () => { it('0 cons Nil is [0]', () => { - expect(cons).toDeferEvaluationOf(_n(0), Nil); expect($na(cons._(_n(0))._(Nil))).toEqual([0]); }); it('0 cons [1] is [0, 1]', () => { - expect(cons).toDeferEvaluationOf(_n(0), _na([1])); expect($na(cons._(_n(0))._(_na([1])))).toEqual([0, 1]); }); it('0 cons [1, 2] is [0, 1, 2]', () => { - expect(cons).toDeferEvaluationOf(_n(0), _na([1, 2])); expect($na(cons._(_n(0))._(_na([1, 2])))).toEqual([0, 1, 2]); }); @@ -59,17 +56,14 @@ describe('List', () => { describe('concat', () => { it('Nil concat Nil is Nil', () => { - expect(concat).toDeferEvaluationOf(Nil, Nil); expect($na(concat._(Nil)._(Nil))).toEqual([]); }); it('[0] concat [1] is [0, 1]', () => { - expect(concat).toDeferEvaluationOf(_na([0]), _na([1])); expect($na(concat._(_na([0]))._(_na([1])))).toEqual([0, 1]); }); it('[0, 1] concat [2, 3] is [0, 1, 2, 3]', () => { - expect(concat).toDeferEvaluationOf(_na([0, 1]), _na([2, 3])); expect($na(concat._(_na([0, 1]))._(_na([2, 3])))).toEqual([0, 1, 2, 3]); }); @@ -80,17 +74,14 @@ describe('List', () => { describe('head', () => { it('returns nothing from Nil', () => { - expect(() => head._(Nil)).not.toThrow(); expect(() => toNative(head._(Nil))).toThrow(); }); it('returns only item', () => { - expect(head).toDeferEvaluationOf(_na([0])); expect($n(head._(_na([0])))).toBe(0); }); it('returns first item', () => { - expect(head).toDeferEvaluationOf(_na([0, 1])); expect($n(head._(_na([0, 1])))).toBe(0); }); @@ -101,17 +92,14 @@ describe('List', () => { describe('tail', () => { it('drops nothing from Nil', () => { - expect(() => tail._(Nil)).not.toThrow(); expect(() => $na(tail._(Nil))).toThrow(); }); it('drops only item', () => { - expect(tail).toDeferEvaluationOf(_na([0])); expect($na(tail._(_na([0])))).toEqual([]); }); it('drops first item', () => { - expect(tail).toDeferEvaluationOf(_na([0, 1])); expect($na(tail._(_na([0, 1])))).toEqual([1]); }); @@ -122,58 +110,49 @@ describe('List', () => { describe('foldl', () => { it('folds Nil', () => { - expect(foldl._(sum)).toDeferEvaluationOf(Zero, _na([])); expect($n(foldl._(sum)._(Zero)._(_na([])))).toBe(0); }); it('folds single item', () => { - expect(foldl._(sum)).toDeferEvaluationOf(Zero, _na([1])); expect($n(foldl._(sum)._(Zero)._(_na([1])))).toBe(1); }); it('folds several items', () => { - expect(foldl._(sum)).toDeferEvaluationOf(Zero, _na([1, 2])); expect($n(foldl._(sum)._(Zero)._(_na([1, 2])))).toBe(3); }); }); describe('foldlz', () => { it('does not fold Nil', () => { - expect(() => foldlz._(sum)._(Nil)).not.toThrow(); expect(() => $n(foldlz._(sum)._(Nil))).toThrow(); }); it('folds single item', () => { - expect(foldlz._(sum)).toDeferEvaluationOf(_na([1])); expect($n(foldlz._(sum)._(_na([1])))).toBe(1); }); it('folds several items', () => { - expect(foldlz._(sum)).toDeferEvaluationOf(_na([1, 2])); expect($n(foldlz._(sum)._(_na([1, 2])))).toBe(3); }); }); describe('foldr', () => { it('folds Nil', () => { - expect(foldr._(sum)).toDeferEvaluationOf(Zero, _na([])); expect($n(foldr._(sum)._(Zero)._(_na([])))).toBe(0); }); it('folds single item', () => { - expect(foldr._(sum)).toDeferEvaluationOf(Zero, _na([1])); expect($n(foldr._(sum)._(Zero)._(_na([1])))).toBe(1); }); it('folds several items', () => { - expect(foldr._(sum)).toDeferEvaluationOf(Zero, _na([1, 2])); expect($n(foldr._(sum)._(Zero)._(_na([1, 2])))).toBe(3); }); it('handles infinite lists', () => { expect($n( foldr - ._(_(x => _(a => iif._(eqn._(x)._(_n(5)))._(x)._(sum._(x)._(a))))) + ._(_(x => _(a => sum._(x)._(iif._(eqn._(_n(5))._(x))._(Zero)._(a))))) ._(Zero) ._(infinite))) .toBe(15); @@ -182,24 +161,21 @@ describe('List', () => { describe('foldrz', () => { it('does not fold Nil', () => { - expect(() => foldrz._(sum)._(Nil)).not.toThrow(); expect(() => $n(foldrz._(sum)._(Nil))).toThrow(); }); it.failing('folds single item', () => { - expect(foldrz._(sum)).toDeferEvaluationOf(_na([1])); expect($n(foldrz._(sum)._(_na([1])))).toBe(1); }); it.failing('folds several items', () => { - expect(foldrz._(sum)).toDeferEvaluationOf(_na([1, 2])); expect($n(foldrz._(sum)._(_na([1, 2])))).toBe(3); }); it.failing('handles infinite lists', () => { expect($n( foldrz - ._(_(x => _(a => iif._(eqn._(x)._(_n(5)))._(x)._(sum._(x)._(a))))) + ._(_(x => _(a => sum._(x)._(iif._(eqn._(_n(5))._(x))._(Zero)._(a))))) ._(infinite))) .toBe(15); }); @@ -207,65 +183,52 @@ describe('List', () => { describe('eq', () => { it('Nil eq Nil is True', () => { - expect(eq).toDeferEvaluationOf(eqn, _na([]), _na([])); expect($b(eq._(eqn)._(_na([]))._(_na([])))).toEqual(true); }); it('Nil eq [0, 1, 2] is False', () => { - expect(eq).toDeferEvaluationOf(eqn, _na([]), _na([0, 1, 2])); expect($b(eq._(eqn)._(_na([]))._(_na([0, 1, 2])))).toEqual(false); }); it('[0, 1, 2] eq Nil is False', () => { - expect(eq).toDeferEvaluationOf(eqn, _na([0, 1, 2]), _na([])); expect($b(eq._(eqn)._(_na([0, 1, 2]))._(_na([])))).toEqual(false); }); it('[0] eq [0, 1, 2] is False', () => { - expect(eq).toDeferEvaluationOf(eqn, _na([0]), _na([0, 1, 2])); expect($b(eq._(eqn)._(_na([0]))._(_na([0, 1, 2])))).toEqual(false); }); it('[0, 1, 2] eq [0] is False', () => { - expect(eq).toDeferEvaluationOf(eqn, _na([0, 1, 2]), _na([0])); expect($b(eq._(eqn)._(_na([0, 1, 2]))._(_na([0])))).toEqual(false); }); - it('[1, 2, 0] eq [0, 1, 2] is False', () => { - expect(eq).toDeferEvaluationOf(eqn, _na([1, 2, 0]), _na([0, 1, 2])); expect($b(eq._(eqn)._(_na([1, 2, 0]))._(_na([0, 1, 2])))).toEqual(false); }); it('[0, 1, 2] eq [1, 2, 0] is False', () => { - expect(eq).toDeferEvaluationOf(eqn, _na([0, 1, 2]), _na([1, 2, 0])); expect($b(eq._(eqn)._(_na([0, 1, 2]))._(_na([1, 2, 0])))).toEqual(false); }); it('[0, 1, 2] eq [0, 1, 2] is True', () => { - expect(eq).toDeferEvaluationOf(eqn, _na([0, 1, 2]), _na([0, 1, 2])); expect($b(eq._(eqn)._(_na([0, 1, 2]))._(_na([0, 1, 2])))).toEqual(true); }); it('handles infinite lists', () => { - expect($b(eq._(eqn)._(infinite)._(_na([0, 1, 2])))).toEqual(false); expect($b(eq._(eqn)._(_na([0, 1, 2]))._(infinite))).toEqual(false); }); }); describe('map', () => { it('maps Nil', () => { - expect(map._(succ)).toDeferEvaluationOf(_na([])); expect($na(map._(succ)._(_na([])))).toEqual([]); }); it('maps only item', () => { - expect(map._(succ)).toDeferEvaluationOf(_na([0])); expect($na(map._(succ)._(_na([0])))).toEqual([1]); }); it('maps several items', () => { - expect(map._(succ)).toDeferEvaluationOf(_na([0, 1])); expect($na(map._(succ)._(_na([0, 1])))).toEqual([1, 2]); }); @@ -276,17 +239,14 @@ describe('List', () => { describe('filter', () => { it('filter Nil', () => { - expect(filter._(odd)).toDeferEvaluationOf(_na([])); expect($na(filter._(odd)._(_na([])))).toEqual([]); }); it('filters only item', () => { - expect(filter._(odd)).toDeferEvaluationOf(_na([0])); expect($na(filter._(odd)._(_na([])))).toEqual([]); }); it('filters several items', () => { - expect(filter._(odd)).toDeferEvaluationOf(_na([0, 1])); expect($na(filter._(odd)._(_na([0, 1])))).toEqual([1]); }); @@ -297,17 +257,14 @@ describe('List', () => { describe('any', () => { it('any Nil is False', () => { - expect(any._(odd)).toDeferEvaluationOf(_na([])); expect($b(any._(odd)._(_na([])))).toEqual(false); }); it('any for no matching item is False', () => { - expect(any._(odd)).toDeferEvaluationOf(_na([0, 2])); expect($b(any._(odd)._(_na([0, 2])))).toEqual(false); }); it('any for matching item is True', () => { - expect(any._(odd)).toDeferEvaluationOf(_na([0, 1, 2])); expect($b(any._(odd)._(_na([0, 1, 2])))).toEqual(true); }); @@ -318,17 +275,14 @@ describe('List', () => { describe('all', () => { it('all Nil is True', () => { - expect(all._(even)).toDeferEvaluationOf(_na([])); expect($b(all._(even)._(_na([])))).toEqual(true); }); it('all for non-matching item is False', () => { - expect(all._(even)).toDeferEvaluationOf(_na([0, 1, 2])); expect($b(all._(even)._(_na([0, 1, 2])))).toEqual(false); }); it('all for no non-matching item is True', () => { - expect(all._(even)).toDeferEvaluationOf(_na([0, 2])); expect($b(all._(even)._(_na([0, 2])))).toEqual(true); }); diff --git a/test/num.spec.ts b/test/num.spec.ts index f6cef47..6229f89 100644 --- a/test/num.spec.ts +++ b/test/num.spec.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from '@jest/globals'; -import { L } from '../src/core'; +import { L, undef } from '../src/core'; import { toBoolean as $b, fromBoolean as _b } from '../src/bool'; import { toNumber as $, Num, Succ, Zero, fromNumber as _, eq, ifz, mul, pred, sub, succ, sum } from '../src/num'; @@ -36,17 +36,14 @@ describe('Num', () => { describe('ifz', () => { it('returns zero branch', () => { - expect(ifz).toDeferEvaluationOf(_(0), true, false); expect($b(ifz._(_(0))._(_b(true))._(_b(false)))).toBe(true); }); it('returns non-zero branch', () => { - expect(ifz).toDeferEvaluationOf(_(1), true, false); expect($b(ifz._(_(1))._(_b(true))._(_b(false)))).toBe(false); }); it('returns non-zero branch', () => { - expect(ifz).toDeferEvaluationOf(_(10), true, false); expect($b(ifz._(_(10))._(_b(true))._(_b(false)))).toBe(false); }); @@ -57,148 +54,125 @@ describe('Num', () => { describe('succ', () => { it('succ 0 is 1', () => { - expect(succ).toDeferEvaluationOf(_(0)); expect($(succ._(_(0)))).toBe(1); }); it('succ 1 is 2', () => { - expect(succ).toDeferEvaluationOf(_(0)); expect($(succ._(_(1)))).toBe(2); }); it('succ 10 is 11', () => { - expect(succ).toDeferEvaluationOf(_(10)); expect($(succ._(_(10)))).toBe(11); }); }); describe('pred', () => { it('pred 0 is 0', () => { - expect(pred).toDeferEvaluationOf(_(0)); expect($(pred._(_(0)))).toBe(0); }); it('pred 1 is 0', () => { - expect(pred).toDeferEvaluationOf(_(0)); expect($(pred._(_(1)))).toBe(0); }); it('pred 10 is 9', () => { - expect(pred).toDeferEvaluationOf(_(10)); expect($(pred._(_(10)))).toBe(9); }); }); describe('eq', () => { it('0 eq 0 is True', () => { - expect(eq).toDeferEvaluationOf(_(0), _(0)); expect($b(eq._(_(0))._(_(0)))).toBe(true); }); it('0 eq 1 is False', () => { - expect(eq).toDeferEvaluationOf(_(0), _(1)); expect($b(eq._(_(0))._(_(1)))).toBe(false); }); it('1 eq 0 is False', () => { - expect(eq).toDeferEvaluationOf(_(1), _(0)); expect($b(eq._(_(1))._(_(0)))).toBe(false); }); it('3 eq 3 is True', () => { - expect(eq).toDeferEvaluationOf(_(3), _(4)); expect($b(eq._(_(3))._(_(3)))).toBe(true); }); it('3 eq 7 is False', () => { - expect(eq).toDeferEvaluationOf(_(3), _(7)); expect($b(eq._(_(3))._(_(7)))).toBe(false); }); it('7 eq 3 is False', () => { - expect(eq).toDeferEvaluationOf(_(7), _(3)); expect($b(eq._(_(7))._(_(3)))).toBe(false); }); it('handles infinity', () => { - expect($b(eq._(infinity)._(_(3)))).toBe(false); expect($b(eq._(_(7))._(infinity))).toBe(false); }) }); describe('sum', () => { it('0 sum 0 is 0', () => { - expect(sum).toDeferEvaluationOf(_(0), _(0)); expect($(sum._(_(0))._(_(0)))).toBe(0); }); it('0 sum 1 is 1', () => { - expect(sum).toDeferEvaluationOf(_(0), _(1)); expect($(sum._(_(0))._(_(1)))).toBe(1); }); it('1 sum 0 is 1', () => { - expect(sum).toDeferEvaluationOf(_(1), _(0)); expect($(sum._(_(1))._(_(0)))).toBe(1); }); it('3 sum 4 is 7', () => { - expect(sum).toDeferEvaluationOf(_(3), _(4)); expect($(sum._(_(3))._(_(4)))).toBe(7); }); }); describe('sub', () => { it('0 sub 0 is 0', () => { - expect(sub).toDeferEvaluationOf(_(0), _(0)); expect($(sub._(_(0))._(_(0)))).toBe(0); }); it('0 sub 1 is 0', () => { - expect(sub).toDeferEvaluationOf(_(0), _(1)); expect($(sub._(_(0))._(_(1)))).toBe(0); }); it('1 sub 0 is 1', () => { - expect(sub).toDeferEvaluationOf(_(1), _(0)); expect($(sub._(_(1))._(_(0)))).toBe(1); }); it('7 sub 4 is 3', () => { - expect(sub).toDeferEvaluationOf(_(7), _(4)); expect($(sub._(_(7))._(_(4)))).toBe(3); }); }); describe('mul', () => { it('0 mul 0 is 0', () => { - expect(mul).toDeferEvaluationOf(_(0), _(0)); expect($(mul._(_(0))._(_(0)))).toBe(0); }); it('0 mul 10 is 0', () => { - expect(mul).toDeferEvaluationOf(_(0), expect.skipped(_(10))); expect($(mul._(_(0))._(_(10)))).toBe(0); }); it('10 mul 0 is 0', () => { - expect(mul).toDeferEvaluationOf(_(10), _(0)); expect($(mul._(_(1))._(_(0)))).toBe(0); }); it('1 mul 10 is 10', () => { - expect(mul).toDeferEvaluationOf(_(1), _(10)); expect($(mul._(_(1))._(_(10)))).toBe(10); }); it('10 mul 1 is 10', () => { - expect(mul).toDeferEvaluationOf(_(10), _(1)); expect($(mul._(_(10))._(_(1)))).toBe(10); }); it('3 mul 4 is 12', () => { - expect(mul).toDeferEvaluationOf(_(3), _(4)); expect($(mul._(_(3))._(_(4)))).toBe(12); }); + + it('0 mul ignores second argument', ()=>{ + expect($(mul._(_(0))._(undef))).toBe(0); + }) }); }); diff --git a/test/string.spec.ts b/test/string.spec.ts index 8788ff0..6b42695 100644 --- a/test/string.spec.ts +++ b/test/string.spec.ts @@ -1,7 +1,7 @@ import { describe, expect, it } from '@jest/globals'; import { L, _, undef } from '../src/core'; import { toBoolean as $b } from '../src/bool'; -import { toChar as $c, fromChar as _c, eq as eqc } from '../src/char'; +import { toChar as $c, fromChar as _c } from '../src/char'; import { toString as $s, Cons, Empty, String, fromString as _s, concat, eq } from '../src/string'; describe('String', () => { @@ -37,69 +37,56 @@ describe('String', () => { describe('concat', () => { it('"" concat "" is ""', () => { - expect(concat).toDeferEvaluationOf(Empty, Empty); expect($s(concat._(Empty)._(Empty))).toBe(''); }); it('"a" concat "b" is "ab"', () => { - expect(concat).toDeferEvaluationOf(_s('a'), _s('b')); expect($s(concat._(_s('a'))._(_s('b')))).toBe('ab'); }); it('"abc" concat "def" is "abcdef"', () => { - expect(concat).toDeferEvaluationOf(_s('abc'), _s('def')); expect($s(concat._(_s('abc'))._(_s('def')))).toBe('abcdef'); }); - it('handles infinite lists', () => { + it('handles infinite strings', () => { expect($c(concat._(infinite)._(infinite)._(undef)._(_(x => _(_xs => x))))).toBe('a'); }); }); describe('eq', () => { it('"" eq "" is True', () => { - expect(eq).toDeferEvaluationOf(eqc, _s(''), _s('')); expect($b(eq._(_s(''))._(_s('')))).toBe(true); }); it('"" eq "abc" is False', () => { - expect(eq).toDeferEvaluationOf(eqc, _s(''), _s("abc")); expect($b(eq._(_s(''))._(_s("abc")))).toBe(false); }); it('"abc" eq "" is False', () => { - expect(eq).toDeferEvaluationOf(eqc, _s("abc"), _s('')); expect($b(eq._(_s("abc"))._(_s('')))).toBe(false); }); it('"a" eq "abc" is False', () => { - expect(eq).toDeferEvaluationOf(eqc, _s('a'), _s("abc")); expect($b(eq._(_s('a'))._(_s("abc")))).toBe(false); }); it('"abc" eq "a" is False', () => { - expect(eq).toDeferEvaluationOf(eqc, _s("abc"), _s('a')); expect($b(eq._(_s("abc"))._(_s('a')))).toBe(false); }); - it('"bca" eq "abc" is False', () => { - expect(eq).toDeferEvaluationOf(eqc, _s("bca"), _s("abc")); expect($b(eq._(_s("bca"))._(_s("abc")))).toBe(false); }); it('"abc" eq "bca" is False', () => { - expect(eq).toDeferEvaluationOf(eqc, _s("abc"), _s("bca")); expect($b(eq._(_s("abc"))._(_s("bca")))).toBe(false); }); it('"abc" eq "abc" is True', () => { - expect(eq).toDeferEvaluationOf(eqc, _s("abc"), _s("abc")); expect($b(eq._(_s("abc"))._(_s("abc")))).toBe(true); }); - it('handles infinite lists', () => { - expect($b(eq._(infinite)._(_s("abc")))).toBe(false); + it('handles infinite strings', () => { expect($b(eq._(_s("abc"))._(infinite))).toBe(false); }); }); diff --git a/test/util.ts b/test/util.ts deleted file mode 100644 index 852a69f..0000000 --- a/test/util.ts +++ /dev/null @@ -1,99 +0,0 @@ -import { expect } from '@jest/globals'; -import type { MatcherFunction } from 'expect'; -import { L, P, _, fromNative } from '../src/core'; - -class Deferrable { - constructor( - public readonly when: 'always' | 'deferred' | 'never', - public readonly what: T) { } -} - -const toEvaluateTo: MatcherFunction<[expected: unknown]> = - function (actual, expected) { - if (!(actual instanceof L)) { - throw new TypeError('Actual must be of type Lazy!'); - } - if (this.equals(fromNative(actual), expected)) { - return { - message: () => `expected ${this.utils.printReceived(fromNative(actual))} not to evaluate to ${this.utils.printExpected(expected)}`, - pass: true, - }; - } else { - return { - message: () => `expected ${this.utils.printReceived(fromNative(actual))} to evaluate to ${this.utils.printExpected(expected)}`, - pass: false, - }; - } - }; - -const toDeferEvaluationOf: MatcherFunction<(unknown | Deferrable

)[]> = - function (actual, ...args) { - while (actual instanceof L) { - actual = actual.value; - } - if (typeof actual !== 'function') { - throw new TypeError('Actual must be of type function!'); - } - if (this.isNot) { - throw new TypeError('`toDeferEvaluationOf` can not be `not`ted, use `always`/`deferred`/`never` on arguments!'); - } - const largs: Deferrable

[] = args.map(v => - v instanceof Deferrable ? v - : v instanceof L ? new Deferrable('deferred', _(v.value)) - : new Deferrable('deferred', _(v as any))); - const cargs: Deferrable

[] = []; - let result = actual; - for (const la of largs) { - result = result instanceof L ? result._(la.what) : result(la.what); - cargs.push(la); - const mismatchingArg: Deferrable

| undefined = cargs.find(v => v.when === 'always' ? !v.what.evaluated : v.what.evaluated); - if (mismatchingArg) { - return { - message: () => - mismatchingArg.when === 'always' - ? `expected ${this.utils.printExpected(actual.name)} to evaluate ${this.utils.printReceived(`${largs.indexOf(mismatchingArg) + 1}th argument`)} immediately` - : `expected ${this.utils.printExpected(actual.name)} not to evaluate ${this.utils.printReceived(`${largs.indexOf(mismatchingArg) + 1}th argument`)} immediately`, - pass: false, - }; - } - } - if (!(result instanceof L)) { - throw new TypeError('Actual did not resolve to value, more arguments expected.'); - } - (() => result.value)(); - const mismatchingArg: Deferrable

| undefined = cargs.find(v => v.when === 'never' && v.what.evaluated); - if (mismatchingArg) { - return { - message: () => `expected ${this.utils.printExpected(actual.name)} not to evaluate ${this.utils.printReceived(`${largs.indexOf(mismatchingArg) + 1}th argument`)}`, - pass: false, - }; - } - - return { - message: () => `expected ${this.utils.printExpected('toDeferEvaluationOf')} to ${this.utils.printReceived('fail')}`, - pass: true, - }; - }; - -expect.extend({ - toEvaluateTo, - toDeferEvaluationOf, -}); - -expect.evaluated = (v) => new Deferrable('always', _(v.value) as any); -expect.deferred = (v) => new Deferrable('deferred', _(v.value) as any); -expect.skipped = (v) => new Deferrable('never', _(v.value) as any); - -declare module 'expect' { - interface AsymmetricMatchers { - evaluated(value: T): Deferrable; - deferred(value: T): Deferrable; - skipped(value: T): Deferrable; - toEvaluateTo(value: any): void; - toDeferEvaluationOf(...args: any[]): void; - } - interface Matchers { - toEvaluateTo(value: any): R; - toDeferEvaluationOf(...args: any[]): R; - } -}