Author: Kevin Schoon [kevinschoon@gmail.com]
Hash: 8f51796583d5400d1325c77df95f314876533010
Timestamp: Tue, 22 Jun 2021 18:20:01 +0000 (3 years ago)

+31 -3 +/-2 browse
add helper funcs for manifest
1diff --git a/lib/manifest.ml b/lib/manifest.ml
2index a4a6253..284dd69 100644
3--- a/lib/manifest.ml
4+++ b/lib/manifest.ml
5 @@ -21,7 +21,11 @@ module Item = struct
6 let tags = Ezjsonm.find json [ "tags" ] |> Ezjsonm.get_strings in
7 let parent =
8 match Ezjsonm.find_opt json [ "parent" ] with
9- | Some parent -> Some (parent |> Ezjsonm.get_string)
10+ | Some parent -> (
11+ match parent with
12+ | `Null -> None
13+ | `String name -> Some name
14+ | _ -> failwith "parent should be null or a string")
15 | None -> None
16 in
17 { slug; parent; title; description; tags }
18 @@ -155,3 +159,9 @@ let remove ~path manifest =
19 in
20 { items }
21 | None -> failwith "not found"
22+
23+ let slugs manifest = manifest.items |> List.map ~f:(fun item -> item.slug)
24+
25+ let tags manifest =
26+ manifest.items
27+ |> List.fold ~init:[] ~f:(fun accm item -> List.concat [ accm; item.tags ])
28 diff --git a/test/manifest_test.ml b/test/manifest_test.ml
29index 5126c3c..1c69b8a 100644
30--- a/test/manifest_test.ml
31+++ b/test/manifest_test.ml
32 @@ -1,6 +1,20 @@
33 open Core
34 open Note_lib
35
36+ let test_recurse () =
37+ let manifest =
38+ Manifest.empty
39+ |> Manifest.insert ~path:"/" ~slug:"note-00000000-0" ~title:"a"
40+ ~description:"" ~tags:[]
41+ |> Manifest.insert ~path:"/a" ~slug:"note-00000000-1" ~title:"b"
42+ ~description:"" ~tags:[]
43+ |> Manifest.insert ~path:"/a/b" ~slug:"note-00000000-2" ~title:"c"
44+ ~description:"" ~tags:[]
45+ |> Manifest.insert ~path:"/a/b/c" ~slug:"note-00000000-3" ~title:"d"
46+ ~description:"" ~tags:[]
47+ in
48+ Alcotest.(check int) "n_results" 4 (List.length manifest.items)
49+
50 let test_manifest () =
51 let temp_db = Filename.temp_file "note-test" "" in
52 Manifest.empty |> Manifest.save ~path:temp_db;
53 @@ -17,7 +31,8 @@ let test_manifest () =
54 ~description:"" ~tags:[]
55 in
56 let result = manifest |> Manifest.find ~path:"/fuu/bar" in
57- Alcotest.(check bool) "manifest loaded" (result |> Option.is_some) true;
58+ Alcotest.(check bool)
59+ "manifest /fuu/bar inserted" (result |> Option.is_some) true;
60 let result_path = Option.value_exn result |> Manifest.to_path ~manifest in
61 Alcotest.(check string) "result path" "/fuu/bar" result_path;
62 let manifest =
63 @@ -42,4 +57,7 @@ let test_manifest () =
64
65 let () =
66 Alcotest.run "Config"
67- [ ("load", [ Alcotest.test_case "test manifest" `Quick test_manifest ]) ]
68+ [
69+ ("recurse", [ Alcotest.test_case "test recurse" `Quick test_recurse ]);
70+ ("load", [ Alcotest.test_case "test manifest" `Quick test_manifest ]);
71+ ]