2016-10-15 16:26:56 +00:00
|
|
|
defmodule RDF.LiteralTest do
|
|
|
|
use ExUnit.Case
|
|
|
|
|
2017-04-10 01:06:20 +00:00
|
|
|
import RDF.Sigils
|
2017-04-16 21:13:39 +00:00
|
|
|
import RDF.TestLiterals
|
2017-04-10 01:06:20 +00:00
|
|
|
|
2020-04-10 21:40:33 +00:00
|
|
|
alias RDF.{Literal, LangString}
|
|
|
|
alias RDF.Literal.{Generic, Datatype}
|
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-04-10 21:40:33 +00:00
|
|
|
RDF.XSD.String => ["foo"],
|
|
|
|
RDF.XSD.Integer => [42],
|
|
|
|
RDF.XSD.Double => [3.14],
|
|
|
|
RDF.XSD.Decimal => [Decimal.from_float(3.14)],
|
|
|
|
RDF.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
|
2017-04-23 21:41:29 +00:00
|
|
|
Enum.each @examples, fn {datatype, example_values} ->
|
|
|
|
@tag example: %{datatype: datatype, values: example_values}
|
2020-04-10 21:40:33 +00:00
|
|
|
test "coercion from #{datatype |> Module.split |> List.last |> to_string}", %{example: example} do
|
2017-04-23 21:41:29 +00:00
|
|
|
Enum.each example.values, fn example_value ->
|
|
|
|
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)
|
2017-04-23 21:41:29 +00:00
|
|
|
end
|
|
|
|
end
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|
|
|
|
|
2020-04-10 21:40:33 +00:00
|
|
|
test "with typed literals" do
|
2020-04-11 21:58:11 +00:00
|
|
|
Enum.each Datatype.Registry.datatypes() -- [RDF.LangString], fn datatype ->
|
2020-04-10 21:40:33 +00:00
|
|
|
literal_type = datatype.literal_type()
|
|
|
|
assert %Literal{literal: typed_literal} = Literal.new(literal_type.new("foo"))
|
|
|
|
assert typed_literal.__struct__ == literal_type
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-23 21:41:29 +00:00
|
|
|
test "when options without datatype given" do
|
2020-04-10 21:40:33 +00:00
|
|
|
assert Literal.new(true, []) == RDF.XSD.Boolean.new(true)
|
|
|
|
assert Literal.new(42, []) == RDF.XSD.Integer.new(42)
|
|
|
|
assert Literal.new!(true, []) == RDF.XSD.Boolean.new!(true)
|
|
|
|
assert Literal.new!(42, []) == RDF.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-04-10 21:40:33 +00:00
|
|
|
assert Literal.new(true, datatype: NS.XSD.boolean) == RDF.XSD.Boolean.new(true)
|
|
|
|
assert Literal.new(false, datatype: NS.XSD.boolean) == RDF.XSD.Boolean.new(false)
|
|
|
|
assert Literal.new("true", datatype: NS.XSD.boolean) == RDF.XSD.Boolean.new("true")
|
|
|
|
assert Literal.new("false", datatype: NS.XSD.boolean) == RDF.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-04-10 21:40:33 +00:00
|
|
|
assert Literal.new(42, datatype: NS.XSD.integer) == RDF.XSD.Integer.new(42)
|
|
|
|
assert Literal.new("42", datatype: NS.XSD.integer) == RDF.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-04-10 21:40:33 +00:00
|
|
|
assert Literal.new(3.14, datatype: NS.XSD.double) == RDF.XSD.Double.new(3.14)
|
|
|
|
assert Literal.new("3.14", datatype: NS.XSD.double) == RDF.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-04-10 21:40:33 +00:00
|
|
|
assert Literal.new(3.14, datatype: NS.XSD.decimal) == RDF.XSD.Decimal.new(3.14)
|
|
|
|
assert Literal.new("3.14", datatype: NS.XSD.decimal) == RDF.XSD.Decimal.new("3.14")
|
|
|
|
assert Literal.new(Decimal.from_float(3.14), datatype: NS.XSD.decimal) ==
|
|
|
|
RDF.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
|
|
|
|
assert Literal.new(42, datatype: NS.XSD.unsignedInt) == RDF.XSD.UnsignedInt.new(42)
|
|
|
|
assert Literal.new("42", datatype: NS.XSD.unsignedInt) == RDF.XSD.UnsignedInt.new("42")
|
|
|
|
end
|
|
|
|
|
2017-04-23 21:41:29 +00:00
|
|
|
test "string" do
|
2020-04-10 21:40:33 +00:00
|
|
|
assert Literal.new("foo", datatype: NS.XSD.string) == RDF.XSD.String.new("foo")
|
2017-04-23 21:41:29 +00:00
|
|
|
end
|
2016-10-30 18:36:46 +00:00
|
|
|
|
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-04-10 21:40:33 +00:00
|
|
|
assert Literal.new("Eule", datatype: RDF.langString, language: "de") ==
|
|
|
|
LangString.new("Eule", language: "de")
|
2017-04-16 21:13:39 +00:00
|
|
|
assert_raise ArgumentError, fn ->
|
2020-04-10 21:40:33 +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-04-11 21:58:11 +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 ->
|
|
|
|
Literal.new!("Eule", datatype: RDF.langString)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-08-26 03:46:18 +00:00
|
|
|
|
2020-04-10 21:40:33 +00:00
|
|
|
describe "has_datatype?" do
|
|
|
|
Enum.each literals(~W[all_simple all_plain_lang]a), fn literal ->
|
|
|
|
@tag literal: literal
|
|
|
|
test "#{inspect literal} has no datatype", %{literal: literal} do
|
|
|
|
refute Literal.has_datatype?(literal)
|
|
|
|
end
|
2018-08-26 03:46:18 +00:00
|
|
|
end
|
|
|
|
|
2020-04-10 21:40:33 +00:00
|
|
|
Enum.each literals(:all) -- literals(~W[all_simple all_plain_lang]a), fn literal ->
|
|
|
|
@tag literal: literal
|
|
|
|
test "Literal for #{inspect literal} has a datatype", %{literal: literal} do
|
|
|
|
assert Literal.has_datatype?(literal)
|
|
|
|
end
|
2017-04-16 21:13:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-10 21:40:33 +00:00
|
|
|
describe "plain?" do
|
|
|
|
Enum.each literals(:all_plain), fn literal ->
|
2017-04-16 21:13:39 +00:00
|
|
|
@tag literal: literal
|
2020-04-10 21:40:33 +00:00
|
|
|
test "#{inspect literal} is plain", %{literal: literal} do
|
|
|
|
assert Literal.plain?(literal)
|
2017-04-16 21:13:39 +00:00
|
|
|
end
|
|
|
|
end
|
2020-04-10 21:40:33 +00:00
|
|
|
Enum.each literals(:all) -- literals(:all_plain), fn literal ->
|
2017-04-16 21:13:39 +00:00
|
|
|
@tag literal: literal
|
2020-04-10 21:40:33 +00:00
|
|
|
test "Literal for #{inspect literal} is not plain", %{literal: literal} do
|
|
|
|
refute Literal.plain?(literal)
|
2017-04-16 21:13:39 +00:00
|
|
|
end
|
|
|
|
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
|
|
|
|
Enum.each literals(:all_simple), fn literal ->
|
|
|
|
@tag literal: literal
|
|
|
|
test "#{inspect literal} is simple", %{literal: literal} do
|
|
|
|
assert Literal.simple?(literal)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Enum.each literals(:all) -- literals(:all_simple), fn literal ->
|
|
|
|
@tag literal: literal
|
|
|
|
test "Literal for #{inspect literal} is not simple", %{literal: literal} do
|
|
|
|
refute Literal.simple?(literal)
|
|
|
|
end
|
2017-04-16 21:13:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-10 21:40:33 +00:00
|
|
|
describe "datatype/1" do
|
2017-04-16 21:13:39 +00:00
|
|
|
Enum.each literals(:all_simple), fn literal ->
|
|
|
|
@tag literal: literal
|
|
|
|
test "simple literal #{inspect literal} has datatype xsd:string", %{literal: literal} do
|
2020-04-10 21:40:33 +00:00
|
|
|
assert Literal.datatype(literal) == NS.XSD.string
|
2017-04-16 21:13:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
%{
|
|
|
|
123 => "integer",
|
|
|
|
true => "boolean",
|
|
|
|
false => "boolean",
|
|
|
|
9223372036854775807 => "integer",
|
|
|
|
3.1415 => "double",
|
|
|
|
~D[2017-04-13] => "date",
|
|
|
|
~N[2017-04-14 15:32:07] => "dateTime",
|
|
|
|
~T[01:02:03] => "time"
|
|
|
|
}
|
|
|
|
|> Enum.each(fn {value, type} ->
|
|
|
|
@tag data: %{literal: literal = Literal.new(value), type: type}
|
2020-04-10 21:40:33 +00:00
|
|
|
test "Literal for #{inspect literal} has datatype xsd:#{type}",
|
2017-04-16 21:13:39 +00:00
|
|
|
%{data: %{literal: literal, type: type}} do
|
2020-04-10 21:40:33 +00:00
|
|
|
assert Literal.datatype(literal) == apply(NS.XSD, String.to_atom(type), [])
|
2017-04-16 21:13:39 +00:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2020-04-10 21:40:33 +00:00
|
|
|
describe "language" do
|
|
|
|
Enum.each literals(:all_plain_lang), fn literal ->
|
2017-04-16 21:13:39 +00:00
|
|
|
@tag literal: literal
|
2020-04-10 21:40:33 +00:00
|
|
|
test "#{inspect literal} has correct language", %{literal: literal} do
|
|
|
|
assert Literal.language(literal) == "en"
|
2017-04-16 21:13:39 +00:00
|
|
|
end
|
|
|
|
end
|
2020-04-10 21:40:33 +00:00
|
|
|
Enum.each literals(:all) -- literals(:all_plain_lang), fn literal ->
|
2017-04-16 21:13:39 +00:00
|
|
|
@tag literal: literal
|
2020-04-10 21:40:33 +00:00
|
|
|
test "Literal for #{inspect literal} has no language", %{literal: literal} do
|
|
|
|
assert is_nil(Literal.language(literal))
|
2017-04-16 21:13:39 +00:00
|
|
|
end
|
|
|
|
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
|
|
|
|
[
|
|
|
|
RDF.XSD.String.new("foo"),
|
|
|
|
RDF.XSD.Byte.new(42),
|
|
|
|
|
|
|
|
]
|
|
|
|
|> Enum.each(fn
|
|
|
|
canonical_literal ->
|
|
|
|
assert Literal.canonical(canonical_literal) == canonical_literal
|
|
|
|
end)
|
|
|
|
assert RDF.XSD.Integer.new("042") |> Literal.canonical() == Literal.new(42)
|
|
|
|
assert Literal.new(3.14) |> Literal.canonical() == Literal.new(3.14) |> RDF.XSD.Double.canonical()
|
|
|
|
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
|
|
|
|
assert RDF.XSD.Integer.new("foo") |> Literal.valid?() == 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.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
|
|
|
|
assert Literal.equal_value?(Literal.new(42), RDF.XSD.Byte.new(42)) == true
|
|
|
|
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
|
|
|
|
assert Literal.equal_value?(Literal.new("foo", language: "en"),
|
|
|
|
Literal.new("foo", language: "en")) == true
|
|
|
|
assert Literal.equal_value?(Literal.new("foo", language: "en"), Literal.new("foo")) == false
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with generic literal" do
|
|
|
|
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")) == false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "compare/2" do
|
|
|
|
test "with XSD.Datatype literal" do
|
|
|
|
assert Literal.compare(Literal.new("foo"), Literal.new("bar")) == :gt
|
|
|
|
assert Literal.compare(Literal.new(42), RDF.XSD.Byte.new(43)) == :lt
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with RDF.LangString literal" do
|
|
|
|
assert Literal.compare(Literal.new("foo", language: "en"),
|
|
|
|
Literal.new("bar", language: "en")) == :gt
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with generic literal" do
|
|
|
|
assert Literal.compare(Literal.new("foo", datatype: "http://example.com/dt"),
|
|
|
|
Literal.new("bar", datatype: "http://example.com/dt")) == :gt
|
|
|
|
end
|
|
|
|
end
|
2017-04-23 21:41:29 +00:00
|
|
|
|
2020-04-10 21:40:33 +00:00
|
|
|
@poem RDF.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
|
|
|
|
[
|
|
|
|
{~L"abracadabra", ~L"bra", true},
|
|
|
|
{~L"abracadabra", ~L"^a.*a$", true},
|
|
|
|
{~L"abracadabra", ~L"^bra", false},
|
|
|
|
{@poem, ~L"Kaum.*krähen", false},
|
|
|
|
{@poem, ~L"^Kaum.*gesehen,$", false},
|
2019-07-05 23:59:03 +00:00
|
|
|
{~L"foobar", ~L"foo$", false},
|
|
|
|
|
|
|
|
{~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},
|
|
|
|
{"abracadabra", "bra", true},
|
2020-04-10 21:40:33 +00:00
|
|
|
{RDF.XSD.Integer.new("42"), ~L"4", true},
|
|
|
|
{RDF.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)
|
|
|
|
assert result == expected_result,
|
|
|
|
"expected RDF.Literal.matches?(#{inspect literal}, #{inspect pattern}) to return #{inspect expected_result}, but got #{result}"
|
|
|
|
end)
|
2019-04-20 21:33:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "with flags" do
|
|
|
|
[
|
|
|
|
{@poem, ~L"Kaum.*krähen", ~L"s", true},
|
|
|
|
{@poem, ~L"^Kaum.*gesehen,$", ~L"m", true},
|
|
|
|
{@poem, ~L"kiki", ~L"i", true},
|
|
|
|
]
|
|
|
|
|> 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
|
|
|
|
[
|
|
|
|
{~L"abcd", ~L".*", ~L"q", false},
|
|
|
|
{~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.
|
|
|
|
{~L"abcd", ~L".*", ~L"mq", true},
|
|
|
|
{~L"abcd", ~L".*", ~L"qim", true},
|
|
|
|
{~L"abcd", ~L".*", ~L"xqm", true},
|
|
|
|
]
|
|
|
|
|> 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
|
|
|
|
|
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"
|
|
|
|
assert RDF.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
|