2017-04-10 19:24:43 +00:00
|
|
|
defmodule RDF.NTriples.DecoderTest do
|
2016-10-30 18:36:46 +00:00
|
|
|
use ExUnit.Case, async: false
|
|
|
|
|
2017-04-10 19:24:43 +00:00
|
|
|
doctest RDF.NTriples.Decoder
|
2016-10-30 18:36:46 +00:00
|
|
|
|
2017-07-06 19:47:04 +00:00
|
|
|
alias RDF.Graph
|
2016-10-30 18:36:46 +00:00
|
|
|
|
2017-03-12 13:27:52 +00:00
|
|
|
|
|
|
|
use RDF.Vocabulary.Namespace
|
|
|
|
|
|
|
|
defvocab EX,
|
2017-08-20 20:35:14 +00:00
|
|
|
base_iri: "http://example.org/#",
|
2017-03-12 13:27:52 +00:00
|
|
|
terms: [], strict: false
|
|
|
|
|
|
|
|
defvocab P,
|
2017-08-20 20:35:14 +00:00
|
|
|
base_iri: "http://www.perceive.net/schemas/relationship/",
|
2017-03-12 13:27:52 +00:00
|
|
|
terms: [], strict: false
|
|
|
|
|
2016-10-30 18:36:46 +00:00
|
|
|
|
|
|
|
test "an empty string is deserialized to an empty graph" do
|
2017-04-10 19:24:43 +00:00
|
|
|
assert RDF.NTriples.Decoder.decode!("") == Graph.new
|
|
|
|
assert RDF.NTriples.Decoder.decode!(" \n\r\r\n ") == Graph.new
|
2016-10-30 18:36:46 +00:00
|
|
|
end
|
|
|
|
|
2017-04-10 19:24:43 +00:00
|
|
|
test "decoding comments" do
|
|
|
|
assert RDF.NTriples.Decoder.decode!("# just a comment") == Graph.new
|
2016-10-30 18:36:46 +00:00
|
|
|
|
2017-04-10 19:24:43 +00:00
|
|
|
assert RDF.NTriples.Decoder.decode!("""
|
2016-10-30 18:36:46 +00:00
|
|
|
<http://example.org/#S> <http://example.org/#p> _:1 . # a comment
|
|
|
|
""") == Graph.new({EX.S, EX.p, RDF.bnode("1")})
|
|
|
|
|
2017-04-10 19:24:43 +00:00
|
|
|
assert RDF.NTriples.Decoder.decode!("""
|
2016-10-30 18:36:46 +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-04-10 19:24:43 +00:00
|
|
|
assert RDF.NTriples.Decoder.decode!("""
|
2016-10-30 18:36:46 +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-04-10 19:24:43 +00:00
|
|
|
assert RDF.NTriples.Decoder.decode!("""
|
2016-10-30 18:36:46 +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},
|
|
|
|
])
|
|
|
|
end
|
|
|
|
|
|
|
|
test "empty lines" do
|
2017-04-10 19:24:43 +00:00
|
|
|
assert RDF.NTriples.Decoder.decode!("""
|
2016-10-30 18:36:46 +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-04-10 19:24:43 +00:00
|
|
|
assert RDF.NTriples.Decoder.decode!("""
|
2016-10-30 18:36:46 +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-04-10 19:24:43 +00:00
|
|
|
assert RDF.NTriples.Decoder.decode!("""
|
2016-10-30 18:36:46 +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},
|
|
|
|
])
|
|
|
|
end
|
|
|
|
|
2017-08-20 20:35:14 +00:00
|
|
|
test "decoding a single triple with iris" do
|
2017-04-10 19:24:43 +00:00
|
|
|
assert RDF.NTriples.Decoder.decode!("""
|
2016-10-30 18:36:46 +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})
|
|
|
|
end
|
|
|
|
|
2017-04-10 19:24:43 +00:00
|
|
|
test "decoding a single triple with a blank node" do
|
|
|
|
assert RDF.NTriples.Decoder.decode!("""
|
2016-10-30 18:36:46 +00:00
|
|
|
_:foo <http://example.org/#p> <http://example.org/#O> .
|
|
|
|
""") == Graph.new({RDF.bnode("foo"), EX.p, EX.O})
|
2017-04-10 19:24:43 +00:00
|
|
|
assert RDF.NTriples.Decoder.decode!("""
|
2016-10-30 18:36:46 +00:00
|
|
|
<http://example.org/#S> <http://example.org/#p> _:1 .
|
|
|
|
""") == Graph.new({EX.S, EX.p, RDF.bnode("1")})
|
2017-04-10 19:24:43 +00:00
|
|
|
assert RDF.NTriples.Decoder.decode!("""
|
2016-10-30 18:36:46 +00:00
|
|
|
_:foo <http://example.org/#p> _:bar .
|
|
|
|
""") == Graph.new({RDF.bnode("foo"), EX.p, RDF.bnode("bar")})
|
|
|
|
end
|
|
|
|
|
2017-04-10 19:24:43 +00:00
|
|
|
test "decoding a single triple with an untyped string literal" do
|
|
|
|
assert RDF.NTriples.Decoder.decode!("""
|
2016-10-30 18:36:46 +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")})
|
|
|
|
end
|
|
|
|
|
2017-04-10 19:24:43 +00:00
|
|
|
test "decoding a single triple with a typed literal" do
|
|
|
|
assert RDF.NTriples.Decoder.decode!("""
|
2016-10-30 18:36:46 +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)})
|
|
|
|
end
|
|
|
|
|
2017-04-10 19:24:43 +00:00
|
|
|
test "decoding a single triple with a language tagged literal" do
|
|
|
|
assert RDF.NTriples.Decoder.decode!("""
|
2016-10-30 18:36:46 +00:00
|
|
|
<http://example.org/#S> <http://example.org/#p> "foo"@en .
|
|
|
|
""") == Graph.new({EX.S, EX.p, RDF.literal("foo", language: "en")})
|
|
|
|
end
|
|
|
|
|
2017-04-10 19:24:43 +00:00
|
|
|
test "decoding multiple triples" do
|
|
|
|
assert RDF.NTriples.Decoder.decode!("""
|
2016-10-30 18:36:46 +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},
|
|
|
|
])
|
2017-04-10 19:24:43 +00:00
|
|
|
assert RDF.NTriples.Decoder.decode!("""
|
2016-10-30 18:36:46 +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> .
|
|
|
|
<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}
|
|
|
|
])
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|