1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
require "spec_helper"
describe SafeYAML::Transform::ToInteger do
it "returns true when the value matches a valid Integer" do
subject.transform?("10").should == [true, 10]
end
it "returns false when the value does not match a valid Integer" do
subject.transform?("foobar").should be_false
end
it "returns false when the value spans multiple lines" do
subject.transform?("10\nNOT AN INTEGER").should be_false
end
it "allows commas in the number" do
subject.transform?("1,000").should == [true, 1000]
end
it "correctly parses numbers in octal format" do
subject.transform?("010").should == [true, 8]
end
it "correctly parses numbers in hexadecimal format" do
subject.transform?("0x1FF").should == [true, 511]
end
it "defaults to a string for a number that resembles octal format but is not" do
subject.transform?("09").should be_false
end
it "correctly parses 0 in decimal" do
subject.transform?("0").should == [true, 0]
end
it "defaults to a string for a number that resembles hexadecimal format but is not" do
subject.transform?("0x1G").should be_false
end
it "correctly parses all formats in the YAML spec" do
# canonical
subject.transform?("685230").should == [true, 685230]
# decimal
subject.transform?("+685_230").should == [true, 685230]
# octal
subject.transform?("02472256").should == [true, 685230]
# hexadecimal:
subject.transform?("0x_0A_74_AE").should == [true, 685230]
# binary
subject.transform?("0b1010_0111_0100_1010_1110").should == [true, 685230]
# sexagesimal
subject.transform?("190:20:30").should == [true, 685230]
end
# see https://github.com/dtao/safe_yaml/pull/51
it "strips out underscores before parsing decimal values" do
subject.transform?("_850_").should == [true, 850]
end
end
|