Sha256: 8c9ce57074fd3b6de319170d16eacdbf43d07fc8dc0acc97522b12e658a63305

Contents?: true

Size: 1.83 KB

Versions: 3

Compression:

Stored size: 1.83 KB

Contents

require 'spec_helper'
require 'input_sanitizer/extended_converters'

describe InputSanitizer::AllowNil do
  it "passes blanks" do
    lambda { |_| 1 }.extend(InputSanitizer::AllowNil).call("").should be_nil
  end

  it "passes things the extended sanitizer passes" do
    lambda { |_| :something }.extend(InputSanitizer::AllowNil).call(:stuff).
      should eq(:something)
  end

  it "raises error if the extended sanitizer raises error" do
    action = lambda do
      lambda { |_| raise "Some error" }.extend(InputSanitizer::AllowNil).call(:stuff)
    end

    action.should raise_error
  end
end

describe InputSanitizer::PositiveIntegerConverter do
  let(:converter) { InputSanitizer::PositiveIntegerConverter.new }

  it "raises error if integer less than zero" do
    lambda { converter.call("-3") }.should raise_error(InputSanitizer::ConversionError)
  end

  it "raises error if integer equals zero" do
    lambda { converter.call("0") }.should raise_error(InputSanitizer::ConversionError)
  end
end

describe InputSanitizer::CommaJoinedIntegersConverter do
  let(:converter) { InputSanitizer::CommaJoinedIntegersConverter.new }

  it "parses to array of ids" do
    converter.call("1,2,3,5").should == [1, 2, 3, 5]
  end

  it "raises on invalid character" do
    lambda { converter.call(":") }.should raise_error(InputSanitizer::ConversionError)
  end
end

describe InputSanitizer::SpecificValuesConverter do
  let(:converter) { InputSanitizer::SpecificValuesConverter.new([:a, :b]) }

  it "converts valid value to symbol" do
    converter.call("b").should == :b
  end

  it "raises on invalid value" do
    lambda { converter.call("c") }.should raise_error(InputSanitizer::ConversionError)
  end

  it "converts valid value to string" do
    converter = InputSanitizer::SpecificValuesConverter.new(["a", "b"])
    converter.call("a").should == "a"
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
input_sanitizer-0.2.0 spec/extended_converters_spec.rb
input_sanitizer-0.1.10 spec/extended_converters_spec.rb
input_sanitizer-0.1.9 spec/extended_converters_spec.rb