-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomparator.test.ts
More file actions
125 lines (99 loc) · 3.03 KB
/
comparator.test.ts
File metadata and controls
125 lines (99 loc) · 3.03 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import {join, reversed, keyed, ranking} from './comparator';
describe('join', () => {
interface Thing {
id: number;
name: string;
}
function byId(a: Thing, b: Thing): number {
return a.id - b.id;
}
function byName(a: Thing, b: Thing): number {
return a.name.localeCompare(b.name);
}
byName.displayName = 'byLocaleName';
const w: Thing = {id: 3, name: 'w'};
const x: Thing = {id: 3, name: 'x'};
const y: Thing = {id: 7, name: 'y'};
const z: Thing = {id: 7, name: 'z'};
it('does not alter a single comparator', () => {
const withoutJoin = [z, x, w, y].sort(byName);
const withJoin = [z, x, w, y].sort(join(byName));
expect(withJoin).toEqual(withoutJoin);
});
it('combines two comparators correctly', () => {
const result = [z, x, w, y].sort(join(byId, byName));
expect(result).toEqual([w, x, y, z]);
});
it('defines a debuggale function name', () => {
const joined = join(byId, byName);
expect(joined.name).toBe(
`joinedComparator(${byId.name}, ${byName.displayName})`,
);
});
});
describe('reversed', () => {
function byNumber(a: number, b: number): number {
return a - b;
}
const byNumberReversed = reversed(byNumber);
it('retains equality', () => {
expect(byNumber(1, 1)).toBe(0);
expect(byNumberReversed(1, 1)).toEqual(-0);
});
it('flips less than to greater than', () => {
expect(byNumber(5, 9000)).toBeLessThan(0);
expect(byNumberReversed(5, 9000)).toBeGreaterThan(0);
});
it('flips greater than to less than', () => {
expect(byNumber(9000, 5)).toBeGreaterThan(0);
expect(byNumberReversed(9000, 5)).toBeLessThan(0);
});
it('sorts numbers in reverse', () => {
const result = [1, 3, 5, 2, 4].sort(byNumberReversed);
expect(result).toEqual([5, 4, 3, 2, 1]);
});
});
describe('keyed', () => {
interface Thing {
id: number;
}
const w: Thing = {id: 7};
const x: Thing = {id: 3};
const y: Thing = {id: 5};
function byNumber(a: number, b: number): number {
return a - b;
}
it('derives a comparator correctly', () => {
const byThingId = keyed(({id}: Thing) => id, byNumber);
expect(byThingId(x, y)).toBeLessThan(0);
expect(byThingId(x, x)).toBe(0);
expect(byThingId(w, x)).toBeGreaterThan(0);
});
});
describe('ranking', () => {
enum Spam {
Foo = 'foo',
Bar = 'bar',
}
it('takes a scoring function', () => {
const f = ranking<Spam>(s => {
switch (s) {
case Spam.Foo:
return 1;
case Spam.Bar:
return 2;
}
});
expect(f(Spam.Foo, Spam.Foo)).toEqual(0);
expect(f(Spam.Bar, Spam.Bar)).toEqual(0);
expect(f(Spam.Foo, Spam.Bar)).toBeLessThan(0);
expect(f(Spam.Bar, Spam.Foo)).toBeGreaterThan(0);
});
it('generates a scoring function from an array', () => {
const f = ranking([Spam.Foo, Spam.Bar]);
expect(f(Spam.Foo, Spam.Foo)).toEqual(0);
expect(f(Spam.Bar, Spam.Bar)).toEqual(0);
expect(f(Spam.Foo, Spam.Bar)).toBeLessThan(0);
expect(f(Spam.Bar, Spam.Foo)).toBeGreaterThan(0);
});
});