aboutsummaryrefslogtreecommitdiffstats
path: root/spec/attributes/boolean_spec.rb
diff options
context:
space:
mode:
authorCarl Lerche <carllerche@mac.com>2010-03-12 14:34:13 -0800
committerCarl Lerche <carllerche@mac.com>2010-03-12 14:34:13 -0800
commit61916e408d86d19d1659cd8042de6503aecc6c98 (patch)
tree28cbadb9b39b44d993a033af5cf254655cdab58a /spec/attributes/boolean_spec.rb
parent83c27c0b5e2e341307b7a160d831fb930a9552b4 (diff)
downloadrails-61916e408d86d19d1659cd8042de6503aecc6c98.tar.gz
rails-61916e408d86d19d1659cd8042de6503aecc6c98.tar.bz2
rails-61916e408d86d19d1659cd8042de6503aecc6c98.zip
Add a bunch of specs for attribute type casting.
Diffstat (limited to 'spec/attributes/boolean_spec.rb')
-rw-r--r--spec/attributes/boolean_spec.rb57
1 files changed, 57 insertions, 0 deletions
diff --git a/spec/attributes/boolean_spec.rb b/spec/attributes/boolean_spec.rb
new file mode 100644
index 0000000000..bb7eeb886d
--- /dev/null
+++ b/spec/attributes/boolean_spec.rb
@@ -0,0 +1,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 \ No newline at end of file