spec/structure_spec.rb in structure-0.3.1 vs spec/structure_spec.rb in structure-0.4.0
- old
+ new
@@ -5,22 +5,10 @@
it "is enumerable" do
person.should respond_to :map
end
- context "when frozen" do
- before do
- person.freeze
- end
-
- it "raises an error" do
- expect do
- person.name = 'Joe'
- end.to raise_error TypeError
- end
- end
-
describe ".key" do
it "defines accessors" do
%w{name name=}.each { |method| person.should respond_to method }
end
@@ -110,10 +98,46 @@
expect do
Person.key :married, :type => Boolean, :default => false
end.not_to raise_error
end
end
+
+ context "when typecasting a set value" do
+ before(:all) do
+ Person.key :vegetarian, :type => Boolean
+ end
+
+ it "typecasts 'true' to true" do
+ person.vegetarian = 'true'
+ person.vegetarian.should be_true
+ end
+
+ it "typecasts 'TRUE' to true" do
+ person.vegetarian = 'TRUE'
+ person.vegetarian.should be_true
+ end
+
+ it "typecasts '1' to true" do
+ person.vegetarian = '1'
+ person.vegetarian.should be_true
+ end
+
+ it "typecasts all other strings to false" do
+ person.vegetarian = 'foo'
+ person.vegetarian.should be_false
+ end
+
+ it "typecasts 0 to false" do
+ person.vegetarian = 0
+ person.vegetarian.should be_false
+ end
+
+ it "typecasts all other integers to true" do
+ person.vegetarian = 1
+ person.vegetarian.should be_true
+ end
+ end
end
context "when type is Hash" do
before(:all) do
Person.key :education, :type => Hash
@@ -136,9 +160,21 @@
end
context "when a default is not specified" do
it "defaults to nil" do
person.age.should be_nil
+ end
+ end
+
+ context "when frozen" do
+ before do
+ person.freeze
+ end
+
+ it "raises an error" do
+ expect do
+ person.name = 'Joe'
+ end.to raise_error TypeError
end
end
context "when setting the value of an attribute to nil" do
it "does not typecast the value" do