-
Notifications
You must be signed in to change notification settings - Fork 17
/
ini.spec.ts
244 lines (216 loc) · 6.93 KB
/
ini.spec.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import {
comment,
definitionLine,
definitionList,
EmptyLine,
GlobalSection,
globalSection,
identifier,
IniFile,
iniFile,
NamedSection,
Property,
section,
sectionHeader,
value
} from "../../examples/ini";
describe("comments", () => {
it("can parse a ; comment", () => {
expect(comment.parse("; this is a comment")).toBeSuccessful("this is a comment");
});
it("can parse a # comment", () => {
expect(comment.parse("# this is a comment")).toBeSuccessful("this is a comment");
});
it("parse a comment at the end of the file", () => {
expect(comment.parse("; this is a comment")).toBeSuccessful("this is a comment");
});
});
describe("identifiers", () => {
it("can parse a simple identifier", () => {
expect(identifier.parse("abc")).toBeSuccessful("abc");
});
it("cannot be empty", () => {
expect(identifier.parse("")).toBeFailure();
});
});
describe("values", () => {
it("can parse a simple value", () => {
expect(value.parse("abc")).toBeSuccessful("abc");
});
it("cannot be empty", () => {
expect(value.parse("")).toBeFailure();
});
it("can contain spaces", () => {
expect(value.parse("abc def")).toBeSuccessful("abc def");
});
it("can contain tabs", () => {
expect(value.parse("abc\tdef")).toBeSuccessful("abc\tdef");
});
});
describe("definitionLine", () => {
it("can parse a simple definition", () => {
expect(definitionLine.parse("abc=def")).toBeSuccessful(new Property("abc", "def"));
});
it("can parse a definition with spaces", () => {
expect(definitionLine.parse("abc = def ")).toBeSuccessful(new Property("abc", "def "));
});
it("can parse a definition with tabs", () => {
expect(definitionLine.parse("abc\t=\tdef ")).toBeSuccessful(new Property("abc", "def "));
});
it("can parse a definition with a comment", () => {
expect(definitionLine.parse("abc=def ; comment")).toBeSuccessful(
new Property("abc", "def ")
);
});
it("converts the name to lowercase", () => {
expect(definitionLine.parse("ABC=def")).toBeSuccessful(new Property("abc", "def"));
});
});
describe("definitionList", () => {
it("can parse many definitions", () => {
const input = [
//
"abc=def",
"ghi=jkl"
].join("\n");
expect(definitionList.parse(input)).toBeSuccessful([
new Property("abc", "def"),
new Property("ghi", "jkl")
]);
});
it("can have empty lines between definitions", () => {
const input = [
//
"abc=def",
"",
"ghi=jkl"
].join("\n");
expect(definitionList.parse(input)).toBeSuccessful([
new Property("abc", "def"),
new EmptyLine(),
new Property("ghi", "jkl")
]);
});
it("can have empty lines with comments between definitions", () => {
const input = [
//
"abc=def",
"; comment",
"ghi=jkl"
].join("\n");
expect(definitionList.parse(input)).toBeSuccessful([
new Property("abc", "def"),
new EmptyLine(),
new Property("ghi", "jkl")
]);
});
});
describe("section headers", () => {
it("can parse a simple section header", () => {
expect(sectionHeader.parse("[abc]")).toBeSuccessful("abc");
});
it("cannot be empty", () => {
expect(sectionHeader.parse("[]")).toBeFailure();
});
it("can contain spaces", () => {
expect(sectionHeader.parse("[abc def]")).toBeSuccessful("abc def");
});
it("can contain tabs", () => {
expect(sectionHeader.parse("[abc\tdef]")).toBeSuccessful("abc\tdef");
});
it("can have a comment at the end", () => {
expect(sectionHeader.parse("[abc] ; comment")).toBeSuccessful("abc");
});
});
describe("sections", () => {
it("can parse a simple section", () => {
const input = [
//
"[abc]",
"def=ghi",
"jkl=mno"
].join("\n");
expect(section.parse(input)).toBeSuccessful(
new NamedSection("abc", [new Property("def", "ghi"), new Property("jkl", "mno")])
);
});
});
describe("global section", () => {
it("can parse a simple global section", () => {
const input = [
//
"def=ghi",
"",
"; comment",
"jkl=mno"
].join("\n");
expect(globalSection.parse(input)).toBeSuccessful(
new GlobalSection([
new Property("def", "ghi"),
new EmptyLine(),
new EmptyLine(),
new Property("jkl", "mno")
])
);
});
});
describe("ini file", () => {
it("can parse a simple ini file", () => {
const input = [
"; This is the global section",
"globalKey1=globalValue1",
"globalKey2=globalValue2",
"",
"[Section1]",
"Key1=Value1",
"; line comment",
"# another line comment",
"Key2=Value2",
"[Section2]",
"KeyA=ValueA",
"",
"KeyB=ValueB"
].join("\n");
expect(iniFile.parse(input)).toBeSuccessful(
new IniFile(
new GlobalSection([
new EmptyLine(),
new Property("globalkey1", "globalValue1"),
new Property("globalkey2", "globalValue2"),
new EmptyLine()
]),
[
new NamedSection("Section1", [
new Property("key1", "Value1"),
new EmptyLine(),
new EmptyLine(),
new Property("key2", "Value2")
]),
new NamedSection("Section2", [
new Property("keya", "ValueA"),
new EmptyLine(),
new Property("keyb", "ValueB")
])
]
)
);
});
it("isn't required to have a global section", () => {
const input = ["[Section1]", "Key1=Value1"].join("\n");
expect(iniFile.parse(input)).toBeSuccessful(
new IniFile(new GlobalSection([]), [
new NamedSection("Section1", [new Property("key1", "Value1")])
])
);
});
it("can have an empty section", () => {
const input = ["[Section1]", ""].join("\n");
expect(iniFile.parse(input)).toBeSuccessful(
new IniFile(new GlobalSection([]), [new NamedSection("Section1", [])])
);
});
it("can have no contents", () => {
const input = "";
expect(iniFile.parse(input)).toBeSuccessful(new IniFile(new GlobalSection([]), []));
});
});