From 34eaacdf37291ef3dbc3af0a832922d3f9639b84 Mon Sep 17 00:00:00 2001 From: Marcel Otto Date: Sat, 8 Jun 2019 22:56:22 +0200 Subject: [PATCH] true and false with capital letters are no longer valid RDF.Booleans --- CHANGELOG.md | 4 +++- lib/rdf/datatypes/boolean.ex | 6 +++--- test/unit/datatypes/boolean_test.exs | 6 +----- test/unit/equality_test.exs | 6 +++--- test/unit/turtle_encoder_test.exs | 6 +++--- 5 files changed, 13 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1bf0f2e..95f44f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,7 +20,9 @@ This project adheres to [Semantic Versioning](http://semver.org/) and - language literals were not properly unescaped during Turtle parsing - `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) diff --git a/lib/rdf/datatypes/boolean.ex b/lib/rdf/datatypes/boolean.ex index e28415f..0825ceb 100644 --- a/lib/rdf/datatypes/boolean.ex +++ b/lib/rdf/datatypes/boolean.ex @@ -14,10 +14,10 @@ defmodule RDF.Boolean do def convert(value, _) when is_boolean(value), do: value def convert(value, opts) when is_binary(value) do - with normalized_value = String.downcase(value) do + with value do cond do - normalized_value in ~W[true 1] -> true - normalized_value in ~W[false 0] -> false + value in ~W[true 1] -> true + value in ~W[false 0] -> false true -> super(value, opts) end diff --git a/test/unit/datatypes/boolean_test.exs b/test/unit/datatypes/boolean_test.exs index 458ed4d..c37b96d 100644 --- a/test/unit/datatypes/boolean_test.exs +++ b/test/unit/datatypes/boolean_test.exs @@ -8,12 +8,10 @@ defmodule RDF.BooleanTest do 1 => { true , nil , "true" }, "true" => { true , nil , "true" }, "false" => { false , nil , "false" }, - "tRuE" => { true , "tRuE" , "true" }, - "FaLsE" => { false , "FaLsE" , "false" }, "0" => { false , "0" , "false" }, "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 @@ -53,11 +51,9 @@ defmodule RDF.BooleanTest 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("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("0") |> RDF.Boolean.cast() == RDF.false end diff --git a/test/unit/equality_test.exs b/test/unit/equality_test.exs index bf9be0f..9750550 100644 --- a/test/unit/equality_test.exs +++ b/test/unit/equality_test.exs @@ -109,13 +109,13 @@ defmodule RDF.EqualityTest do {RDF.boolean("foo"), RDF.boolean("bar")}, ] @value_equal_booleans [ - {RDF.true, RDF.boolean("TRUE")}, - {RDF.boolean(1), RDF.true}, + {RDF.true, RDF.boolean("1")}, + {RDF.boolean(0), RDF.false}, # invalid literals {RDF.boolean("foo"), RDF.boolean("foo")}, ] @value_unequal_booleans [ - {RDF.true, RDF.boolean("FALSE")}, + {RDF.true, RDF.boolean("false")}, {RDF.boolean(0), RDF.true}, # invalid literals {RDF.boolean("foo"), RDF.boolean("bar")}, diff --git a/test/unit/turtle_encoder_test.exs b/test/unit/turtle_encoder_test.exs index ea6a70b..fbba658 100644 --- a/test/unit/turtle_encoder_test.exs +++ b/test/unit/turtle_encoder_test.exs @@ -534,11 +534,9 @@ defmodule RDF.Turtle.EncoderTest do [ {true, "true ."}, {"true", "true ."}, - {"TrUe", "true ."}, {"1", "true ."}, {false, "false ."}, {"false", "false ."}, - {"FaLsE", "false ."}, {"0", "false ."}, ] |> Enum.each(fn {value, output} -> @@ -550,7 +548,9 @@ defmodule RDF.Turtle.EncoderTest do test "invalid booleans" do [ {"string", ~s{"string"^^}}, - {"42", ~s{"42"^^}} + {"42", ~s{"42"^^}}, + {"TrUe", ~s{"TrUe"^^}}, + {"FaLsE", ~s{"FaLsE"^^}}, ] |> Enum.each(fn {value, output} -> Graph.new({EX.S, EX.p, RDF.Boolean.new(value)})