(*--------------------------------------------------------------------------- Copyright (c) 2026 The ocaml-cff programmers. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*) (* Test the CFF Eio backend *) let minimal_cff = {| cff-version: 1.2.0 message: If you use this software, please cite it title: Test Software authors: - family-names: Smith given-names: Jane |} ;; let simple_cff = {| cff-version: 1.2.0 message: Please cite this software using these metadata. title: My Research Software authors: - family-names: Druskat given-names: Stephan orcid: https://orcid.org/0000-0003-4925-7248 version: 1.0.0 doi: 10.5281/zenodo.1234567 date-released: 2021-08-11 |} ;; let test_parse_string () = let cff = Cff_eio.of_yaml_string minimal_cff in Alcotest.(check string) "cff-version" "1.2.0" (Cff.cff_version cff); Alcotest.(check string) "title" "Test Software" (Cff.title cff); Alcotest.(check int) "authors count" 1 (List.length (Cff.authors cff)) ;; let test_roundtrip_string () = let cff1 = Cff_eio.of_yaml_string simple_cff in let yaml = Cff_eio.to_yaml_string cff1 in let cff2 = Cff_eio.of_yaml_string yaml in Alcotest.(check string) "title preserved" (Cff.title cff1) (Cff.title cff2); Alcotest.(check string) "cff-version preserved" (Cff.cff_version cff1) (Cff.cff_version cff2) ;; let test_parse_error () = let invalid_yaml = {| cff-version: 1.2.0 title: Missing authors field |} in match Cff_eio.of_yaml_string invalid_yaml with | _ -> Alcotest.fail "Expected parse error" | exception Eio.Exn.Io (Cff_eio.E _, _) -> () | exception ex -> Alcotest.fail (Printf.sprintf "Wrong exception type: %s" (Printexc.to_string ex)) ;; let test_file_read env = let fs = Eio.Stdenv.fs env in let cff = Cff_eio.of_file ~fs "../vendor/git/citation-file-format/examples/1.2.0/pass/minimal/CITATION.cff" in Alcotest.(check string) "cff-version" "1.2.0" (Cff.cff_version cff); Alcotest.(check bool) "has title" true (String.length (Cff.title cff) > 0); Alcotest.(check bool) "has authors" true (List.length (Cff.authors cff) > 0) ;; let test_file_not_found env = let fs = Eio.Stdenv.fs env in match Cff_eio.of_file ~fs "nonexistent_file.cff" with | _ -> Alcotest.fail "Expected file not found error" | exception Eio.Exn.Io _ -> () | exception ex -> Alcotest.fail (Printf.sprintf "Wrong exception type: %s" (Printexc.to_string ex)) ;; let test_file_roundtrip env = let fs = Eio.Stdenv.fs env in let cff1 = Cff_eio.of_file ~fs "../vendor/git/citation-file-format/examples/1.2.0/pass/simple/CITATION.cff" in let tmp_path = "_build/test_roundtrip.cff" in Cff_eio.to_file ~fs tmp_path cff1; let cff2 = Cff_eio.of_file ~fs tmp_path in Alcotest.(check string) "title preserved" (Cff.title cff1) (Cff.title cff2); Alcotest.(check string) "cff-version preserved" (Cff.cff_version cff1) (Cff.cff_version cff2) ;; let () = Eio_main.run @@ fun env -> Alcotest.run "CFF Eio" [ ( "string parsing" , [ Alcotest.test_case "parse string" `Quick test_parse_string ; Alcotest.test_case "roundtrip string" `Quick test_roundtrip_string ; Alcotest.test_case "parse error" `Quick test_parse_error ] ) ; ( "file operations" , [ Alcotest.test_case "read file" `Quick (fun () -> test_file_read env) ; Alcotest.test_case "file not found" `Quick (fun () -> test_file_not_found env) ; Alcotest.test_case "file roundtrip" `Quick (fun () -> test_file_roundtrip env) ] ) ] ;;