Skip to content

Commit

Permalink
add integer validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jreidinger committed Apr 28, 2020
1 parent a329721 commit 6b0a4b0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
3 changes: 2 additions & 1 deletion library/xml/src/modules/XML.rb
Expand Up @@ -247,7 +247,7 @@ def XMLError
}.freeze

def parse_node(node, result)
text = node.xpath("text()").text.sub(/\A\s+\z/, "")
text = node.xpath("text()").text.strip
# use only element children
children = node.children
children = children.select(&:element?)
Expand Down Expand Up @@ -278,6 +278,7 @@ def parse_node(node, result)
value = text.to_sym
when "integer"
raise XMLInvalidContent, "xml node '#{node.name}' is empty. Forbidden for integer." if text.empty?
raise XMLInvalidContent, "xml node '#{node.name}' is invalid integer." if text !~ /-?\d+/

value = text.to_i
when "boolean"
Expand Down
44 changes: 42 additions & 2 deletions library/xml/test/xml_test.rb
Expand Up @@ -168,19 +168,59 @@
end

describe ".XMLToYCPString" do
it "returns string for xml element with type=\"string\"" do
input = "<?xml version=\"1.0\"?>\n" \
"<!DOCTYPE test SYSTEM \"Testing system\">\n" \
"<test xmlns=\"http://www.suse.com/1.0/yast2ns\" xmlns:config=\"http://www.suse.com/1.0/configns\">\n" \
" <test config:type=\"string\">5</test>\n" \
" <lest config:type=\"string\"> \n" \
" -5 \n" \
" </lest>\n" \
"</test>\n"
expected = { "test" => "5", "lest" => "-5"}

expect(subject.XMLToYCPString(input)).to eq expected
end

it "returns string for xml element without type and with text" do
input = "<?xml version=\"1.0\"?>\n" \
"<!DOCTYPE test SYSTEM \"Testing system\">\n" \
"<test xmlns=\"http://www.suse.com/1.0/yast2ns\" xmlns:config=\"http://www.suse.com/1.0/configns\">\n" \
" <test>5</test>\n" \
" <lest>\n" \
" -5 \n" \
" </lest>\n" \
"</test>\n"
expected = { "test" => "5", "lest" => "-5"}

expect(subject.XMLToYCPString(input)).to eq expected
end

it "returns integer for xml element with type=\"integer\"" do
input = "<?xml version=\"1.0\"?>\n" \
"<!DOCTYPE test SYSTEM \"Testing system\">\n" \
"<test xmlns=\"http://www.suse.com/1.0/yast2ns\" xmlns:config=\"http://www.suse.com/1.0/configns\">\n" \
" <test config:type=\"integer\">5</test>\n" \
" <lest config:type=\"integer\">-5</lest>\n" \
" <invalid config:type=\"integer\">invalid</invalid>\n" \
"</test>\n"
expected = { "test" => 5, "lest" => -5, "invalid" => 0 }
expected = { "test" => 5, "lest" => -5 }

expect(subject.XMLToYCPString(input)).to eq expected
end


it "raises XMLInvalidContent for invalid integers" do
input = "<?xml version=\"1.0\"?>\n" \
"<!DOCTYPE test SYSTEM \"Testing system\">\n" \
"<test xmlns=\"http://www.suse.com/1.0/yast2ns\" xmlns:config=\"http://www.suse.com/1.0/configns\">\n" \
" <test config:type=\"integer\">5</test>\n" \
" <lest config:type=\"integer\">-5</lest>\n" \
" <invalid config:type=\"integer\">invalid</invalid>\n" \
"</test>\n"

expect{subject.XMLToYCPString(input)}.to raise_error(Yast::XMLInvalidContent)
end

it "returns symbol for xml element with type=\"symbol\"" do
input = "<?xml version=\"1.0\"?>\n" \
"<!DOCTYPE test SYSTEM \"Testing system\">\n" \
Expand Down

0 comments on commit 6b0a4b0

Please sign in to comment.