aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorarie <arie@alleycat.cc>2021-06-07 11:19:22 +0200
committerarie <arie@alleycat.cc>2021-06-28 21:02:44 +0200
commit8bb06986550c075d9120e0a3619b3e7af01b2871 (patch)
tree758b668319793d367353adbe72fada50b49ffabc
parent157927df5907a085c238169b85b0c489384e6bfd (diff)
Rdf_turtle:
added some tests, changed some types.
-rw-r--r--lib/turtle/rdf_turtle.ml110
-rw-r--r--lib/turtle/rdf_turtle.mli12
-rw-r--r--test/alcotest/rdf_alcotest.ml4
-rw-r--r--test/alcotest/rdf_alcotest.mli2
-rw-r--r--test/turtle/main.ml92
5 files changed, 155 insertions, 65 deletions
diff --git a/lib/turtle/rdf_turtle.ml b/lib/turtle/rdf_turtle.ml
index d280985..a468db9 100644
--- a/lib/turtle/rdf_turtle.ml
+++ b/lib/turtle/rdf_turtle.ml
@@ -177,34 +177,34 @@ module AST = struct
| Obj_blank_node of Blank_node.t
| Obj_literal of Literal.t
| Obj_coll of collection
- | Obj_BnodPs of bnodep list
+ | Obj_BnodPs of bnodeps
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 bnodep = BNodeP of predobjs
+ and bnodeps = BNodePs of predobjs
and predobjs = (Predicate.t * object' list) list
let rec object_equal a b =
- match (a, b) with
- | (Obj_iri ia, Obj_iri ib) -> Iri.equal ia ib
- | (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) -> list_equal bnodep_equal bas bbs
+ match a, b with
+ | Obj_iri ia, Obj_iri ib -> Iri.equal ia ib
+ | 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
| _ -> false
and collection_equal (Collection obsa) (Collection obsb) =
list_equal object_equal obsa obsb
and subject_equal a b =
- match (a, b) with
- | (Sub_iri ia, Sub_iri ib) -> Iri.equal ia ib
- | (Sub_blank_node ba, Sub_blank_node bb) -> Blank_node.equal ba bb
- | (Sub_coll ca, Sub_coll cb) -> collection_equal ca cb
+ match a, b with
+ | Sub_iri ia, Sub_iri ib -> Iri.equal ia ib
+ | 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 bnodep_equal (BNodeP poa) (BNodeP pob) =
- predobjs_equal poa pob
+ and bnodeps_equal (BNodePs a) (BNodePs b) =
+ predobjs_equal a b
and predobjs_equal a b =
list_equal
(fun (p, obsa) (q, obsb) -> Predicate.equal p q && list_equal object_equal obsa obsb)
@@ -215,20 +215,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 ->
- List.iter
- (fun bnodp -> Fmt.pf ppf "@[<h 1><obj_bnodps@ %a>@]" bnodep_pp bnodp)
- bnodps
+ | Obj_BnodPs bnodps -> Fmt.pf ppf "@[<h 1><obj_bnodps@ %a>@]" bnodeps_pp bnodps
and collection_pp ppf (Collection objs) =
- List.iter
- (fun obj -> Fmt.pf ppf "@[<h 1><collection obj@ %a>@]" object_pp obj)
- 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 bnodep_pp ppf (BNodeP predobjs) =
- Fmt.pf ppf "@[<h 1><obj_bnodep@ %a>@]" predobjs_pp predobjs
+ and bnodeps_pp ppf (BNodePs predobjs) =
+ Fmt.pf ppf "@[<h 1><obj_bnodeps@ %a>@]" predobjs_pp predobjs
and predobjs_pp ppf l =
let objlist_pp ppf l =
List.iter
@@ -242,10 +237,9 @@ module AST = struct
l
-
type triples =
SubjPredObjs of subject * predobjs
- | BNodePs of (bnodep list)
+ | Triple_BNodePs of bnodeps
type directive = PrefixID of string * Iriref.t | Base of Iriref.t
@@ -338,7 +332,7 @@ 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 bnodep =
+ let object_ collection bnodeps =
choice [
(lift
(fun literal -> AST.Obj_literal literal)
@@ -352,50 +346,56 @@ module Parser = struct
(fun iri -> AST.Obj_iri iri)
iri);
(lift
+ (fun bnodeps -> AST.Obj_BnodPs bnodeps)
+ bnodeps
+ );
+ (lift
(fun collection -> AST.Obj_coll collection)
collection
);
- (lift
- (fun bnodeps -> AST.Obj_BnodPs bnodeps)
- (many bnodep)
- )
]
- let collection_ bnodep =
+ let collection_ bnodeps =
fix (fun collection ->
- let object' = object_ collection bnodep in
+ let object' = object_ collection bnodeps in
(lift
(fun collection -> AST.Collection collection)
- (char '('
- *>
- (many object')
- <* char ')'
+ (
+ char '('
+ *> whitespace
+ *>
+ (sep_by whitespace object')
+ <* whitespace
+ <* char ')'
)
)
)
- let subject_ bnodep =
- let collection = collection_ bnodep in
+ let subject_ bnodeps =
+ let collection = collection_ bnodeps in
choice [
(lift
(fun iri -> AST.Sub_iri iri)
- (iri)
+ iri
);
(lift
(fun blank_node -> AST.Sub_blank_node blank_node)
- (blank_node)
+ blank_node
);
(lift
(fun collection -> AST.Sub_coll collection)
- (collection)
+ collection
)
]
- let bnodep_ predobjs =
+ let bnodeps_ predobjs =
lift
- (fun predobjs -> AST.BNodeP predobjs)
- ( char '['
+ (fun predobjs -> AST.BNodePs predobjs)
+ (
+ char '['
+ *> whitespace
*> predobjs
+ <* whitespace
<* char ']'
)
@@ -411,21 +411,21 @@ module Parser = struct
<* whitespace
in
fix (fun predobjs ->
- let bnodep = bnodep_ predobjs in
- let collection = collection_ bnodep in
- let object' = object_ collection bnodep in
- sep_by1 semicolon (
+ let bnodeps = bnodeps_ predobjs in
+ let collection = collection_ bnodeps in
+ let object' = object_ collection bnodeps in
+ sep_by semicolon (
lift2
(fun p objs -> (p, objs))
- predicate
+ (whitespace *> predicate <* whitespace)
(sep_by1 comma object')
)
)
- let bnodep = bnodep_ predobjs
- let subject = subject_ bnodep
- let collection = collection_ bnodep
- let object' = object_ collection bnodep
+ let bnodeps = bnodeps_ predobjs
+ let subject = subject_ bnodeps
+ let collection = collection_ bnodeps
+ let object' = object_ collection bnodeps
let triples =
choice [
@@ -434,8 +434,8 @@ module Parser = struct
subject
predobjs);
(lift
- (fun bnodeps -> AST.BNodePs bnodeps)
- (many bnodep)
+ (fun bnodeps -> AST.Triple_BNodePs bnodeps)
+ bnodeps
)
]
diff --git a/lib/turtle/rdf_turtle.mli b/lib/turtle/rdf_turtle.mli
index 5573c3a..2f5318d 100644
--- a/lib/turtle/rdf_turtle.mli
+++ b/lib/turtle/rdf_turtle.mli
@@ -130,20 +130,20 @@ module AST : sig
| Obj_blank_node of Blank_node.t
| Obj_literal of Literal.t
| Obj_coll of collection
- | Obj_BnodPs of bnodep list
+ | Obj_BnodPs of bnodeps
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 bnodep = BNodeP of predobjs
+ and bnodeps = BNodePs 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 bnodep_equal : bnodep -> bnodep -> bool
+ val bnodeps_equal : bnodeps -> bnodeps -> bool
val predobjs_equal : predobjs -> predobjs -> bool
val object_pp : object' Fmt.t
@@ -155,7 +155,7 @@ module AST : sig
val subject_pp : subject Fmt.t
[@@ocaml.toplevel_printer]
- val bnodep_pp : bnodep Fmt.t
+ val bnodeps_pp : bnodeps Fmt.t
[@@ocaml.toplevel_printer]
val predobjs_pp : predobjs Fmt.t
@@ -163,7 +163,7 @@ module AST : sig
type triples =
SubjPredObjs of subject * predobjs
- | BNodePs of (bnodep list)
+ | Triple_BNodePs of bnodeps
type directive = PrefixID of string * Iriref.t | Base of Iriref.t
@@ -201,7 +201,7 @@ module Parser : sig
val predobjs : AST.predobjs Angstrom.t
- val bnodep : AST.bnodep Angstrom.t
+ val bnodeps : AST.bnodeps Angstrom.t
val subject : AST.subject Angstrom.t
diff --git a/test/alcotest/rdf_alcotest.ml b/test/alcotest/rdf_alcotest.ml
index 8d8ecc1..87d57fe 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_bnodep =
- Alcotest.testable Rdf_turtle.AST.bnodep_pp Rdf_turtle.AST.bnodep_equal
+let ast_bnodeps =
+ Alcotest.testable Rdf_turtle.AST.bnodeps_pp Rdf_turtle.AST.bnodeps_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 2514165..1cf4402 100644
--- a/test/alcotest/rdf_alcotest.mli
+++ b/test/alcotest/rdf_alcotest.mli
@@ -38,6 +38,6 @@ val ast_collection : Rdf_turtle.AST.collection Alcotest.testable
val ast_subject : Rdf_turtle.AST.subject Alcotest.testable
-val ast_bnodep : Rdf_turtle.AST.bnodep Alcotest.testable
+val ast_bnodeps : Rdf_turtle.AST.bnodeps Alcotest.testable
val ast_predobjs : Rdf_turtle.AST.predobjs Alcotest.testable
diff --git a/test/turtle/main.ml b/test/turtle/main.ml
index e5c3a59..c2c3b86 100644
--- a/test/turtle/main.ml
+++ b/test/turtle/main.ml
@@ -105,6 +105,23 @@ let literal_test_case =
(Result.ok @@ v))
cases)
+let predicate_test_case =
+ let cases = [
+ "<http://one.example/subject1>",
+ Turtle.Predicate.of_iri @@ Turtle.Iri.of_iriref @@ Turtle.Iriref.of_string "http://one.example/subject1" ;
+ "p:subject3",
+ Turtle.Predicate.of_iri @@ Turtle.Iri.of_prefixed_name @@ Turtle.Prefixed_name.of_strings "p" "subject3" ;
+ ] in
+ test_case "predicate" `Quick
+ (fun () ->
+ List.iter
+ (fun (enc, v) ->
+ check (result Rdf_alcotest.ast_predicate string)
+ "can parse"
+ (parse Parser.predicate enc)
+ (Result.ok @@ v))
+ cases)
+
let object_test_case =
let cases = [
(* Literal *)
@@ -134,8 +151,24 @@ let object_test_case =
(Turtle.Iri.of_prefixed_name @@ Turtle.Prefixed_name.of_strings "xsd" "string")) ;
Turtle.Obj_literal (
Turtle.Literal.make
+ ("banana")
+ (Turtle.Iri.of_prefixed_name @@ Turtle.Prefixed_name.of_strings "xsd" "string")) ;
+ ]
+ )
+ ) ;
+ (* BNodePlist *)
+ "[ foaf:name \"Bob\" ]",
+ Turtle.Obj_coll (
+ Turtle.Collection (
+ [
+ Turtle.Obj_literal (
+ Turtle.Literal.make
("apple")
(Turtle.Iri.of_prefixed_name @@ Turtle.Prefixed_name.of_strings "xsd" "string")) ;
+ Turtle.Obj_literal (
+ Turtle.Literal.make
+ ("banana")
+ (Turtle.Iri.of_prefixed_name @@ Turtle.Prefixed_name.of_strings "xsd" "string")) ;
]
)
) ;
@@ -150,6 +183,60 @@ let object_test_case =
(Result.ok @@ v))
cases)
+let bnodeps_test_case =
+ let cases = [
+ ] in
+ test_case "bnodeps" `Quick
+ (fun () ->
+ List.iter
+ (fun (enc, v) ->
+ check (result Rdf_alcotest.ast_bnodeps string)
+ "can parse"
+ (parse Parser.bnodeps enc)
+ (Result.ok @@ v))
+ cases)
+
+let predobjs_test_case =
+ let cases = [
+ " foaf:name \"Bob\" ",
+ [ Turtle.Predicate.of_iri @@ Turtle.Iri.of_prefixed_name @@ Turtle.Prefixed_name.of_strings "foaf" "name",
+ [ Turtle.Obj_literal ( Turtle.Literal.make
+ ("Bob")
+ (Turtle.Iri.of_prefixed_name @@ Turtle.Prefixed_name.of_strings "xsd" "string"))
+ ]
+ ]
+ ;
+ ] in
+ test_case "predobjs" `Quick
+ (fun () ->
+ List.iter
+ (fun (enc, v) ->
+ check (result Rdf_alcotest.ast_predobjs string)
+ "can parse"
+ (parse Parser.predobjs enc)
+ (Result.ok @@ v))
+ cases)
+
+let test_test_case =
+ let cases = [
+ " foaf:name \"Bob\" ",
+ [ Turtle.Predicate.of_iri @@ Turtle.Iri.of_prefixed_name @@ Turtle.Prefixed_name.of_strings "foaf" "name",
+ [ Turtle.Obj_literal ( Turtle.Literal.make
+ ("Bob")
+ (Turtle.Iri.of_prefixed_name @@ Turtle.Prefixed_name.of_strings "xsd" "string"))
+ ]
+ ]
+ ;
+ ] in
+ test_case "predobjs" `Quick
+ (fun () ->
+ List.iter
+ (fun (enc, v) ->
+ check (result Rdf_alcotest.ast_predobjs string)
+ "can parse"
+ (parse Parser.test_parser enc)
+ (Result.ok @@ v))
+ cases)
let () =
Alcotest.run "Turtle" [
"Basic parsers", [
@@ -159,6 +246,9 @@ let () =
blank_node_test_case;
iri_test_case;
literal_test_case;
- object_test_case;
+ predicate_test_case;
+(* object_test_case; *)
+ bnodeps_test_case;
+ predobjs_test_case;
]
]