From 3743b0e40651db49eb9adec1252f934dd7d45bbb Mon Sep 17 00:00:00 2001 From: Marcel Otto Date: Wed, 27 May 2020 23:55:43 +0200 Subject: [PATCH] Add RDF.Literal.is_a?/2 --- lib/rdf/literal.ex | 15 +++++++++++++++ test/unit/literal_test.exs | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/lib/rdf/literal.ex b/lib/rdf/literal.ex index 8cc9280..3311d26 100644 --- a/lib/rdf/literal.ex +++ b/lib/rdf/literal.ex @@ -167,6 +167,21 @@ defmodule RDF.Literal do """ 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 """ Returns if the literal uses the `RDF.Literal.Generic` datatype or on of the dedicated builtin or custom `RDF.Literal.Datatype`s. """ diff --git a/test/unit/literal_test.exs b/test/unit/literal_test.exs index 3c45119..060288e 100644 --- a/test/unit/literal_test.exs +++ b/test/unit/literal_test.exs @@ -191,6 +191,44 @@ defmodule RDF.LiteralTest do 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 Enum.each literals(~W[all_simple all_plain_lang]a), fn literal -> @tag literal: literal