Sha256: 9f2e02db9550c8ecd389d35d383fba68b595449b3fc05ea099164ba4a05a304c

Contents?: true

Size: 1.81 KB

Versions: 4

Compression:

Stored size: 1.81 KB

Contents

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

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
whos_dated_who-0.1.0 vendor/bundle/gems/safe_yaml-1.0.3/spec/transform/to_integer_spec.rb
whos_dated_who-0.0.1 vendor/bundle/gems/safe_yaml-1.0.3/spec/transform/to_integer_spec.rb
safe_yaml-1.0.3 spec/transform/to_integer_spec.rb
safe_yaml-1.0.2 spec/transform/to_integer_spec.rb