eini_tests.erl 18.7 KB
Newer Older
Shunichi Shinohara's avatar
Shunichi Shinohara committed
1
2
3
4
5
6
-module(eini_tests).

-author('shino@accense.com').

-include_lib("eunit/include/eunit.hrl").

7
-import(eini, [parse/1]).
Shunichi Shinohara's avatar
Shunichi Shinohara committed
8
9
10
11
12
13
14
15
16
17
18
19

setup() ->
  ok.

teardown(_) ->
  ok.

empty_test_() ->
  {setup,
   fun setup/0,
   fun teardown/1,
   [
Shunichi Shinohara's avatar
Shunichi Shinohara committed
20
21
    ?_assertEqual({ok, []}, parse("")),
    ?_assertEqual({ok, []}, parse("\n"))
Shunichi Shinohara's avatar
Shunichi Shinohara committed
22
23
   ]}.

Shunichi Shinohara's avatar
Shunichi Shinohara committed
24
one_section_title_only_test_() ->
Shunichi Shinohara's avatar
Shunichi Shinohara committed
25
26
27
28
  {setup,
   fun setup/0,
   fun teardown/1,
   [
29
30
31
32
33
    %% comment only
    ?_assertEqual({ok, []},
                  parse(
                    ";"
                   )),
34
35
36
37
    ?_assertEqual({ok, []},
                  parse(
                      "#"
                  )),
38
39
40
41
42
43
44
45
    ?_assertEqual({ok, []},
                  parse(
                    ";    "
                   )),
    ?_assertEqual({ok, []},
                  parse(
                    "; comment"
                   )),
46
47
48
49
    ?_assertEqual({ok, []},
                  parse(
                      "# comment"
                  )),
50
51
52
53
    ?_assertEqual({ok, []},
                  parse(
                    "; comment in Japanese 日本語"
                   )),
Shunichi Shinohara's avatar
Shunichi Shinohara committed
54
    %% Title only
Shunichi Shinohara's avatar
Shunichi Shinohara committed
55
    ?_assertEqual({ok, [
56
                        {title, []}
Shunichi Shinohara's avatar
Shunichi Shinohara committed
57
                       ]},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
58
                  parse(
59
                    "[title]\n"
Shunichi Shinohara's avatar
Shunichi Shinohara committed
60
                   )),
Shunichi Shinohara's avatar
Shunichi Shinohara committed
61
62
    %% Title only, but trailing spaces
    ?_assertEqual({ok, [
63
                        {title, []}
Shunichi Shinohara's avatar
Shunichi Shinohara committed
64
                       ]},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
65
                  parse(
Shunichi Shinohara's avatar
Shunichi Shinohara committed
66
67
                    "[title]  \n"
                   )),
Shunichi Shinohara's avatar
Shunichi Shinohara committed
68
69
    %% Title only, but comment lines
    ?_assertEqual({ok, [
70
                        {title, []}
Shunichi Shinohara's avatar
Shunichi Shinohara committed
71
                       ]},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
72
                  parse(
73
74
                    "; comment line 1\n"
                    "# comment line 2\n"
Shunichi Shinohara's avatar
Shunichi Shinohara committed
75
76
77
78
                    "  \n"
                    "[title]\n"
                   )),
    ?_assertEqual({ok, [
79
                        {title, []}
Shunichi Shinohara's avatar
Shunichi Shinohara committed
80
                       ]},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
81
                  parse(
82
                    "# comment line\n"
Shunichi Shinohara's avatar
Shunichi Shinohara committed
83
                    "; comment line 2\n"
Shunichi Shinohara's avatar
Shunichi Shinohara committed
84
85
86
87
                    "  \n"
                    "[title]\n"
                   )),
    ?_assertEqual({ok, [
88
                        {title, []}
Shunichi Shinohara's avatar
Shunichi Shinohara committed
89
                       ]},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
90
                  parse(
Shunichi Shinohara's avatar
Shunichi Shinohara committed
91
92
                    "; comment line\n"
                    "; comment line 2\n"
Shunichi Shinohara's avatar
Shunichi Shinohara committed
93
94
95
96
                    "  \n"
                    "[title]\n"
                    "; comment after section title"
                   )),
Shunichi Shinohara's avatar
Shunichi Shinohara committed
97
98
    %% Title only, but preceding blank lines
    ?_assertEqual({ok, [
99
                        {title, []}
Shunichi Shinohara's avatar
Shunichi Shinohara committed
100
                       ]},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
101
                  parse(
Shunichi Shinohara's avatar
Shunichi Shinohara committed
102
103
104
105
                    "\n"
                    "  \n"
                    "[title]\n"
                   )),
106
    ?_assertEqual({ok, [
107
                        {title, []}
108
                       ]},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
109
                  parse(
110
111
112
113
                    "  \n"
                    "\n"
                    "[title]\n"
                   )),
Shunichi Shinohara's avatar
Shunichi Shinohara committed
114
115
    %% Title only, but preceding blank lines and trailing spaces
    ?_assertEqual({ok, [
116
                        {title, []}
Shunichi Shinohara's avatar
Shunichi Shinohara committed
117
                       ]},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
118
                  parse(
Shunichi Shinohara's avatar
Shunichi Shinohara committed
119
120
121
122
123
124
                    "\n"
                    "  \n"
                    "[title]\t\s\n"
                   )),
    %% Title only, but trailing blank lines
    ?_assertEqual({ok, [
125
                        {title, []}
Shunichi Shinohara's avatar
Shunichi Shinohara committed
126
                       ]},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
127
                  parse(
Shunichi Shinohara's avatar
Shunichi Shinohara committed
128
129
130
131
132
133
134
                    "[title]\n"
                    "\n"
                    "  \n"
                    "\n"
                   )),
    %% Title only, but trailing spaces and trailing blank lines
    ?_assertEqual({ok, [
135
                        {title, []}
Shunichi Shinohara's avatar
Shunichi Shinohara committed
136
                       ]},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
137
                  parse(
Shunichi Shinohara's avatar
Shunichi Shinohara committed
138
139
140
                    "[title]  \n"
                    "\n"
                    "\n"
IUnknown's avatar
IUnknown committed
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
                   )),
    %% blank char in section title
    ?_assertEqual({ok, [
                        {'tit le', []}
                       ]},
                  parse("[tit le]")),
    ?_assertEqual({ok, [
                        {'tit le A', []}
                       ]},
                  parse("[tit le A]")),
    ?_assertEqual({ok, [
                        {'tit le A', []}
                       ]},
                  parse("[  tit le A  ]")),
    ?_assertEqual({ok, [
                        {'tit  le', []}
                       ]},
                  parse("[  tit  le]")),
    ?_assertEqual({ok, [
                        {'tit le', []}
                       ]},
                  parse("[tit le  ]")),
    ?_assertEqual({ok, [
                        {'tit  le', []}
                       ]},
                  parse("[  tit  le  ]")),
    ?_assertEqual({ok, [
                        {'title', []}
                       ]},
                  parse("[  title  ]")),
    ?_assertEqual({ok, [
                        {'title', []}
                       ]},
                  parse("[  title]")),
    ?_assertEqual({ok, [
                        {'title', []}
                       ]},
                  parse("[title  ]"))
Shunichi Shinohara's avatar
Shunichi Shinohara committed
179
180
   ]}.

Shunichi Shinohara's avatar
Shunichi Shinohara committed
181
182
183
184
185
186
one_section_title_only_syntax_error_test_() ->
  {setup,
   fun setup/0,
   fun teardown/1,
   [
    %% No ] char
187
    ?_assertMatch({error, {syntax_error, 1, _Reason}},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
188
                  parse(
Shunichi Shinohara's avatar
Shunichi Shinohara committed
189
190
191
192
                    "[title\n"
                   ))
   ]}.

Shunichi Shinohara's avatar
Shunichi Shinohara committed
193
194
195
196
197
198
one_section_title_and_one_prop_test_() ->
  {setup,
   fun setup/0,
   fun teardown/1,
   [
    %% Simple case
Shunichi Shinohara's avatar
Shunichi Shinohara committed
199
    ?_assertEqual({ok, [
200
                        {title, [{key1, <<"value1">>}]}
Shunichi Shinohara's avatar
Shunichi Shinohara committed
201
                       ]},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
202
                  parse(
Shunichi Shinohara's avatar
Shunichi Shinohara committed
203
204
205
                    "[title]\n"
                    "key1=value1\n"
                   )),
Shunichi Shinohara's avatar
Shunichi Shinohara committed
206
    %% title has trailing spaces
Shunichi Shinohara's avatar
Shunichi Shinohara committed
207
    ?_assertEqual({ok, [
208
                        {title, [{key1, <<"value1">>}]}
Shunichi Shinohara's avatar
Shunichi Shinohara committed
209
                       ]},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
210
                  parse(
Shunichi Shinohara's avatar
Shunichi Shinohara committed
211
                    "[title]  \n"
212
                    "key1=value1\n"
Shunichi Shinohara's avatar
Shunichi Shinohara committed
213
214
215
                   )),
    %% Single blank line between title and a prop
    ?_assertEqual({ok, [
216
                        {title, [{key1, <<"value1">>}]}
Shunichi Shinohara's avatar
Shunichi Shinohara committed
217
                       ]},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
218
                  parse(
Shunichi Shinohara's avatar
Shunichi Shinohara committed
219
220
221
222
                    "[title]  \n"
                    "\n"
                    "key1=value1\n"
                   )),
Shunichi Shinohara's avatar
Shunichi Shinohara committed
223
224
    %% Single comment line between title and a prop
    ?_assertEqual({ok, [
225
                        {title, [{key1, <<"value1">>}]}
Shunichi Shinohara's avatar
Shunichi Shinohara committed
226
                       ]},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
227
                  parse(
Shunichi Shinohara's avatar
Shunichi Shinohara committed
228
229
230
231
232
233
                    "[title]  \n"
                    "; comment\n"
                    "key1=value1\n"
                   )),
    %% Single comment line after a prop
    ?_assertEqual({ok, [
234
                        {title, [{key1, <<"value1">>}]}
Shunichi Shinohara's avatar
Shunichi Shinohara committed
235
                       ]},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
236
                  parse(
Shunichi Shinohara's avatar
Shunichi Shinohara committed
237
238
239
240
                    "[title]  \n"
                    "key1=value1\n"
                    "; comment\n"
                   )),
Shunichi Shinohara's avatar
Shunichi Shinohara committed
241
242
    %% Multi blank lines between title and a prop
    ?_assertEqual({ok, [
243
                        {title, [{key1, <<"value1">>}]}
Shunichi Shinohara's avatar
Shunichi Shinohara committed
244
                       ]},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
245
                  parse(
Shunichi Shinohara's avatar
Shunichi Shinohara committed
246
247
248
249
250
251
252
253
                    "[title]  \n"
                    "\n"
                    "    \n"
                    "\n"
                    "key1=value1\n"
                   )),
    %% Multi blank lines after a prop
    ?_assertEqual({ok, [
254
                        {title,
255
                         [{key1, <<"value1">>}]}
Shunichi Shinohara's avatar
Shunichi Shinohara committed
256
                       ]},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
257
                  parse(
Shunichi Shinohara's avatar
Shunichi Shinohara committed
258
259
260
261
262
263
264
265
266
                    "[title]  \n"
                    "key1=value1\n"
                    "\n"
                    "    \n"
                    "\n"
                   )),
    %% Multi blank lines between title and a prop and
    %% multi blank lines after a prop
    ?_assertEqual({ok, [
267
                        {title, [{key1, <<"value1">>}]}
Shunichi Shinohara's avatar
Shunichi Shinohara committed
268
                       ]},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
269
                  parse(
Shunichi Shinohara's avatar
Shunichi Shinohara committed
270
271
272
273
274
275
276
277
278
279
280
281
                    "[title]  \n"
                    "\n"
                    "    \n"
                    "\n"
                    "key1=value1\n"
                    "\n"
                    "    \n"
                    "\n"
                   )),

    %% value has [ char
    ?_assertEqual({ok, [
282
                        {title, [{key1, <<"va[lue1">>}]}
Shunichi Shinohara's avatar
Shunichi Shinohara committed
283
                       ]},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
284
                  parse(
Shunichi Shinohara's avatar
Shunichi Shinohara committed
285
286
287
288
289
                    "[title]  \n"
                    "key1=va[lue1\n"
                   )),
    %% value has ] char
    ?_assertEqual({ok, [
290
                        {title, [{key1, <<"valu]e1">>}]}
Shunichi Shinohara's avatar
Shunichi Shinohara committed
291
                       ]},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
292
                  parse(
Shunichi Shinohara's avatar
Shunichi Shinohara committed
293
294
295
296
297
                    "[title]  \n"
                    "key1=valu]e1\n"
                   )),
    %% value has [ and ] chars
    ?_assertEqual({ok, [
298
                        {title, [{key1, <<"va[lu]e1">>}]}
Shunichi Shinohara's avatar
Shunichi Shinohara committed
299
                       ]},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
300
                  parse(
Shunichi Shinohara's avatar
Shunichi Shinohara committed
301
302
303
                    "[title]  \n"
                    "key1=va[lu]e1\n"
                   )),
304
305
306
307
308
309
310
311
    %% value has < and > chars
    ?_assertEqual({ok, [
                        {title, [{key1, <<"va<lu>e1">>}]}
                       ]},
                  parse(
                    "[title]  \n"
                    "key1=va<lu>e1\n"
                   )),
Shunichi Shinohara's avatar
Shunichi Shinohara committed
312
313
    %% value has ; char
    ?_assertEqual({ok, [
314
                        {title, [{key1, <<"value1;continue">>}]}
Shunichi Shinohara's avatar
Shunichi Shinohara committed
315
                       ]},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
316
                  parse(
Shunichi Shinohara's avatar
Shunichi Shinohara committed
317
318
                    "[title]  \n"
                    "key1=value1;continue\n"
319
                   )),
320
321
322
323
324
325
326
327
    %% key has preceding spaces
    ?_assertEqual({ok, [
                        {title, [{key1, <<"value1">>}]}
                       ]},
                  parse(
                    "[title]  \n"
                    "  key1=value1\n"
                   )),
328
329
    %% key has trailing spaces
    ?_assertEqual({ok, [
330
                        {title, [{key1, <<"value1">>}]}
331
                       ]},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
332
                  parse(
333
334
335
                    "[title]  \n"
                    "key1   =value1\n"
                   )),
336
337
338
339
340
341
342
343
    %% key has preceding and trailing spaces
    ?_assertEqual({ok, [
                        {title, [{key1, <<"value1">>}]}
                       ]},
                  parse(
                    "[title]  \n"
                    "   key1   =value1\n"
                   )),
344
345
    %% value has preceding and trailing spaces
    ?_assertEqual({ok, [
346
                        {title, [{key1, <<"value1">>}]}
347
                       ]},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
348
                  parse(
349
350
351
352
353
                    "[title]  \n"
                    "key1=  value1  \n"
                   )),
    %% value has characters which can not used in titles or keys
    ?_assertEqual({ok, [
354
                        {title, [{key1, <<"value1$% '""#!+*=@/:+">>}]}
355
                       ]},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
356
                  parse(
357
358
                    "[title]\n"
                    "key1=value1$% '""#!+*=@/:+\n"
Shunichi Shinohara's avatar
Shunichi Shinohara committed
359
360
361
362
363
364
365
366
367
368
                   ))
   ]}.

one_section_title_and_two_props_test_() ->
  {setup,
   fun setup/0,
   fun teardown/1,
   [
    %% Simple case
    ?_assertEqual({ok, [
369
370
371
                        {title,
                         [{key1, <<"value1">>},
                          {key2, <<"value2">>}]}
Shunichi Shinohara's avatar
Shunichi Shinohara committed
372
                       ]},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
373
                  parse(
Shunichi Shinohara's avatar
Shunichi Shinohara committed
374
375
376
377
378
379
                    "[title]\n"
                    "key1=value1\n"
                    "key2=value2\n"
                   )),
    %% Blank lines
    ?_assertEqual({ok, [
380
381
382
                        {title,
                         [{key1, <<"value1">>},
                          {key2, <<"value2">>}]}
Shunichi Shinohara's avatar
Shunichi Shinohara committed
383
                       ]},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
384
                  parse(
Shunichi Shinohara's avatar
Shunichi Shinohara committed
385
386
387
388
389
390
391
392
                    "[title]\n"
                    "\n"
                    "key1=value1\n"
                    "  \n"
                    "\n"
                    "key2=value2\n"
                    "\n"
                    "\n"
393
                   ))
Shunichi Shinohara's avatar
Shunichi Shinohara committed
394
395
   ]}.

Shunichi Shinohara's avatar
Shunichi Shinohara committed
396
397
398
399
400
401
two_section_test_() ->
  {setup,
   fun setup/0,
   fun teardown/1,
   [
    ?_assertEqual({ok, [
402
403
                        {title_A, []},
                        {'title-B', []}
Shunichi Shinohara's avatar
Shunichi Shinohara committed
404
                       ]},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
405
                  parse(
406
407
                    "[title_A]\n"
                    "[title-B]\n"
Shunichi Shinohara's avatar
Shunichi Shinohara committed
408
409
                   )),
    ?_assertEqual({ok, [
410
411
412
413
                        {'Title_A',
                         [{'Key_A1', <<"value_A1">>}]},
                        {'Title-B',
                         [{'Key-B1', <<"value-B1">>}]}
Shunichi Shinohara's avatar
Shunichi Shinohara committed
414
                       ]},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
415
                  parse(
416
417
418
419
                    "[Title_A]\n"
                    "Key_A1=value_A1\n"
                    "[Title-B]  \n"
                    "Key-B1=value-B1\n"
Shunichi Shinohara's avatar
Shunichi Shinohara committed
420
421
422
                   ))
   ]}.

423
424
425
426
427
428
429
430
431
binary_two_section_test_() ->
  {setup,
   fun setup/0,
   fun teardown/1,
   [
    ?_assertEqual({ok, [
                        {titleA, []},
                        {titleB, []}
                       ]},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
432
                  parse(
433
434
435
436
437
438
439
440
441
                    "[titleA]\n"
                    "[titleB]\n"
                   )),
    ?_assertEqual({ok, [
                        {titleA,
                         [{keyA1, <<"valueA1">>}]},
                        {titleB,
                         [{keyB1, <<"valueB1">>}]}
                       ]},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
442
                  parse(
443
444
445
446
447
448
449
                    <<"[titleA]\n"
                      "keyA1=valueA1\n"
                      "[titleB]  \n"
                      "keyB1=valueB1\n">>
                   ))
   ]}.

450
451
452
453
454
lex_error_title_test_() ->
  {setup,
   fun setup/0,
   fun teardown/ 1,
   [
455
    %% vertical tab in section title
456
    ?_assertMatch({error, {syntax_error, 1, _Reason}},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
457
                  parse("[ti\vtle]")),
458
    ?_assertMatch({error, {syntax_error, 3, _Reason}},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
459
                  parse(
460
461
                    "[titleA]\n"
                    "keyA1=valueA1\n"
462
                    "[tit\vleB]  \n"
463
464
465
                    "keyB1=valueB1\n"
                   ))
   ]}.
466

467
syntax_error_title_test_() ->
468
469
  %% TODO: Erlang 17 lost the ability to correctly report line numbers from errors.
  %% Put the numbers back in some day when the fix is released
Shunichi Shinohara's avatar
Shunichi Shinohara committed
470
471
472
473
  {setup,
   fun setup/0,
   fun teardown/1,
   [
474
    %% blank char before section title
475
    ?_assertMatch({error, {syntax_error, _, ["syntax error before: ", _]}},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
476
                  parse("  [title]")),
477
    %% blank char before section title, with a preceding empty line
478
    ?_assertMatch({error, {syntax_error, _, ["syntax error before: ", _]}},
479
480
481
                  parse("\n"
                        "  [title]")),
    %% blank char before section title, with preceding empty lines
482
    ?_assertMatch({error, {syntax_error, _, ["syntax error before: ", _]}},
483
484
485
486
                  parse("\n"
                        "\n"
                        "  [title]")),
    %% blank char before section title, with preceding blank lines
487
    ?_assertMatch({error, {syntax_error, _, ["syntax error before: ", _]}},
488
489
490
491
                  parse("  \n"
                        "\t\n"
                        "  [title]")),
    %% blank char before section title, with preceding comment lines
492
    ?_assertMatch({error, {syntax_error, _, ["syntax error before: ", _]}},
493
494
495
496
                  parse("; comment 1\n"
                        ";\n"
                        "; comment 2\n"
                        "  [title]")),
497
    %% comment after title
498
    ?_assertMatch({error, {syntax_error, _, ["syntax error before: ", _]}},
499
500
                  parse("[title] ;comment")),
    %% comment after blank
501
    ?_assertMatch({error, {syntax_error, _, ["syntax error before: ", _]}},
502
503
                  parse("[title]\n"
                        " ;comment\n"))
Shunichi Shinohara's avatar
Shunichi Shinohara committed
504
   ]}.
505

506
507
508
509
510
511
512
513
514
515
syntax_error_property_test_() ->
  {setup,
   fun setup/0,
   fun teardown/1,
   [
    %% blank char in key
    ?_assertMatch({error, {syntax_error, 2, _Reason}},
                  parse(
                    "[title]\n"
                    "key with blank=value\n"
516
517
518
519
520
                   )),
    %% comment after blank
    ?_assertMatch({error, {syntax_error, 2, ["syntax error before: ", _]}},
                  parse("[title]\n"
                        "key;comment=value\n"))
521
   ]}.
522

Shunichi Shinohara's avatar
Shunichi Shinohara committed
523
524
525
526
527
dup_title_test_() ->
  {setup,
   fun setup/0,
   fun teardown/ 1,
   [
528
    ?_assertEqual({error, {duplicate_title, titleA}},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
529
                  parse(
Shunichi Shinohara's avatar
Shunichi Shinohara committed
530
531
532
533
534
535
                    "[titleA]\n"
                    "keyA1=valueA1\n"
                    "[titleA]  \n"
                    "keyB1=valueB1\n"
                   ))
   ]}.
536

Shunichi Shinohara's avatar
Shunichi Shinohara committed
537
538
539
540
541
dup_key_test_() ->
  {setup,
   fun setup/0,
   fun teardown/ 1,
   [
542
    ?_assertEqual({error, {duplicate_key, titleB, key2}},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
543
                  parse(
Shunichi Shinohara's avatar
Shunichi Shinohara committed
544
545
546
547
548
549
550
551
                    "[titleA]\n"
                    "key1=value1\n"
                    "[titleB]  \n"
                    "key1=value1\n"
                    "key2=value2\n"
                    "key3=value3\n"
                    "key2=value4\n"
                   )),
552
    ?_assertEqual({error, {duplicate_key, titleB, key2}},
Shunichi Shinohara's avatar
Shunichi Shinohara committed
553
                  parse(
Shunichi Shinohara's avatar
Shunichi Shinohara committed
554
555
556
557
558
559
560
561
562
                    "[titleA]\n"
                    "key1=value1\n"
                    "[titleB]  \n"
                    "key1=value1\n"
                    "key2=value2\n"
                    "key3=value3\n"
                    "key2  =  value4\n"
                   ))
   ]}.
Ryosuke Nakai's avatar
Ryosuke Nakai committed
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587

register_test_() ->
  {foreach,
    fun() ->
      application:start(eini)
    end,
    fun(_) ->
      application:stop(eini)
    end,
    [
      {"syntax Error",
        ?_assertMatch({error, {syntax_error, 1, _Reason}},
                       eini:register("spam.ini" ,"[title\n"))},
      {"",
        fun() ->
          ?assertEqual(ok,
                       eini:register("spam.ini", "[title]\nkey=value")),
          ?assertEqual(<<"value">>,
                       eini:lookup_value("spam.ini", title, key)),
          ?assertEqual(not_found,
                       eini:lookup_value("spam.ini", title, key1)),
          ?assertEqual(ok,
                       eini:register("spam.ini", title, key1, <<"value">>)),
          ?assertEqual(<<"value">>,
                       eini:lookup_value("spam.ini", title, key1)),
Ryosuke Nakai's avatar
Ryosuke Nakai committed
588
          ?assertEqual({error, {duplicate_key, title, key1}},
Ryosuke Nakai's avatar
Ryosuke Nakai committed
589
                       eini:register("spam.ini", title, key1, <<"value">>)),
Ryosuke Nakai's avatar
Ryosuke Nakai committed
590
          ?assertEqual({error, {duplicate_key, title, key}},
Ryosuke Nakai's avatar
Ryosuke Nakai committed
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
                       eini:register("spam.ini", "[title]\nkey=value"))
        end}
    ]
  }.

is_section_test_() ->
  {foreach,
    fun() ->
      application:start(eini)
    end,
    fun(_) ->
      application:stop(eini)
    end,
    [
      {"",
        fun() ->
          ?assertEqual(ok,
                       eini:register("spam.ini", "[title]\nkey=value")),
          ?assertEqual(true,
                       eini:is_section("spam.ini", title)),
          ?assertEqual(false,
                       eini:is_section("spam.ini", title1))
        end}
    ]
  }.
616
617
618
619
620
621
622

unicode_test_() ->
  {setup,
   fun setup/0,
   fun teardown/1,
   [
    ?_assertEqual({ok, [
623
                        {title, [{key1, <<"\xD1\x8E\xD0\xBD\xD0\xB8\xD0\xBA\xD0\xBE\xD0\xB4">>}]}
624
625
626
627
628
629
                       ]},
                  parse(
                    "[title]\n"
                    "key1=  \xD1\x8E\xD0\xBD\xD0\xB8\xD0\xBA\xD0\xBE\xD0\xB4  \n"
                   )),
     ?_assertEqual({ok, [
630
                        {title, [{key1, <<"\xD1\x8E\xD0\xBD\xD0\xB8\xD0\xBA\xD0\xBE\xD0\xB4">>}]}
631
                       ]},
632
633
                   parse(
                     <<91,116,105,116,108,101,93,10,107,101,121,49,61,209,142,208,189,208,184,208,186,208,190,208,180,10>>
634
635
                   ))
   ]}.