aboutsummaryrefslogtreecommitdiff
path: root/test/turtle/main.ml
blob: e299132b7aa0c5c32a0097fbe22d4b7e5a653199 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
module Parser = Rdf_turtle.Parser
module Turtle = Rdf_turtle.AST

open Turtle
open Alcotest

let parse p =
  Angstrom.parse_string
    ~consume:Angstrom.Consume.All
    p

let iriref_test_case =
  let cases = [
    "<http://one.example/subject1>",
    Iriref.of_string "http://one.example/subject1" ;
  ] in
  test_case "iriref" `Quick
    (fun () ->
       List.iter
         (fun (enc, v) ->
            check (result Rdf_alcotest.ast_iriref string)
              "can parse"
              (parse Parser.iriref enc)
              (Result.ok @@ v))
         cases)

let language_test_case =
  test_case "language" `Quick
    (fun () ->
       check (result Rdf_alcotest.ast_language string)
         "can parse"
         (parse Parser.language "@fr-be")
         (Result.ok @@ Language.of_string "fr-be")
    )

let prefixed_name_test_case =
  let cases = [
    "rdf:first",
    Prefixed_name.of_strings "rdf" "first" ;
    ":first",
    Prefixed_name.of_strings "" "first" ;
  ] in
  test_case "prefixed_name" `Quick
    (fun () ->
       List.iter
         (fun (enc, v) ->
            check (result Rdf_alcotest.ast_prefixed_name string)
              "can parse"
              (parse Parser.prefixed_name enc)
              (Result.ok @@ v))
         cases)

let blank_node_test_case =
  test_case "blank_node" `Quick
    (fun () ->
       check (result Rdf_alcotest.ast_blank_node string)
         "can parse"
         (parse Parser.blank_node "_:alice")
         (Result.ok @@ Blank_node.of_string "alice")
    )

let iri_test_case =
  let cases = [
    "<http://one.example/subject1>",
    Iri.of_iriref @@ Iriref.of_string "http://one.example/subject1" ;
    "p:subject3",
    Iri.of_prefixed_name @@ Prefixed_name.of_strings "p" "subject3" ;
  ] in
  test_case "iri" `Quick
    (fun () ->
       List.iter
         (fun (enc, v) ->
            check (result Rdf_alcotest.ast_iri string)
              "can parse"
              (parse Parser.iri enc)
              (Result.ok @@ v))
         cases)

let literal_test_case =
  let cases = [
    "\"That Seventies Show\"^^xsd:string",
    Literal.make
      ("That Seventies Show")
      (Iri.of_prefixed_name @@ Prefixed_name.of_strings "xsd" "string") ;
    "\"That Seventies Show\"^^<http://www.w3.org/2001/XMLSchema#string>",
    Literal.make
      ("That Seventies Show")
      (Iri.of_iriref @@ Iriref.of_string "http://www.w3.org/2001/XMLSchema#string");
    "\"That Seventies Show\"",
    Literal.make
      ("That Seventies Show")
      (Iri.of_prefixed_name @@ Prefixed_name.of_strings "xsd" "string") ;
    "\"Cette Série des Années Septantei\"@fr-be",
    Literal.make
      "Cette Série des Années Septantei"
      (Iri.of_prefixed_name @@ Prefixed_name.of_strings "xsd" "string")
      ~language:"fr-be" ;
  ] in
  test_case "literal" `Quick
    (fun () ->
       List.iter
         (fun (enc, v) ->
            check (result Rdf_alcotest.ast_literal string)
              "can parse"
              (parse Parser.literal enc)
              (Result.ok @@ v))
         cases)

let predicate_test_case =
  let cases = [
    "<http://one.example/subject1>",
    Predicate.of_iri @@ Iri.of_iriref @@ Iriref.of_string "http://one.example/subject1" ;
    "p:subject3",
    Predicate.of_iri @@ Iri.of_prefixed_name @@ 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 *)
    "\"That Seventies Show\"^^xsd:string",
    Literal.make
      ("That Seventies Show")
      (Iri.of_prefixed_name @@ Prefixed_name.of_strings "xsd" "string")
    |> (fun lit -> Obj_literal lit) ;
    (* Iri *)
    "xsd:string",
    Iri.of_prefixed_name @@ Prefixed_name.of_strings "xsd" "string"
    |> (fun iri -> Obj_iri iri) ;
    (* Iri *)
    ":q",
    Iri.of_prefixed_name @@ Prefixed_name.of_strings "" "q"
    |> (fun iri -> Obj_iri iri) ;
    (* Blank_node *)
    "_:string",
    Obj_blank_node (Blank_node.of_string "string") ;
    (* Blank_node *)
    "_:string",
    Obj_blank_node (Blank_node.of_string "string") ;
    (* Collection *)
    "( \"apple\" \"banana\" )",
    Obj_coll (
      Collection (
        [
          Obj_literal (
            Literal.make
              ("apple")
              (Iri.of_prefixed_name @@ Prefixed_name.of_strings "xsd" "string")) ;
          Obj_literal (
            Literal.make
              ("banana")
              (Iri.of_prefixed_name @@ Prefixed_name.of_strings "xsd" "string")) ;
        ]
      )
    ) ;
    (*  BNodePlist *)
    "[ foaf:name \"Bob\" ]",
    Obj_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 "object" `Quick
    (fun () ->
       List.iter
         (fun (enc, v) ->
            check (result Rdf_alcotest.ast_object string)
              "can parse"
              (parse Parser.object' enc)
              (Result.ok @@ v))
         cases)

let bnodeps_test_case =
  let cases = [
    "[ foaf:name \"Bob\" ]",
    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"))
              ]
            ])
    ;
    "[:p :WWWW]",
    BnodPs ([ Predicate.of_iri @@ Iri.of_prefixed_name @@ Prefixed_name.of_strings "" "p",
              [ Obj_iri (Iri.of_prefixed_name @@ Prefixed_name.of_strings "" "WWWW"); ] ; ]
           );
  ]
  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 = [
    ":p :WWWWWWWWWWWWWWWWWWWwwW",
    [ Predicate.of_iri @@ Iri.of_prefixed_name @@ Prefixed_name.of_strings "" "p",
              [ Obj_iri (Iri.of_prefixed_name @@ Prefixed_name.of_strings "" "WWWWWWWWWWWWWWWWWWWwwW"); ] ; ]
    ;
    " foaf:name \"Bob\" ",
    [ 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"))
      ]
    ]
    ;
    "rel:enemyOf <#green-goblin> ;
        a foaf:Person ;
            foaf:name \"Spiderman\", \"Человек-паук\"@ru",
    [ Predicate.of_iri @@ Iri.of_prefixed_name @@ Prefixed_name.of_strings "rel" "enemyOf",
      [ Obj_iri ( Iri.of_iriref (Iriref.of_string "#green-goblin"));]
      ;
      Predicate.a,
      [ Obj_iri (Iri.of_prefixed_name @@ Prefixed_name.of_strings "foaf" "Person");]
      ;
      Predicate.of_iri @@ Iri.of_prefixed_name @@ Prefixed_name.of_strings "foaf" "name",
      [  Obj_literal ( Literal.make
                               ("Spiderman")
                               (Iri.of_prefixed_name @@ Prefixed_name.of_strings "xsd" "string"));
        Obj_literal ( Literal.make
                               ("Человек-паук")
                               (Iri.of_prefixed_name @@ Prefixed_name.of_strings "xsd" "string")
                               ~language:"ru");
      ]
    ] ;
  ] 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 subject_test_case =
  let cases = [
  (*  Iri *)
    "<http://one.example/>",
    Sub_iri (Iri.of_iriref (Iriref.of_string "http://one.example/"));
  (*  Collection *)
    "(\"1\" [:p :q] ( \"2\" ) )",
    Sub_coll (
      Collection (
        [
          Obj_literal (
            Literal.make
              ("1")
              (Iri.of_prefixed_name @@ Prefixed_name.of_strings "xsd" "string")) ;
          Obj_BnodPs (
            BnodPs (
              [ Predicate.of_iri @@ Iri.of_prefixed_name @@ Prefixed_name.of_strings "" "p",
                [ Obj_iri (Iri.of_prefixed_name @@ Prefixed_name.of_strings "" "q"); ] ; ]
            )
          ) ;
          Obj_coll (
            Collection (
              [ Obj_literal (
                  Literal.make
                    ("2")
                    (Iri.of_prefixed_name @@ Prefixed_name.of_strings "xsd" "string")) ;
              ]
            )
          )
        ]
      )
    ) ;
  ] in
  test_case "subject" `Quick
    (fun () ->
       List.iter
         (fun (enc, v) ->
            check (result Rdf_alcotest.ast_subject string)
              "can parse"
              (parse Parser.subject enc)
              (Result.ok @@ v))
         cases)

let directive_test_case =
  let cases = [
    "@base <http://one.example/> .",
    Directive.of_iriref (Iriref.of_string "http://one.example/")
  ] in
  test_case "directive" `Quick
    (fun () ->
       List.iter
         (fun (enc, v) ->
            check (result Rdf_alcotest.ast_directive string)
              "can parse"
              (parse Parser.directive enc)
              (Result.ok @@ v))
         cases)

let () =
  Alcotest.run "Turtle" [
    "Basic parsers", [
      iriref_test_case;
      language_test_case;
      prefixed_name_test_case;
      blank_node_test_case;
      iri_test_case;
      literal_test_case;
      predicate_test_case;
      object_test_case;
      bnodeps_test_case;
      predobjs_test_case;
      subject_test_case;
      directive_test_case;
    ]
  ]