spec/compel/builder_spec.rb in compel-0.4.2 vs spec/compel/builder_spec.rb in compel-0.4.3

- old
+ new

@@ -215,11 +215,11 @@ context 'String' do subject(:builder) { Compel.string } - context 'in' do + context '#in' do it 'should set in value' do builder.in(['a', 'b']) expect(builder.options[:in][:value].length).to eq 2 @@ -364,10 +364,49 @@ end end + context 'Any' do + + context '#if' do + + it 'should have a proc' do + _proc = Proc.new {|value| value == 1 } + + schema = Compel.any.if(_proc) + + expect(schema.options[:if][:value]).to eq _proc + end + + it 'should have a block with string or symbol value' do + schema = Compel.any.if{:is_valid_one} + expect(schema.options[:if][:value].call).to eq :is_valid_one + + schema = Compel.any.if{'is_valid_one'} + expect(schema.options[:if][:value].call).to eq 'is_valid_one' + end + + it 'should raise_error for missing value' do + expect{ Compel.any.if() }.to \ + raise_error Compel::TypeError, 'invalid proc for if' + end + + it 'should raise_error for invalid proc' do + expect{ Compel.any.if('proc') }.to \ + raise_error Compel::TypeError, 'invalid proc for if' + end + + 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 + + end + + end + end context 'Validate' do context 'Any' do @@ -524,14 +563,14 @@ expect(schema.validate(OpenStruct).errors).to \ include('cannot have length different than 1') end it 'should use custom error message' do - schema = Compel.any.length(2, message: 'not the same size') + schema = Compel.any.length(2, message: '({{value}}) does not have the right size') - expect(schema.validate(12).errors).to \ - include('not the same size') + expect(schema.validate(123).errors).to \ + include('(123) does not have the right size') end end context 'valid' do @@ -550,10 +589,16 @@ result = schema.validate(OpenStruct) expect(result.valid?).to be true end + it 'should validate a constant object' do + schema = Compel.any.length(2) + + expect(schema.validate(12).valid?).to eq(true) + end + end end context '#min_length' do @@ -578,9 +623,138 @@ it 'should use custom error message' do schema = Compel.any.max_length(2, message: 'max is two') expect(schema.validate(123).errors).to \ include('max is two') + end + + end + + end + + context '#if' do + + class CustomValidationsKlass + + attr_reader :value + + def initialize(value) + @value = value + end + + def validate + Compel.any.if{:is_custom_valid?}.validate(value) + end + + def validate_with_lambda(lambda) + Compel.any.if(lambda).validate(value) + end + + private + + def is_custom_valid?(value) + value == assert_value + end + + def assert_value + [1, 2, 3] + end + + end + + context 'invalid' do + + it 'should validate with custom method' do + def is_valid_one(value) + value == 1 + end + + result = Compel.any.if{:is_valid_one}.validate(2) + + expect(result.valid?).to eq(false) + expect(result.errors).to include("'2' 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") + 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") + 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") + end + + it 'should use custom message with parsed value' do + schema = \ + Compel.any.if(Proc.new {|value| value == 2 }, message: 'give me a {{value}}!') + + result = schema.validate(1) + + expect(result.valid?).to eq(false) + expect(result.errors).to include('give me a 1!') + end + + end + + context 'valid' do + + it 'should validate with custom method' do + def is_valid_one(value) + value == 1 + end + + result = Compel.any.if{:is_valid_one}.validate(1) + + expect(result.valid?).to eq(true) + end + + it 'should validate with custom method 1' do + def is_valid_one(value) + value == 1 + end + + result = Compel.any.if{|value| value == 1 }.validate(1) + + expect(result.valid?).to eq(true) + end + + it 'should validate with lambda' do + result = Compel.any.if(Proc.new {|value| value == 2 }).validate(2) + + expect(result.valid?).to eq(true) + end + + it 'should validate within an instance method' do + result = CustomValidationsKlass.new([1, 2, 3]).validate + + expect(result.valid?).to eq(true) + end + + it 'should validate within an instance method' do + result = CustomValidationsKlass.new([1, 2, 3]).validate + expect(result.valid?).to eq(true) + end + + 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 end end