true and false with capital letters are no longer valid RDF.Booleans

This commit is contained in:
Marcel Otto 2019-06-08 22:56:22 +02:00
parent 5b10ccd58b
commit 34eaacdf37
5 changed files with 13 additions and 15 deletions

View file

@ -20,7 +20,9 @@ This project adheres to [Semantic Versioning](http://semver.org/) and
- language literals were not properly unescaped during Turtle parsing - language literals were not properly unescaped during Turtle parsing
- `RDF.Literal.new/1` can take decimals and infers the datatype `xsd:decimal` - `RDF.Literal.new/1` can take decimals and infers the datatype `xsd:decimal`
correctly correctly
- `true` and `false` with capital letters are no longer valid `RDF.Boolean`s
following the XSD specification; the same applies for booleans in Turtle
[Compare v0.6.0...HEAD](https://github.com/marcelotto/rdf-ex/compare/v0.6.0...HEAD) [Compare v0.6.0...HEAD](https://github.com/marcelotto/rdf-ex/compare/v0.6.0...HEAD)

View file

@ -14,10 +14,10 @@ defmodule RDF.Boolean do
def convert(value, _) when is_boolean(value), do: value def convert(value, _) when is_boolean(value), do: value
def convert(value, opts) when is_binary(value) do def convert(value, opts) when is_binary(value) do
with normalized_value = String.downcase(value) do with value do
cond do cond do
normalized_value in ~W[true 1] -> true value in ~W[true 1] -> true
normalized_value in ~W[false 0] -> false value in ~W[false 0] -> false
true -> true ->
super(value, opts) super(value, opts)
end end

View file

@ -8,12 +8,10 @@ defmodule RDF.BooleanTest do
1 => { true , nil , "true" }, 1 => { true , nil , "true" },
"true" => { true , nil , "true" }, "true" => { true , nil , "true" },
"false" => { false , nil , "false" }, "false" => { false , nil , "false" },
"tRuE" => { true , "tRuE" , "true" },
"FaLsE" => { false , "FaLsE" , "false" },
"0" => { false , "0" , "false" }, "0" => { false , "0" , "false" },
"1" => { true , "1" , "true" }, "1" => { true , "1" , "true" },
}, },
invalid: ~w(foo 10) ++ [42, 3.14, "true false", "true foo"] invalid: ~w(foo 10) ++ [42, 3.14, "tRuE", "FaLsE", "true false", "true foo"]
import RDF.Sigils import RDF.Sigils
@ -53,11 +51,9 @@ defmodule RDF.BooleanTest do
test "casting a string with a value from the lexical value space of xsd:boolean" do test "casting a string with a value from the lexical value space of xsd:boolean" do
assert RDF.string("true") |> RDF.Boolean.cast() == RDF.true assert RDF.string("true") |> RDF.Boolean.cast() == RDF.true
assert RDF.string("tRuE") |> RDF.Boolean.cast() == RDF.true
assert RDF.string("1") |> RDF.Boolean.cast() == RDF.true assert RDF.string("1") |> RDF.Boolean.cast() == RDF.true
assert RDF.string("false") |> RDF.Boolean.cast() == RDF.false assert RDF.string("false") |> RDF.Boolean.cast() == RDF.false
assert RDF.string("FaLsE") |> RDF.Boolean.cast() == RDF.false
assert RDF.string("0") |> RDF.Boolean.cast() == RDF.false assert RDF.string("0") |> RDF.Boolean.cast() == RDF.false
end end

View file

@ -109,13 +109,13 @@ defmodule RDF.EqualityTest do
{RDF.boolean("foo"), RDF.boolean("bar")}, {RDF.boolean("foo"), RDF.boolean("bar")},
] ]
@value_equal_booleans [ @value_equal_booleans [
{RDF.true, RDF.boolean("TRUE")}, {RDF.true, RDF.boolean("1")},
{RDF.boolean(1), RDF.true}, {RDF.boolean(0), RDF.false},
# invalid literals # invalid literals
{RDF.boolean("foo"), RDF.boolean("foo")}, {RDF.boolean("foo"), RDF.boolean("foo")},
] ]
@value_unequal_booleans [ @value_unequal_booleans [
{RDF.true, RDF.boolean("FALSE")}, {RDF.true, RDF.boolean("false")},
{RDF.boolean(0), RDF.true}, {RDF.boolean(0), RDF.true},
# invalid literals # invalid literals
{RDF.boolean("foo"), RDF.boolean("bar")}, {RDF.boolean("foo"), RDF.boolean("bar")},

View file

@ -534,11 +534,9 @@ defmodule RDF.Turtle.EncoderTest do
[ [
{true, "true ."}, {true, "true ."},
{"true", "true ."}, {"true", "true ."},
{"TrUe", "true ."},
{"1", "true ."}, {"1", "true ."},
{false, "false ."}, {false, "false ."},
{"false", "false ."}, {"false", "false ."},
{"FaLsE", "false ."},
{"0", "false ."}, {"0", "false ."},
] ]
|> Enum.each(fn {value, output} -> |> Enum.each(fn {value, output} ->
@ -550,7 +548,9 @@ defmodule RDF.Turtle.EncoderTest do
test "invalid booleans" do test "invalid booleans" do
[ [
{"string", ~s{"string"^^<http://www.w3.org/2001/XMLSchema#boolean>}}, {"string", ~s{"string"^^<http://www.w3.org/2001/XMLSchema#boolean>}},
{"42", ~s{"42"^^<http://www.w3.org/2001/XMLSchema#boolean>}} {"42", ~s{"42"^^<http://www.w3.org/2001/XMLSchema#boolean>}},
{"TrUe", ~s{"TrUe"^^<http://www.w3.org/2001/XMLSchema#boolean>}},
{"FaLsE", ~s{"FaLsE"^^<http://www.w3.org/2001/XMLSchema#boolean>}},
] ]
|> Enum.each(fn {value, output} -> |> Enum.each(fn {value, output} ->
Graph.new({EX.S, EX.p, RDF.Boolean.new(value)}) Graph.new({EX.S, EX.p, RDF.Boolean.new(value)})