rdf-ex/test/unit/turtle_decoder_test.exs

432 lines
16 KiB
Elixir
Raw Normal View History

2017-07-05 21:27:55 +00:00
defmodule RDF.Turtle.DecoderTest do
use ExUnit.Case, async: false
doctest RDF.Turtle.Decoder
import RDF.Sigils
alias RDF.{Turtle, Graph, NS, XSD}
2017-07-05 21:27:55 +00:00
use RDF.Vocabulary.Namespace
2020-06-29 08:37:42 +00:00
defvocab EX, base_iri: "http://example.org/#", terms: [], strict: false
2017-07-05 21:27:55 +00:00
2020-06-29 08:37:42 +00:00
defvocab P, base_iri: "http://www.perceive.net/schemas/relationship/", terms: [], strict: false
2017-07-05 21:27:55 +00:00
test "an empty string is deserialized to an empty graph" do
2020-06-29 08:37:42 +00:00
assert Turtle.Decoder.decode!("") == Graph.new()
assert Turtle.Decoder.decode!(" \n\r\r\n ") == Graph.new()
2017-07-05 21:27:55 +00:00
end
2017-07-07 15:56:48 +00:00
test "comments" do
2020-06-29 08:37:42 +00:00
assert Turtle.Decoder.decode!("# just a comment") == Graph.new()
2017-07-05 21:27:55 +00:00
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
<http://example.org/#S> <http://example.org/#p> _:1 . # a comment
""") == Graph.new({EX.S, EX.p(), RDF.bnode("1")})
2017-07-05 21:27:55 +00:00
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
# a comment
<http://example.org/#S> <http://example.org/#p> <http://example.org/#O> .
""") == Graph.new({EX.S, EX.p(), EX.O})
2017-07-05 21:27:55 +00:00
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
<http://example.org/#S> <http://example.org/#p> <http://example.org/#O> .
# a comment
""") == Graph.new({EX.S, EX.p(), EX.O})
2017-07-05 21:27:55 +00:00
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
# Header line 1
# Header line 2
<http://example.org/#S1> <http://example.org/#p1> <http://example.org/#O1> .
# 1st comment
<http://example.org/#S1> <http://example.org/#p2> <http://example.org/#O2> . # 2nd comment
# last comment
""") ==
Graph.new([
{EX.S1, EX.p1(), EX.O1},
{EX.S1, EX.p2(), EX.O2}
])
2017-07-05 21:27:55 +00:00
end
test "empty lines" do
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
<http://example.org/#spiderman> <http://www.perceive.net/schemas/relationship/enemyOf> <http://example.org/#green_goblin> .
""") == Graph.new({EX.spiderman(), P.enemyOf(), EX.green_goblin()})
2017-07-05 21:27:55 +00:00
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
<http://example.org/#spiderman> <http://www.perceive.net/schemas/relationship/enemyOf> <http://example.org/#green_goblin> .
2017-07-05 21:27:55 +00:00
2020-06-29 08:37:42 +00:00
""") == Graph.new({EX.spiderman(), P.enemyOf(), EX.green_goblin()})
2017-07-05 21:27:55 +00:00
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
<http://example.org/#S1> <http://example.org/#p1> <http://example.org/#O1> .
2017-07-05 21:27:55 +00:00
2020-06-29 08:37:42 +00:00
<http://example.org/#S1> <http://example.org/#p2> <http://example.org/#O2> .
2017-07-05 21:27:55 +00:00
2020-06-29 08:37:42 +00:00
""") ==
Graph.new([
{EX.S1, EX.p1(), EX.O1},
{EX.S1, EX.p2(), EX.O2}
])
2017-07-05 21:27:55 +00:00
end
2017-07-07 15:56:48 +00:00
describe "statements" do
test "a N-Triple-style statement" do
assert Turtle.Decoder.decode!(
2020-06-29 08:37:42 +00:00
"<http://example.org/#Aaron> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/#Person> ."
) == Graph.new({EX.Aaron, RDF.type(), EX.Person})
2017-07-07 15:56:48 +00:00
end
test "a statement with the 'a' keyword" do
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
<http://example.org/#Aaron> a <http://example.org/#Person> .
""") == Graph.new({EX.Aaron, RDF.type(), EX.Person})
2017-07-07 15:56:48 +00:00
end
test "multiple N-Triple-style statement" do
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
<http://example.org/#S1> <http://example.org/#p1> <http://example.org/#O1> .
<http://example.org/#S1> <http://example.org/#p2> <http://example.org/#O2> .
""") ==
Graph.new([
{EX.S1, EX.p1(), EX.O1},
{EX.S1, EX.p2(), EX.O2}
])
assert Turtle.Decoder.decode!("""
<http://example.org/#S1> <http://example.org/#p1> <http://example.org/#O1> .
<http://example.org/#S1> <http://example.org/#p2> <http://example.org/#O2> .
<http://example.org/#S2> <http://example.org/#p3> <http://example.org/#O3> .
""") ==
Graph.new([
{EX.S1, EX.p1(), EX.O1},
{EX.S1, EX.p2(), EX.O2},
{EX.S2, EX.p3(), EX.O3}
])
2017-07-07 15:56:48 +00:00
end
test "statement with multiple objects" do
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
<http://example.org/#Foo> <http://example.org/#bar> "baz", 1, true .
""") ==
Graph.new([
{EX.Foo, EX.bar(), "baz"},
{EX.Foo, EX.bar(), 1},
{EX.Foo, EX.bar(), true}
])
2017-07-07 15:56:48 +00:00
end
test "statement with multiple predications" do
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
<http://example.org/#Foo> <http://example.org/#bar> "baz";
<http://example.org/#baz> 42 .
""") ==
Graph.new([
{EX.Foo, EX.bar(), "baz"},
{EX.Foo, EX.baz(), 42}
])
2017-07-07 15:56:48 +00:00
end
end
describe "blank node property lists" do
2017-07-09 23:46:33 +00:00
test "blank node property list on object position" do
2017-07-07 15:56:48 +00:00
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
<http://example.org/#Foo> <http://example.org/#bar> [ <http://example.org/#baz> 42 ] .
""") ==
Graph.new([
{EX.Foo, EX.bar(), RDF.bnode("b0")},
{RDF.bnode("b0"), EX.baz(), 42}
])
2017-07-07 15:56:48 +00:00
end
2017-07-09 23:46:33 +00:00
test "blank node property list on subject position" do
2017-07-07 15:56:48 +00:00
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
[ <http://example.org/#baz> 42 ] <http://example.org/#bar> false .
""") ==
Graph.new([
{RDF.bnode("b0"), EX.baz(), 42},
{RDF.bnode("b0"), EX.bar(), false}
])
2017-07-07 15:56:48 +00:00
end
test "a single blank node property list" do
assert Turtle.Decoder.decode!("[ <http://example.org/#foo> 42 ] .") ==
2020-06-29 08:37:42 +00:00
Graph.new([{RDF.bnode("b0"), EX.foo(), 42}])
2017-07-07 15:56:48 +00:00
end
2017-07-09 23:46:33 +00:00
test "nested blank node property list" do
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
[ <http://example.org/#p1> [ <http://example.org/#p2> <http://example.org/#o2> ] ; <http://example.org/#p> <http://example.org/#o> ].
""") ==
Graph.new([
{RDF.bnode("b0"), EX.p1(), RDF.bnode("b1")},
{RDF.bnode("b1"), EX.p2(), EX.o2()},
{RDF.bnode("b0"), EX.p(), EX.o()}
])
2017-07-09 23:46:33 +00:00
end
test "blank node via []" do
2017-07-07 15:56:48 +00:00
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
[] <http://xmlns.com/foaf/0.1/name> "Aaron Swartz" .
""") ==
Graph.new({RDF.bnode("b0"), ~I<http://xmlns.com/foaf/0.1/name>, "Aaron Swartz"})
2017-07-07 15:56:48 +00:00
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
<http://example.org/#Foo> <http://example.org/#bar> [] .
""") == Graph.new({EX.Foo, EX.bar(), RDF.bnode("b0")})
2017-07-07 15:56:48 +00:00
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
<http://example.org/#Foo> <http://example.org/#bar> [ ] .
""") == Graph.new({EX.Foo, EX.bar(), RDF.bnode("b0")})
2017-07-07 15:56:48 +00:00
end
end
test "blank node" do
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
_:foo <http://example.org/#p> <http://example.org/#O> .
""") == Graph.new({RDF.bnode("foo"), EX.p(), EX.O})
2017-07-07 15:56:48 +00:00
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
<http://example.org/#S> <http://example.org/#p> _:1 .
""") == Graph.new({EX.S, EX.p(), RDF.bnode("1")})
2017-07-07 15:56:48 +00:00
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
_:foo <http://example.org/#p> _:bar .
""") == Graph.new({RDF.bnode("foo"), EX.p(), RDF.bnode("bar")})
2017-07-07 15:56:48 +00:00
end
describe "quoted literals" do
test "an untyped string literal" do
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
<http://example.org/#spiderman> <http://www.perceive.net/schemas/relationship/realname> "Peter Parker" .
""") == Graph.new({EX.spiderman(), P.realname(), RDF.literal("Peter Parker")})
2017-07-07 15:56:48 +00:00
end
test "an untyped long quoted string literal" do
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
<http://example.org/#spiderman> <http://www.perceive.net/schemas/relationship/realname> '''Peter Parker''' .
""") == Graph.new({EX.spiderman(), P.realname(), RDF.literal("Peter Parker")})
2017-07-07 15:56:48 +00:00
end
test "a typed literal" do
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
<http://example.org/#spiderman> <http://example.org/#p> "42"^^<http://www.w3.org/2001/XMLSchema#integer> .
""") == Graph.new({EX.spiderman(), EX.p(), RDF.literal(42)})
2017-07-07 15:56:48 +00:00
end
2017-07-09 17:27:21 +00:00
test "a typed literal with type as a prefixed name" do
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
<http://example.org/#spiderman> <http://example.org/#p> "42"^^xsd:integer .
""") ==
Graph.new({EX.spiderman(), EX.p(), RDF.literal(42)}, prefixes: %{xsd: NS.XSD})
2017-07-09 17:27:21 +00:00
end
2017-07-07 15:56:48 +00:00
test "a language tagged literal" do
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
<http://example.org/#S> <http://example.org/#p> "foo"@en .
""") == Graph.new({EX.S, EX.p(), RDF.literal("foo", language: "en")})
2017-07-07 15:56:48 +00:00
end
test "a '@prefix' or '@base' language tagged literal" do
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
<http://example.org/#S> <http://example.org/#p> "foo"@prefix .
""") == Graph.new({EX.S, EX.p(), RDF.literal("foo", language: "prefix")})
2017-07-07 15:56:48 +00:00
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
<http://example.org/#S> <http://example.org/#p> "foo"@base .
""") == Graph.new({EX.S, EX.p(), RDF.literal("foo", language: "base")})
2017-07-07 15:56:48 +00:00
end
end
describe "shorthand literals" do
2017-07-09 23:46:33 +00:00
test "boolean" do
2017-07-07 15:56:48 +00:00
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
<http://example.org/#Foo> <http://example.org/#bar> true .
""") == Graph.new({EX.Foo, EX.bar(), XSD.true()})
2017-07-07 15:56:48 +00:00
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
<http://example.org/#Foo> <http://example.org/#bar> false .
""") == Graph.new({EX.Foo, EX.bar(), XSD.false()})
2017-07-07 15:56:48 +00:00
end
2017-07-09 23:46:33 +00:00
test "integer" do
2017-07-07 15:56:48 +00:00
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
<http://example.org/#Foo> <http://example.org/#bar> 42 .
""") == Graph.new({EX.Foo, EX.bar(), XSD.integer(42)})
2017-07-07 15:56:48 +00:00
end
2017-07-09 23:46:33 +00:00
test "decimal" do
2017-07-07 15:56:48 +00:00
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
<http://example.org/#Foo> <http://example.org/#bar> 3.14 .
""") == Graph.new({EX.Foo, EX.bar(), XSD.decimal("3.14")})
2017-07-07 15:56:48 +00:00
end
2017-07-09 23:46:33 +00:00
test "double" do
2017-07-07 15:56:48 +00:00
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
<http://example.org/#Foo> <http://example.org/#bar> 1.2e3 .
""") == Graph.new({EX.Foo, EX.bar(), XSD.double("1.2e3")})
2017-07-07 15:56:48 +00:00
end
end
describe "prefixed names" do
2017-07-09 23:46:33 +00:00
test "non-empty prefixed names" do
prefixes = RDF.PrefixMap.new(ex: ~I<http://example.org/#>)
2020-06-29 08:37:42 +00:00
2017-07-07 15:56:48 +00:00
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
@prefix ex: <http://example.org/#> .
ex:Aaron <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ex:Person .
""") == Graph.new({EX.Aaron, RDF.type(), EX.Person}, prefixes: prefixes)
2017-07-07 15:56:48 +00:00
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
@prefix ex: <http://example.org/#> .
ex:Aaron <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ex:Person .
""") == Graph.new({EX.Aaron, RDF.type(), EX.Person}, prefixes: prefixes)
2017-07-07 15:56:48 +00:00
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
PREFIX ex: <http://example.org/#>
ex:Aaron <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ex:Person .
""") == Graph.new({EX.Aaron, RDF.type(), EX.Person}, prefixes: prefixes)
2017-07-07 15:56:48 +00:00
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
prefix ex: <http://example.org/#>
ex:Aaron <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ex:Person .
""") == Graph.new({EX.Aaron, RDF.type(), EX.Person}, prefixes: prefixes)
2017-07-07 15:56:48 +00:00
end
2017-07-09 23:46:33 +00:00
test "empty prefixed name" do
prefixes = RDF.PrefixMap.new("": ~I<http://example.org/#>)
2020-06-29 08:37:42 +00:00
2017-07-07 15:56:48 +00:00
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
@prefix : <http://example.org/#> .
:Aaron <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> :Person .
""") == Graph.new({EX.Aaron, RDF.type(), EX.Person}, prefixes: prefixes)
2017-07-07 15:56:48 +00:00
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
PREFIX : <http://example.org/#>
:Aaron <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> :Person .
""") == Graph.new({EX.Aaron, RDF.type(), EX.Person}, prefixes: prefixes)
2017-07-07 15:56:48 +00:00
end
end
describe "collections" do
2017-07-09 23:46:33 +00:00
test "non-empty collection" do
2017-07-07 15:56:48 +00:00
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
@prefix : <http://example.org/#> .
:subject :predicate ( :a :b :c ) .
""") ==
Graph.new(
[
{EX.subject(), EX.predicate(), RDF.bnode("b0")},
{RDF.bnode("b0"), RDF.first(), EX.a()},
{RDF.bnode("b0"), RDF.rest(), RDF.bnode("b1")},
{RDF.bnode("b1"), RDF.first(), EX.b()},
{RDF.bnode("b1"), RDF.rest(), RDF.bnode("b2")},
{RDF.bnode("b2"), RDF.first(), EX.c()},
{RDF.bnode("b2"), RDF.rest(), RDF.nil()}
],
prefixes: %{"": ~I<http://example.org/#>}
)
2017-07-07 15:56:48 +00:00
end
2017-07-09 23:46:33 +00:00
test "empty collection" do
2017-07-07 15:56:48 +00:00
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
@prefix : <http://example.org/#> .
:subject :predicate () .
""") ==
Graph.new({EX.subject(), EX.predicate(), RDF.nil()},
prefixes: %{"": ~I<http://example.org/#>}
)
2017-07-07 15:56:48 +00:00
end
2017-07-09 23:46:33 +00:00
test "nested collection" do
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
@prefix : <http://example.org/#> .
:subject :predicate ( :a (:b :c) ) .
""") ==
Graph.new(
[
{EX.subject(), EX.predicate(), RDF.bnode("b0")},
{RDF.bnode("b0"), RDF.first(), EX.a()},
{RDF.bnode("b0"), RDF.rest(), RDF.bnode("b3")},
{RDF.bnode("b3"), RDF.first(), RDF.bnode("b1")},
{RDF.bnode("b3"), RDF.rest(), RDF.nil()},
{RDF.bnode("b1"), RDF.first(), EX.b()},
{RDF.bnode("b1"), RDF.rest(), RDF.bnode("b2")},
{RDF.bnode("b2"), RDF.first(), EX.c()},
{RDF.bnode("b2"), RDF.rest(), RDF.nil()}
],
prefixes: %{"": ~I<http://example.org/#>}
)
2017-07-09 23:46:33 +00:00
end
2017-07-07 15:56:48 +00:00
end
describe "relative IRIs" do
test "without explicit in-doc base and no document_base option option given" do
assert_raise RuntimeError, fn ->
Turtle.Decoder.decode!(
2020-06-29 08:37:42 +00:00
"<#Aaron> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <#Person> ."
)
end
end
test "without explicit in-doc base, but document_base option given" do
2020-06-29 08:37:42 +00:00
assert Turtle.Decoder.decode!(
"""
<#Aaron> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <#Person> .
""",
base: "http://example.org/"
) ==
Graph.new({EX.Aaron, RDF.type(), EX.Person}, base_iri: ~I<http://example.org/>)
end
test "with @base given" do
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
@base <http://example.org/> .
<#Aaron> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <#Person> .
""") ==
Graph.new({EX.Aaron, RDF.type(), EX.Person}, base_iri: ~I<http://example.org/>)
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
@base <http://example.org/#> .
<#Aaron> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <#Person> .
""") ==
Graph.new({EX.Aaron, RDF.type(), EX.Person}, base_iri: ~I<http://example.org/#>)
end
test "with BASE given" do
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
BASE <http://example.org/>
<#Aaron> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <#Person> .
""") ==
Graph.new({EX.Aaron, RDF.type(), EX.Person}, base_iri: ~I<http://example.org/>)
assert Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
base <http://example.org/#>
<#Aaron> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <#Person> .
""") ==
Graph.new({EX.Aaron, RDF.type(), EX.Person}, base_iri: ~I<http://example.org/#>)
end
test "when a given base is itself relative" do
2017-07-09 17:27:21 +00:00
assert_raise RuntimeError, fn ->
Turtle.Decoder.decode!("""
2020-06-29 08:37:42 +00:00
@base <foo> .
<#Aaron> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <#Person> .
""")
end
2020-06-29 08:37:42 +00:00
assert_raise RuntimeError, fn ->
Turtle.Decoder.decode!(
"<#Aaron> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <#Person> .",
2020-06-29 08:37:42 +00:00
base: "foo"
)
end
end
end
2017-07-05 21:27:55 +00:00
end