-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathfile-tree.business.spec.ts
More file actions
230 lines (206 loc) · 6.13 KB
/
file-tree.business.spec.ts
File metadata and controls
230 lines (206 loc) · 6.13 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import { parseFileTreeText } from './file-tree.business';
describe('parseFileTreeText', () => {
describe('Basic functionality', () => {
it.each([
['+ Documents', 'folder', 'Documents'],
['- Downloads', 'subfolder', 'Downloads'],
['* README.md', 'file', 'README.md'],
['Projects', 'folder', 'Projects'],
])(
'should parse %s as %s with text "%s"',
(text, expectedType, expectedText) => {
// Arrange
// Act
const result = parseFileTreeText(text);
// Assert
expect(result).toEqual([
{
type: expectedType,
text: expectedText,
level: 0,
},
]);
}
);
});
describe('Indentation levels', () => {
it.each<{ description: string; text: string; expectedLevel: number }>([
{
description: 'no spaces create level 0',
text: '+ Root',
expectedLevel: 0,
},
{
description: '3 spaces create level 1',
text: ' + Subfolder',
expectedLevel: 1,
},
{
description: '6 spaces create level 2',
text: ' * File',
expectedLevel: 2,
},
{
description: '9 spaces create level 3',
text: ' + Deep folder',
expectedLevel: 3,
},
])('$description', ({ text, expectedLevel }) => {
// Arrange
// Act
const result = parseFileTreeText(text);
// Assert
expect(result[0].level).toBe(expectedLevel);
});
it('should handle indentation with non-standard spacing', () => {
// Arrange
const text = ` + Two spaces (level 0)
+ Four spaces (level 1)
+ Five spaces (level 1)
+ Seven spaces (level 2)`;
// Act
const result = parseFileTreeText(text);
// Assert
expect(result[0].level).toBe(0); // 2/3 = 0
expect(result[1].level).toBe(1); // 4/3 = 1
expect(result[2].level).toBe(1); // 5/3 = 1
expect(result[3].level).toBe(2); // 7/3 = 2
});
it('should handle complex nested structure', () => {
// Arrange
const text = `+ Root
- Subfolder 1
* File 1
+ Subfolder 2
- Deep subfolder
* Deep file
+ Deep folder
- Deep subfolder 2
* Deep file 2`;
// Act
const result = parseFileTreeText(text);
// Assert
expect(result).toEqual([
{ type: 'folder', text: 'Root', level: 0 },
{ type: 'subfolder', text: 'Subfolder 1', level: 1 },
{ type: 'file', text: 'File 1', level: 2 },
{ type: 'folder', text: 'Subfolder 2', level: 1 },
{ type: 'subfolder', text: 'Deep subfolder', level: 1 },
{ type: 'file', text: 'Deep file', level: 2 },
{ type: 'folder', text: 'Deep folder', level: 3 },
{ type: 'subfolder', text: 'Deep subfolder 2', level: 5 },
{ type: 'file', text: 'Deep file 2', level: 6 },
]);
});
});
describe('Corner cases', () => {
it.each<{ description: string; input: string; expected: any[] }>([
{
description: 'return empty array for empty string',
input: '',
expected: [],
},
{
description:
'filter out lines with only newlines between valid content',
input: `
+ Folder
* File
`,
expected: [
{ type: 'folder', text: 'Folder', level: 0 },
{ type: 'file', text: 'File', level: 0 },
],
},
{
description: 'return empty array for text with only newlines',
input: '\n\n\n',
expected: [],
},
])('should $description', ({ input, expected }) => {
// Arrange
// Act
const result = parseFileTreeText(input);
// Assert
expect(result).toEqual(expected);
});
});
describe('Edge cases with symbols', () => {
it.each<{ text: string; expected: any[]; description: string }>([
{
description:
'ignore extra spaces after the symbol and keep correct type',
text: `+ Documents
- Downloads
* README.md`,
expected: [
{ type: 'folder', text: 'Documents', level: 0 },
{ type: 'subfolder', text: 'Downloads', level: 0 },
{ type: 'file', text: 'README.md', level: 0 },
],
},
{
description: 'handle symbols without space after as plain text',
text: `+
-
*`,
expected: [
{ type: 'folder', text: '+', level: 0 },
{ type: 'folder', text: '-', level: 0 },
{ type: 'folder', text: '*', level: 0 },
],
},
{
description: 'trim leading/trailing whitespace in text',
text: `+ Documents
- Downloads
* README.md `,
expected: [
{ type: 'folder', text: 'Documents', level: 0 },
{ type: 'subfolder', text: 'Downloads', level: 0 },
{ type: 'file', text: 'README.md', level: 0 },
],
},
{
description: 'recognize symbols with space but no text',
text: `+
-
* `,
expected: [
{ type: 'folder', text: '', level: 0 },
{ type: 'subfolder', text: '', level: 0 },
{ type: 'file', text: '', level: 0 },
],
},
{
description:
'handle lines starting with symbols but not followed by space, as folder and plain text',
text: `+Documents
-Downloads
*README.md`,
expected: [
{ type: 'folder', text: '+Documents', level: 0 },
{ type: 'folder', text: '-Downloads', level: 0 },
{ type: 'folder', text: '*README.md', level: 0 },
],
},
])('should $description', ({ text, expected }) => {
// Arrange
// Act
const result = parseFileTreeText(text);
// Assert
expect(result).toEqual(expected);
});
});
describe('Large indentation values', () => {
it('should handle 27 spaces creating level 9 indentation', () => {
// Arrange
const spaces = ' '; // 27 spaces
// Act
const text = `${spaces}+ Deep folder`;
const result = parseFileTreeText(text);
// Assert
expect(result[0].level).toBe(9);
});
});
});