first commit
This commit is contained in:
commit
e1c6aeeeed
42 changed files with 1305 additions and 0 deletions
115
test/unit/main.ml
Normal file
115
test/unit/main.ml
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
open Scfg
|
||||
|
||||
(** Testing other functions in Parse module. *)
|
||||
let () =
|
||||
let chan = open_in "test_chan.scfg" in
|
||||
match Parse.from_channel chan with
|
||||
| Error _e -> assert false
|
||||
| Ok config ->
|
||||
let expected = "a b c" in
|
||||
let s = Format.asprintf "%a" Pp.config config in
|
||||
assert (s = expected)
|
||||
|
||||
let () =
|
||||
let s = {|a b c|} in
|
||||
match Parse.from_string s with
|
||||
| Error (`Msg e) ->
|
||||
Format.eprintf "ERROR: %s@\n" e;
|
||||
assert false
|
||||
| Ok config ->
|
||||
let expected = {|a b c|} in
|
||||
let s = Format.asprintf "%a" Pp.config config in
|
||||
assert (s = expected)
|
||||
|
||||
(** Testing queries. *)
|
||||
let file =
|
||||
match Fpath.of_string "query.scfg" with
|
||||
| Error _e -> assert false
|
||||
| Ok file -> file
|
||||
|
||||
let () =
|
||||
let config =
|
||||
match Parse.from_file file with
|
||||
| Error _e -> assert false
|
||||
| Ok config -> config
|
||||
in
|
||||
assert (List.length config = 5);
|
||||
let n1 = Query.get_dirs "n1" config in
|
||||
assert (List.length n1 = 2);
|
||||
let n11 = Query.get_dir "n1" n1 in
|
||||
let n11 = match n11 with None -> assert false | Some n11 -> n11 in
|
||||
let n12 =
|
||||
match Query.get_dir "n1.2" n11.children with
|
||||
| None -> assert false
|
||||
| Some n12 -> n12
|
||||
in
|
||||
let pn12 =
|
||||
match Query.get_params 2 n12 with
|
||||
| Error _e -> assert false
|
||||
| Ok pn12 -> pn12
|
||||
in
|
||||
assert (pn12 = [ "p1"; "p2" ]);
|
||||
begin match Query.get_params 3 n12 with
|
||||
| Error (`Msg "directive n1.2: want 3 params, got only 2") -> ()
|
||||
| Error _ | Ok _ -> assert false
|
||||
end;
|
||||
begin match Query.get_param 0 n12 with
|
||||
| Error _ -> assert false
|
||||
| Ok p -> assert (p = "p1")
|
||||
end;
|
||||
match Query.get_param 5 n12 with
|
||||
| Error (`Msg "directive n1.2: want param at index 5, got only 2") -> ()
|
||||
| Error _ | Ok _ -> assert false
|
||||
|
||||
(** Testing schema. *)
|
||||
module Test_schema = struct
|
||||
open Schema
|
||||
|
||||
let rev_string : (string, param) t -> (string, param) t =
|
||||
let decode =
|
||||
fun s ->
|
||||
let len = String.length s in
|
||||
let s_rev = String.init len (fun i -> String.get s (len - i - 1)) in
|
||||
Ok s_rev
|
||||
in
|
||||
custom decode
|
||||
|
||||
let len_string : (string, param) t -> (int, param) t =
|
||||
let decode = fun s -> String.length s |> Result.ok in
|
||||
custom decode
|
||||
|
||||
let schema =
|
||||
directive "dir" (t3 bool string string)
|
||||
(t2
|
||||
(directive "dir_1"
|
||||
(t2 bool (rev_string string))
|
||||
(directive "dir_1_1" nil nil) )
|
||||
(directive "dir_2" (t2 string (len_string string)) nil) )
|
||||
|
||||
let txt =
|
||||
Parse.from_string
|
||||
{|dir true str1 "str2" {
|
||||
dir_1 false naquadah {
|
||||
dir_1_1
|
||||
}
|
||||
dir_2 str3 four
|
||||
}|}
|
||||
|> Result.get_ok
|
||||
|
||||
let () =
|
||||
let v = conv schema txt |> Result.get_ok in
|
||||
assert (
|
||||
v
|
||||
= { name = "dir"
|
||||
; params = (true, "str1", "str2")
|
||||
; children =
|
||||
( { name = "dir_1"
|
||||
; params = (false, "hadauqan")
|
||||
; children = { name = "dir_1_1"; params = Nil; children = Nil }
|
||||
}
|
||||
, { name = "dir_2"; params = ("str3", 4); children = Nil } )
|
||||
} );
|
||||
()
|
||||
end
|
||||
|
||||
let () = Format.printf "all tests OK! 🐱"
|
||||
Loading…
Add table
Add a link
Reference in a new issue