Skip to content
This repository was archived by the owner on Feb 2, 2021. It is now read-only.

Commit d96a91e

Browse files
Add unit tests
1 parent 9c24826 commit d96a91e

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

test/unit-tests/file-system.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,57 @@ function isOsCaseSensitive(testInjector: IInjector): boolean {
1818
};
1919
temp.track();
2020

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+
2172
function createTestInjector(): IInjector {
2273
let testInjector = new Yok();
2374

@@ -226,4 +277,37 @@ describe("FileSystem", () => {
226277
assert.deepEqual(emptyDirectories, _.reverse(removedDirectories));
227278
});
228279
});
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+
});
229313
});

0 commit comments

Comments
 (0)