2016-10-15 16:26:56 +00:00
|
|
|
defmodule RDF.LiteralTest do
|
2020-05-07 13:37:21 +00:00
|
|
|
use RDF.Test.Case
|
2016-10-15 16:26:56 +00:00
|
|
|
|
2017-04-16 21:13:39 +00:00
|
|
|
import RDF.TestLiterals
|
2017-04-10 01:06:20 +00:00
|
|
|
|
2020-05-05 21:58:44 +00:00
|
|
|
alias RDF.{Literal, XSD, LangString}
|
2020-04-10 21:40:33 +00:00
|
|
|
alias RDF.Literal.{Generic, Datatype}
|
2020-05-05 21:58:44 +00:00
|
|
|
alias Decimal, as: D
|
2017-03-12 13:27:52 +00:00
|
|
|
|
2016-10-15 16:26:56 +00:00
|
|
|
doctest RDF.Literal
|
|
|
|
|
2020-04-10 21:40:33 +00:00
|
|
|
alias RDF.NS
|
|
|
|
|
2017-04-23 21:41:29 +00:00
|
|
|
@examples %{
|
2020-06-29 08:37:42 +00:00
|
|
|
XSD.String => ["foo"],
|
2020-05-05 21:58:44 +00:00
|
|
|
XSD.Integer => [42],
|
2020-06-29 08:37:42 +00:00
|
|
|
XSD.Double => [3.14],
|
2020-05-05 21:58:44 +00:00
|
|
|
XSD.Decimal => [Decimal.from_float(3.14)],
|
2020-06-29 08:37:42 +00:00
|
|
|
XSD.Boolean => [true, false]
|
2017-04-23 21:41:29 +00:00
|
|
|
}
|
2016-10-15 16:26:56 +00:00
|
|
|
|
2020-04-10 21:40:33 +00:00
|
|
|
describe "new/1" do
|
2020-06-29 08:37:42 +00:00
|
|
|
Enum.each(@examples, fn {datatype, example_values} ->
|
2017-04-23 21:41:29 +00:00
|
|
|
@tag example: %{datatype: datatype, values: example_values}
|
2020-06-29 08:37:42 +00:00
|
|
|
test "coercion from #{datatype |> Module.split() |> List.last() |> to_string}", %{
|
|
|
|
example: example
|
|
|
|
} do
|
|
|
|
Enum.each(example.values, fn example_value ->
|
2017-04-23 21:41:29 +00:00
|
|
|
assert Literal.new(example_value) == example.datatype.new(example_value)
|
2020-04-10 21:40:33 +00:00
|
|
|
assert Literal.new!(example_value) == example.datatype.new!(example_value)
|
2020-06-29 08:37:42 +00:00
|
|
|
end)
|
2017-04-23 21:41:29 +00:00
|
|
|
end
|
2020-06-29 08:37:42 +00:00
|
|
|
end)
|
2016-10-15 16:26:56 +00:00
|
|
|
|
2020-05-15 15:13:31 +00:00
|
|
|
test "with builtin datatype literals" do
|
2020-06-29 08:37:42 +00:00
|
|
|
Enum.each(Datatype.Registry.builtin_datatypes(), fn datatype ->
|
2020-05-06 20:31:33 +00:00
|
|
|
datatype_literal = datatype.new("foo").literal
|
|
|
|
assert %Literal{literal: ^datatype_literal} = Literal.new(datatype_literal)
|
2020-06-29 08:37:42 +00:00
|
|
|
end)
|
2020-04-10 21:40:33 +00:00
|
|
|
end
|
|
|
|
|
2020-05-07 13:37:21 +00:00
|
|
|
test "with custom datatype literals" do
|
|
|
|
datatype_literal = RDF.TestDatatypes.Age.new(42).literal
|
2020-10-09 14:39:59 +00:00
|
|
|
assert %Literal{literal: ^datatype_literal} = Literal.new(datatype_literal)
|
2020-05-07 13:37:21 +00:00
|
|
|
end
|
|
|
|
|
2017-04-23 21:41:29 +00:00
|
|
|
test "when options without datatype given" do
|
2020-05-05 21:58:44 +00:00
|
|
|
assert Literal.new(true, []) == XSD.Boolean.new(true)
|
2020-06-29 08:37:42 +00:00
|
|
|
assert Literal.new(42, []) == XSD.Integer.new(42)
|
2020-05-05 21:58:44 +00:00
|
|
|
assert Literal.new!(true, []) == XSD.Boolean.new!(true)
|
2020-06-29 08:37:42 +00:00
|
|
|
assert Literal.new!(42, []) == XSD.Integer.new!(42)
|
2017-04-20 21:09:55 +00:00
|
|
|
end
|
2016-10-30 18:36:46 +00:00
|
|
|
end
|
|
|
|
|
2017-04-20 21:09:55 +00:00
|
|
|
describe "typed construction" do
|
|
|
|
test "boolean" do
|
2020-06-29 08:37:42 +00:00
|
|
|
assert Literal.new(true, datatype: NS.XSD.boolean()) == XSD.Boolean.new(true)
|
|
|
|
assert Literal.new(false, datatype: NS.XSD.boolean()) == XSD.Boolean.new(false)
|
|
|
|
assert Literal.new("true", datatype: NS.XSD.boolean()) == XSD.Boolean.new("true")
|
|
|
|
assert Literal.new("false", datatype: NS.XSD.boolean()) == XSD.Boolean.new("false")
|
2017-04-20 21:09:55 +00:00
|
|
|
end
|
2016-10-30 18:36:46 +00:00
|
|
|
|
2017-04-20 21:09:55 +00:00
|
|
|
test "integer" do
|
2020-06-29 08:37:42 +00:00
|
|
|
assert Literal.new(42, datatype: NS.XSD.integer()) == XSD.Integer.new(42)
|
|
|
|
assert Literal.new("42", datatype: NS.XSD.integer()) == XSD.Integer.new("42")
|
2017-04-20 21:09:55 +00:00
|
|
|
end
|
2016-10-30 18:36:46 +00:00
|
|
|
|
2017-04-26 00:48:49 +00:00
|
|
|
test "double" do
|
2020-06-29 08:37:42 +00:00
|
|
|
assert Literal.new(3.14, datatype: NS.XSD.double()) == XSD.Double.new(3.14)
|
|
|
|
assert Literal.new("3.14", datatype: NS.XSD.double()) == XSD.Double.new("3.14")
|
2017-04-26 00:48:49 +00:00
|
|
|
end
|
|
|
|
|
2018-06-15 23:42:08 +00:00
|
|
|
test "decimal" do
|
2020-06-29 08:37:42 +00:00
|
|
|
assert Literal.new(3.14, datatype: NS.XSD.decimal()) == XSD.Decimal.new(3.14)
|
|
|
|
assert Literal.new("3.14", datatype: NS.XSD.decimal()) == XSD.Decimal.new("3.14")
|
|
|
|
|
|
|
|
assert Literal.new(Decimal.from_float(3.14), datatype: NS.XSD.decimal()) ==
|
2020-05-05 21:58:44 +00:00
|
|
|
XSD.Decimal.new(Decimal.from_float(3.14))
|
2018-06-15 23:42:08 +00:00
|
|
|
end
|
|
|
|
|
2020-04-12 21:55:34 +00:00
|
|
|
test "unsignedInt" do
|
2020-06-29 08:37:42 +00:00
|
|
|
assert Literal.new(42, datatype: NS.XSD.unsignedInt()) == XSD.UnsignedInt.new(42)
|
|
|
|
assert Literal.new("42", datatype: NS.XSD.unsignedInt()) == XSD.UnsignedInt.new("42")
|
2020-04-12 21:55:34 +00:00
|
|
|
end
|
|
|
|
|
2017-04-23 21:41:29 +00:00
|
|
|
test "string" do
|
2020-06-29 08:37:42 +00:00
|
|
|
assert Literal.new("foo", datatype: NS.XSD.string()) == XSD.String.new("foo")
|
2017-04-23 21:41:29 +00:00
|
|
|
end
|
2016-10-30 18:36:46 +00:00
|
|
|
|
2020-05-07 13:37:21 +00:00
|
|
|
test "registered custom datatype" do
|
|
|
|
assert Literal.new(42, datatype: EX.Age) == RDF.TestDatatypes.Age.new(42)
|
|
|
|
end
|
|
|
|
|
2017-04-23 21:41:29 +00:00
|
|
|
test "unmapped/unknown datatype" do
|
2020-04-10 21:40:33 +00:00
|
|
|
assert Literal.new("custom typed value", datatype: "http://example/dt") ==
|
|
|
|
Generic.new("custom typed value", datatype: "http://example/dt")
|
2016-10-30 18:36:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-20 21:09:55 +00:00
|
|
|
describe "language tagged construction" do
|
|
|
|
test "string literal with a language tag" do
|
2020-04-10 21:40:33 +00:00
|
|
|
assert Literal.new("Eule", language: "de") == LangString.new("Eule", language: "de")
|
|
|
|
assert Literal.new!("Eule", language: "de") == LangString.new!("Eule", language: "de")
|
2017-04-16 21:13:39 +00:00
|
|
|
end
|
|
|
|
|
2020-04-10 21:40:33 +00:00
|
|
|
test "non-string literals with a language tag" do
|
|
|
|
assert Literal.new(1, language: "de") == LangString.new(1, language: "de")
|
|
|
|
assert Literal.new!(1, language: "de") == LangString.new!(1, language: "de")
|
2017-04-16 21:13:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "construction of an other than rdf:langString typed and language-tagged literal fails" do
|
2020-06-29 08:37:42 +00:00
|
|
|
assert Literal.new("Eule", datatype: RDF.langString(), language: "de") ==
|
2020-04-10 21:40:33 +00:00
|
|
|
LangString.new("Eule", language: "de")
|
2020-06-29 08:37:42 +00:00
|
|
|
|
2017-04-16 21:13:39 +00:00
|
|
|
assert_raise ArgumentError, fn ->
|
2020-06-29 08:37:42 +00:00
|
|
|
Literal.new("Eule", datatype: NS.XSD.string(), language: "de")
|
2017-04-16 21:13:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-10 21:40:33 +00:00
|
|
|
test "construction of a rdf:langString works, but results in an invalid literal" do
|
2020-06-29 08:37:42 +00:00
|
|
|
assert Literal.new("Eule", datatype: RDF.langString()) == LangString.new("Eule", [])
|
|
|
|
|
2020-04-10 21:40:33 +00:00
|
|
|
assert_raise RDF.Literal.InvalidError, fn ->
|
2020-06-29 08:37:42 +00:00
|
|
|
Literal.new!("Eule", datatype: RDF.langString())
|
2020-04-10 21:40:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-08-26 03:46:18 +00:00
|
|
|
|
2020-05-05 21:58:44 +00:00
|
|
|
describe "coerce/1" do
|
|
|
|
test "with boolean" do
|
|
|
|
assert Literal.coerce(true) == XSD.true()
|
|
|
|
assert Literal.coerce(false) == XSD.false()
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with string" do
|
|
|
|
assert Literal.coerce("foo") == XSD.string("foo")
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with integer" do
|
|
|
|
assert Literal.coerce(42) == XSD.integer(42)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with float" do
|
|
|
|
assert Literal.coerce(3.14) == XSD.double(3.14)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with decimal" do
|
|
|
|
assert D.from_float(3.14) |> Literal.coerce() == XSD.decimal(3.14)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with datetime" do
|
|
|
|
assert DateTime.from_iso8601("2002-04-02T12:00:00+00:00") |> elem(1) |> Literal.coerce() ==
|
|
|
|
DateTime.from_iso8601("2002-04-02T12:00:00+00:00") |> elem(1) |> XSD.datetime()
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with naive datetime" do
|
|
|
|
assert ~N"2002-04-02T12:00:00" |> Literal.coerce() ==
|
|
|
|
~N"2002-04-02T12:00:00" |> XSD.datetime()
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with date" do
|
|
|
|
assert ~D"2002-04-02" |> Literal.coerce() ==
|
|
|
|
~D"2002-04-02" |> XSD.date()
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with time" do
|
|
|
|
assert ~T"12:00:00" |> Literal.coerce() ==
|
|
|
|
~T"12:00:00" |> XSD.time()
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with URI" do
|
|
|
|
assert URI.parse("http://example.com") |> Literal.coerce() ==
|
|
|
|
XSD.any_uri("http://example.com")
|
|
|
|
end
|
|
|
|
|
2020-05-16 22:53:36 +00:00
|
|
|
test "with a resolvable vocabulary namespace term atom" do
|
|
|
|
assert Literal.coerce(EX.Foo) == EX.Foo |> RDF.iri() |> IRI.parse() |> XSD.any_uri()
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with a non-resolvable atom" do
|
|
|
|
refute Literal.coerce(Foo)
|
|
|
|
refute Literal.coerce(:foo)
|
|
|
|
end
|
|
|
|
|
2020-05-05 21:58:44 +00:00
|
|
|
test "with RDF.Literals" do
|
|
|
|
assert XSD.integer(42) |> Literal.coerce() == XSD.integer(42)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with RDF datatype Literals" do
|
|
|
|
assert %XSD.Integer{value: 42} |> Literal.coerce() == XSD.integer(42)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with inconvertible values" do
|
2020-05-16 22:53:36 +00:00
|
|
|
refute Literal.coerce(nil)
|
|
|
|
refute Literal.coerce(self())
|
2020-05-05 21:58:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-27 21:55:43 +00:00
|
|
|
describe "is_a?/2" do
|
|
|
|
test "with RDF.Literal.Datatype module" do
|
|
|
|
assert ~L"foo" |> Literal.is_a?(XSD.String)
|
|
|
|
assert ~L"foo"en |> Literal.is_a?(RDF.LangString)
|
|
|
|
assert XSD.integer(42) |> Literal.is_a?(XSD.Integer)
|
|
|
|
assert XSD.byte(42) |> Literal.is_a?(XSD.Integer)
|
2020-06-29 08:37:42 +00:00
|
|
|
|
|
|
|
assert RDF.literal("foo", datatype: "http://example.com/dt")
|
|
|
|
|> RDF.Literal.is_a?(RDF.Literal.Generic)
|
|
|
|
|
2020-05-27 21:55:43 +00:00
|
|
|
refute XSD.float(3.14) |> Literal.is_a?(XSD.Integer)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with XSD.Numeric" do
|
|
|
|
assert XSD.integer(42) |> Literal.is_a?(XSD.Numeric)
|
|
|
|
assert XSD.byte(42) |> Literal.is_a?(XSD.Numeric)
|
|
|
|
assert XSD.decimal(3.14) |> Literal.is_a?(XSD.Numeric)
|
|
|
|
refute ~L"foo" |> Literal.is_a?(XSD.Numeric)
|
|
|
|
refute ~L"foo"en |> Literal.is_a?(XSD.Numeric)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with XSD.Datatype" do
|
|
|
|
assert XSD.integer(42) |> Literal.is_a?(XSD.Datatype)
|
|
|
|
assert XSD.byte(42) |> Literal.is_a?(XSD.Datatype)
|
|
|
|
assert XSD.decimal(3.14) |> Literal.is_a?(XSD.Datatype)
|
|
|
|
assert ~L"foo" |> Literal.is_a?(XSD.Datatype)
|
|
|
|
refute ~L"foo"en |> Literal.is_a?(XSD.Datatype)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with non-datatype modules" do
|
|
|
|
refute ~L"foo" |> Literal.is_a?(String)
|
|
|
|
refute ~L"foo" |> Literal.is_a?(Regex)
|
|
|
|
refute XSD.integer(42) |> Literal.is_a?(Integer)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with non-literal" do
|
|
|
|
refute "foo" |> Literal.is_a?(XSD.String)
|
|
|
|
refute 42 |> Literal.is_a?(XSD.Numeric)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-10 21:40:33 +00:00
|
|
|
describe "has_datatype?" do
|
2020-06-29 08:37:42 +00:00
|
|
|
Enum.each(literals(~W[all_simple all_plain_lang]a), fn literal ->
|
2020-04-10 21:40:33 +00:00
|
|
|
@tag literal: literal
|
2020-06-29 08:37:42 +00:00
|
|
|
test "#{inspect(literal)} has no datatype", %{literal: literal} do
|
2020-04-10 21:40:33 +00:00
|
|
|
refute Literal.has_datatype?(literal)
|
|
|
|
end
|
2020-06-29 08:37:42 +00:00
|
|
|
end)
|
2018-08-26 03:46:18 +00:00
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
Enum.each(literals(:all) -- literals(~W[all_simple all_plain_lang]a), fn literal ->
|
2020-04-10 21:40:33 +00:00
|
|
|
@tag literal: literal
|
2020-06-29 08:37:42 +00:00
|
|
|
test "Literal for #{inspect(literal)} has a datatype", %{literal: literal} do
|
2020-04-10 21:40:33 +00:00
|
|
|
assert Literal.has_datatype?(literal)
|
|
|
|
end
|
2020-06-29 08:37:42 +00:00
|
|
|
end)
|
2017-04-16 21:13:39 +00:00
|
|
|
end
|
|
|
|
|
2020-04-10 21:40:33 +00:00
|
|
|
describe "plain?" do
|
2020-06-29 08:37:42 +00:00
|
|
|
Enum.each(literals(:all_plain), fn literal ->
|
2017-04-16 21:13:39 +00:00
|
|
|
@tag literal: literal
|
2020-06-29 08:37:42 +00:00
|
|
|
test "#{inspect(literal)} is plain", %{literal: literal} do
|
2020-04-10 21:40:33 +00:00
|
|
|
assert Literal.plain?(literal)
|
2017-04-16 21:13:39 +00:00
|
|
|
end
|
2020-06-29 08:37:42 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
Enum.each(literals(:all) -- literals(:all_plain), fn literal ->
|
2017-04-16 21:13:39 +00:00
|
|
|
@tag literal: literal
|
2020-06-29 08:37:42 +00:00
|
|
|
test "Literal for #{inspect(literal)} is not plain", %{literal: literal} do
|
2020-04-10 21:40:33 +00:00
|
|
|
refute Literal.plain?(literal)
|
2017-04-16 21:13:39 +00:00
|
|
|
end
|
2020-06-29 08:37:42 +00:00
|
|
|
end)
|
2020-04-10 21:40:33 +00:00
|
|
|
end
|
2017-04-16 21:13:39 +00:00
|
|
|
|
2020-04-10 21:40:33 +00:00
|
|
|
describe "simple?" do
|
2020-06-29 08:37:42 +00:00
|
|
|
Enum.each(literals(:all_simple), fn literal ->
|
2020-04-10 21:40:33 +00:00
|
|
|
@tag literal: literal
|
2020-06-29 08:37:42 +00:00
|
|
|
test "#{inspect(literal)} is simple", %{literal: literal} do
|
2020-04-10 21:40:33 +00:00
|
|
|
assert Literal.simple?(literal)
|
|
|
|
end
|
2020-06-29 08:37:42 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
Enum.each(literals(:all) -- literals(:all_simple), fn literal ->
|
2020-04-10 21:40:33 +00:00
|
|
|
@tag literal: literal
|
2020-06-29 08:37:42 +00:00
|
|
|
test "Literal for #{inspect(literal)} is not simple", %{literal: literal} do
|
2020-04-10 21:40:33 +00:00
|
|
|
refute Literal.simple?(literal)
|
|
|
|
end
|
2020-06-29 08:37:42 +00:00
|
|
|
end)
|
2017-04-16 21:13:39 +00:00
|
|
|
end
|
|
|
|
|
2020-05-15 20:59:05 +00:00
|
|
|
describe "datatype_id/1" do
|
2020-06-29 08:37:42 +00:00
|
|
|
Enum.each(literals(:all_simple), fn literal ->
|
2017-04-16 21:13:39 +00:00
|
|
|
@tag literal: literal
|
2020-06-29 08:37:42 +00:00
|
|
|
test "simple literal #{inspect(literal)} has datatype xsd:string", %{literal: literal} do
|
|
|
|
assert Literal.datatype_id(literal) == NS.XSD.string()
|
2017-04-16 21:13:39 +00:00
|
|
|
end
|
2020-06-29 08:37:42 +00:00
|
|
|
end)
|
2017-04-16 21:13:39 +00:00
|
|
|
|
|
|
|
%{
|
2020-06-29 08:37:42 +00:00
|
|
|
123 => "integer",
|
|
|
|
true => "boolean",
|
|
|
|
false => "boolean",
|
|
|
|
9_223_372_036_854_775_807 => "integer",
|
|
|
|
3.1415 => "double",
|
|
|
|
~D[2017-04-13] => "date",
|
|
|
|
~N[2017-04-14 15:32:07] => "dateTime",
|
|
|
|
~T[01:02:03] => "time"
|
2017-04-16 21:13:39 +00:00
|
|
|
}
|
|
|
|
|> Enum.each(fn {value, type} ->
|
2020-06-29 08:37:42 +00:00
|
|
|
@tag data: %{literal: literal = Literal.new(value), type: type}
|
|
|
|
test "Literal for #{inspect(literal)} has datatype xsd:#{type}",
|
|
|
|
%{data: %{literal: literal, type: type}} do
|
|
|
|
assert Literal.datatype_id(literal) == apply(NS.XSD, String.to_atom(type), [])
|
|
|
|
end
|
|
|
|
end)
|
2017-04-16 21:13:39 +00:00
|
|
|
end
|
|
|
|
|
2020-04-10 21:40:33 +00:00
|
|
|
describe "language" do
|
2020-06-29 08:37:42 +00:00
|
|
|
Enum.each(literals(:all_plain_lang), fn literal ->
|
2017-04-16 21:13:39 +00:00
|
|
|
@tag literal: literal
|
2020-06-29 08:37:42 +00:00
|
|
|
test "#{inspect(literal)} has correct language", %{literal: literal} do
|
2020-04-10 21:40:33 +00:00
|
|
|
assert Literal.language(literal) == "en"
|
2017-04-16 21:13:39 +00:00
|
|
|
end
|
2020-06-29 08:37:42 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
Enum.each(literals(:all) -- literals(:all_plain_lang), fn literal ->
|
2017-04-16 21:13:39 +00:00
|
|
|
@tag literal: literal
|
2020-06-29 08:37:42 +00:00
|
|
|
test "Literal for #{inspect(literal)} has no language", %{literal: literal} do
|
2020-04-10 21:40:33 +00:00
|
|
|
assert is_nil(Literal.language(literal))
|
2017-04-16 21:13:39 +00:00
|
|
|
end
|
2020-06-29 08:37:42 +00:00
|
|
|
end)
|
2020-04-10 21:40:33 +00:00
|
|
|
|
|
|
|
test "with RDF.LangString literal" do
|
|
|
|
assert Literal.new("Upper", language: "en") |> Literal.language() == "en"
|
|
|
|
assert Literal.new("Upper", language: "EN") |> Literal.language() == "en"
|
|
|
|
assert Literal.new("Upper", language: "") |> Literal.language() == nil
|
|
|
|
assert Literal.new("Upper", language: nil) |> Literal.language() == nil
|
|
|
|
end
|
2017-04-16 21:13:39 +00:00
|
|
|
end
|
|
|
|
|
2020-04-10 21:40:33 +00:00
|
|
|
describe "value/1" do
|
|
|
|
test "with XSD.Datatype literal" do
|
|
|
|
assert Literal.new("foo") |> Literal.value() == "foo"
|
|
|
|
assert Literal.new(42) |> Literal.value() == 42
|
|
|
|
end
|
2017-04-23 21:41:29 +00:00
|
|
|
|
2020-04-10 21:40:33 +00:00
|
|
|
test "with RDF.LangString literal" do
|
|
|
|
assert Literal.new("foo", language: "en") |> Literal.value() == "foo"
|
2017-04-16 21:13:39 +00:00
|
|
|
end
|
2020-04-10 21:40:33 +00:00
|
|
|
|
|
|
|
test "with generic literal" do
|
|
|
|
assert Literal.new("foo", datatype: "http://example.com/dt") |> Literal.value() == "foo"
|
2017-04-16 21:13:39 +00:00
|
|
|
end
|
2016-10-30 18:36:46 +00:00
|
|
|
end
|
|
|
|
|
2020-04-10 21:40:33 +00:00
|
|
|
describe "lexical/1" do
|
|
|
|
test "with XSD.Datatype literal" do
|
|
|
|
assert Literal.new("foo") |> Literal.lexical() == "foo"
|
|
|
|
assert Literal.new(42) |> Literal.lexical() == "42"
|
|
|
|
end
|
2017-04-23 21:41:29 +00:00
|
|
|
|
2020-04-10 21:40:33 +00:00
|
|
|
test "with RDF.LangString literal" do
|
|
|
|
assert Literal.new("foo", language: "en") |> Literal.lexical() == "foo"
|
2017-04-16 21:13:39 +00:00
|
|
|
end
|
2020-04-10 21:40:33 +00:00
|
|
|
|
|
|
|
test "with generic literal" do
|
|
|
|
assert Literal.new("foo", datatype: "http://example.com/dt") |> Literal.lexical() == "foo"
|
2017-04-16 21:13:39 +00:00
|
|
|
end
|
2016-10-30 18:36:46 +00:00
|
|
|
end
|
|
|
|
|
2020-04-10 21:40:33 +00:00
|
|
|
describe "canonical/1" do
|
|
|
|
test "with XSD.Datatype literal" do
|
|
|
|
[
|
2020-05-05 21:58:44 +00:00
|
|
|
XSD.String.new("foo"),
|
2020-06-29 08:37:42 +00:00
|
|
|
XSD.Byte.new(42)
|
2020-04-10 21:40:33 +00:00
|
|
|
]
|
|
|
|
|> Enum.each(fn
|
|
|
|
canonical_literal ->
|
|
|
|
assert Literal.canonical(canonical_literal) == canonical_literal
|
|
|
|
end)
|
2020-06-29 08:37:42 +00:00
|
|
|
|
2020-05-05 21:58:44 +00:00
|
|
|
assert XSD.Integer.new("042") |> Literal.canonical() == Literal.new(42)
|
2020-06-29 08:37:42 +00:00
|
|
|
|
|
|
|
assert Literal.new(3.14) |> Literal.canonical() ==
|
|
|
|
Literal.new(3.14) |> XSD.Double.canonical()
|
2020-04-10 21:40:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "with RDF.LangString literal" do
|
|
|
|
assert Literal.new("foo", language: "en") |> Literal.canonical() ==
|
|
|
|
Literal.new("foo", language: "en")
|
|
|
|
end
|
2017-04-23 21:41:29 +00:00
|
|
|
|
2020-04-10 21:40:33 +00:00
|
|
|
test "with generic literal" do
|
|
|
|
assert Literal.new("foo", datatype: "http://example.com/dt") |> Literal.canonical() ==
|
|
|
|
Literal.new("foo", datatype: "http://example.com/dt")
|
|
|
|
end
|
|
|
|
end
|
2017-04-23 21:41:29 +00:00
|
|
|
|
2020-04-10 21:40:33 +00:00
|
|
|
describe "canonical?/1" do
|
|
|
|
test "with XSD.Datatype literal" do
|
|
|
|
assert Literal.new("foo") |> Literal.canonical?() == true
|
|
|
|
assert Literal.new(42) |> Literal.canonical?() == true
|
|
|
|
assert Literal.new(3.14) |> Literal.canonical?() == false
|
|
|
|
end
|
2017-04-23 21:41:29 +00:00
|
|
|
|
2020-04-10 21:40:33 +00:00
|
|
|
test "with RDF.LangString literal" do
|
|
|
|
assert Literal.new("foo", language: "en") |> Literal.canonical?() == true
|
2017-04-23 21:41:29 +00:00
|
|
|
end
|
|
|
|
|
2020-04-10 21:40:33 +00:00
|
|
|
test "with generic literal" do
|
|
|
|
assert Literal.new("foo", datatype: "http://example.com/dt") |> Literal.canonical?() == true
|
2017-04-23 21:41:29 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "validation" do
|
2020-04-10 21:40:33 +00:00
|
|
|
test "with XSD.Datatype literal" do
|
|
|
|
assert Literal.new("foo") |> Literal.valid?() == true
|
|
|
|
assert Literal.new(42) |> Literal.valid?() == true
|
2020-05-05 21:58:44 +00:00
|
|
|
assert XSD.Integer.new("foo") |> Literal.valid?() == false
|
2020-04-10 21:40:33 +00:00
|
|
|
end
|
2017-04-23 21:41:29 +00:00
|
|
|
|
2020-04-10 21:40:33 +00:00
|
|
|
test "with RDF.LangString literal" do
|
|
|
|
assert Literal.new("foo", language: "en") |> Literal.valid?() == true
|
|
|
|
end
|
2017-04-23 21:41:29 +00:00
|
|
|
|
2020-04-10 21:40:33 +00:00
|
|
|
test "with generic literal" do
|
|
|
|
assert Literal.new("foo", datatype: "http://example.com/dt") |> Literal.valid?() == true
|
|
|
|
assert Literal.new("foo", datatype: "") |> Literal.valid?() == false
|
2017-04-23 21:41:29 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-10 21:40:33 +00:00
|
|
|
describe "equal_value?/2" do
|
|
|
|
test "with XSD.Datatype literal" do
|
|
|
|
assert Literal.equal_value?(Literal.new("foo"), Literal.new("foo")) == true
|
2020-05-05 21:58:44 +00:00
|
|
|
assert Literal.equal_value?(Literal.new(42), XSD.Byte.new(42)) == true
|
2020-04-10 21:40:33 +00:00
|
|
|
assert Literal.equal_value?(Literal.new("foo"), "foo") == true
|
|
|
|
assert Literal.equal_value?(Literal.new(42), 42) == true
|
|
|
|
assert Literal.equal_value?(Literal.new(42), 42.0) == true
|
|
|
|
assert Literal.equal_value?(Literal.new(false), false) == true
|
|
|
|
assert Literal.equal_value?(Literal.new(false), true) == false
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with RDF.LangString literal" do
|
2020-06-29 08:37:42 +00:00
|
|
|
assert Literal.equal_value?(
|
|
|
|
Literal.new("foo", language: "en"),
|
|
|
|
Literal.new("foo", language: "en")
|
|
|
|
) == true
|
|
|
|
|
2020-04-18 19:27:36 +00:00
|
|
|
assert Literal.equal_value?(Literal.new("foo", language: "en"), Literal.new("foo")) == nil
|
2020-04-10 21:40:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "with generic literal" do
|
2020-06-29 08:37:42 +00:00
|
|
|
assert Literal.equal_value?(
|
|
|
|
Literal.new("foo", datatype: "http://example.com/dt"),
|
|
|
|
Literal.new("foo", datatype: "http://example.com/dt")
|
|
|
|
) == true
|
|
|
|
|
|
|
|
assert Literal.equal_value?(
|
|
|
|
Literal.new("foo", datatype: "http://example.com/dt"),
|
|
|
|
Literal.new("foo")
|
|
|
|
) == nil
|
2020-04-10 21:40:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "compare/2" do
|
|
|
|
test "with XSD.Datatype literal" do
|
|
|
|
assert Literal.compare(Literal.new("foo"), Literal.new("bar")) == :gt
|
2020-05-05 21:58:44 +00:00
|
|
|
assert Literal.compare(Literal.new(42), XSD.Byte.new(43)) == :lt
|
2020-04-10 21:40:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "with RDF.LangString literal" do
|
2020-06-29 08:37:42 +00:00
|
|
|
assert Literal.compare(
|
|
|
|
Literal.new("foo", language: "en"),
|
|
|
|
Literal.new("bar", language: "en")
|
|
|
|
) == :gt
|
2020-04-10 21:40:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "with generic literal" do
|
2020-06-29 08:37:42 +00:00
|
|
|
assert Literal.compare(
|
|
|
|
Literal.new("foo", datatype: "http://example.com/dt"),
|
|
|
|
Literal.new("bar", datatype: "http://example.com/dt")
|
|
|
|
) == :gt
|
2020-04-10 21:40:33 +00:00
|
|
|
end
|
|
|
|
end
|
2017-04-23 21:41:29 +00:00
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
@poem XSD.String.new("""
|
|
|
|
<poem author="Wilhelm Busch">
|
|
|
|
Kaum hat dies der Hahn gesehen,
|
|
|
|
Fängt er auch schon an zu krähen:
|
|
|
|
Kikeriki! Kikikerikih!!
|
|
|
|
Tak, tak, tak! - da kommen sie.
|
|
|
|
</poem>
|
|
|
|
""")
|
2019-04-20 21:33:09 +00:00
|
|
|
|
|
|
|
describe "matches?" do
|
|
|
|
test "without flags" do
|
|
|
|
[
|
2020-06-29 08:37:42 +00:00
|
|
|
{~L"abracadabra", ~L"bra", true},
|
2019-04-20 21:33:09 +00:00
|
|
|
{~L"abracadabra", ~L"^a.*a$", true},
|
2020-06-29 08:37:42 +00:00
|
|
|
{~L"abracadabra", ~L"^bra", false},
|
|
|
|
{@poem, ~L"Kaum.*krähen", false},
|
2019-04-20 21:33:09 +00:00
|
|
|
{@poem, ~L"^Kaum.*gesehen,$", false},
|
2020-06-29 08:37:42 +00:00
|
|
|
{~L"foobar", ~L"foo$", false},
|
2019-07-05 23:59:03 +00:00
|
|
|
{~L"noe\u0308l", ~L"noe\\u0308l", true},
|
|
|
|
{~L"noe\\u0308l", ~L"noe\\\\u0308l", true},
|
2019-06-23 13:47:18 +00:00
|
|
|
{~L"\u{01D4B8}", ~L"\\U0001D4B8", true},
|
2019-07-05 23:59:03 +00:00
|
|
|
{~L"\\U0001D4B8", ~L"\\\U0001D4B8", true},
|
2019-04-20 21:33:09 +00:00
|
|
|
{~L"abracadabra"en, ~L"bra", true},
|
2020-06-29 08:37:42 +00:00
|
|
|
{"abracadabra", "bra", true},
|
|
|
|
{XSD.Integer.new("42"), ~L"4", true},
|
|
|
|
{XSD.Integer.new("42"), ~L"en", false}
|
2019-04-20 21:33:09 +00:00
|
|
|
]
|
|
|
|
|> Enum.each(fn {literal, pattern, expected_result} ->
|
2020-04-10 21:40:33 +00:00
|
|
|
result = Literal.matches?(literal, pattern)
|
2020-06-29 08:37:42 +00:00
|
|
|
|
2020-04-10 21:40:33 +00:00
|
|
|
assert result == expected_result,
|
2020-06-29 08:37:42 +00:00
|
|
|
"expected RDF.Literal.matches?(#{inspect(literal)}, #{inspect(pattern)}) to return #{
|
|
|
|
inspect(expected_result)
|
|
|
|
}, but got #{result}"
|
2020-04-10 21:40:33 +00:00
|
|
|
end)
|
2019-04-20 21:33:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "with flags" do
|
|
|
|
[
|
2020-06-29 08:37:42 +00:00
|
|
|
{@poem, ~L"Kaum.*krähen", ~L"s", true},
|
2019-04-20 21:33:09 +00:00
|
|
|
{@poem, ~L"^Kaum.*gesehen,$", ~L"m", true},
|
2020-06-29 08:37:42 +00:00
|
|
|
{@poem, ~L"kiki", ~L"i", true}
|
2019-04-20 21:33:09 +00:00
|
|
|
]
|
|
|
|
|> Enum.each(fn {literal, pattern, flags, result} ->
|
2020-04-10 21:40:33 +00:00
|
|
|
assert Literal.matches?(literal, pattern, flags) == result
|
|
|
|
end)
|
2019-04-20 21:33:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "with q flag" do
|
|
|
|
[
|
2020-06-29 08:37:42 +00:00
|
|
|
{~L"abcd", ~L".*", ~L"q", false},
|
2019-04-20 21:33:09 +00:00
|
|
|
{~L"Mr. B. Obama", ~L"B. OBAMA", ~L"iq", true},
|
|
|
|
|
|
|
|
# If the q flag is used together with the m, s, or x flag, that flag has no effect.
|
2020-06-29 08:37:42 +00:00
|
|
|
{~L"abcd", ~L".*", ~L"mq", true},
|
|
|
|
{~L"abcd", ~L".*", ~L"qim", true},
|
|
|
|
{~L"abcd", ~L".*", ~L"xqm", true}
|
2019-04-20 21:33:09 +00:00
|
|
|
]
|
|
|
|
|> Enum.each(fn {literal, pattern, flags, result} ->
|
2020-04-10 21:40:33 +00:00
|
|
|
assert Literal.matches?(literal, pattern, flags) == result
|
|
|
|
end)
|
2019-04-20 21:33:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-16 18:57:10 +00:00
|
|
|
describe "update/2" do
|
|
|
|
test "it updates value and lexical form" do
|
2020-05-05 21:58:44 +00:00
|
|
|
assert XSD.string("foo")
|
2020-04-16 18:57:10 +00:00
|
|
|
|> Literal.update(fn s when is_binary(s) -> s <> "bar" end) ==
|
2020-05-05 21:58:44 +00:00
|
|
|
XSD.string("foobar")
|
2020-06-29 08:37:42 +00:00
|
|
|
|
2020-05-05 21:58:44 +00:00
|
|
|
assert XSD.integer(1) |> Literal.update(fn i when is_integer(i) -> i + 1 end) ==
|
|
|
|
XSD.integer(2)
|
2020-06-29 08:37:42 +00:00
|
|
|
|
2020-05-05 21:58:44 +00:00
|
|
|
assert XSD.byte(42) |> Literal.update(fn i when is_integer(i) -> i + 1 end) ==
|
|
|
|
XSD.byte(43)
|
2020-06-29 08:37:42 +00:00
|
|
|
|
2020-05-05 21:58:44 +00:00
|
|
|
assert XSD.integer(1)
|
2020-04-16 18:57:10 +00:00
|
|
|
|> Literal.update(fn i when is_integer(i) -> "0" <> to_string(i) end) ==
|
2020-05-05 21:58:44 +00:00
|
|
|
XSD.integer("01")
|
2020-04-16 18:57:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "it does not change the datatype of generic literals" do
|
|
|
|
assert RDF.literal("foo", datatype: "http://example.com/dt")
|
|
|
|
|> Literal.update(fn s when is_binary(s) -> s <> "bar" end) ==
|
|
|
|
RDF.literal("foobar", datatype: "http://example.com/dt")
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it does not change the language of language literals" do
|
|
|
|
assert RDF.langString("foo", language: "en")
|
|
|
|
|> Literal.update(fn s when is_binary(s) -> s <> "bar" end) ==
|
|
|
|
RDF.langString("foobar", language: "en")
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with as: :lexical opt it passes the lexical form" do
|
2020-05-05 21:58:44 +00:00
|
|
|
assert XSD.integer(1)
|
2020-06-29 08:37:42 +00:00
|
|
|
|> Literal.update(fn i when is_binary(i) -> "0" <> i end, as: :lexical) ==
|
2020-05-05 21:58:44 +00:00
|
|
|
XSD.integer("01")
|
2020-04-16 18:57:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-16 21:13:39 +00:00
|
|
|
describe "String.Chars protocol implementation" do
|
2020-04-10 21:40:33 +00:00
|
|
|
test "with XSD.Datatype literal" do
|
|
|
|
assert Literal.new("foo") |> to_string() == "foo"
|
|
|
|
assert Literal.new(42) |> to_string() == "42"
|
2020-05-05 21:58:44 +00:00
|
|
|
assert XSD.Integer.new("foo") |> to_string() == "foo"
|
2017-04-16 21:13:39 +00:00
|
|
|
end
|
|
|
|
|
2020-04-10 21:40:33 +00:00
|
|
|
test "with RDF.LangString literal" do
|
|
|
|
assert Literal.new("foo", language: "en") |> to_string() == "foo"
|
|
|
|
end
|
2016-10-30 18:36:46 +00:00
|
|
|
|
2020-04-10 21:40:33 +00:00
|
|
|
test "with generic literal" do
|
|
|
|
assert Literal.new("foo", datatype: "http://example.com/dt") |> to_string() == "foo"
|
|
|
|
assert Literal.new("foo", datatype: "") |> to_string() == "foo"
|
|
|
|
end
|
|
|
|
end
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|