module S = Colitur_kernel.Slug module L = Colitur_kernel.Lang let ok_slug s = match S.of_string s with | Ok t -> t | Error e -> Alcotest.failf "slug %S: %s" s e let test_slug_accepts () = Alcotest.(check string) "plain" "ef-advent-sunday-1" (S.to_string (ok_slug "ef-advent-sunday-1")); Alcotest.(check string) "digits" "ef-lent-3-monday" (S.to_string (ok_slug "ef-lent-3-monday")) let test_slug_rejects () = List.iter (fun bad -> Alcotest.(check bool) (Printf.sprintf "reject %S" bad) true (Result.is_error (S.of_string bad))) [ ""; "Ef-Advent"; "ef advent"; "ef_advent"; "-leading"; "trailing-"; "ef/advent" ] let test_slug_sexp () = let s = ok_slug "ef-easter-sunday" in Alcotest.(check bool) "roundtrip" true (S.equal (S.t_of_sexp (S.sexp_of_t s)) s); (* A malformed slug in a data file must be rejected, not silently accepted. *) Alcotest.check_raises "bad slug in sexp" (Sexplib0.Sexp_conv_error.Of_sexp_error (Failure "slug \"Bad Slug\": only [a-z0-9-] allowed", Sexplib0.Sexp.Atom "Bad Slug")) (fun () -> ignore (S.t_of_sexp (Sexplib0.Sexp.Atom "Bad Slug"))) let test_lang () = Alcotest.(check string) "la" "la" (L.to_string (match L.of_string "la" with | Ok t -> t | Error e -> Alcotest.failf "%s" e)); List.iter (fun bad -> Alcotest.(check bool) (Printf.sprintf "reject %S" bad) true (Result.is_error (L.of_string bad))) [ ""; "e"; "eng"; "EN"; "e1" ] let test_lang_sexp () = let l = match L.of_string "la" with Ok t -> t | Error e -> Alcotest.failf "%s" e in Alcotest.(check bool) "roundtrip" true (L.equal (L.t_of_sexp (L.sexp_of_t l)) l); (* A malformed lang code in a data file must be rejected, not silently accepted. "e1" is shaped like a lang code (2 chars) but fails the lowercase-a-z rule, so this pins the actual validation, not just the sexp shape. *) Alcotest.check_raises "bad lang in sexp" (Sexplib0.Sexp_conv_error.Of_sexp_error (Failure "lang \"e1\": must be lowercase a-z", Sexplib0.Sexp.Atom "e1")) (fun () -> ignore (L.t_of_sexp (Sexplib0.Sexp.Atom "e1"))) let suite = ( "Slug/Lang", [ Alcotest.test_case "slug accepts" `Quick test_slug_accepts; Alcotest.test_case "slug rejects" `Quick test_slug_rejects; Alcotest.test_case "slug sexp validates" `Quick test_slug_sexp; Alcotest.test_case "lang" `Quick test_lang; Alcotest.test_case "lang sexp validates" `Quick test_lang_sexp ] )