Add RDF.Literal.is_a?/2
This commit is contained in:
parent
98adbaf878
commit
3743b0e406
2 changed files with 53 additions and 0 deletions
|
@ -167,6 +167,21 @@ defmodule RDF.Literal do
|
||||||
"""
|
"""
|
||||||
defdelegate datatype?(value), to: RDF.Literal.Datatype.Registry, as: :datatype?
|
defdelegate datatype?(value), to: RDF.Literal.Datatype.Registry, as: :datatype?
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Checks if 'literal' is literal with the given `datatype`.
|
||||||
|
|
||||||
|
`datatype` can be one of the following:
|
||||||
|
|
||||||
|
- a `RDF.Literal.Datatype` module which checks if the literal is of this datatype or derived from it
|
||||||
|
- `RDF.XSD.Numeric` which checks if the literal is one of the numeric XSD datatypes or derived of one of them
|
||||||
|
- `RDF.XSD.Datatype` which checks if the literal is a XSD datatype or derived of one of them
|
||||||
|
|
||||||
|
"""
|
||||||
|
def is_a?(literal, RDF.XSD.Numeric), do: RDF.XSD.Numeric.datatype?(literal)
|
||||||
|
def is_a?(literal, RDF.XSD.Datatype), do: RDF.XSD.datatype?(literal)
|
||||||
|
def is_a?(literal, RDF.Literal.Datatype), do: datatype?(literal)
|
||||||
|
def is_a?(literal, datatype), do: datatype?(datatype) and datatype.datatype?(literal)
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Returns if the literal uses the `RDF.Literal.Generic` datatype or on of the dedicated builtin or custom `RDF.Literal.Datatype`s.
|
Returns if the literal uses the `RDF.Literal.Generic` datatype or on of the dedicated builtin or custom `RDF.Literal.Datatype`s.
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -191,6 +191,44 @@ defmodule RDF.LiteralTest do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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)
|
||||||
|
assert RDF.literal("foo", datatype: "http://example.com/dt") |> RDF.Literal.is_a?(RDF.Literal.Generic)
|
||||||
|
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
|
||||||
|
|
||||||
describe "has_datatype?" do
|
describe "has_datatype?" do
|
||||||
Enum.each literals(~W[all_simple all_plain_lang]a), fn literal ->
|
Enum.each literals(~W[all_simple all_plain_lang]a), fn literal ->
|
||||||
@tag literal: literal
|
@tag literal: literal
|
||||||
|
|
Loading…
Reference in a new issue