@@ -8,7 +8,7 @@ import { invariant } from '../../jsutils/invariant';
8
8
9
9
import { Lexer } from '../lexer' ;
10
10
import { Source } from '../source' ;
11
- import { printBlockString } from '../blockString' ;
11
+ import { printBlockString , isPrintableAsBlockString } from '../blockString' ;
12
12
13
13
function lexValue ( str : string ) : string {
14
14
const lexer = new Lexer ( new Source ( str ) ) ;
@@ -19,6 +19,34 @@ function lexValue(str: string): string {
19
19
return value ;
20
20
}
21
21
22
+ function testPrintableBlockString (
23
+ testValue : string ,
24
+ options ?: { minimize : boolean } ,
25
+ ) : void {
26
+ const blockString = printBlockString ( testValue , options ) ;
27
+ const printedValue = lexValue ( blockString ) ;
28
+ invariant (
29
+ testValue === printedValue ,
30
+ dedent `
31
+ Expected lexValue(${ inspectStr ( blockString ) } )
32
+ to equal ${ inspectStr ( testValue ) }
33
+ but got ${ inspectStr ( printedValue ) }
34
+ ` ,
35
+ ) ;
36
+ }
37
+
38
+ function testNonPrintableBlockString ( testValue : string ) : void {
39
+ const blockString = printBlockString ( testValue ) ;
40
+ const printedValue = lexValue ( blockString ) ;
41
+ invariant (
42
+ testValue !== printedValue ,
43
+ dedent `
44
+ Expected lexValue(${ inspectStr ( blockString ) } )
45
+ to not equal ${ inspectStr ( testValue ) }
46
+ ` ,
47
+ ) ;
48
+ }
49
+
22
50
describe ( 'printBlockString' , ( ) => {
23
51
it ( 'correctly print random strings' , ( ) => {
24
52
// Testing with length >7 is taking exponentially more time. However it is
@@ -27,39 +55,13 @@ describe('printBlockString', () => {
27
55
allowedChars : [ '\n' , '\t' , ' ' , '"' , 'a' , '\\' ] ,
28
56
maxLength : 7 ,
29
57
} ) ) {
30
- const testStr = '"""' + fuzzStr + '"""' ;
31
-
32
- let testValue ;
33
- try {
34
- testValue = lexValue ( testStr ) ;
35
- } catch ( e ) {
36
- continue ; // skip invalid values
58
+ if ( ! isPrintableAsBlockString ( fuzzStr ) ) {
59
+ testNonPrintableBlockString ( fuzzStr ) ;
60
+ continue ;
37
61
}
38
- invariant ( typeof testValue === 'string' ) ;
39
-
40
- const printedValue = lexValue ( printBlockString ( testValue ) ) ;
41
-
42
- invariant (
43
- testValue === printedValue ,
44
- dedent `
45
- Expected lexValue(printBlockString(${ inspectStr ( testValue ) } ))
46
- to equal ${ inspectStr ( testValue ) }
47
- but got ${ inspectStr ( printedValue ) }
48
- ` ,
49
- ) ;
50
-
51
- const printedMultilineString = lexValue (
52
- printBlockString ( testValue , true ) ,
53
- ) ;
54
62
55
- invariant (
56
- testValue === printedMultilineString ,
57
- dedent `
58
- Expected lexValue(printBlockString(${ inspectStr ( testValue ) } , true))
59
- to equal ${ inspectStr ( testValue ) }
60
- but got ${ inspectStr ( printedMultilineString ) }
61
- ` ,
62
- ) ;
63
+ testPrintableBlockString ( fuzzStr ) ;
64
+ testPrintableBlockString ( fuzzStr , { minimize : true } ) ;
63
65
}
64
66
} ) . timeout ( 20000 ) ;
65
67
} ) ;
0 commit comments