-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add property based tests on diff-sequences #9072
Changes from 1 commit
2ada316
9360d9c
dd3ccfd
f281006
5c2e725
ed21dbb
8770811
03d6b64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import diff from '../'; | ||
import fc from 'fast-check' | ||
|
||
const findCommonItems = (a: string[], b: string[]): string[] => { | ||
const array: string[] = []; | ||
diff( | ||
a.length, | ||
b.length, | ||
(aIndex: number, bIndex: number) => a[aIndex] === b[bIndex], | ||
(nCommon: number, aCommon: number, bCommon: number) => { | ||
for (; nCommon !== 0; nCommon -= 1, aCommon += 1) { | ||
array.push(a[aCommon]); | ||
} | ||
}, | ||
); | ||
return array; | ||
}; | ||
|
||
const flatten = (data: string[][]) => { | ||
const array: string[] = []; | ||
for (const items of data) { | ||
array.push(...items) | ||
} | ||
return array; | ||
} | ||
|
||
test('should be reflexive', () => { | ||
dubzzz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fc.assert(fc.property( | ||
fc.array(fc.char()), | ||
(a) => { | ||
expect(findCommonItems(a, a)).toEqual(a) | ||
} | ||
)) | ||
}) | ||
|
||
test('should find the same number of common items when switching the inputs', () => { | ||
// findCommonItems is not symmetric as: | ||
// > findCommonItems(["Z"," "], [" ","Z"]) = [" "] | ||
// > findCommonItems([" ","Z"], ["Z"," "]) = ["Z"] | ||
fc.assert(fc.property( | ||
fc.array(fc.char()), fc.array(fc.char()), | ||
(a, b) => { | ||
const commonItems = findCommonItems(a, b); | ||
const symmetricCommonItems = findCommonItems(b, a); | ||
expect(symmetricCommonItems).toHaveLength(commonItems.length) | ||
} | ||
)) | ||
}) | ||
|
||
test('should have at most the length of its inputs', () => { | ||
fc.assert(fc.property( | ||
fc.array(fc.char()), fc.array(fc.char()), | ||
(a, b) => { | ||
const commonItems = findCommonItems(a, b) | ||
expect(commonItems.length).toBeLessThanOrEqual(a.length); | ||
expect(commonItems.length).toBeLessThanOrEqual(b.length); | ||
} | ||
)) | ||
}) | ||
|
||
test('should be no-op when passing common items', () => { | ||
fc.assert(fc.property( | ||
fc.array(fc.char()), fc.array(fc.char()), | ||
(a, b) => { | ||
const commonItems = findCommonItems(a, b) | ||
expect(findCommonItems(a, commonItems)).toEqual(commonItems) | ||
expect(findCommonItems(commonItems, a)).toEqual(commonItems) | ||
} | ||
)) | ||
}) | ||
|
||
test('should find at the least the maximal known subset', () => { | ||
fc.assert(fc.property( | ||
fc.array(fc.array(fc.char())), | ||
(data) => { | ||
const allData = flatten(data); // [...data[0], ...data[1], ...data[2], ...data[3], ...] | ||
const partialData = flatten(data.filter((_, i) => i % 2 === 1)) // [...data[1], ...data[3], ...] | ||
const commonItems = findCommonItems(allData, partialData) | ||
expect(commonItems.length).toBeGreaterThanOrEqual(partialData.length) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test also passes with Exploring what it would look like to use the preceding test alone to assert the property: it('should find subsequence', () => {
fc.assert(fc.property(
fc.array(fc.char()), fc.array(fc.char()),
(a, b) => {
const commonItems = findCommonItems(a, b)
expect(findCommonItems(a, commonItems)).toEqual(commonItems)
expect(findCommonItems(commonItems, a)).toEqual(commonItems)
expect(findCommonItems(b, commonItems)).toEqual(commonItems)
expect(findCommonItems(commonItems, b)).toEqual(commonItems)
}
))
}); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I am right your suggestion for 💭 Let's suppose for a minute that we remove the test called In that case if we use Actually this last test is the only test covering the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think if the assertion becomes: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're totally right as we have both:
We have: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It makes this You can ignore my comment:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, in the last test, let‘s assert the items themselves instead of the length. If you agree in the second to last test, let‘s include assertions for both |
||
} | ||
)) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lint-and-typecheck
failed on CI withArray type using 'string[]' is forbidden. Use 'Array<string>' instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you. It looks like a few more lint errors. Does
yarn lint --fix
locally display them?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought I did, I'll retry
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pedrottimark This time I did it for sure ^^ I might not be at head. Maybe I could just merge onto master on branch to get the latest changes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohhh, now it’s about the Facebook comment at the beginning of the file. I always forget it.
You can copy and paste from
index.test.ts
fileThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it:
Facebook copyright header check failed for the following files