aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorarie <arie@alleycat.cc>2021-05-17 12:37:52 +0200
committerpukkamustard <pukkamustard@posteo.net>2021-05-20 10:45:33 +0200
commit7ad40d7c0d985e5fbb892e52b12255fd36bf5ab3 (patch)
tree52b98b34c689da3a7ca9e9ea0ace14288b6e0f7c /test
parent6594560859ddc445ea287260eebae27efedf7eb6 (diff)
Rdf_json:
improve tests using the 'testables' from rdf_alcotest. Include pp functions for subject, object, predicate in order to make those testables.
Diffstat (limited to 'test')
-rw-r--r--test/alcotest/rdf_alcotest.ml12
-rw-r--r--test/alcotest/rdf_alcotest.mli8
-rw-r--r--test/json/dune2
-rw-r--r--test/json/main.ml88
4 files changed, 74 insertions, 36 deletions
diff --git a/test/alcotest/rdf_alcotest.ml b/test/alcotest/rdf_alcotest.ml
index b50a922..cb6b9b0 100644
--- a/test/alcotest/rdf_alcotest.ml
+++ b/test/alcotest/rdf_alcotest.ml
@@ -7,5 +7,17 @@ let blank_node =
let literal =
Alcotest.testable Rdf.Literal.pp Rdf.Literal.equal
+let subject =
+ Alcotest.testable Rdf.Triple.Subject.pp Rdf.Triple.Subject.equal
+
+let predicate =
+ Alcotest.testable Rdf.Triple.Predicate.pp Rdf.Triple.Predicate.equal
+
+let object' =
+ Alcotest.testable Rdf.Triple.Object.pp Rdf.Triple.Object.equal
+
let term =
Alcotest.testable Rdf.Term.pp Rdf.Term.equal
+
+let triple =
+ Alcotest.testable Rdf.Triple.pp Rdf.Triple.equal
diff --git a/test/alcotest/rdf_alcotest.mli b/test/alcotest/rdf_alcotest.mli
index 1f3fc50..77dfa0e 100644
--- a/test/alcotest/rdf_alcotest.mli
+++ b/test/alcotest/rdf_alcotest.mli
@@ -6,4 +6,12 @@ val blank_node : Rdf.Blank_node.t Alcotest.testable
val literal : Rdf.Literal.t Alcotest.testable
+val subject : Rdf.Triple.Subject.t Alcotest.testable
+
+val predicate : Rdf.Triple.Predicate.t Alcotest.testable
+
+val object' : Rdf.Triple.Object.t Alcotest.testable
+
val term : Rdf.Term.t Alcotest.testable
+
+val triple : Rdf.Triple.t Alcotest.testable
diff --git a/test/json/dune b/test/json/dune
index da3cc99..23ce55b 100644
--- a/test/json/dune
+++ b/test/json/dune
@@ -1,6 +1,6 @@
(executables
(names main)
- (libraries rdf_json rdf_gen
+ (libraries rdf_json rdf_gen rdf_alcotest
yojson alcotest qcheck-alcotest))
(rule
diff --git a/test/json/main.ml b/test/json/main.ml
index 7a0db60..2c96da7 100644
--- a/test/json/main.ml
+++ b/test/json/main.ml
@@ -7,75 +7,93 @@ let rec list_equal equal l1 l2 = match l1, l2 with
| [], _ | _, [] -> 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 ->
+ (fun acc y ->
if not acc then false
- else if not (list_contains equal elt l2) then false
+ else if not (List.exists (fun x -> equal y x) l2) then false
else true)
true)
&&
(l2 |> List.fold_left
- (fun acc elt ->
+ (fun acc y ->
if not acc then false
- else if not (list_contains equal elt l1) then false
+ else if not (List.exists (fun x -> equal y x) l1) then false
else true)
true)
let encode_decode_subject =
- QCheck.Test.make ~name:"decode encode subject"
+ QCheck.Test.make
+ ~count:1000
+ ~name:"decode/encode subject"
Rdf_gen.subject
- (fun subject -> Triple.Subject.equal
- subject
- (subject |> Rdf_json.encode_subject |> Rdf_json.decode_subject))
+ (fun subject ->
+ Alcotest.check Rdf_alcotest.subject
+ "decode/encode"
+ subject (subject
+ |> Rdf_json.encode_subject
+ |> Rdf_json.decode_subject
+ );
+ true
+ )
let encode_decode_predicate =
- QCheck.Test.make ~name:"decode encode predicate"
+ QCheck.Test.make
+ ~count:1000
+ ~name:"decode/encode predicate"
Rdf_gen.predicate
- (fun predicate -> Triple.Predicate.equal
- predicate
- (predicate |> Rdf_json.encode_predicate |> Rdf_json.decode_predicate))
+ (fun predicate ->
+ Alcotest.check Rdf_alcotest.predicate
+ "decode/encode"
+ predicate (predicate
+ |> Rdf_json.encode_predicate
+ |> Rdf_json.decode_predicate
+ );
+ true
+ )
let encode_decode_object =
- QCheck.Test.make ~name:"decode encode object"
+ QCheck.Test.make
+ ~count:1000
+ ~name:"decode/encode object"
Rdf_gen.object'
(fun object' ->
- Triple.Object.equal
- object'
- (object' |> Rdf_json.encode_object |> Rdf_json.decode_object))
+ Alcotest.check Rdf_alcotest.object'
+ "decode/encode"
+ object' (object'
+ |> Rdf_json.encode_object
+ |> Rdf_json.decode_object
+ );
+ true
+ )
-let encode_decode_triple_test =
- QCheck.Test.make ~name:"decode encode list with one elt"
- Rdf_gen.triple
- (fun triple ->
- Triple.equal
- triple
- ([triple] |> Rdf_json.encode |> Rdf_json.decode |> List.hd))
+(* The test uses the 'list_equal_up_to_order' function, which checks whether two lists contain the same elements *)
+(* (so it evaluates to true when the order is different). This is because the decode / encode does not preserve the order. *)
+(* Hence the form of the test is different from the ones above, which use a 'testable'. *)
+(* To make a testable for lists of triples, we would need an equal function. *)
-let encode_decode_triple_list_test =
+let encode_decode_triple_list =
let triple_list_arbitrary =
Rdf_gen.triple
|> QCheck.small_list
in
- QCheck.Test.make ~name:"decode encode small list"
+ QCheck.Test.make
+ ~count:1000
+ ~name:"decode encode small list"
triple_list_arbitrary
(fun lst ->
list_equal_up_to_order Triple.equal
- lst
- (lst |> Rdf_json.encode |> Rdf_json.decode))
+ 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;
encode_decode_subject;
encode_decode_predicate;
encode_decode_object;