aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorarie <arie@alleycat.cc>2021-06-08 12:49:16 +0200
committerarie <arie@alleycat.cc>2021-06-28 21:02:45 +0200
commitbbb98e4944c3c0a2438aff3c6feae93f749880c9 (patch)
treeb2208159a8cf4d63ba88069cdac54bef10e9c8a2
parent87ac72ad5621c0a6358ee486e4610c82ece9fb30 (diff)
Rdf turtle test:
add remaining tests. All the parsers have tests now, and they work. It would be good to include more tests, e.g. all the examples from the w3 specifications. Literals cannot be integers, that remains a todo (so all those examples won't work).
-rw-r--r--lib/turtle/rdf_turtle.ml97
-rw-r--r--lib/turtle/rdf_turtle.mli22
-rw-r--r--test/alcotest/rdf_alcotest.ml4
-rw-r--r--test/alcotest/rdf_alcotest.mli2
-rw-r--r--test/turtle/main.ml119
5 files changed, 186 insertions, 58 deletions
diff --git a/lib/turtle/rdf_turtle.ml b/lib/turtle/rdf_turtle.ml
index 1425307..41875c2 100644
--- a/lib/turtle/rdf_turtle.ml
+++ b/lib/turtle/rdf_turtle.ml
@@ -178,14 +178,14 @@ module AST = struct
| Obj_blank_node of Blank_node.t
| Obj_literal of Literal.t
| Obj_coll of collection
- | Obj_BnodPs of bnodeps
+ | Obj_BnodPs of bnodps
and collection =
Collection of object' list
and subject =
Sub_iri of Iri.t
| Sub_blank_node of Blank_node.t
| Sub_coll of collection
- and bnodeps = BnodPs of predobjs
+ and bnodps = BnodPs of predobjs
and predobjs = (Predicate.t * object' list) list
let rec object_equal a b =
@@ -194,7 +194,7 @@ module AST = struct
| Obj_blank_node ba, Obj_blank_node bb -> Blank_node.equal ba bb
| Obj_literal la, Obj_literal lb -> Literal.equal la lb
| Obj_coll ca, Obj_coll cb -> collection_equal ca cb
- | Obj_BnodPs bas, Obj_BnodPs bbs -> bnodeps_equal bas bbs
+ | Obj_BnodPs bas, Obj_BnodPs bbs -> bnodps_equal bas bbs
| _ -> false
and collection_equal (Collection obsa) (Collection obsb) =
list_equal object_equal obsa obsb
@@ -204,7 +204,7 @@ module AST = struct
| Sub_blank_node ba, Sub_blank_node bb -> Blank_node.equal ba bb
| Sub_coll ca, Sub_coll cb -> collection_equal ca cb
| _ -> false
- and bnodeps_equal (BnodPs a) (BnodPs b) =
+ and bnodps_equal (BnodPs a) (BnodPs b) =
predobjs_equal a b
and predobjs_equal a b =
list_equal
@@ -216,15 +216,15 @@ module AST = struct
| Obj_blank_node bnode -> Fmt.pf ppf "@[<h 1><obj_blank_node@ %a>@]" Blank_node.pp bnode
| Obj_literal lit -> Fmt.pf ppf "@[<h 1><obj_literal@ %a>@]" Literal.pp lit
| Obj_coll coll -> Fmt.pf ppf "@[<h 1><obj_coll@ %a>@]" collection_pp coll
- | Obj_BnodPs bnodps -> Fmt.pf ppf "@[<h 1><obj_bnodps@ %a>@]" bnodeps_pp bnodps
+ | Obj_BnodPs bnodps -> Fmt.pf ppf "@[<h 1><obj_bnodps@ %a>@]" bnodps_pp bnodps
and collection_pp ppf (Collection objs) =
Fmt.pf ppf "@[<h 1><collection obj@ %a>@]" (Fmt.list object_pp) objs
and subject_pp ppf = function
| Sub_iri iri ->Fmt.pf ppf "@[<h 1><obj_iri@ %a>@]" Iri.pp iri
| Sub_blank_node bnode -> Fmt.pf ppf "@[<h 1><obj_blank_node@ %a>@]" Blank_node.pp bnode
| Sub_coll collection -> Fmt.pf ppf "@[<h 1><obj_collection@ %a>@]" collection_pp collection
- and bnodeps_pp ppf (BnodPs predobjs) =
- Fmt.pf ppf "@[<h 1><obj_bnodeps@ %a>@]" predobjs_pp predobjs
+ and bnodps_pp ppf (BnodPs predobjs) =
+ Fmt.pf ppf "@[<h 1><obj_bnodps@ %a>@]" predobjs_pp predobjs
and predobjs_pp ppf l =
let objlist_pp ppf l =
List.iter
@@ -241,14 +241,20 @@ module AST = struct
module Triples = struct
type t = SubjPredObjs of subject * predobjs
- | BnodPs of bnodeps
+ | BnodPs of bnodps
+
+ let of_subject_and_predobjs s p =
+ SubjPredObjs (s, p)
+
+ let of_bnodps b =
+ BnodPs b
let equal a b =
match a, b with
| SubjPredObjs (asub, ap), SubjPredObjs (bsub, bp) ->
subject_equal asub bsub && predobjs_equal ap bp
| BnodPs ab, BnodPs bb ->
- bnodeps_equal ab bb
+ bnodps_equal ab bb
| _, _ -> false
let pp ppf = function
@@ -257,7 +263,7 @@ module AST = struct
subject_pp s
predobjs_pp p
| BnodPs b ->
- Fmt.pf ppf "@[<h 1><triples@ bnodeps %a>@]" bnodeps_pp b
+ Fmt.pf ppf "@[<h 1><triples@ bnodps %a>@]" bnodps_pp b
end
@@ -292,6 +298,12 @@ module AST = struct
type t = Directive of Directive.t | Triples of Triples.t
+ let of_directive d =
+ Directive d
+
+ let of_triples t =
+ Triples t
+
let equal a b =
match a, b with
| Directive a, Directive b -> Directive.equal a b
@@ -310,11 +322,14 @@ module AST = struct
type t = Statement.t list
- let equal a b =
- list_equal Statement.equal a b
+ let of_statement_lst lst =
+ lst
+
+ let equal a b =
+ list_equal Statement.equal a b
- let pp ppf l =
- Fmt.pf ppf "@[<h 1><turtle@ statement list %a>@]" (Fmt.list Statement.pp) l
+ let pp ppf l =
+ Fmt.pf ppf "@[<h 1><turtle@ statement list %a>@]" (Fmt.list Statement.pp) l
end
@@ -408,42 +423,37 @@ module Parser = struct
(* This is because the prefixed name parser accepts, for example: *)
(* "\"That Seventies Show\"^^xsd:string" *)
(* Same thing for blank_node! *)
- let object_ collection bnodeps =
+ let object_ collection bnodps =
choice [
(lift
(fun collection ->
- Fmt.pr "collection@.";
AST.Obj_coll collection)
collection
);
(lift
- (fun bnodeps ->
- Fmt.pr "bnodeps@.";
- AST.Obj_BnodPs bnodeps)
- bnodeps
+ (fun bnodps ->
+ AST.Obj_BnodPs bnodps)
+ bnodps
);
(lift
(fun literal ->
- Fmt.pr "literal: %a@." AST.Literal.pp literal;
AST.Obj_literal literal)
literal
);
(lift
(fun blank_node ->
- Fmt.pr "blank node@.";
AST.Obj_blank_node blank_node)
blank_node
);
(lift
(fun iri ->
- Fmt.pr "iri: %a@." AST.Iri.pp iri;
AST.Obj_iri iri)
iri);
]
- let collection_ bnodeps =
+ let collection_ bnodps =
fix (fun collection ->
- let object' = object_ collection bnodeps in
+ let object' = object_ collection bnodps in
(lift
(fun collection -> AST.Collection collection)
(
@@ -457,8 +467,8 @@ module Parser = struct
)
)
- let subject_ bnodeps =
- let collection = collection_ bnodeps in
+ let subject_ bnodps =
+ let collection = collection_ bnodps in
choice [
(lift
(fun collection -> AST.Sub_coll collection)
@@ -474,11 +484,9 @@ module Parser = struct
);
]
- let bnodeps_ predobjs =
+ let bnodps predobjs =
lift
- (fun predobjs ->
- Fmt.pr "BNODEPS";
- AST.BnodPs predobjs)
+ (fun predobjs -> AST.BnodPs predobjs)
(
char '['
*> whitespace
@@ -499,23 +507,21 @@ module Parser = struct
<* whitespace
in
fix (fun predobjs ->
- let bnodeps = bnodeps_ predobjs in
- let collection = collection_ bnodeps in
- let object' = object_ collection bnodeps in
+ let bnodps = bnodps predobjs in
+ let collection = collection_ bnodps in
+ let object' = object_ collection bnodps in
sep_by1 semicolon (
lift2
- (fun p objs ->
- Fmt.pr "PREDICATE %a @." AST.Predicate.pp p;
- (p, objs))
+ (fun p objs -> (p, objs))
(whitespace *> predicate <* whitespace)
(sep_by1 comma (whitespace *> object' <* whitespace))
)
)
- let bnodeps = bnodeps_ predobjs
- let subject = subject_ bnodeps
- let collection = collection_ bnodeps
- let object' = object_ collection bnodeps
+ let bnodps = bnodps predobjs
+ let subject = subject_ bnodps
+ let collection = collection_ bnodps
+ let object' = object_ collection bnodps
let triples =
choice [
@@ -524,8 +530,8 @@ module Parser = struct
subject
predobjs);
(lift
- (fun bnodeps -> AST.Triples.BnodPs bnodeps)
- bnodeps
+ (fun bnodps -> AST.Triples.BnodPs bnodps)
+ bnodps
)
]
@@ -540,7 +546,9 @@ module Parser = struct
<* whitespace)
(iriref
<* whitespace
- <* char '.')
+ <* char '.'
+ <* whitespace
+ )
;
lift
(fun iriref -> AST.Directive.Base iriref)
@@ -563,6 +571,7 @@ module Parser = struct
(triples
<* whitespace
<* char '.'
+ <* whitespace
)
]
diff --git a/lib/turtle/rdf_turtle.mli b/lib/turtle/rdf_turtle.mli
index d1845fa..1e41686 100644
--- a/lib/turtle/rdf_turtle.mli
+++ b/lib/turtle/rdf_turtle.mli
@@ -132,20 +132,20 @@ module AST : sig
| Obj_blank_node of Blank_node.t
| Obj_literal of Literal.t
| Obj_coll of collection
- | Obj_BnodPs of bnodeps
+ | Obj_BnodPs of bnodps
and collection =
Collection of object' list
and subject =
Sub_iri of Iri.t
| Sub_blank_node of Blank_node.t
| Sub_coll of collection
- and bnodeps = BnodPs of predobjs
+ and bnodps = BnodPs of predobjs
and predobjs = (Predicate.t * object' list) list
val object_equal : object' -> object' -> bool
val collection_equal : collection -> collection -> bool
val subject_equal : subject -> subject -> bool
- val bnodeps_equal : bnodeps -> bnodeps -> bool
+ val bnodps_equal : bnodps -> bnodps -> bool
val predobjs_equal : predobjs -> predobjs -> bool
val object_pp : object' Fmt.t
@@ -157,7 +157,7 @@ module AST : sig
val subject_pp : subject Fmt.t
[@@ocaml.toplevel_printer]
- val bnodeps_pp : bnodeps Fmt.t
+ val bnodps_pp : bnodps Fmt.t
[@@ocaml.toplevel_printer]
val predobjs_pp : predobjs Fmt.t
@@ -166,7 +166,11 @@ module AST : sig
module Triples : sig
type t = SubjPredObjs of subject * predobjs
- | BnodPs of bnodeps
+ | BnodPs of bnodps
+
+ val of_subject_and_predobjs : subject -> predobjs -> t
+
+ val of_bnodps : bnodps -> t
val equal : t -> t -> bool
@@ -194,6 +198,10 @@ module AST : sig
type t = Directive of Directive.t | Triples of Triples.t
+ val of_directive : Directive.t -> t
+
+ val of_triples : Triples.t -> t
+
val equal : t -> t -> bool
val pp : t Fmt.t
@@ -205,6 +213,8 @@ module AST : sig
type t = Statement.t list
+ val of_statement_lst : Statement.t list -> t
+
val equal : t -> t -> bool
val pp : t Fmt.t
@@ -242,7 +252,7 @@ module Parser : sig
val predobjs : AST.predobjs Angstrom.t
- val bnodeps : AST.bnodeps Angstrom.t
+ val bnodps : AST.bnodps Angstrom.t
val subject : AST.subject Angstrom.t
diff --git a/test/alcotest/rdf_alcotest.ml b/test/alcotest/rdf_alcotest.ml
index 88e9831..973072b 100644
--- a/test/alcotest/rdf_alcotest.ml
+++ b/test/alcotest/rdf_alcotest.ml
@@ -55,8 +55,8 @@ let ast_collection =
let ast_subject =
Alcotest.testable Rdf_turtle.AST.subject_pp Rdf_turtle.AST.subject_equal
-let ast_bnodeps =
- Alcotest.testable Rdf_turtle.AST.bnodeps_pp Rdf_turtle.AST.bnodeps_equal
+let ast_bnodps =
+ Alcotest.testable Rdf_turtle.AST.bnodps_pp Rdf_turtle.AST.bnodps_equal
let ast_predobjs =
Alcotest.testable Rdf_turtle.AST.predobjs_pp Rdf_turtle.AST.predobjs_equal
diff --git a/test/alcotest/rdf_alcotest.mli b/test/alcotest/rdf_alcotest.mli
index 56a0e57..d0d6395 100644
--- a/test/alcotest/rdf_alcotest.mli
+++ b/test/alcotest/rdf_alcotest.mli
@@ -38,7 +38,7 @@ val ast_collection : Rdf_turtle.AST.collection Alcotest.testable
val ast_subject : Rdf_turtle.AST.subject Alcotest.testable
-val ast_bnodeps : Rdf_turtle.AST.bnodeps Alcotest.testable
+val ast_bnodps : Rdf_turtle.AST.bnodps Alcotest.testable
val ast_predobjs : Rdf_turtle.AST.predobjs Alcotest.testable
diff --git a/test/turtle/main.ml b/test/turtle/main.ml
index e299132..3c70f28 100644
--- a/test/turtle/main.ml
+++ b/test/turtle/main.ml
@@ -181,7 +181,7 @@ let object_test_case =
(Result.ok @@ v))
cases)
-let bnodeps_test_case =
+let bnodps_test_case =
let cases = [
"[ foaf:name \"Bob\" ]",
BnodPs ([ Predicate.of_iri @@ Iri.of_prefixed_name @@ Prefixed_name.of_strings "foaf" "name",
@@ -197,13 +197,13 @@ let bnodeps_test_case =
);
]
in
- test_case "bnodeps" `Quick
+ test_case "bnodps" `Quick
(fun () ->
List.iter
(fun (enc, v) ->
- check (result Rdf_alcotest.ast_bnodeps string)
+ check (result Rdf_alcotest.ast_bnodps string)
"can parse"
- (parse Parser.bnodeps enc)
+ (parse Parser.bnodps enc)
(Result.ok @@ v))
cases)
@@ -294,10 +294,44 @@ let subject_test_case =
(Result.ok @@ v))
cases)
+let triples_test_case =
+ let cases = [
+ "<http://example.org/#spiderman> <http://www.perceive.net/schemas/relationship/enemyOf> <http://example.org/#green-goblin>",
+ Triples.of_subject_and_predobjs
+ (Sub_iri (Iri.of_iriref (Iriref.of_string @@ "http://example.org/#spiderman")))
+ ([ Predicate.of_iri @@ Iri.of_iriref "http://www.perceive.net/schemas/relationship/enemyOf",
+ [ Obj_iri (Iri.of_iriref "http://example.org/#green-goblin"); ] ; ])
+ ;
+ "[ foaf:name \"Bob\" ]",
+ Triples.of_bnodps (
+ BnodPs ([ Predicate.of_iri @@ Iri.of_prefixed_name @@ Prefixed_name.of_strings "foaf" "name",
+ [ Obj_literal ( Literal.make
+ ("Bob")
+ (Iri.of_prefixed_name @@ Prefixed_name.of_strings "xsd" "string"))
+ ]
+ ]))
+ ;
+ ] in
+ test_case "triples" `Quick
+ (fun () ->
+ List.iter
+ (fun (enc, v) ->
+ check (result Rdf_alcotest.ast_triples string)
+ "can parse"
+ (parse Parser.triples enc)
+ (Result.ok @@ v))
+ cases)
+
let directive_test_case =
let cases = [
"@base <http://one.example/> .",
Directive.of_iriref (Iriref.of_string "http://one.example/")
+ ;
+ "@prefix foaf: <http://xmlns.com/foaf/0.1/> .",
+ Directive.of_string_and_iriref
+ "foaf"
+ (Iriref.of_string "http://xmlns.com/foaf/0.1/")
+ ;
] in
test_case "directive" `Quick
(fun () ->
@@ -309,6 +343,78 @@ let directive_test_case =
(Result.ok @@ v))
cases)
+let statement_test_case =
+ let cases = [
+ "@prefix foaf: <http://xmlns.com/foaf/0.1/> .",
+ Statement.of_directive (
+ Directive.of_string_and_iriref
+ "foaf"
+ (Iriref.of_string "http://xmlns.com/foaf/0.1/"))
+ ;
+ "<http://example.org/#spiderman> <http://www.perceive.net/schemas/relationship/enemyOf> <http://example.org/#green-goblin> .",
+ Statement.of_triples (
+ Triples.of_subject_and_predobjs
+ (Sub_iri (Iri.of_iriref (Iriref.of_string @@ "http://example.org/#spiderman")))
+ ([ Predicate.of_iri @@ Iri.of_iriref "http://www.perceive.net/schemas/relationship/enemyOf",
+ [ Obj_iri (Iri.of_iriref "http://example.org/#green-goblin"); ] ; ]))
+ ;
+ ] in
+ test_case "statement" `Quick
+ (fun () ->
+ List.iter
+ (fun (enc, v) ->
+ check (result Rdf_alcotest.ast_statement string)
+ "can parse"
+ (parse Parser.statement enc)
+ (Result.ok @@ v))
+ cases)
+
+let turtle_test_case =
+ let cases = [
+ "@prefix foaf: <http://xmlns.com/foaf/0.1/> .",
+ Turtle.of_statement_lst (
+ [Statement.of_directive (
+ Directive.of_string_and_iriref
+ "foaf"
+ (Iriref.of_string "http://xmlns.com/foaf/0.1/"))])
+ ;
+ "<http://example.org/#spiderman> <http://www.perceive.net/schemas/relationship/enemyOf> <http://example.org/#green-goblin> .",
+ Turtle.of_statement_lst (
+ [Statement.of_triples (
+ Triples.of_subject_and_predobjs
+ (Sub_iri (Iri.of_iriref (Iriref.of_string @@ "http://example.org/#spiderman")))
+ ([ Predicate.of_iri @@ Iri.of_iriref "http://www.perceive.net/schemas/relationship/enemyOf",
+ [ Obj_iri (Iri.of_iriref "http://example.org/#green-goblin"); ] ; ]))])
+ ;
+ "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix show: <http://example.org/vocab/show/> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .",
+ Turtle.of_statement_lst (
+ [ Statement.of_directive (
+ Directive.of_string_and_iriref
+ "rdfs"
+ (Iriref.of_string "http://www.w3.org/2000/01/rdf-schema#"))
+ ;
+ Statement.of_directive (
+ Directive.of_string_and_iriref
+ "show"
+ (Iriref.of_string "http://example.org/vocab/show/"))
+ ;
+ Statement.of_directive (
+ Directive.of_string_and_iriref
+ "xsd"
+ (Iriref.of_string "http://www.w3.org/2001/XMLSchema#"))
+ ])
+ ;
+ ] in
+ test_case "turtle" `Quick
+ (fun () ->
+ List.iter
+ (fun (enc, v) ->
+ check (result Rdf_alcotest.ast_turtle string)
+ "can parse"
+ (parse Parser.turtle enc)
+ (Result.ok @@ v))
+ cases)
+
let () =
Alcotest.run "Turtle" [
"Basic parsers", [
@@ -320,9 +426,12 @@ let () =
literal_test_case;
predicate_test_case;
object_test_case;
- bnodeps_test_case;
+ bnodps_test_case;
predobjs_test_case;
subject_test_case;
+ triples_test_case;
directive_test_case;
+ statement_test_case;
+ turtle_test_case;
]
]