Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import {
3 expectArrayEqualIgnoreOrder,
4 generateInterleavedCombinations,
5 generatePartialCombinations,
6 generatePermutations,
7} from './test-utils'
8β
9describe('test-utils', () => {
10 describe('generatePermutations', () => {
11 const testCases = [
12 {
13 input: ['a', 'b', 'c'],
14 expected: [
15 ['a', 'b', 'c'],
16 ['a', 'c', 'b'],
17 ['b', 'a', 'c'],
18 ['b', 'c', 'a'],
19 ['c', 'a', 'b'],
20 ['c', 'b', 'a'],
21 ],
22 },
23 {
24 input: ['a', 'b'],
25 expected: [
26 ['a', 'b'],
27 ['b', 'a'],
28 ],
29 },
30 {
31 input: ['a'],
32 expected: [['a']],
33 },
34 ]
35β
36 it.each(testCases)(
37 'should generate permutations of $input',
38 ({ input, expected }) => {
39 const permutations = generatePermutations(input)
40 expect(permutations).toStrictEqual(expected)
41 },
42 )
43 })
44β
45 describe('generatePartialCombinations', () => {
46 const testCases = [
47 {
48 input: ['a', 'b', 'c'],
49 minLength: 2,
50 expected: [
51 ['a', 'b'],
52 ['a', 'c'],
53 ['b', 'c'],
54 ['a', 'b', 'c'],
55 ],
56 },
57 {
58 input: ['a', 'b'],
59 expected: [['a', 'b']],
60 minLength: 2,
61 },
62 {
63 input: ['a'],
64 expected: [],
65 minLength: 2,
66 },
67 {
68 input: ['a'],
69 expected: [['a']],
70 minLength: 1,
71 },
72 {
73 input: ['a'],
74 expected: [[], ['a']],
75 minLength: 0,
76 },
77 ]
78β
79 it.each(testCases)(
80 'should generate combinations of $input with at least $minLength items',
81 ({ input, minLength, expected }) => {
82 const combinations = generatePartialCombinations(input, minLength)
83 expectArrayEqualIgnoreOrder(combinations, expected)
84 },
85 )
86 })
87β
88 describe('generateInterleavedCombinations', () => {
89 const testCases = [
90 {
91 data: ['a', 'b'],
92 additional: ['x'],
93 expected: [
94 ['a', 'b'],
95 ['x', 'a', 'b'],
96 ['a', 'x', 'b'],
97 ['a', 'b', 'x'],
98 ],
99 },
100 ]
101β
102 it.each(testCases)(
103 'should interleave $additional into $data',
104 ({ data, additional, expected }) => {
105 const combinations = generateInterleavedCombinations(data, additional)
106 expectArrayEqualIgnoreOrder(combinations, expected)
107 },
108 )
109 })
110})