fix boolean pattern parse

This commit is contained in:
cmrd Senya 2017-01-04 06:25:23 +03:00
parent 9a7fd278b5
commit a1c9998d40
3 changed files with 26 additions and 10 deletions

View file

@ -319,8 +319,8 @@ module DiasporaFederation
when :integer
text.to_i if text =~ /^\d+$/
when :boolean
return true if text =~ /(true|t|yes|y|1)$/i
false if text =~ /(false|f|no|n|0)$/i
return true if text =~ /^(true|t|yes|y|1)$/i
false if text =~ /^(false|f|no|n|0)$/i
else
text
end

View file

@ -34,6 +34,10 @@ module DiasporaFederation
class Entity < DiasporaFederation::Entity
property :test, :string
end
class TestEntityWithBoolean < DiasporaFederation::Entity
property :test, :boolean
end
end
module Validators

View file

@ -195,16 +195,28 @@ XML
it "parses boolean fields with false value" do
xml = <<-XML.strip
<test_default_entity>
<test1>qwer</test1>
<test2>qwer</test2>
<test3>false</test3>
</test_default_entity>
<test_entity_with_boolean>
<test>false</test>
</test_entity_with_boolean>
XML
entity = Entities::TestDefaultEntity.from_xml(Nokogiri::XML::Document.parse(xml).root)
expect(entity).to be_an_instance_of Entities::TestDefaultEntity
expect(entity.test3).to eq(false)
entity = Entities::TestEntityWithBoolean.from_xml(Nokogiri::XML::Document.parse(xml).root)
expect(entity).to be_an_instance_of Entities::TestEntityWithBoolean
expect(entity.test).to eq(false)
end
it "parses boolean fields with a randomly matching pattern as erroneous" do
%w(ttFFFtt yesFFDSFSDy noDFDSFFDFn fXf LLyes).each do |weird_value|
xml = <<-XML.strip
<test_entity_with_boolean>
<test>#{weird_value}</test>
</test_entity_with_boolean>
XML
expect {
Entities::TestEntityWithBoolean.from_xml(Nokogiri::XML::Document.parse(xml).root)
}.to raise_error Entity::ValidationError, "missing required properties: test"
end
end
end