spec/compel/builder_spec.rb in compel-0.4.3 vs spec/compel/builder_spec.rb in compel-0.5.0
- old
+ new
@@ -399,10 +399,15 @@
it 'should raise_error for invalid proc arity' do
expect{ Compel.any.if(Proc.new {|a, b| a == b }) }.to \
raise_error Compel::TypeError, 'invalid proc for if'
end
+ it 'should raise_error for invalid proc 1' do
+ expect{ Compel.any.if(1) }.to \
+ raise_error Compel::TypeError, 'invalid proc for if'
+ end
+
end
end
end
@@ -669,34 +674,34 @@
end
result = Compel.any.if{:is_valid_one}.validate(2)
expect(result.valid?).to eq(false)
- expect(result.errors).to include("'2' is invalid")
+ expect(result.errors).to include('is invalid')
end
it 'should validate with lambda' do
result = Compel.any.if(Proc.new {|value| value == 2 }).validate(1)
expect(result.valid?).to eq(false)
- expect(result.errors).to include("'1' is invalid")
+ expect(result.errors).to include('is invalid')
end
it 'should validate within an instance method' do
result = CustomValidationsKlass.new(1).validate
expect(result.valid?).to eq(false)
- expect(result.errors).to include("'1' is invalid")
+ expect(result.errors).to include('is invalid')
end
it 'should validate within an instance method 1' do
result = \
CustomValidationsKlass.new('two')
.validate_with_lambda(Proc.new {|value| value == [1, 2, 3] })
expect(result.valid?).to eq(false)
- expect(result.errors).to include("'two' is invalid")
+ expect(result.errors).to include('is invalid')
end
it 'should use custom message with parsed value' do
schema = \
Compel.any.if(Proc.new {|value| value == 2 }, message: 'give me a {{value}}!')
@@ -705,10 +710,39 @@
expect(result.valid?).to eq(false)
expect(result.errors).to include('give me a 1!')
end
+ it 'should validate a date value' do
+ to_validate = '1969-01-01T00:00:00'
+
+ def validate_time(value)
+ value > Time.at(0)
+ end
+
+ result = Compel.time.if{:validate_time}.validate(to_validate)
+
+ expect(result.valid?).to eq(false)
+ expect(result.errors).to include('is invalid')
+ end
+
+ it 'should raise_error for invalid custom method arity' do
+ def custom_method_arity_two(value, extra_arg)
+ false
+ end
+
+ def custom_method_arity_zero
+ false
+ end
+
+ expect{ Compel.integer.if{:custom_method_arity_two}.validate(1) }.to \
+ raise_error ArgumentError
+
+ expect{ Compel.integer.if{:custom_method_arity_zero}.validate(1) }.to \
+ raise_error ArgumentError
+ end
+
end
context 'valid' do
it 'should validate with custom method' do
@@ -750,9 +784,21 @@
it 'should validate within an instance method 1' do
result = \
CustomValidationsKlass.new([1, 2, 3])
.validate_with_lambda(Proc.new {|value| value == [1, 2, 3] })
+
+ expect(result.valid?).to eq(true)
+ end
+
+ it 'should validate a date value' do
+ to_validate = '1969-01-01T00:00:00'
+
+ def validate_time(value)
+ value < Time.at(0)
+ end
+
+ result = Compel.time.if{:validate_time}.validate(to_validate)
expect(result.valid?).to eq(true)
end
end