0
@@ -53,29 +53,116 @@ describe DataMapper::Validate::NumericValidator do
0
- it "should validate with autovalidate" do
0
- include DataMapper::Resource
0
- property :id, Integer, :serial => true
0
- property :scales, Integer
0
- property :average_weight, Float
0
+ describe 'auto validation' do
0
+ include DataMapper::Resource
0
+ property :id, Integer, :serial => true
0
+ property :scales, Integer
0
- include DataMapper::Resource
0
- property :id, Integer, :serial => true
0
- property :scales, Integer
0
- property :average_weight, Float, :scale => 10, :precision => 0, :auto_validation => false
0
- validates_is_number :average_weight
0
+ describe 'with default scale and precision' do
0
+ class RobotFish < Fish
0
+ property :average_weight, Float
0
- fish1.scales = fish2.scales = 1
0
- fish1.average_weight = fish2.average_weight = 20.22
0
- fish1.valid?.should == true
0
- fish2.valid?.should == true
0
+ @robot_fish = RobotFish.new
0
+ it 'should allow up to 10 digits before the decimal' do
0
+ @robot_fish.average_weight = 0
0
+ @robot_fish.should be_valid
0
+ @robot_fish.average_weight = 9_999_999_999
0
+ @robot_fish.should be_valid
0
+ @robot_fish.average_weight = 10_000_000_000
0
+ @robot_fish.should_not be_valid
0
+ it 'should allow 0 digits of precision after the decimal' do
0
+ @robot_fish.average_weight = 0
0
+ @robot_fish.should be_valid
0
+ it 'should allow 1 digit of precision after the decimal if it is a zero' do
0
+ @robot_fish.average_weight = 0.0
0
+ @robot_fish.should be_valid
0
+ @robot_fish.average_weight = 9_999_999_999.0
0
+ @robot_fish.should be_valid
0
+ @robot_fish.average_weight = 0.1
0
+ @robot_fish.should_not be_valid
0
+ describe 'with a scale of 4 and a precision of 2' do
0
+ property :average_weight, Float, :scale => 4, :precision => 2
0
+ @gold_fish = GoldFish.new
0
+ it 'should allow up to 2 digits before the decimal' do
0
+ @gold_fish.average_weight = 0
0
+ @gold_fish.should be_valid
0
+ @gold_fish.average_weight = 99
0
+ @gold_fish.should be_valid
0
+ @gold_fish.average_weight = 100
0
+ @gold_fish.should_not be_valid
0
+ it 'should allow 2 digits of precision after the decimal' do
0
+ @gold_fish.average_weight = 99.99
0
+ @gold_fish.should be_valid
0
+ @gold_fish.average_weight = 99.999
0
+ @gold_fish.should_not be_valid
0
+ describe 'with a scale of 2 and a precision of 2' do
0
+ class SilverFish < Fish
0
+ property :average_weight, Float, :scale => 2, :precision => 2
0
+ @silver_fish = SilverFish.new
0
+ it 'should allow a 0 before the decimal' do
0
+ @silver_fish.average_weight = 0
0
+ @silver_fish.should be_valid
0
+ @silver_fish.average_weight = 0.1
0
+ @silver_fish.should be_valid
0
+ @silver_fish.average_weight = 1
0
+ @silver_fish.should_not be_valid
0
+ it 'should allow 2 digits of precision after the decimal' do
0
+ @silver_fish.average_weight = 0.99
0
+ @silver_fish.should be_valid
0
+ @silver_fish.average_weight = 0.999
0
+ @silver_fish.should_not be_valid