-
Notifications
You must be signed in to change notification settings - Fork 243
/
literate.test.ts
130 lines (119 loc) · 2.65 KB
/
literate.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import {
includeAndRenderExamples,
typescriptSourceToMarkdown,
} from '../lib/literate';
test('simple file gets wrapped in tags', () => {
assertRendersTo(
['const x = 1;', 'console.log(x);'],
['```ts', 'const x = 1;', 'console.log(x);', '```'],
);
});
test('can switch on and off', () => {
assertRendersTo(
[
'const x = 1;',
'/// !show',
'console.log(x);',
'/// !hide',
'console.log("It worked")',
],
['```ts', 'console.log(x);', '```'],
);
});
test('common whitespace in a single block gets stripped', () => {
assertRendersTo(
[
'const x = 1;',
'if (x) {',
' /// !show',
' console.log(x);',
' /// !hide',
'}',
],
['```ts', 'console.log(x);', '```'],
);
});
test('inline markdown with indentation still gets rendered', () => {
assertRendersTo(
[
'const x = 1;',
'if (x) {',
' /// This is how we render x',
' console.log(x);',
'}',
],
[
'```ts',
'const x = 1;',
'if (x) {',
'```',
'This is how we render x',
'```ts',
' console.log(x);',
'}',
'```',
],
);
});
test('subsequent code blocks get joined', () => {
assertRendersTo(
[
'/// !show',
'let x = 1;',
'/// !hide',
'x += 1;',
'/// !show',
'console.log(x);',
'/// !hide',
],
['```ts', 'let x = 1;', 'console.log(x);', '```'],
);
});
test('can add inline MarkDown', () => {
assertRendersTo(
['const x = 1;', '/// This is how we print x', 'console.log(x);'],
[
'```ts',
'const x = 1;',
'```',
'This is how we print x',
'```ts',
'console.log(x);',
'```',
],
);
});
test('can do example inclusion', async () => {
const inputMarkDown = [
'This is a preamble',
'[included here](test/something.lit.ts)',
'This is a postamble',
];
const fakeLoader = (fileName: string) => {
expect(fileName).toBe('test/something.lit.ts');
return {
fullPath: fileName,
lines: ['const x = 1;', '/// This is how we print x', 'console.log(x);'],
};
};
const rendered = await includeAndRenderExamples(
inputMarkDown,
fakeLoader,
'.',
);
expect(rendered).toEqual([
'This is a preamble',
'```ts lit=test/something.lit.ts',
'const x = 1;',
'```',
'This is how we print x',
'```ts lit=test/something.lit.ts',
'console.log(x);',
'```',
'This is a postamble',
]);
});
function assertRendersTo(source: string[], expected: string[]) {
const rendered = typescriptSourceToMarkdown(source, []);
expect(expected).toEqual(rendered);
}