forked from graphql/graphql-js
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
blockString-test: add fuzzing test for 'printBlockString'
Motivation graphql#2512
- Loading branch information
1 parent
437cc1b
commit 14b054d
Showing
7 changed files
with
239 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// @flow strict | ||
|
||
import { expect } from 'chai'; | ||
import { describe, it } from 'mocha'; | ||
|
||
import genFuzzStrings from '../genFuzzStrings'; | ||
|
||
function expectFuzzStrings(options) { | ||
return expect(Array.from(genFuzzStrings(options))); | ||
} | ||
|
||
describe('genFuzzStrings', () => { | ||
it('always provide empty string', () => { | ||
expectFuzzStrings({ allowedChars: [], maxLength: 0 }).to.deep.equal(['']); | ||
expectFuzzStrings({ allowedChars: [], maxLength: 1 }).to.deep.equal(['']); | ||
expectFuzzStrings({ allowedChars: ['a'], maxLength: 0 }).to.deep.equal([ | ||
'', | ||
]); | ||
}); | ||
|
||
it('generate strings with single character', () => { | ||
expectFuzzStrings({ allowedChars: ['a'], maxLength: 1 }).to.deep.equal([ | ||
'', | ||
'a', | ||
]); | ||
|
||
expectFuzzStrings({ | ||
allowedChars: ['a', 'b', 'c'], | ||
maxLength: 1, | ||
}).to.deep.equal(['', 'a', 'b', 'c']); | ||
}); | ||
|
||
it('generate strings with multiple character', () => { | ||
expectFuzzStrings({ allowedChars: ['a'], maxLength: 2 }).to.deep.equal([ | ||
'', | ||
'a', | ||
'aa', | ||
]); | ||
|
||
expectFuzzStrings({ | ||
allowedChars: ['a', 'b', 'c'], | ||
maxLength: 2, | ||
}).to.deep.equal([ | ||
'', | ||
'a', | ||
'b', | ||
'c', | ||
'aa', | ||
'ab', | ||
'ac', | ||
'ba', | ||
'bb', | ||
'bc', | ||
'ca', | ||
'cb', | ||
'cc', | ||
]); | ||
}); | ||
|
||
it('generate strings longer than possible number of characters', () => { | ||
expectFuzzStrings({ | ||
allowedChars: ['a', 'b'], | ||
maxLength: 3, | ||
}).to.deep.equal([ | ||
'', | ||
'a', | ||
'b', | ||
'aa', | ||
'ab', | ||
'ba', | ||
'bb', | ||
'aaa', | ||
'aab', | ||
'aba', | ||
'abb', | ||
'baa', | ||
'bab', | ||
'bba', | ||
'bbb', | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// @flow strict | ||
|
||
import { expect } from 'chai'; | ||
import { describe, it } from 'mocha'; | ||
|
||
import inspectStr from '../inspectStr'; | ||
|
||
describe('inspectStr', () => { | ||
it('handles null and undefined values', () => { | ||
expect(inspectStr(null)).to.equal('null'); | ||
expect(inspectStr(undefined)).to.equal('null'); | ||
}); | ||
|
||
it('correctly print various strings', () => { | ||
expect(inspectStr('')).to.equal('``'); | ||
expect(inspectStr('a')).to.equal('`a`'); | ||
expect(inspectStr('"')).to.equal('`"`'); | ||
expect(inspectStr("'")).to.equal("`'`"); | ||
expect(inspectStr('\\"')).to.equal('`\\"`'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// @flow strict | ||
|
||
/** | ||
* Generator that produces all possible combinations of allowed characters. | ||
*/ | ||
export default function* genFuzzStrings(options: {| | ||
allowedChars: Array<string>, | ||
maxLength: number, | ||
|}): Generator<string, void, void> { | ||
const { allowedChars, maxLength } = options; | ||
const numAllowedChars = allowedChars.length; | ||
|
||
let numCombinations = 0; | ||
for (let length = 1; length <= maxLength; ++length) { | ||
numCombinations += numAllowedChars ** length; | ||
} | ||
|
||
yield ''; // special case for empty string | ||
for (let combination = 0; combination < numCombinations; ++combination) { | ||
let permutation = ''; | ||
|
||
let leftOver = combination; | ||
while (leftOver >= 0) { | ||
const reminder = leftOver % numAllowedChars; | ||
permutation = allowedChars[reminder] + permutation; | ||
leftOver = (leftOver - reminder) / numAllowedChars - 1; | ||
} | ||
|
||
yield permutation; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// @flow strict | ||
|
||
/** | ||
* Special inspect function to produce readable string literal for error messages in tests | ||
*/ | ||
export default function inspectStr(str: ?string): string { | ||
if (str == null) { | ||
return 'null'; | ||
} | ||
return JSON.stringify(str) | ||
.replace(/^"|"$/g, '`') | ||
.replace(/\\"/g, '"') | ||
.replace(/\\\\/g, '\\'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters