Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 50e209f10c
Fetching contributors…

Cannot retrieve contributors at this time

file 70 lines (59 sloc) 2.472 kb
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 65 66 67 68 69 70
require "spec_helper"

describe "Common Fields" do

  let(:subject) { described_class.new('') }

  describe Bsm::Constrainable::Field::Number do
    it { subject.class.should have(9).operators }
    it { subject.convert("1").should == 1 }
    it { subject.convert("1a").should == nil }
    it { subject.convert([1, "2", 3]).should == [1, 2, 3] }
    it { subject.convert("1.5").should == 1.5 }
    it { subject.convert("a").should be_nil }
    it { subject.convert("").should be_nil }
    it { subject.convert(" ").should be_nil }
  end

  describe Bsm::Constrainable::Field::Integer do
    it { subject.class.should have(9).operators }
    it { should be_a(Bsm::Constrainable::Field::Number) }
    it { subject.convert("1.5").should == 1 }
  end

  describe Bsm::Constrainable::Field::Decimal do
    it { subject.class.should have(9).operators }
    it { should be_a(Bsm::Constrainable::Field::Number) }
  end

  describe Bsm::Constrainable::Field::String do
    it { subject.class.should have(2).operators }
    it { subject.convert(1).should == "1" }
    it { subject.convert(["a", 1]).should == ["a", "1"] }
    it { subject.convert("").should be_nil }
    it {
      subject.options[:allow_blank] = true
      subject.convert("").should == ""
    }
  end

  describe Bsm::Constrainable::Field::Timestamp do
    it { subject.class.should have(7).operators }
    it { subject.convert("a").should == nil }
    it { subject.convert("2010-01-01").should == Time.utc(2010) }
    it { subject.convert("2011-11-11 11:11").should == Time.utc(2011, 11, 11, 11, 11) }
  end

  describe Bsm::Constrainable::Field::Datetime do
    it { subject.class.should have(7).operators }
  end

  describe Bsm::Constrainable::Field::Date do
    it { subject.class.should have(7).operators }
    it { subject.convert("a").should == nil }
    it { subject.convert("2010-01-01").should == Date.civil(2010) }
    it { subject.convert("2011-11-11 11:11").should == Date.civil(2011, 11, 11) }
  end

  describe Bsm::Constrainable::Field::Boolean do
    it { subject.class.should have(2).operators }
    it { subject.convert("true").should == true }
    it { subject.convert("t").should == true }
    it { subject.convert("1").should == true }
    it { subject.convert(1).should == true }
    it { subject.convert("false").should == false }
    it { subject.convert("0").should == false }
    it { subject.convert("").should == nil }
    it { subject.convert("invalid").should == nil }
  end

end
Something went wrong with that request. Please try again.