rdf-ex/test/unit/turtle_encoder_test.exs

936 lines
29 KiB
Elixir
Raw Normal View History

2017-08-09 21:24:37 +00:00
defmodule RDF.Turtle.EncoderTest do
use ExUnit.Case, async: false
alias RDF.Turtle
doctest Turtle.Encoder
alias RDF.{Graph, PrefixMap}
alias RDF.NS
alias RDF.NS.{RDFS, OWL}
2017-08-09 21:24:37 +00:00
import RDF.Sigils
use RDF.Vocabulary.Namespace
2020-06-29 08:37:42 +00:00
defvocab EX, base_iri: "http://example.org/#", terms: [], strict: false
2017-08-09 21:24:37 +00:00
describe "serializing a graph" do
test "an empty graph is serialized to an empty string" do
2020-06-29 08:37:42 +00:00
assert Turtle.Encoder.encode!(Graph.new(), prefixes: %{}) == ""
2017-08-09 21:24:37 +00:00
end
test "statements with IRIs only" do
2020-06-29 08:37:42 +00:00
assert Turtle.Encoder.encode!(
Graph.new([
{EX.S1, EX.p1(), EX.O1},
{EX.S1, EX.p1(), EX.O2},
{EX.S1, EX.p2(), EX.O3},
{EX.S2, EX.p3(), EX.O4}
]),
prefixes: %{}
) ==
"""
<http://example.org/#S1>
<http://example.org/#p1> <http://example.org/#O1>, <http://example.org/#O2> ;
<http://example.org/#p2> <http://example.org/#O3> .
<http://example.org/#S2>
<http://example.org/#p3> <http://example.org/#O4> .
"""
2017-08-09 21:24:37 +00:00
end
test "statements with prefixed names" do
2020-06-29 08:37:42 +00:00
assert Turtle.Encoder.encode!(
Graph.new([
{EX.S1, EX.p1(), EX.O1},
{EX.S1, EX.p1(), EX.O2},
{EX.S1, EX.p2(), EX.O3},
{EX.S2, EX.p3(), EX.O4}
]),
prefixes: %{
ex: EX.__base_iri__(),
xsd: NS.XSD.__base_iri__()
}
) ==
"""
@prefix ex: <#{to_string(EX.__base_iri__())}> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
ex:S1
ex:p1 ex:O1, ex:O2 ;
ex:p2 ex:O3 .
ex:S2
ex:p3 ex:O4 .
"""
2017-08-09 21:24:37 +00:00
end
test "when no prefixes are given, the prefixes from the given graph are used" do
2020-06-29 08:37:42 +00:00
assert Turtle.Encoder.encode!(
Graph.new(
[
{EX.S1, EX.p1(), EX.O1},
{EX.S1, EX.p1(), EX.O2},
{EX.S1, EX.p2(), NS.XSD.integer()},
{EX.S2, EX.p3(), EX.O4}
],
prefixes: %{
"": EX.__base_iri__(),
xsd: NS.XSD.__base_iri__()
}
)
) ==
"""
2020-06-29 08:37:42 +00:00
@prefix : <#{to_string(EX.__base_iri__())}> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
:S1
:p1 :O1, :O2 ;
:p2 xsd:integer .
:S2
:p3 :O4 .
"""
end
test "when no base IRI is given, the base IRI from the given graph is used" do
2020-06-29 08:37:42 +00:00
assert Turtle.Encoder.encode!(
Graph.new([{EX.S1, EX.p1(), EX.O1}],
prefixes: %{},
base_iri: EX.__base_iri__()
)
) ==
"""
2020-06-29 08:37:42 +00:00
@base <#{to_string(EX.__base_iri__())}> .
<S1>
<p1> <O1> .
"""
base_without_hash = "http://example.com/foo"
assert Turtle.Encoder.encode!(
Graph.new(
[
{
RDF.iri(base_without_hash <> "#S1"),
RDF.iri(base_without_hash <> "#p1"),
RDF.iri(base_without_hash <> "#O1")
}
],
prefixes: %{},
base_iri: base_without_hash
)
) ==
"""
@base <#{base_without_hash}> .
<#S1>
<#p1> <#O1> .
"""
end
test "when a base IRI is given, it has used instead of the base IRI of the given graph" do
2020-06-29 08:37:42 +00:00
assert Turtle.Encoder.encode!(
Graph.new([{EX.S1, EX.p1(), EX.O1}],
prefixes: %{},
base_iri: EX.other()
),
base_iri: EX
2020-06-29 08:37:42 +00:00
) ==
"""
2020-06-29 08:37:42 +00:00
@base <#{to_string(EX.__base_iri__())}> .
<S1>
<p1> <O1> .
"""
end
test "when no prefixes are given and no prefixes are in the given graph the default_prefixes are used" do
2020-06-29 08:37:42 +00:00
assert Turtle.Encoder.encode!(Graph.new({EX.S, EX.p(), NS.XSD.string()})) ==
"""
2020-06-29 08:37:42 +00:00
@prefix rdf: <#{to_string(RDF.__base_iri__())}> .
@prefix rdfs: <#{to_string(RDFS.__base_iri__())}> .
@prefix xsd: <#{to_string(NS.XSD.__base_iri__())}> .
<http://example.org/#S>
<http://example.org/#p> xsd:string .
"""
end
2017-08-09 21:24:37 +00:00
test "statements with empty prefixed names" do
2020-06-29 08:37:42 +00:00
assert Turtle.Encoder.encode!(Graph.new({EX.S, EX.p(), EX.O}),
prefixes: %{"" => EX.__base_iri__()}
) ==
"""
@prefix : <#{to_string(EX.__base_iri__())}> .
2017-08-09 21:24:37 +00:00
2020-06-29 08:37:42 +00:00
:S
:p :O .
"""
2017-08-09 21:24:37 +00:00
end
test "statements with literals" do
2020-06-29 08:37:42 +00:00
assert Turtle.Encoder.encode!(
Graph.new([
{EX.S1, EX.p1(), ~L"foo"},
{EX.S1, EX.p1(), ~L"foo"en},
{EX.S2, EX.p2(), RDF.literal("strange things", datatype: EX.custom())}
]),
prefixes: %{}
) ==
"""
<http://example.org/#S1>
<http://example.org/#p1> "foo"@en, "foo" .
2017-08-09 21:24:37 +00:00
2020-06-29 08:37:42 +00:00
<http://example.org/#S2>
<http://example.org/#p2> "strange things"^^<#{EX.custom()}> .
"""
2017-08-09 21:24:37 +00:00
end
test "statements with blank nodes" do
2020-06-29 08:37:42 +00:00
assert Turtle.Encoder.encode!(
Graph.new([
{EX.S1, EX.p1(), [RDF.bnode(1), RDF.bnode("foo"), RDF.bnode(:bar)]},
{EX.S2, EX.p1(), [RDF.bnode(1), RDF.bnode("foo"), RDF.bnode(:bar)]}
]),
prefixes: %{}
) ==
"""
<http://example.org/#S1>
<http://example.org/#p1> _:b1, _:bar, _:foo .
2017-08-09 21:24:37 +00:00
2020-06-29 08:37:42 +00:00
<http://example.org/#S2>
<http://example.org/#p1> _:b1, _:bar, _:foo .
2020-06-29 08:37:42 +00:00
"""
2017-08-09 21:24:37 +00:00
end
test "ordering of descriptions" do
2020-06-29 08:37:42 +00:00
assert Turtle.Encoder.encode!(
Graph.new([
{EX.__base_iri__(), RDF.type(), OWL.Ontology},
{EX.S1, RDF.type(), EX.O},
{EX.S2, RDF.type(), RDFS.Class},
{EX.S3, RDF.type(), RDF.Property}
]),
base_iri: EX.__base_iri__(),
prefixes: %{
rdf: RDF.__base_iri__(),
rdfs: RDFS.__base_iri__(),
owl: OWL.__base_iri__()
}
) ==
"""
@base <#{to_string(EX.__base_iri__())}> .
@prefix owl: <#{to_string(OWL.__base_iri__())}> .
2020-06-29 08:37:42 +00:00
@prefix rdf: <#{to_string(RDF.__base_iri__())}> .
@prefix rdfs: <#{to_string(RDFS.__base_iri__())}> .
<>
a owl:Ontology .
<S2>
a rdfs:Class .
<S1>
a <O> .
<S3>
a rdf:Property .
"""
end
test "directive_style option" do
assert Turtle.Encoder.encode!(Graph.new({EX.S, RDFS.subClassOf(), EX.O}),
prefixes: %{rdfs: RDFS.__base_iri__()},
base_iri: EX.__base_iri__(),
directive_style: :turtle
) ==
"""
@base <#{to_string(EX.__base_iri__())}> .
@prefix rdfs: <#{to_string(RDFS.__base_iri__())}> .
<S>
rdfs:subClassOf <O> .
"""
assert Turtle.Encoder.encode!(Graph.new({EX.S, RDFS.subClassOf(), EX.O}),
prefixes: %{rdfs: RDFS.__base_iri__()},
base_iri: EX.__base_iri__(),
directive_style: :sparql
) ==
"""
BASE <#{to_string(EX.__base_iri__())}>
PREFIX rdfs: <#{to_string(RDFS.__base_iri__())}>
<S>
rdfs:subClassOf <O> .
"""
end
test "partial document" do
graph =
Graph.new({EX.S, RDFS.subClassOf(), EX.O},
prefixes: %{rdfs: RDFS.__base_iri__()},
base_iri: EX.__base_iri__()
)
assert Turtle.Encoder.encode!(graph, only: :triples) ==
"""
<S>
rdfs:subClassOf <O> .
"""
assert Turtle.Encoder.encode!(graph, only: :prefixes) ==
"""
@prefix rdfs: <#{to_string(RDFS.__base_iri__())}> .
"""
assert Turtle.Encoder.encode!(graph, only: :base) ==
"""
@base <#{to_string(EX.__base_iri__())}> .
"""
assert Turtle.Encoder.encode!(graph, only: :directives, directive_style: :sparql) ==
"""
BASE <#{to_string(EX.__base_iri__())}>
PREFIX rdfs: <#{to_string(RDFS.__base_iri__())}>
"""
assert_raise RuntimeError, "unknown Turtle document element: :undefined", fn ->
Turtle.Encoder.encode!(graph, only: :undefined)
end
end
test "indent option" do
graph =
Graph.new(
[
{EX.S, RDFS.subClassOf(), EX.O},
{RDF.bnode("foo"), EX.p(), EX.O2}
],
prefixes: %{rdfs: RDFS.__base_iri__()},
base_iri: EX.__base_iri__()
)
assert Turtle.Encoder.encode!(graph, indent: 2) ==
"""
@base <#{to_string(EX.__base_iri__())}> .
@prefix rdfs: <#{to_string(RDFS.__base_iri__())}> .
<S>
rdfs:subClassOf <O> .
[
<p> <O2>
] .
"""
end
2017-08-09 21:24:37 +00:00
end
2020-11-02 14:59:14 +00:00
test "serializing a description" do
description = EX.S |> EX.p(EX.O)
assert Turtle.Encoder.encode!(description) ==
description |> Graph.new() |> Turtle.Encoder.encode!()
end
describe "serializing a dataset" do
test "prefixes of the graphs are merged properly" do
dataset =
RDF.Dataset.new()
|> RDF.Dataset.add(
Graph.new(
[
{EX.__base_iri__(), RDF.type(), OWL.Ontology},
{EX.S1, RDF.type(), EX.O}
],
base_iri: EX.__base_iri__(),
prefixes: %{
rdf: RDF,
owl: OWL
}
)
)
|> RDF.Dataset.add(
Graph.new(
{EX.S3, EX.p(), EX.O},
name: EX.Graph1,
prefixes: %{
ex: EX,
rdf: RDF
}
)
)
|> RDF.Dataset.add(
Graph.new(
{~I<http://other.example.com/S2>, RDF.type(), RDFS.Class},
name: EX.Graph2,
prefixes: %{
ex: "http://other.example.com/",
rdf: RDF,
rdfs: RDFS
}
)
)
assert Turtle.Encoder.encode!(dataset) ==
"""
@prefix ex: <http://example.org/#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
<http://other.example.com/S2>
a rdfs:Class .
ex:
a owl:Ontology .
ex:S1
a ex:O .
ex:S3
ex:p ex:O .
"""
end
test "when none of the graphs uses prefixes the default prefixes are used" do
assert RDF.Dataset.new({EX.S, EX.p(), EX.O})
|> Turtle.Encoder.encode!() ==
"""
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
<http://example.org/#S>
<http://example.org/#p> <http://example.org/#O> .
"""
end
end
2017-08-09 21:24:37 +00:00
describe "prefixed_name/2" do
setup do
{:ok,
prefixes:
PrefixMap.new(
ex: EX,
ex2: ~I<http://example.org/>
)}
2017-08-09 21:24:37 +00:00
end
test "hash iri with existing prefix", %{prefixes: prefixes} do
2020-06-29 08:37:42 +00:00
assert Turtle.Encoder.prefixed_name(EX.foo(), prefixes) ==
"ex:foo"
2017-08-09 21:24:37 +00:00
end
test "hash iri namespace without name", %{prefixes: prefixes} do
2020-06-29 08:37:42 +00:00
assert Turtle.Encoder.prefixed_name(RDF.iri(EX.__base_iri__()), prefixes) ==
"ex:"
2017-08-09 21:24:37 +00:00
end
test "hash iri with non-existing prefix" do
refute Turtle.Encoder.prefixed_name(EX.foo(), PrefixMap.new())
2017-08-09 21:24:37 +00:00
end
test "slash iri with existing prefix", %{prefixes: prefixes} do
2017-08-09 21:24:37 +00:00
assert Turtle.Encoder.prefixed_name(~I<http://example.org/foo>, prefixes) ==
2020-06-29 08:37:42 +00:00
"ex2:foo"
2017-08-09 21:24:37 +00:00
end
test "slash iri namespace without name", %{prefixes: prefixes} do
2017-08-09 21:24:37 +00:00
assert Turtle.Encoder.prefixed_name(~I<http://example.org/>, prefixes) ==
2020-06-29 08:37:42 +00:00
"ex2:"
2017-08-09 21:24:37 +00:00
end
test "slash iri with non-existing prefix" do
refute Turtle.Encoder.prefixed_name(~I<http://example.org/foo>, PrefixMap.new())
2017-08-09 21:24:37 +00:00
end
end
%{
"full IRIs without base" => %{
2017-08-09 21:24:37 +00:00
input: "<http://a/b> <http://a/c> <http://a/d> .",
2020-06-29 08:37:42 +00:00
matches: [~r(<http://a/b>\s+<http://a/c>\s+<http://a/d>\s+\.)]
2017-08-09 21:24:37 +00:00
},
"relative IRIs with base" => %{
2017-08-09 21:24:37 +00:00
input: "<http://a/b> <http://a/c> <http://a/d> .",
2020-06-29 08:37:42 +00:00
matches: [~r(@base\s+<http://a/>\s+\.), ~r(<b>\s+<c>\s+<d>\s+\.)m],
base_iri: "http://a/"
2017-08-09 21:24:37 +00:00
},
"pname IRIs with prefix" => %{
2017-08-09 21:24:37 +00:00
input: "<http://example.com/b> <http://example.com/c> <http://example.com/d> .",
matches: [
~r(@prefix\s+ex:\s+<http://example.com/>\s+\.),
~r(ex:b\s+ex:c\s+ex:d\s+\.)
],
prefixes: %{ex: "http://example.com/"}
},
"pname IRIs with empty prefix" => %{
2017-08-09 21:24:37 +00:00
input: "<http://example.com/b> <http://example.com/c> <http://example.com/d> .",
2020-06-29 08:37:42 +00:00
matches: [
2017-08-09 21:24:37 +00:00
~r(@prefix\s+:\s+<http://example.com/>\s+\.),
~r(:b\s+:c\s+:d\s+\.)
],
prefixes: %{"" => "http://example.com/"}
},
"object list" => %{
input: "@prefix ex: <http://example.com/> . ex:b ex:c ex:d, ex:e .",
matches: [
~r(@prefix\s+ex:\s+<http://example.com/>\s+\.),
2020-06-29 08:37:42 +00:00
~r(ex:b\s+ex:c\s+ex:[de],\s++ex:[de]\s+\.)m
2017-08-09 21:24:37 +00:00
],
prefixes: %{"ex" => "http://example.com/"}
},
"property list" => %{
input: "@prefix ex: <http://example.com/> . ex:b ex:c ex:d; ex:e ex:f .",
matches: [
~r(@prefix\s+ex:\s+<http://example.com/>\s+\.),
~r(ex:b\s+ex:c\s+ex:d\s+;),
~r(\s++ex:e\s+ex:f\s+\.)
],
prefixes: %{"ex" => "http://example.com/"}
},
"reuses BNode labels by default" => %{
input: "@prefix ex: <http://example.com/> . _:a ex:b _:a .",
matches: [~r(\s*_:a\s+ex:b\s+_:a\s+\.)],
prefixes: %{"ex" => "http://example.com/"}
},
"bare anon" => %{
input: "@prefix ex: <http://example.com/> . [ex:a ex:b] .",
matches: [~r(^\[\s*ex:a\s+ex:b\s\]\s+\.)m],
prefixes: %{"ex" => "http://example.com/"}
},
"anon as subject" => %{
input: "@prefix ex: <http://example.com/> . [ex:a ex:b] ex:c ex:d .",
matches: [
~r(\[\s*ex:a\s+ex:b\s*;)m,
~r(\sex:c\s+ex:d\s*\]\s+\.)m
],
prefixes: %{"ex" => "http://example.com/"}
},
"anon as object" => %{
input: "@prefix ex: <http://example.com/> . ex:a ex:b [ex:c ex:d] .",
matches: [~r(ex:a\s+ex:b\s+\[\s*ex:c\s+ex:d\s*\]\s+\.)],
neg_matches: [~r(_:\w+\s+\s*ex:c\s+ex:d\s+\.)],
prefixes: %{"ex" => "http://example.com/"}
},
2020-06-29 08:37:42 +00:00
# "generated BNodes with :unique_bnodes" => %{
# input: "@prefix ex: <http://example.com/> . _:a ex:b _:a .",
# matches: [~r(^\s+*_:g\w+\s+ex:b\s+_:g\w+\s+\.$)],
# unique_bnodes: true
# },
# "standard prefixes" => %{
# input: """
# <a> a <http://xmlns.com/foaf/0.1/Person>;
# <http://purl.org/dc/terms/title> "Person" .
# """,
# matches: [
# ~r(^@prefix foaf: <http://xmlns.com/foaf/0.1/> \.$),
# ~r(^@prefix dc: <http://purl.org/dc/terms/> \.$),
# ~r(^<a> a foaf:Person;$),
# ~r(dc:title "Person" \.$),
# ],
# standard_prefixes: true, prefixes: %{}
# }
"order properties" => %{
input: """
@prefix ex: <http://example.com/> .
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
ex:b ex:c ex:d .
ex:b dc:title "title" .
ex:b a ex:class .
ex:b rdfs:label "label" .
""",
matches: [
~r(ex:b\s+a\s+ex:class\s*;)m,
~r(ex:class\s*;\s+rdfs:label\s+"label")m,
~r("label"\s*;\s++ex:c\s+ex:d)m,
~r(ex:d\s*;\s+dc:title\s+"title"\s+\.)m
],
prefixes: %{
2020-06-29 08:37:42 +00:00
"ex" => "http://example.com/",
"dc" => "http://purl.org/dc/elements/1.1/",
"rdfs" => "http://www.w3.org/2000/01/rdf-schema#"
}
2020-06-29 08:37:42 +00:00
}
2017-08-09 21:24:37 +00:00
}
|> Enum.each(fn {name, data} ->
2020-06-29 08:37:42 +00:00
@tag data: data
test name, %{data: data} do
assert_serialization(Turtle.read_string!(data.input), Keyword.new(data))
end
end)
2017-08-09 21:24:37 +00:00
describe "lists" do
test "should generate literal list" do
Turtle.read_string!(
~s[@prefix ex: <http://example.com/> . ex:a ex:b ( "apple" "banana" ) .]
)
|> assert_serialization(
2020-06-29 08:37:42 +00:00
prefixes: %{ex: ~I<http://example.com/>},
matches: [
{~r[ex:a\s+ex:b\s+\("apple" "banana"\)\s+\.],
"doesn't include the list as a Turtle list"}
]
)
2017-08-09 21:24:37 +00:00
end
test "should generate empty list" do
2020-06-29 08:37:42 +00:00
Turtle.read_string!(~s[@prefix ex: <http://example.com/> . ex:a ex:b () .])
2017-08-09 21:24:37 +00:00
|> assert_serialization(
2020-06-29 08:37:42 +00:00
prefixes: %{ex: ~I<http://example.com/>},
matches: [
{~r[ex:a\s+ex:b\s+\(\)\s+\.], "doesn't include the list as a Turtle list"}
]
)
2017-08-09 21:24:37 +00:00
end
test "should generate empty list as subject" do
2020-06-29 08:37:42 +00:00
Turtle.read_string!(~s[@prefix ex: <http://example.com/> . () ex:a ex:b .])
2017-08-09 21:24:37 +00:00
|> assert_serialization(
2020-06-29 08:37:42 +00:00
prefixes: %{ex: ~I<http://example.com/>},
matches: [
{~r[\(\)\s+ex:a\s+ex:b\s+\.], "doesn't include the list as a Turtle list"}
]
)
2017-08-09 21:24:37 +00:00
end
test "should generate list as subject" do
2020-06-29 08:37:42 +00:00
Turtle.read_string!(~s[@prefix ex: <http://example.com/> . (ex:a) ex:b ex:c .])
2017-08-09 21:24:37 +00:00
|> assert_serialization(
2020-06-29 08:37:42 +00:00
prefixes: %{ex: ~I<http://example.com/>},
matches: [
{~r[\(ex:a\)\s+ex:b\s+ex:c\s+\.], "doesn't include the list as a Turtle list"}
]
)
2017-08-09 21:24:37 +00:00
end
test "should generate list of empties" do
2020-06-29 08:37:42 +00:00
graph =
Turtle.read_string!(~s{@prefix ex: <http://example.com/> . [ex:listOf2Empties (() ())] .})
2017-08-09 21:24:37 +00:00
serialization =
2020-06-29 08:37:42 +00:00
assert_serialization(graph,
prefixes: %{ex: ~I<http://example.com/>},
matches: [
{~r[\[\s*ex:listOf2Empties \(\(\) \(\)\)\s\]\s+\.],
"doesn't include the list as a Turtle list"}
]
)
refute String.contains?(serialization, to_string(RDF.first())),
~s[output\n\n#{serialization}\n\ncontains #{to_string(RDF.first())}]
2017-08-09 21:24:37 +00:00
2020-06-29 08:37:42 +00:00
refute String.contains?(serialization, to_string(RDF.rest())),
~s[output\n\n#{serialization}\n\ncontains #{to_string(RDF.rest())}]
2017-08-09 21:24:37 +00:00
end
test "should generate list anon" do
Turtle.read_string!(
~s{@prefix ex: <http://example.com/> . [ex:twoAnons ([a ex:mother] [a ex:father])] .}
)
|> assert_serialization(
2020-06-29 08:37:42 +00:00
prefixes: %{ex: ~I<http://example.com/>},
matches: [
{~r[\[\s*ex:twoAnons \(\[\s*a ex:mother\s*\]\s+\[\s*a ex:father\s*\]\s*\)\s*\]\s+\.],
"doesn't include the list as a Turtle list"}
]
)
end
# TODO: Why should this test from RDF.rb work? Why should the `a owl:Class` statements about the list nodes be ignored?
# test "should generate owl:unionOf list" do
# Turtle.read_string!("""
# @prefix ex: <http://example.com/> .
# @prefix owl: <http://www.w3.org/2002/07/owl#> .
# @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
# @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
# ex:a rdfs:domain [
# a owl:Class;
# owl:unionOf [
# a owl:Class;
# rdf:first ex:b;
# rdf:rest [
# a owl:Class;
# rdf:first ex:c;
# rdf:rest rdf:nil
# ]
# ]
# ] .
# """)
# |> assert_serialization(
# prefixes: %{
# ex: ~I<http://example.com/>,
# rdf: RDF.NS.RDF.__base_iri__,
# rdfs: RDFS.__base_iri__,
# owl: OWL.__base_iri__,
# },
# matches: [
# {~r[ex:a\s+rdfs:domain \[\s+a owl:Class;\s+owl:unionOf\s+\(ex:b\s+ex:c\)\s*\]\s*\.],
# "doesn't include the list as a Turtle list"}
# ]
# )
#
# end
2017-08-09 21:24:37 +00:00
test "when one of the list nodes is referenced in other statements the whole list is not represented as a Turtle list structure" do
Graph.new(
2020-06-29 08:37:42 +00:00
~B<Foo>
|> RDF.first(EX.Foo)
|> RDF.rest(~B<Bar>)
)
2017-08-09 21:24:37 +00:00
|> Graph.add(
2020-06-29 08:37:42 +00:00
~B<Bar>
|> RDF.first(EX.Bar)
|> RDF.rest(RDF.nil())
)
|> Graph.add({EX.Baz, EX.quux(), ~B<Bar>})
2017-08-09 21:24:37 +00:00
|> assert_serialization(
2020-06-29 08:37:42 +00:00
prefixes: %{ex: EX.__base_iri__()},
# TODO: provide a positive match
neg_matches: [
{~r[\(\s*ex:Foo\s+ex:Bar\s*\)], "does include the list as a Turtle list"}
]
)
2017-08-09 21:24:37 +00:00
end
test "when given an invalid list" do
Graph.new(
2020-06-29 08:37:42 +00:00
~B<Foo>
|> RDF.first(1)
|> RDF.rest(EX.Foo)
2017-08-09 21:24:37 +00:00
)
|> assert_serialization(
2020-06-29 08:37:42 +00:00
prefixes: %{ex: ~I<http://example.com/>},
# TODO: provide a positive match
neg_matches: [
{~r[\[\s*_:Foo \(\(\) \(\)\)\]\s+\.], "does include the invalid list as a Turtle list"}
]
)
2017-08-09 21:24:37 +00:00
end
end
describe "literals" do
test "plain literals with newlines embedded are encoded with long quotes" do
2020-06-29 08:37:42 +00:00
Turtle.read_string!(~s[<http://a> <http:/b> """testing string parsing in Turtle.
""" .])
|> assert_serialization(matches: [~s["""testing string parsing in Turtle.\n]])
2017-08-09 21:24:37 +00:00
end
test "plain literals escaping" do
2020-06-29 08:37:42 +00:00
Turtle.read_string!(~s[<http://a> <http:/b> """string with " escaped quote marks""" .])
2017-08-09 21:24:37 +00:00
|> assert_serialization(
2020-06-29 08:37:42 +00:00
matches: [
~r[string with \\" escaped quote mark]
]
)
2017-08-09 21:24:37 +00:00
end
test "language tagged literals specifies language for literal with language" do
Turtle.read_string!(~s[<http://a> <http:/b> "string"@en .])
|> assert_serialization(matches: [~r["string"@en]])
end
test "typed literals" do
Turtle.read_string!(
~s[@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . <http://a> <http:/b> "http://foo/"^^xsd:anyURI .]
)
|> assert_serialization(
2020-06-29 08:37:42 +00:00
matches: [
~r["http://foo/"\^\^<http://www.w3.org/2001/XMLSchema#anyURI> \.]
]
)
2017-08-09 21:24:37 +00:00
end
test "typed literals use declared prefixes" do
Turtle.read_string!(
~s[@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . <http://a> <http:/b> "http://foo/"^^xsd:anyURI .]
)
|> assert_serialization(
2020-06-29 08:37:42 +00:00
matches: [
~r[@prefix xsd: <http://www.w3.org/2001/XMLSchema#> \.],
~r["http://foo/"\^\^xsd:anyURI \.]
],
prefixes: %{xsd: NS.XSD.__base_iri__()}
)
2017-08-09 21:24:37 +00:00
end
test "valid booleans" do
[
2020-06-29 08:37:42 +00:00
{true, "true ."},
{"true", "true ."},
{"1", "true ."},
{false, "false ."},
2017-08-09 21:24:37 +00:00
{"false", "false ."},
2020-06-29 08:37:42 +00:00
{"0", "false ."}
2017-08-09 21:24:37 +00:00
]
|> Enum.each(fn {value, output} ->
2020-06-29 08:37:42 +00:00
Graph.new({EX.S, EX.p(), RDF.XSD.boolean(value)})
|> assert_serialization(matches: [output])
end)
2017-08-09 21:24:37 +00:00
end
test "invalid booleans" do
[
{"string", ~s{"string"^^<http://www.w3.org/2001/XMLSchema#boolean>}},
2020-06-29 08:37:42 +00:00
{"42", ~s{"42"^^<http://www.w3.org/2001/XMLSchema#boolean>}},
{"TrUe", ~s{"TrUe"^^<http://www.w3.org/2001/XMLSchema#boolean>}},
{"FaLsE", ~s{"FaLsE"^^<http://www.w3.org/2001/XMLSchema#boolean>}}
2017-08-09 21:24:37 +00:00
]
|> Enum.each(fn {value, output} ->
2020-06-29 08:37:42 +00:00
Graph.new({EX.S, EX.p(), RDF.XSD.boolean(value)})
|> assert_serialization(matches: [output])
end)
2017-08-09 21:24:37 +00:00
end
test "valid integers" do
[
2020-06-29 08:37:42 +00:00
{0, "0 ."},
{"0", "0 ."},
{1, "1 ."},
{"1", "1 ."},
{-1, "-1 ."},
{"-1", "-1 ."},
{10, "10 ."},
{"10", "10 ."},
{"0010", "10 ."}
2017-08-09 21:24:37 +00:00
]
|> Enum.each(fn {value, output} ->
2020-06-29 08:37:42 +00:00
Graph.new({EX.S, EX.p(), RDF.XSD.integer(value)})
|> assert_serialization(matches: [output])
end)
2017-08-09 21:24:37 +00:00
end
test "invalid integers" do
[
{"string", ~s{"string"^^<http://www.w3.org/2001/XMLSchema#integer>}},
2020-06-29 08:37:42 +00:00
{"true", ~s{"true"^^<http://www.w3.org/2001/XMLSchema#integer>}}
2017-08-09 21:24:37 +00:00
]
|> Enum.each(fn {value, output} ->
2020-06-29 08:37:42 +00:00
Graph.new({EX.S, EX.p(), RDF.XSD.integer(value)})
|> assert_serialization(matches: [output])
end)
2017-08-09 21:24:37 +00:00
end
test "valid decimals" do
[
2020-06-29 08:37:42 +00:00
{1.0, "1.0 ."},
{"1.0", "1.0 ."},
{0.1, "0.1 ."},
{"0.1", "0.1 ."},
{-1, "-1.0 ."},
{"-1", "-1.0 ."},
{10.02, "10.02 ."},
{"10.02", "10.02 ."},
{"010.020", "10.02 ."}
2017-08-09 21:24:37 +00:00
]
|> Enum.each(fn {value, output} ->
2020-06-29 08:37:42 +00:00
Graph.new({EX.S, EX.p(), RDF.XSD.decimal(value)})
|> assert_serialization(matches: [output])
end)
2017-08-09 21:24:37 +00:00
end
test "invalid decimals" do
[
{"string", ~s{"string"^^<http://www.w3.org/2001/XMLSchema#decimal>}},
2020-06-29 08:37:42 +00:00
{"true", ~s{"true"^^<http://www.w3.org/2001/XMLSchema#decimal>}}
2017-08-09 21:24:37 +00:00
]
|> Enum.each(fn {value, output} ->
2020-06-29 08:37:42 +00:00
Graph.new({EX.S, EX.p(), RDF.XSD.decimal(value)})
|> assert_serialization(matches: [output])
end)
2017-08-09 21:24:37 +00:00
end
test "valid doubles" do
[
2020-06-29 08:37:42 +00:00
{1.0e1, "1.0E1 ."},
{"1.0e1", "1.0E1 ."},
{0.1e1, "1.0E0 ."},
{"0.1e1", "1.0E0 ."},
{10.02e1, "1.002E2 ."},
{"10.02e1", "1.002E2 ."},
{"010.020", "1.002E1 ."},
{14, "1.4E1 ."},
{-1, "-1.0E0 ."},
{"-1", "-1.0E0 ."}
2017-08-09 21:24:37 +00:00
]
|> Enum.each(fn {value, output} ->
2020-06-29 08:37:42 +00:00
Graph.new({EX.S, EX.p(), RDF.XSD.double(value)})
|> assert_serialization(matches: [output])
end)
2017-08-09 21:24:37 +00:00
end
test "invalid doubles" do
[
{"string", ~s{"string"^^<http://www.w3.org/2001/XMLSchema#double>}},
2020-06-29 08:37:42 +00:00
{"true", ~s{"true"^^<http://www.w3.org/2001/XMLSchema#double>}}
2017-08-09 21:24:37 +00:00
]
|> Enum.each(fn {value, output} ->
2020-06-29 08:37:42 +00:00
Graph.new({EX.S, EX.p(), RDF.XSD.double(value)})
|> assert_serialization(matches: [output])
end)
2017-08-09 21:24:37 +00:00
end
end
describe "W3C test suite roundtrip" do
@tag skip: "TODO: We need a Graph isomorphism comparison to implement this."
test "..."
end
defp assert_serialization(graph, opts) do
2020-06-29 08:37:42 +00:00
with prefixes = Keyword.get(opts, :prefixes, %{}),
base_iri = Keyword.get(opts, :base_iri),
matches = Keyword.get(opts, :matches, []),
neg_matches = Keyword.get(opts, :neg_matches, []) do
2017-08-09 21:24:37 +00:00
assert {:ok, serialized} =
2020-06-29 08:37:42 +00:00
Turtle.write_string(graph, prefixes: prefixes, base_iri: base_iri)
2017-08-09 21:24:37 +00:00
matches
|> Stream.map(fn
2020-06-29 08:37:42 +00:00
{pattern, message} ->
{pattern, ~s[output\n\n#{serialized}\n\n#{message}]}
pattern ->
{pattern, ~s[output\n\n#{serialized}\n\ndoesn't include #{inspect(pattern)}]}
end)
2017-08-09 21:24:37 +00:00
|> Enum.each(fn
2020-06-29 08:37:42 +00:00
{%Regex{} = pattern, message} ->
assert Regex.match?(pattern, serialized), message
{contents, message} ->
assert String.contains?(serialized, contents), message
end)
2017-08-09 21:24:37 +00:00
neg_matches
|> Stream.map(fn
2020-06-29 08:37:42 +00:00
{pattern, message} ->
{pattern, ~s[output\n\n#{serialized}\n\n#{message}]}
pattern ->
{pattern, ~s[output\n\n#{serialized}\n\ndoes include #{inspect(pattern)}]}
end)
2017-08-09 21:24:37 +00:00
|> Enum.each(fn
2020-06-29 08:37:42 +00:00
{%Regex{} = pattern, message} ->
refute Regex.match?(pattern, serialized), message
{contents, message} ->
refute String.contains?(serialized, contents), message
end)
2017-08-09 21:24:37 +00:00
serialized
end
end
end