|
| 1 | +(* YOCaml a static blog generator. |
| 2 | + Copyright (C) 2025 The Funkyworkers and The YOCaml's developers |
| 3 | +
|
| 4 | + This program is free software: you can redistribute it and/or modify |
| 5 | + it under the terms of the GNU General Public License as published by |
| 6 | + the Free Software Foundation, either version 3 of the License, or |
| 7 | + (at your option) any later version. |
| 8 | +
|
| 9 | + This program is distributed in the hope that it will be useful, |
| 10 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + GNU General Public License for more details. |
| 13 | +
|
| 14 | + You should have received a copy of the GNU General Public License |
| 15 | + along with this program. If not, see <https://www.gnu.org/licenses/>. *) |
| 16 | + |
| 17 | +type t = { |
| 18 | + page : Yocaml.Archetype.Page.t |
| 19 | + ; category : string option |
| 20 | + ; authors : string list |
| 21 | +} |
| 22 | + |
| 23 | +let normalize_t { page; category; authors } = |
| 24 | + let open Yocaml.Data in |
| 25 | + record |
| 26 | + (Yocaml.Archetype.Page.normalize page |
| 27 | + @ [ |
| 28 | + ("category", option string category) |
| 29 | + ; ("authors", (list_of string) authors) |
| 30 | + ]) |
| 31 | + |
| 32 | +let testable_t = |
| 33 | + let pp ppf x = Format.fprintf ppf "%a" Yocaml.Data.pp (normalize_t x) |
| 34 | + and equal a b = Yocaml.Data.equal (normalize_t a) (normalize_t b) in |
| 35 | + Alcotest.testable pp equal |
| 36 | + |
| 37 | +let validate_t = |
| 38 | + let open Yocaml.Data.Validation in |
| 39 | + record (fun fields -> |
| 40 | + let+ page = sub_record fields Yocaml.Archetype.Page.validate |
| 41 | + and+ category = optional fields "category" string |
| 42 | + and+ authors = |
| 43 | + optional_or ~default:[] fields "authors" (list_of string) |
| 44 | + in |
| 45 | + { page; category; authors }) |
| 46 | + |
| 47 | +let test_validate_with_subpage_1 = |
| 48 | + let open Alcotest in |
| 49 | + test_case "Validate using a page inside a different value - 1" `Quick |
| 50 | + (fun () -> |
| 51 | + let input = |
| 52 | + let open Yocaml.Data in |
| 53 | + record [] |
| 54 | + and input_page = |
| 55 | + let open Yocaml.Data in |
| 56 | + record [] |
| 57 | + and category = None |
| 58 | + and authors = [] in |
| 59 | + let expected = |
| 60 | + Result.map |
| 61 | + (fun page -> { page; category; authors }) |
| 62 | + (Yocaml.Archetype.Page.validate input_page) |
| 63 | + and computed = validate_t input in |
| 64 | + check |
| 65 | + Test_lib.Testable.(validated_value testable_t) |
| 66 | + "shoudl be equal" expected computed) |
| 67 | + |
| 68 | +let test_validate_with_subpage_2 = |
| 69 | + let open Alcotest in |
| 70 | + test_case "Validate using a page inside a different value - 2" `Quick |
| 71 | + (fun () -> |
| 72 | + let input = |
| 73 | + let open Yocaml.Data in |
| 74 | + record [ ("page_title", string "foo") ] |
| 75 | + and input_page = |
| 76 | + let open Yocaml.Data in |
| 77 | + record [ ("page_title", string "foo") ] |
| 78 | + and category = None |
| 79 | + and authors = [] in |
| 80 | + let expected = |
| 81 | + Result.map |
| 82 | + (fun page -> { page; category; authors }) |
| 83 | + (Yocaml.Archetype.Page.validate input_page) |
| 84 | + and computed = validate_t input in |
| 85 | + check |
| 86 | + Test_lib.Testable.(validated_value testable_t) |
| 87 | + "shoudl be equal" expected computed) |
| 88 | + |
| 89 | +let test_validate_with_subpage_3 = |
| 90 | + let open Alcotest in |
| 91 | + test_case "Validate using a page inside a different value - 3" `Quick |
| 92 | + (fun () -> |
| 93 | + let input = |
| 94 | + let open Yocaml.Data in |
| 95 | + record |
| 96 | + [ |
| 97 | + ("page_title", string "foo") |
| 98 | + ; ("category", string "article") |
| 99 | + ; ("authors", list_of string [ "xvw"; "xhtmlboi"; "msp"; "grm" ]) |
| 100 | + ] |
| 101 | + and input_page = |
| 102 | + let open Yocaml.Data in |
| 103 | + record [ ("page_title", string "foo") ] |
| 104 | + and category = Some "article" |
| 105 | + and authors = [ "xvw"; "xhtmlboi"; "msp"; "grm" ] in |
| 106 | + let expected = |
| 107 | + Result.map |
| 108 | + (fun page -> { page; category; authors }) |
| 109 | + (Yocaml.Archetype.Page.validate input_page) |
| 110 | + and computed = validate_t input in |
| 111 | + check |
| 112 | + Test_lib.Testable.(validated_value testable_t) |
| 113 | + "shoudl be equal" expected computed) |
| 114 | + |
| 115 | +let cases = |
| 116 | + ( "Yocaml.Archetype.Path" |
| 117 | + , [ |
| 118 | + test_validate_with_subpage_1 |
| 119 | + ; test_validate_with_subpage_2 |
| 120 | + ; test_validate_with_subpage_3 |
| 121 | + ] ) |
0 commit comments