aboutsummaryrefslogtreecommitdiff
path: root/test/turtle/main.ml
blob: 6cd5c320d7806b4d2887124b4a9a5a812eae1476 (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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
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 bnodps_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 "bnodps" `Quick
    (fun () ->
       List.iter
         (fun (enc, v) ->
            check (result Rdf_alcotest.ast_bnodps string)
              "can parse"
              (parse Parser.bnodps 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 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"); ] ; ])
    ;
(*     TODO write test with proper predobjs *)
    "[ 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 () ->
       List.iter
         (fun (enc, v) ->
            check (result Rdf_alcotest.ast_directive string)
              "can parse"
              (parse Parser.directive enc)
              (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", [
      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;
      bnodps_test_case;
      predobjs_test_case;
      subject_test_case;
      triples_test_case;
      directive_test_case;
      statement_test_case;
      turtle_test_case;
    ]
  ]