summaryrefslogtreecommitdiff
path: root/test/test_lectionary.ml
blob: 45a01ab14d841add8b0328869e102b63a92bd185 (plain) (blame)
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
open Colitur_kernel

let slug = Slug.of_string_exn
let cit part reference = { Citation.part; reference }

let entry s cs = (slug s, cs)

let ok_exn = function Ok x -> x | Error e -> Alcotest.fail e

(* Stdlib-only substring search -- Astring is not among the frozen deps. *)
let contains_substring ~needle haystack =
  let hn = String.length needle and hh = String.length haystack in
  let rec go i = i + hn <= hh && (String.sub haystack i hn = needle || go (i + 1)) in
  hn = 0 || go 0

let test_find_present () =
  let l =
    ok_exn
      (Lectionary.of_entries
         [ entry "ef-advent-sunday-1"
             [ cit Citation.First "Rom 13:11-14"; cit Citation.Gospel "Luke 21:25-33" ] ])
  in
  match Lectionary.find l (slug "ef-advent-sunday-1") with
  | Some [ a; b ] ->
      Alcotest.(check string) "epistle" "Rom 13:11-14" a.Citation.reference;
      Alcotest.(check string) "gospel" "Luke 21:25-33" b.Citation.reference
  | Some _ -> Alcotest.fail "expected exactly two citations"
  | None -> Alcotest.fail "slug not found"

let test_find_absent () =
  let l = ok_exn (Lectionary.of_entries []) in
  Alcotest.(check bool) "absent" true
    (Lectionary.find l (slug "ef-advent-sunday-1") = None)

let test_duplicate_slug_rejected () =
  let e = entry "ef-advent-sunday-1" [ cit Citation.First "Rom 13:11-14" ] in
  match Lectionary.of_entries [ e; e ] with
  | Ok _ -> Alcotest.fail "duplicate slug must be rejected"
  | Error msg ->
      (* Strengthened beyond the brief's "non-empty and contains 'e'" check:
         the interface doc promises the error names the slug, so assert the
         offending slug string actually appears in the message. *)
      Alcotest.(check bool) "names the slug" true
        (contains_substring ~needle:"ef-advent-sunday-1" msg)

let test_sexp_round_trip () =
  let l =
    ok_exn
      (Lectionary.of_entries
         [ entry "ef-lent-1-monday"
             [ cit Citation.First "Ezech 34:11-16"; cit Citation.Gospel "Matt 25:31-46" ] ])
  in
  let l' = Lectionary.t_of_sexp (Lectionary.sexp_of_t l) in
  Alcotest.(check bool) "round trips" true (l = l')

let suite =
  [ ("find present", `Quick, test_find_present);
    ("find absent", `Quick, test_find_absent);
    ("duplicate slug rejected", `Quick, test_duplicate_slug_rejected);
    ("sexp round-trip", `Quick, test_sexp_round_trip) ]