spec/renum_spec.rb in renum-1.3.1 vs spec/renum_spec.rb in renum-1.4.0
- old
+ new
@@ -1,6 +1,6 @@
-require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+require 'spec_helper'
enum :Status, [ :NOT_STARTED, :IN_PROGRESS, :COMPLETE ]
describe "basic enum" do
@@ -41,10 +41,14 @@
end
it "makes values comparable" do
Color::RED.should < Color::GREEN
end
+
+ it "doesn't let Comparable's == confuse things" do
+ Color[0].should_not == Status[0]
+ end
end
module MyNamespace
enum :FooValue, %w( Bar Baz Bat )
end
@@ -156,16 +160,37 @@
end
end
describe "prevention of subtle and annoying bugs" do
it "prevents you modifying the values array" do
- lambda { Color.values << 'some crazy value' }.should raise_error(TypeError, /can't modify frozen/)
+ expect { Color.values << 'some crazy value' }.to raise_error(/modify frozen/)
end
it "prevents you modifying the name hash" do
- lambda { Color.values_by_name['MAGENTA'] = 'some crazy value' }.should raise_error(TypeError, /can't modify frozen/)
+ expect { Color.values_by_name['MAGENTA'] = 'some crazy value' }.to raise_error(/modify frozen/)
end
it "prevents you modifying the name of a value" do
- lambda { Color::RED.name << 'dish-Brown' }.should raise_error(TypeError, /can't modify frozen/)
+ expect { Color::RED.name << 'dish-Brown' }.to raise_error(/modify frozen/)
+ end
+end
+
+describe "serialization (for ActiveRecord or what-have-you)" do
+ it "can serialize to and deserialize from its name string" do
+ serializer = Renum::NameSerializer.new Color
+ serializer.dump(Color::RED).should == "RED"
+ serializer.dump("RED").should == "RED" # for convenient param assignment
+ serializer.load("RED").should == Color::RED
+
+ serializer.dump(nil).should == nil
+ serializer.load(nil).should == nil
+ end
+
+ it "can serialize to and deserialize from its positional index" do
+ serializer = Renum::IndexSerializer.new Color
+ serializer.dump(Color::RED).should == 0
+ serializer.load(0).should == Color::RED
+
+ serializer.dump(nil).should == nil
+ serializer.load(nil).should == nil
end
end