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
|
module C = Colitur_naming.Config
let ok = function Ok x -> x | Error e -> Alcotest.failf "parse: %s" e
let sample =
"[defaults]\n\
lang = pl\n\
overlay = ~/a.ini\n\
overlay = ~/b.ini\n\
template = ~/my-ordo.tex\n\
format = json\n"
let test_reads_defaults () =
let c = ok (C.of_string sample) in
Alcotest.(check (option string)) "lang" (Some "pl") (C.lang c);
Alcotest.(check (option string)) "template" (Some "~/my-ordo.tex") (C.template c);
Alcotest.(check (option string)) "format" (Some "json") (C.format c)
(* Repeated keys accumulate for overlay -- a user has more than one. The INI
reader keeps every line, so this asserts we do not silently take the last. *)
let test_overlays_accumulate () =
let c = ok (C.of_string sample) in
Alcotest.(check (list string)) "both overlays" [ "~/a.ini"; "~/b.ini" ] (C.overlays c)
let test_empty_config_is_all_none () =
Alcotest.(check (option string)) "lang" None (C.lang C.empty);
Alcotest.(check (list string)) "overlays" [] (C.overlays C.empty)
(* THE precedence rule: flag > config > default, and the SOURCE is reported,
because a setting that silently comes from a file the user forgot about is
worse than no setting at all. *)
let test_precedence_and_provenance () =
let check ~flag ~config ~default (ev, es) =
let v, s = C.resolve ~flag ~config ~default in
Alcotest.(check string) "value" ev v;
Alcotest.(check string) "source" es s
in
check ~flag:(Some "en") ~config:(Some "pl") ~default:"la" ("en", "flag");
check ~flag:None ~config:(Some "pl") ~default:"la" ("pl", "config");
check ~flag:None ~config:None ~default:"la" ("la", "default")
let test_malformed_is_error_not_crash () =
match C.of_string "[defaults\nbroken" with
| Error _ -> ()
| Ok _ -> Alcotest.fail "a malformed config must be an Error, never accepted"
(* An unknown key is a WARNING case, not a hard error: a config written for a
newer colitur must still work on an older one. But it must be reportable, so
it is not silently dropped either -- assert it is actually COLLECTED, not
only that parsing survives it: a no-op accumulator would also pass a test
that checked survival alone. *)
let test_unknown_key_is_reported_not_fatal () =
match C.of_string "[defaults]\nlang = la\nnonsense = 1\n" with
| Error _ -> Alcotest.fail "an unknown key must not be fatal"
| Ok c ->
Alcotest.(check (option string)) "known key still read" (Some "la") (C.lang c);
Alcotest.(check (list string)) "unknown key reported" [ "nonsense" ] (C.unknown_keys c)
(* The highest-value case: a misspelled SECTION name (not merely a misspelled
key inside a recognised one). Before this fix the whole section, lang
included, vanished with nothing reported -- exactly the typo this feature
exists to surface, in its worst form: a user whose config silently does
nothing has no way to discover why. *)
let test_misspelled_section_is_reported_not_silently_dropped () =
let c = ok (C.of_string "[deafults]\nlang = pl\n") in
Alcotest.(check (option string)) "lang not read from the wrong section" None (C.lang c);
Alcotest.(check (list string)) "section reported" [ "deafults" ] (C.unknown_sections c)
let suite =
( "Config",
[ Alcotest.test_case "reads defaults" `Quick test_reads_defaults;
Alcotest.test_case "overlays accumulate" `Quick test_overlays_accumulate;
Alcotest.test_case "empty is all none" `Quick test_empty_config_is_all_none;
Alcotest.test_case "precedence and provenance" `Quick test_precedence_and_provenance;
Alcotest.test_case "malformed is error" `Quick test_malformed_is_error_not_crash;
Alcotest.test_case "unknown key not fatal" `Quick test_unknown_key_is_reported_not_fatal;
Alcotest.test_case "misspelled section reported"
`Quick test_misspelled_section_is_reported_not_silently_dropped ] )
|