@@ -18,6 +18,57 @@ function isOsCaseSensitive(testInjector: IInjector): boolean {
18
18
} ;
19
19
temp . track ( ) ;
20
20
21
+ function createWriteJsonTestCases ( ) : { exists : boolean , text : string , testCondition : string , expectedIndentation : string } [ ] {
22
+ return [
23
+ {
24
+ exists : true ,
25
+ text : `{\n\t"a" : 5 }` ,
26
+ testCondition : "when the indentation is tab" ,
27
+ expectedIndentation : "\t"
28
+ } , {
29
+ exists : true ,
30
+ text : `{\n "a" : 5 }` ,
31
+ testCondition : "when the indentation is space" ,
32
+ expectedIndentation : " "
33
+ } , {
34
+ exists : true ,
35
+ text : `{\n "a" : 5 }` ,
36
+ testCondition : "when the indentation is two spaces" ,
37
+ expectedIndentation : " "
38
+ } , {
39
+ exists : false ,
40
+ text : `{\n "a" : 5 }` ,
41
+ testCondition : "when the file does not exist" ,
42
+ expectedIndentation : "\t"
43
+ } , {
44
+ exists : true ,
45
+ text : `"just-string"` ,
46
+ testCondition : "when the the content is string" ,
47
+ expectedIndentation : "\t"
48
+ } , {
49
+ exists : true ,
50
+ text : `{ "a" : 5 }` ,
51
+ testCondition : "when the content does not have new line after the {" ,
52
+ expectedIndentation : " "
53
+ } , {
54
+ exists : true ,
55
+ text : `{"a" : 5 }` ,
56
+ testCondition : "when the content is not correctly formatted" ,
57
+ expectedIndentation : "\t"
58
+ } , {
59
+ exists : true ,
60
+ text : `{\r\n "a" : 5 }` ,
61
+ testCondition : "when the new line is in Windows format" ,
62
+ expectedIndentation : " "
63
+ } , {
64
+ exists : true ,
65
+ text : `{\r\n\t"a" : 5 }` ,
66
+ testCondition : "when the new line is in Windows format" ,
67
+ expectedIndentation : "\t"
68
+ }
69
+ ] ;
70
+ }
71
+
21
72
function createTestInjector ( ) : IInjector {
22
73
let testInjector = new Yok ( ) ;
23
74
@@ -226,4 +277,37 @@ describe("FileSystem", () => {
226
277
assert . deepEqual ( emptyDirectories , _ . reverse ( removedDirectories ) ) ;
227
278
} ) ;
228
279
} ) ;
280
+
281
+ describe ( "writeJson" , ( ) => {
282
+ let testCases = createWriteJsonTestCases ( ) ,
283
+ testInjector : IInjector ,
284
+ fs : IFileSystem ;
285
+
286
+ beforeEach ( ( ) => {
287
+ testInjector = createTestInjector ( ) ;
288
+
289
+ fs = testInjector . resolve ( "fs" ) ;
290
+ } ) ;
291
+
292
+ _ . each ( testCases , ( testCase ) => {
293
+ it ( `should use the correct indentation ${ testCase . testCondition } .` , ( ) => {
294
+ fs . readText = ( ) => Future . fromResult ( testCase . text ) ;
295
+ fs . exists = ( ) => Future . fromResult ( testCase . exists ) ;
296
+ fs . writeFile = ( ) => Future . fromResult ( ) ;
297
+
298
+ let actualIndentation : string ;
299
+ let originalJsonStringify = JSON . stringify ;
300
+
301
+ ( < any > JSON ) . stringify = ( value : any , replacer : any [ ] , space : string | number ) => {
302
+ actualIndentation = < string > space ;
303
+ } ;
304
+
305
+ fs . writeJson ( "" , testCase . text ) . wait ( ) ;
306
+
307
+ JSON . stringify = originalJsonStringify ;
308
+
309
+ assert . deepEqual ( actualIndentation , testCase . expectedIndentation ) ;
310
+ } ) ;
311
+ } ) ;
312
+ } ) ;
229
313
} ) ;
0 commit comments