This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
require "spec_helper"
|
||||
|
||||
describe SafeYAML::Transform do
|
||||
it "should return the same encoding when decoding Base64" do
|
||||
value = "c3VyZS4="
|
||||
decoded = SafeYAML::Transform.to_proper_type(value, false, "!binary")
|
||||
|
||||
expect(decoded).to eq("sure.")
|
||||
expect(decoded.encoding).to eq(value.encoding) if decoded.respond_to?(:encoding)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,60 @@
|
||||
require "spec_helper"
|
||||
|
||||
describe SafeYAML::Transform::ToDate do
|
||||
it "returns true when the value matches a valid Date" do
|
||||
expect(subject.transform?("2013-01-01")).to eq([true, Date.parse("2013-01-01")])
|
||||
end
|
||||
|
||||
it "returns false when the value does not match a valid Date" do
|
||||
expect(subject.transform?("foobar")).to be_falsey
|
||||
end
|
||||
|
||||
it "returns false when the value does not end with a Date" do
|
||||
expect(subject.transform?("2013-01-01\nNOT A DATE")).to be_falsey
|
||||
end
|
||||
|
||||
it "returns false when the value does not begin with a Date" do
|
||||
expect(subject.transform?("NOT A DATE\n2013-01-01")).to be_falsey
|
||||
end
|
||||
|
||||
it "correctly parses the remaining formats of the YAML spec" do
|
||||
equivalent_values = [
|
||||
"2001-12-15T02:59:43.1Z", # canonical
|
||||
"2001-12-14t21:59:43.10-05:00", # iso8601
|
||||
"2001-12-14 21:59:43.10 -5", # space separated
|
||||
"2001-12-15 2:59:43.10" # no time zone (Z)
|
||||
]
|
||||
|
||||
equivalent_values.each do |value|
|
||||
success, result = subject.transform?(value)
|
||||
expect(success).to be_truthy
|
||||
expect(result).to eq(Time.utc(2001, 12, 15, 2, 59, 43, 100000))
|
||||
end
|
||||
end
|
||||
|
||||
it "converts times to the local timezone" do
|
||||
success, result = subject.transform?("2012-12-01 10:33:45 +11:00")
|
||||
expect(success).to be_truthy
|
||||
expect(result).to eq(Time.utc(2012, 11, 30, 23, 33, 45))
|
||||
expect(result.gmt_offset).to eq(Time.local(2012, 11, 30).gmt_offset)
|
||||
end
|
||||
|
||||
it "returns strings for invalid dates" do
|
||||
expect(subject.transform?("0000-00-00")).to eq([true, "0000-00-00"])
|
||||
expect(subject.transform?("2013-13-01")).to eq([true, "2013-13-01"])
|
||||
expect(subject.transform?("2014-01-32")).to eq([true, "2014-01-32"])
|
||||
end
|
||||
|
||||
it "returns strings for invalid date/times" do
|
||||
expect(subject.transform?("0000-00-00 00:00:00 -0000")).to eq([true, "0000-00-00 00:00:00 -0000"])
|
||||
expect(subject.transform?("2013-13-01 21:59:43 -05:00")).to eq([true, "2013-13-01 21:59:43 -05:00"])
|
||||
expect(subject.transform?("2013-01-32 21:59:43 -05:00")).to eq([true, "2013-01-32 21:59:43 -05:00"])
|
||||
expect(subject.transform?("2013-01-30 25:59:43 -05:00")).to eq([true, "2013-01-30 25:59:43 -05:00"])
|
||||
expect(subject.transform?("2013-01-30 21:69:43 -05:00")).to eq([true, "2013-01-30 21:69:43 -05:00"])
|
||||
|
||||
# Interesting. It seems that in some older Ruby versions, the below actually parses successfully
|
||||
# w/ DateTime.parse; but it fails w/ YAML.load. Whom to follow???
|
||||
|
||||
# subject.transform?("2013-01-30 21:59:63 -05:00").should == [true, "2013-01-30 21:59:63 -05:00"]
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,42 @@
|
||||
require "spec_helper"
|
||||
|
||||
describe SafeYAML::Transform::ToFloat do
|
||||
it "returns true when the value matches a valid Float" do
|
||||
expect(subject.transform?("20.00")).to eq([true, 20.0])
|
||||
end
|
||||
|
||||
it "returns false when the value does not match a valid Float" do
|
||||
expect(subject.transform?("foobar")).to be_falsey
|
||||
end
|
||||
|
||||
it "returns false when the value spans multiple lines" do
|
||||
expect(subject.transform?("20.00\nNOT A FLOAT")).to be_falsey
|
||||
end
|
||||
|
||||
it "correctly parses all formats in the YAML spec" do
|
||||
# canonical
|
||||
expect(subject.transform?("6.8523015e+5")).to eq([true, 685230.15])
|
||||
|
||||
# exponentioal
|
||||
expect(subject.transform?("685.230_15e+03")).to eq([true, 685230.15])
|
||||
|
||||
# fixed
|
||||
expect(subject.transform?("685_230.15")).to eq([true, 685230.15])
|
||||
|
||||
# sexagesimal
|
||||
expect(subject.transform?("190:20:30.15")).to eq([true, 685230.15])
|
||||
|
||||
# infinity
|
||||
expect(subject.transform?("-.inf")).to eq([true, (-1.0 / 0.0)])
|
||||
|
||||
# not a number
|
||||
# NOTE: can't use == here since NaN != NaN
|
||||
success, result = subject.transform?(".NaN")
|
||||
expect(success).to be_truthy; expect(result).to be_nan
|
||||
end
|
||||
|
||||
# issue 29
|
||||
it "returns false for the string '.'" do
|
||||
expect(subject.transform?(".")).to be_falsey
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,64 @@
|
||||
require "spec_helper"
|
||||
|
||||
describe SafeYAML::Transform::ToInteger do
|
||||
it "returns true when the value matches a valid Integer" do
|
||||
expect(subject.transform?("10")).to eq([true, 10])
|
||||
end
|
||||
|
||||
it "returns false when the value does not match a valid Integer" do
|
||||
expect(subject.transform?("foobar")).to be_falsey
|
||||
end
|
||||
|
||||
it "returns false when the value spans multiple lines" do
|
||||
expect(subject.transform?("10\nNOT AN INTEGER")).to be_falsey
|
||||
end
|
||||
|
||||
it "allows commas in the number" do
|
||||
expect(subject.transform?("1,000")).to eq([true, 1000])
|
||||
end
|
||||
|
||||
it "correctly parses numbers in octal format" do
|
||||
expect(subject.transform?("010")).to eq([true, 8])
|
||||
end
|
||||
|
||||
it "correctly parses numbers in hexadecimal format" do
|
||||
expect(subject.transform?("0x1FF")).to eq([true, 511])
|
||||
end
|
||||
|
||||
it "defaults to a string for a number that resembles octal format but is not" do
|
||||
expect(subject.transform?("09")).to be_falsey
|
||||
end
|
||||
|
||||
it "correctly parses 0 in decimal" do
|
||||
expect(subject.transform?("0")).to eq([true, 0])
|
||||
end
|
||||
|
||||
it "defaults to a string for a number that resembles hexadecimal format but is not" do
|
||||
expect(subject.transform?("0x1G")).to be_falsey
|
||||
end
|
||||
|
||||
it "correctly parses all formats in the YAML spec" do
|
||||
# canonical
|
||||
expect(subject.transform?("685230")).to eq([true, 685230])
|
||||
|
||||
# decimal
|
||||
expect(subject.transform?("+685_230")).to eq([true, 685230])
|
||||
|
||||
# octal
|
||||
expect(subject.transform?("02472256")).to eq([true, 685230])
|
||||
|
||||
# hexadecimal:
|
||||
expect(subject.transform?("0x_0A_74_AE")).to eq([true, 685230])
|
||||
|
||||
# binary
|
||||
expect(subject.transform?("0b1010_0111_0100_1010_1110")).to eq([true, 685230])
|
||||
|
||||
# sexagesimal
|
||||
expect(subject.transform?("190:20:30")).to eq([true, 685230])
|
||||
end
|
||||
|
||||
# see https://github.com/dtao/safe_yaml/pull/51
|
||||
it "strips out underscores before parsing decimal values" do
|
||||
expect(subject.transform?("_850_")).to eq([true, 850])
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,51 @@
|
||||
require "spec_helper"
|
||||
|
||||
describe SafeYAML::Transform::ToSymbol do
|
||||
def with_symbol_deserialization_value(value)
|
||||
symbol_deserialization_flag = SafeYAML::OPTIONS[:deserialize_symbols]
|
||||
SafeYAML::OPTIONS[:deserialize_symbols] = value
|
||||
|
||||
yield
|
||||
|
||||
ensure
|
||||
SafeYAML::OPTIONS[:deserialize_symbols] = symbol_deserialization_flag
|
||||
end
|
||||
|
||||
def with_symbol_deserialization(&block)
|
||||
with_symbol_deserialization_value(true, &block)
|
||||
end
|
||||
|
||||
def without_symbol_deserialization(&block)
|
||||
with_symbol_deserialization_value(false, &block)
|
||||
end
|
||||
|
||||
it "returns true when the value matches a valid Symbol" do
|
||||
with_symbol_deserialization { expect(subject.transform?(":foo")[0]).to be_truthy }
|
||||
end
|
||||
|
||||
it "returns true when the value matches a valid String+Symbol" do
|
||||
with_symbol_deserialization { expect(subject.transform?(':"foo"')[0]).to be_truthy }
|
||||
end
|
||||
|
||||
it "returns true when the value matches a valid String+Symbol with 's" do
|
||||
with_symbol_deserialization { expect(subject.transform?(":'foo'")[0]).to be_truthy }
|
||||
end
|
||||
|
||||
it "returns true when the value has special characters and is wrapped in a String" do
|
||||
with_symbol_deserialization { expect(subject.transform?(':"foo.bar"')[0]).to be_truthy }
|
||||
end
|
||||
|
||||
it "returns false when symbol deserialization is disabled" do
|
||||
without_symbol_deserialization { expect(subject.transform?(":foo")).to be_falsey }
|
||||
end
|
||||
|
||||
it "returns false when the value does not match a valid Symbol" do
|
||||
with_symbol_deserialization { expect(subject.transform?("foo")).to be_falsey }
|
||||
end
|
||||
|
||||
it "returns false when the symbol does not begin the line" do
|
||||
with_symbol_deserialization do
|
||||
expect(subject.transform?("NOT A SYMBOL\n:foo")).to be_falsey
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user