aboutsummaryrefslogtreecommitdiff
path: root/test/turtle/main.ml
diff options
context:
space:
mode:
authorarie <arie@alleycat.cc>2021-06-28 12:20:20 +0200
committerarie <arie@alleycat.cc>2021-06-28 21:08:44 +0200
commit49a0b6e0034fc411963bf3cc2bc1c6f046dcc389 (patch)
tree08c8b2d7d7609d8b42100ca000d95c0e09510e92 /test/turtle/main.ml
parent006fb92bcb85afe6bf404a794ddaefb9e0ad3c9c (diff)
Rdf-turtle test:
Add a test for the turtle parser and the transformation. See the comments in the code for some remarks.
Diffstat (limited to 'test/turtle/main.ml')
-rw-r--r--test/turtle/main.ml71
1 files changed, 70 insertions, 1 deletions
diff --git a/test/turtle/main.ml b/test/turtle/main.ml
index 8f638e3..11e5036 100644
--- a/test/turtle/main.ml
+++ b/test/turtle/main.ml
@@ -4,6 +4,44 @@ module Turtle = Rdf_turtle.Ast
open Turtle
open Alcotest
+(* TODO The following four functions create a 'testable' for the graph type.
+ * Usually, this testable is located in another file: rdf_alcotest,
+ * but since I'm not sure at all wheteher the 'equal' function for graphs is
+ * a reasonable one, and the pp function is also rather ad hoc, I put it here.
+ * It would be nice to have a good implementation for the equal function.
+ * Right now, it first transforms the graph into a list of tiples, and then
+ * checks whether the lists are equal up to ordering. This list_equal_up_to_order
+ * function is also used in the test file for rdf_json. *)
+
+(* Check whether two lists have the same elements *)
+let list_equal_up_to_order equal l1 l2 =
+ (l1 |> List.fold_left
+ (fun acc y ->
+ if not acc then false
+ else if not (List.exists (fun x -> equal y x) l2) then false
+ else true)
+ true)
+ &&
+ (l2 |> List.fold_left
+ (fun acc y ->
+ if not acc then false
+ else if not (List.exists (fun x -> equal y x) l1) then false
+ else true)
+ true)
+
+let graph_equal g1 g2 =
+ let graph_to_list g =
+ g
+ |> Rdf.Graph.to_triples
+ |> List.of_seq
+ in
+ list_equal_up_to_order Rdf.Triple.equal (graph_to_list g1) (graph_to_list g2)
+
+let graph_pp ppf g =
+ Fmt.pf ppf "%s" (Rdf_turtle.encode g)
+
+let graph_testable = Alcotest.testable graph_pp graph_equal
+
let parse p =
Angstrom.parse_string
~consume:Angstrom.Consume.All
@@ -441,9 +479,39 @@ let turtle_test_case =
(Result.ok @@ v))
cases)
+(* TODO This test works as follows: we take a turtle string and create a graph out of it using rdf_turtle.decode,
+ * which is the composition of the turtle parser, and the transformation which transforms instances of the turtle type into a graph.
+ * We compare the result to a graph which the empty graph with a number of triples added.
+ * We have some helper functions to reduce the amount of code.
+ * One issue with this approach is, is that when there are tests that don't pass. it is not
+ * immediately clear where the problems lie, since the decode function is a composition of other
+ * (already quite complicated) funtions. *)
+let turtle_to_graph_test_case =
+ let triple_of_iris_of_strs sub pred obj =
+ (Rdf.Triple.make
+ (Rdf.Triple.Subject.of_iri @@ Rdf.Iri.of_string sub)
+ (Rdf.Triple.Predicate.of_iri @@ Rdf.Iri.of_string pred)
+ (Rdf.Triple.Object.of_iri @@ Rdf.Iri.of_string obj))
+ in
+ let cases = [
+ "<sub> <pred> <obj> .",
+ Rdf.Graph.add Rdf.Graph.empty
+ (triple_of_iris_of_strs "sub" "pred" "obj")
+ ;
+ ] in
+ test_case "turtle parser and transform" `Quick
+ (fun () ->
+ List.iter
+ (fun (enc, v) ->
+ check (graph_testable)
+ "can parse and transform"
+ (Rdf_turtle.decode enc)
+ (v))
+ cases)
+
let () =
Alcotest.run "Turtle" [
- "Basic parsers", [
+ "Turtle parsers", [
iriref_test_case;
language_test_case;
prefixed_name_test_case;
@@ -459,5 +527,6 @@ let () =
directive_test_case;
statement_test_case;
turtle_test_case;
+ turtle_to_graph_test_case;
]
]