aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorarie <arie@alleycat.cc>2021-05-06 12:52:36 +0200
committerarie <arie@alleycat.cc>2021-05-06 12:52:36 +0200
commitc7bf48b69361cfb6434345533b08bcb1a2082c04 (patch)
tree8c8fb78f7962105defbeaf8f9be22093821f3185 /test
parent2e85bf6fb7719993089f99bb0e70a1002391a4c0 (diff)
Rdf_json tests:
Include equal function for triples. Add some util functions for tests, include triple_list test decode encode.
Diffstat (limited to 'test')
-rw-r--r--test/json/main.ml54
1 files changed, 41 insertions, 13 deletions
diff --git a/test/json/main.ml b/test/json/main.ml
index 2b2073d..7a0db60 100644
--- a/test/json/main.ml
+++ b/test/json/main.ml
@@ -1,5 +1,34 @@
module Triple = Rdf.Triple
+(* Check whether lists are equal (and have the same ordering) *)
+(* Used for testing decode encode on lists of triples (i.e. graphs). Then the test fails *)
+let rec list_equal equal l1 l2 = match l1, l2 with
+| [], [] -> true
+| [], _ | _, [] -> false
+| h1::t1, h2::t2 -> if equal h1 h2 then list_equal equal t1 t2 else false
+
+(* Check whether an element is contained in a list *)
+let list_contains equal x lst =
+ lst
+ |> List.fold_left (fun acc elt -> if acc || equal x elt then true else false)
+ false
+
+(* Check whether two lists have the same elements *)
+let list_equal_up_to_order equal l1 l2 =
+ (l1 |> List.fold_left
+ (fun acc elt ->
+ if not acc then false
+ else if not (list_contains equal elt l2) then false
+ else true)
+ true)
+ &&
+ (l2 |> List.fold_left
+ (fun acc elt ->
+ if not acc then false
+ else if not (list_contains equal elt l1) then false
+ else true)
+ true)
+
let encode_decode_subject =
QCheck.Test.make ~name:"decode encode subject"
Rdf_gen.subject
@@ -23,31 +52,30 @@ let encode_decode_object =
(object' |> Rdf_json.encode_object |> Rdf_json.decode_object))
let encode_decode_triple_test =
- QCheck.Test.make ~name:"decode encode"
+ QCheck.Test.make ~name:"decode encode list with one elt"
Rdf_gen.triple
(fun triple ->
- [triple] = (Rdf_json.encode [triple]
- |> Rdf_json.decode))
+ Triple.equal
+ triple
+ ([triple] |> Rdf_json.encode |> Rdf_json.decode |> List.hd))
-let _encode_decode_triple_list_test =
+let encode_decode_triple_list_test =
let triple_list_arbitrary =
Rdf_gen.triple
- |> QCheck.list
+ |> QCheck.small_list
in
- QCheck_alcotest.to_alcotest @@
- QCheck.Test.make ~name:"decode encode"
+ QCheck.Test.make ~name:"decode encode small list"
triple_list_arbitrary
(fun lst ->
- lst = (Rdf_json.encode lst
- |> Rdf_json.decode))
-
-
+ list_equal_up_to_order Triple.equal
+ lst
+ (lst |> Rdf_json.encode |> Rdf_json.decode))
let () =
Alcotest.run "Json" [
"Encoder", List.map QCheck_alcotest.to_alcotest [
-(* encode_decode_triple_list_test; *)
-(* encode_decode_triple_test; *)
+ encode_decode_triple_list_test;
+ encode_decode_triple_test;
encode_decode_subject;
encode_decode_predicate;
encode_decode_object;