|
6 | 6 | *
|
7 | 7 | */
|
8 | 8 |
|
9 |
| -import fc from 'fast-check'; |
| 9 | +import {fc, it} from '@fast-check/jest'; |
10 | 10 | import diff from '../';
|
11 | 11 |
|
12 | 12 | const findCommonItems = (a: Array<string>, b: Array<string>): Array<string> => {
|
@@ -55,81 +55,71 @@ const isSubsequenceOf = (
|
55 | 55 | return iSub === subsequence.length;
|
56 | 56 | };
|
57 | 57 |
|
58 |
| -it('should be reflexive', () => { |
59 |
| - fc.assert( |
60 |
| - fc.property(fc.array(fc.char()), a => { |
61 |
| - expect(findCommonItems(a, a)).toEqual(a); |
62 |
| - }), |
63 |
| - ); |
| 58 | +it.prop([fc.array(fc.char())])('should be reflexive', a => { |
| 59 | + expect(findCommonItems(a, a)).toEqual(a); |
64 | 60 | });
|
65 | 61 |
|
66 |
| -it('should find the same number of common items when switching the inputs', () => { |
| 62 | +it.prop([fc.array(fc.char()), fc.array(fc.char())])( |
| 63 | + 'should find the same number of common items when switching the inputs', |
67 | 64 | // findCommonItems is not symmetric as:
|
68 | 65 | // > findCommonItems(["Z"," "], [" ","Z"]) = [" "]
|
69 | 66 | // > findCommonItems([" ","Z"], ["Z"," "]) = ["Z"]
|
70 |
| - fc.assert( |
71 |
| - fc.property(fc.array(fc.char()), fc.array(fc.char()), (a, b) => { |
72 |
| - const commonItems = findCommonItems(a, b); |
73 |
| - const symmetricCommonItems = findCommonItems(b, a); |
74 |
| - expect(symmetricCommonItems).toHaveLength(commonItems.length); |
75 |
| - }), |
76 |
| - ); |
77 |
| -}); |
| 67 | + (a, b) => { |
| 68 | + const commonItems = findCommonItems(a, b); |
| 69 | + const symmetricCommonItems = findCommonItems(b, a); |
| 70 | + expect(symmetricCommonItems).toHaveLength(commonItems.length); |
| 71 | + }, |
| 72 | +); |
78 | 73 |
|
79 |
| -it('should have at most the length of its inputs', () => { |
80 |
| - fc.assert( |
81 |
| - fc.property(fc.array(fc.char()), fc.array(fc.char()), (a, b) => { |
82 |
| - const commonItems = findCommonItems(a, b); |
83 |
| - expect(commonItems.length).toBeLessThanOrEqual(a.length); |
84 |
| - expect(commonItems.length).toBeLessThanOrEqual(b.length); |
85 |
| - }), |
86 |
| - ); |
87 |
| -}); |
| 74 | +it.prop([fc.array(fc.char()), fc.array(fc.char())])( |
| 75 | + 'should have at most the length of its inputs', |
| 76 | + (a, b) => { |
| 77 | + const commonItems = findCommonItems(a, b); |
| 78 | + expect(commonItems.length).toBeLessThanOrEqual(a.length); |
| 79 | + expect(commonItems.length).toBeLessThanOrEqual(b.length); |
| 80 | + }, |
| 81 | +); |
88 | 82 |
|
89 |
| -it('should have at most the same number of each character as its inputs', () => { |
90 |
| - fc.assert( |
91 |
| - fc.property(fc.array(fc.char()), fc.array(fc.char()), (a, b) => { |
92 |
| - const commonItems = findCommonItems(a, b); |
93 |
| - const commonCount = extractCount(commonItems); |
94 |
| - const aCount = extractCount(a); |
95 |
| - for (const [item, count] of commonCount) { |
96 |
| - const countOfItemInA = aCount.get(item) ?? 0; |
97 |
| - expect(countOfItemInA).toBeGreaterThanOrEqual(count); |
98 |
| - } |
99 |
| - }), |
100 |
| - ); |
101 |
| -}); |
| 83 | +it.prop([fc.array(fc.char()), fc.array(fc.char())])( |
| 84 | + 'should have at most the same number of each character as its inputs', |
| 85 | + (a, b) => { |
| 86 | + const commonItems = findCommonItems(a, b); |
| 87 | + const commonCount = extractCount(commonItems); |
| 88 | + const aCount = extractCount(a); |
| 89 | + for (const [item, count] of commonCount) { |
| 90 | + const countOfItemInA = aCount.get(item) ?? 0; |
| 91 | + expect(countOfItemInA).toBeGreaterThanOrEqual(count); |
| 92 | + } |
| 93 | + }, |
| 94 | +); |
102 | 95 |
|
103 |
| -it('should be a subsequence of its inputs', () => { |
104 |
| - fc.assert( |
105 |
| - fc.property(fc.array(fc.char()), fc.array(fc.char()), (a, b) => { |
106 |
| - const commonItems = findCommonItems(a, b); |
107 |
| - expect(isSubsequenceOf(commonItems, a)).toBe(true); |
108 |
| - expect(isSubsequenceOf(commonItems, b)).toBe(true); |
109 |
| - }), |
110 |
| - ); |
111 |
| -}); |
| 96 | +it.prop([fc.array(fc.char()), fc.array(fc.char())])( |
| 97 | + 'should be a subsequence of its inputs', |
| 98 | + (a, b) => { |
| 99 | + const commonItems = findCommonItems(a, b); |
| 100 | + expect(isSubsequenceOf(commonItems, a)).toBe(true); |
| 101 | + expect(isSubsequenceOf(commonItems, b)).toBe(true); |
| 102 | + }, |
| 103 | +); |
112 | 104 |
|
113 |
| -it('should be no-op when passing common items', () => { |
114 |
| - fc.assert( |
115 |
| - fc.property(fc.array(fc.char()), fc.array(fc.char()), (a, b) => { |
116 |
| - const commonItems = findCommonItems(a, b); |
117 |
| - expect(findCommonItems(a, commonItems)).toEqual(commonItems); |
118 |
| - expect(findCommonItems(commonItems, a)).toEqual(commonItems); |
119 |
| - }), |
120 |
| - ); |
121 |
| -}); |
| 105 | +it.prop([fc.array(fc.char()), fc.array(fc.char())])( |
| 106 | + 'should be no-op when passing common items', |
| 107 | + (a, b) => { |
| 108 | + const commonItems = findCommonItems(a, b); |
| 109 | + expect(findCommonItems(a, commonItems)).toEqual(commonItems); |
| 110 | + expect(findCommonItems(commonItems, a)).toEqual(commonItems); |
| 111 | + }, |
| 112 | +); |
122 | 113 |
|
123 |
| -it('should find the exact common items when one array is subarray of the other', () => { |
124 |
| - fc.assert( |
125 |
| - fc.property(fc.array(fc.array(fc.char())), data => { |
126 |
| - const allData = flatten(data); // [...data[0], ...data[1], ...data[2], ...data[3], ...] |
127 |
| - const partialData = flatten(data.filter((_, i) => i % 2 === 1)); // [...data[1], ...data[3], ...] |
128 |
| - const commonItems = findCommonItems(allData, partialData); |
129 |
| - // We have: |
130 |
| - // 1. commonItems contains at least all the items of partialData as they are in allData too |
131 |
| - // 2. commonItems cannot contain more items than its inputs (partialData for instance) |
132 |
| - expect(commonItems.length).toBeGreaterThanOrEqual(partialData.length); |
133 |
| - }), |
134 |
| - ); |
135 |
| -}); |
| 114 | +it.prop([fc.array(fc.array(fc.char()))])( |
| 115 | + 'should find the exact common items when one array is subarray of the other', |
| 116 | + data => { |
| 117 | + const allData = flatten(data); // [...data[0], ...data[1], ...data[2], ...data[3], ...] |
| 118 | + const partialData = flatten(data.filter((_, i) => i % 2 === 1)); // [...data[1], ...data[3], ...] |
| 119 | + const commonItems = findCommonItems(allData, partialData); |
| 120 | + // We have: |
| 121 | + // 1. commonItems contains at least all the items of partialData as they are in allData too |
| 122 | + // 2. commonItems cannot contain more items than its inputs (partialData for instance) |
| 123 | + expect(commonItems.length).toBeGreaterThanOrEqual(partialData.length); |
| 124 | + }, |
| 125 | +); |
0 commit comments