aboutsummaryrefslogtreecommitdiffstats
path: root/spec/attributes/boolean_spec.rb
blob: 12b243e828e80d3117bf5506f17052a6dc7a7be9 (plain) (blame)
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
require 'spec_helper'

module Arel
  describe "Attributes::Boolean" do

    before :all do
      @relation = Model.build do |r|
        r.engine Testing::Engine.new
        r.attribute :awesome, Attributes::Boolean
      end
    end

    def type_cast(val)
      @relation[:awesome].type_cast(val)
    end

    describe "#type_cast" do
      it "returns same value if passed a boolean" do
        val = true
        type_cast(val).should eql(val)
      end

      it "returns boolean representation (false) of nil" do
        type_cast(nil).should eql(false)
      end

      it "returns boolean representation of 'true', 'false'" do
        type_cast('true').should eql(true)
        type_cast('false').should eql(false)
      end

      it "returns boolean representation of :true, :false" do
        type_cast(:true).should eql(true)
        type_cast(:false).should eql(false)
      end

      it "returns boolean representation of 0, 1" do
        type_cast(1).should == true
        type_cast(0).should == false
      end

      it "calls #to_s on arbitrary objects" do
        obj = Object.new
        obj.extend Module.new { def to_s ; 'true' ; end }
        type_cast(obj).should == true
      end

      [ Object.new, 'string', '00.0', 5 ].each do |value|
        it "raises exception when attempting type_cast of non-boolean value #{value.inspect}" do
          lambda do
            type_cast(value)
          end.should raise_error(TypecastError, /could not typecast/)
        end
      end
    end
  end
end