RFC6901 JSON Pointer implementation in OCaml using jsont
at 2ff17e7b6bec4e8da66dba516015424902f78eba 205 lines 7.2 kB view raw
1(* Test runner for jsont_pointer *) 2 3let read_file path = 4 let ic = open_in path in 5 let n = in_channel_length ic in 6 let s = really_input_string ic n in 7 close_in ic; 8 s 9 10let parse_json s = 11 match Jsont_bytesrw.decode_string Jsont.json s with 12 | Ok json -> json 13 | Error e -> failwith e 14 15let json_to_string json = 16 match Jsont_bytesrw.encode_string Jsont.json json with 17 | Ok s -> s 18 | Error e -> failwith e 19 20(* Test: parse pointer and print indices *) 21let test_parse pointer_str = 22 try 23 let p = Jsont_pointer.of_string pointer_str in 24 let indices = Jsont_pointer.indices p in 25 let index_strs = List.map (fun idx -> 26 match idx with 27 | Jsont_pointer.Index.Mem s -> Printf.sprintf "Mem:%s" s 28 | Jsont_pointer.Index.Nth n -> Printf.sprintf "Nth:%d" n 29 | Jsont_pointer.Index.End -> "End" 30 ) indices in 31 Printf.printf "OK: [%s]\n" (String.concat ", " index_strs) 32 with Jsont.Error e -> 33 Printf.printf "ERROR: %s\n" (Jsont.Error.to_string e) 34 35(* Test: roundtrip pointer string *) 36let test_roundtrip pointer_str = 37 try 38 let p = Jsont_pointer.of_string pointer_str in 39 let s = Jsont_pointer.to_string p in 40 if s = pointer_str then 41 Printf.printf "OK: %s\n" s 42 else 43 Printf.printf "MISMATCH: input=%s output=%s\n" pointer_str s 44 with Jsont.Error e -> 45 Printf.printf "ERROR: %s\n" (Jsont.Error.to_string e) 46 47(* Test: evaluate pointer against JSON *) 48let test_eval json_path pointer_str = 49 try 50 let json = parse_json (read_file json_path) in 51 let p = Jsont_pointer.of_string pointer_str in 52 let result = Jsont_pointer.get p json in 53 Printf.printf "OK: %s\n" (json_to_string result) 54 with 55 | Jsont.Error e -> 56 Printf.printf "ERROR: %s\n" (Jsont.Error.to_string e) 57 | Failure e -> 58 Printf.printf "FAIL: %s\n" e 59 60(* Test: escape token *) 61let test_escape token = 62 let escaped = Jsont_pointer.Token.escape token in 63 Printf.printf "%s\n" escaped 64 65(* Test: unescape token *) 66let test_unescape token = 67 try 68 let unescaped = Jsont_pointer.Token.unescape token in 69 Printf.printf "OK: %s\n" unescaped 70 with Jsont.Error e -> 71 Printf.printf "ERROR: %s\n" (Jsont.Error.to_string e) 72 73(* Test: URI fragment roundtrip *) 74let test_uri_fragment pointer_str = 75 try 76 let p = Jsont_pointer.of_string pointer_str in 77 let frag = Jsont_pointer.to_uri_fragment p in 78 let p2 = Jsont_pointer.of_uri_fragment frag in 79 let s2 = Jsont_pointer.to_string p2 in 80 if s2 = pointer_str then 81 Printf.printf "OK: %s -> %s\n" pointer_str frag 82 else 83 Printf.printf "MISMATCH: %s -> %s -> %s\n" pointer_str frag s2 84 with Jsont.Error e -> 85 Printf.printf "ERROR: %s\n" (Jsont.Error.to_string e) 86 87(* Test: add operation *) 88let test_add json_str pointer_str value_str = 89 try 90 let json = parse_json json_str in 91 let p = Jsont_pointer.of_string pointer_str in 92 let value = parse_json value_str in 93 let result = Jsont_pointer.add p json ~value in 94 Printf.printf "%s\n" (json_to_string result) 95 with Jsont.Error e -> 96 Printf.printf "ERROR: %s\n" (Jsont.Error.to_string e) 97 98(* Test: remove operation *) 99let test_remove json_str pointer_str = 100 try 101 let json = parse_json json_str in 102 let p = Jsont_pointer.of_string pointer_str in 103 let result = Jsont_pointer.remove p json in 104 Printf.printf "%s\n" (json_to_string result) 105 with Jsont.Error e -> 106 Printf.printf "ERROR: %s\n" (Jsont.Error.to_string e) 107 108(* Test: replace operation *) 109let test_replace json_str pointer_str value_str = 110 try 111 let json = parse_json json_str in 112 let p = Jsont_pointer.of_string pointer_str in 113 let value = parse_json value_str in 114 let result = Jsont_pointer.replace p json ~value in 115 Printf.printf "%s\n" (json_to_string result) 116 with Jsont.Error e -> 117 Printf.printf "ERROR: %s\n" (Jsont.Error.to_string e) 118 119(* Test: move operation *) 120let test_move json_str from_str path_str = 121 try 122 let json = parse_json json_str in 123 let from = Jsont_pointer.of_string from_str in 124 let path = Jsont_pointer.of_string path_str in 125 let result = Jsont_pointer.move ~from ~path json in 126 Printf.printf "%s\n" (json_to_string result) 127 with Jsont.Error e -> 128 Printf.printf "ERROR: %s\n" (Jsont.Error.to_string e) 129 130(* Test: copy operation *) 131let test_copy json_str from_str path_str = 132 try 133 let json = parse_json json_str in 134 let from = Jsont_pointer.of_string from_str in 135 let path = Jsont_pointer.of_string path_str in 136 let result = Jsont_pointer.copy ~from ~path json in 137 Printf.printf "%s\n" (json_to_string result) 138 with Jsont.Error e -> 139 Printf.printf "ERROR: %s\n" (Jsont.Error.to_string e) 140 141(* Test: test operation *) 142let test_test json_str pointer_str expected_str = 143 try 144 let json = parse_json json_str in 145 let p = Jsont_pointer.of_string pointer_str in 146 let expected = parse_json expected_str in 147 let result = Jsont_pointer.test p json ~expected in 148 Printf.printf "%b\n" result 149 with Jsont.Error e -> 150 Printf.printf "ERROR: %s\n" (Jsont.Error.to_string e) 151 152(* Test: has operation (checks if pointer exists) *) 153let test_has json_str pointer_str = 154 try 155 let json = parse_json json_str in 156 let p = Jsont_pointer.of_string pointer_str in 157 let result = Jsont_pointer.find p json in 158 Printf.printf "%b\n" (Option.is_some result) 159 with Jsont.Error e -> 160 Printf.printf "ERROR: %s\n" (Jsont.Error.to_string e) 161 162let () = 163 match Array.to_list Sys.argv with 164 | _ :: "parse" :: pointer :: _ -> 165 test_parse pointer 166 | _ :: "roundtrip" :: pointer :: _ -> 167 test_roundtrip pointer 168 | _ :: "eval" :: json_path :: pointer :: _ -> 169 test_eval json_path pointer 170 | _ :: "escape" :: token :: _ -> 171 test_escape token 172 | _ :: "unescape" :: token :: _ -> 173 test_unescape token 174 | _ :: "uri-fragment" :: pointer :: _ -> 175 test_uri_fragment pointer 176 | _ :: "add" :: json :: pointer :: value :: _ -> 177 test_add json pointer value 178 | _ :: "remove" :: json :: pointer :: _ -> 179 test_remove json pointer 180 | _ :: "replace" :: json :: pointer :: value :: _ -> 181 test_replace json pointer value 182 | _ :: "move" :: json :: from :: path :: _ -> 183 test_move json from path 184 | _ :: "copy" :: json :: from :: path :: _ -> 185 test_copy json from path 186 | _ :: "test" :: json :: pointer :: expected :: _ -> 187 test_test json pointer expected 188 | _ :: "has" :: json :: pointer :: _ -> 189 test_has json pointer 190 | _ -> 191 Printf.printf "Usage:\n"; 192 Printf.printf " test_pointer parse <pointer>\n"; 193 Printf.printf " test_pointer roundtrip <pointer>\n"; 194 Printf.printf " test_pointer eval <json-file> <pointer>\n"; 195 Printf.printf " test_pointer escape <token>\n"; 196 Printf.printf " test_pointer unescape <token>\n"; 197 Printf.printf " test_pointer uri-fragment <pointer>\n"; 198 Printf.printf " test_pointer add <json> <pointer> <value>\n"; 199 Printf.printf " test_pointer remove <json> <pointer>\n"; 200 Printf.printf " test_pointer replace <json> <pointer> <value>\n"; 201 Printf.printf " test_pointer move <json> <from> <path>\n"; 202 Printf.printf " test_pointer copy <json> <from> <path>\n"; 203 Printf.printf " test_pointer test <json> <pointer> <expected>\n"; 204 Printf.printf " test_pointer has <json> <pointer>\n"; 205 exit 1