-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunify-tests.ts
61 lines (54 loc) · 2.04 KB
/
unify-tests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* eslint-env jest */
import unify from '../unify'
import { StringType } from '../types'
import { AstNode, AstStringNode, AstNumeralNode, AstNameNode } from '../node-types'
describe('unify', () => {
it('can unify one matching value', () => {
const patterns: AstStringNode[] = [{ type: 'Str', value: 'hai' }]
const values = ['hai']
expect(unify(patterns, values)).toEqual({
didMatch: true,
bindings: {}
})
})
it('can unify several matching values', () => {
const patterns: AstNumeralNode[] = [{ type: 'Numeral', value: 1 }, { type: 'Numeral', value: 2 }]
const values = [1, 2]
expect(unify(patterns, values)).toEqual({
didMatch: true,
bindings: {}
})
})
// TODO this test is better handled by a type system
it('cannot unify a loosely equal value', () => {
const patterns: AstNumeralNode[] = [{ type: 'Numeral', value: 0 }]
const values = [false]
expect(unify(patterns, values)).toEqual({ didMatch: false, bindings: {} })
})
it('cannot unify when the number of patterns and values differs', () => {
const patterns: AstNumeralNode[] = [{ type: 'Numeral', value: 1 }, { type: 'Numeral', value: 2 }]
const values = [1]
expect(unify(patterns, values)).toEqual({ didMatch: false, bindings: {} })
})
it('cannot unify when some but not all patterns match', () => {
const patterns: AstNumeralNode[] = [{ type: 'Numeral', value: 1 }, { type: 'Numeral', value: 2 }]
const values = [1, 3]
expect(unify(patterns, values)).toEqual({ didMatch: false, bindings: {} })
})
it('can unify a variable', () => {
const patterns: AstNameNode[] = [{ type: 'Name', name: 'x' }]
const values = [1]
expect(unify(patterns, values)).toEqual({
didMatch: true,
bindings: { x: 1 }
})
})
it('can unify mixed variables and values', () => {
const patterns: AstNode[] = [{ type: 'Name', name: 'x' }, { type: 'Numeral', value: 2 }]
const values = [1, 2]
expect(unify(patterns, values)).toEqual({
didMatch: true,
bindings: { x: 1 }
})
})
})