spec/permit_params_spec.rb in sinatra-my-params-0.0.8 vs spec/permit_params_spec.rb in sinatra-my-params-0.0.9

- old
+ new

@@ -6,14 +6,10 @@ require 'rack/test' include PermitParams describe 'exceptions' do - before do - class TestClass; end - end - it 'should raise error when at least one param is invalid' do input = { param_1: 'a' } expect do permitted_params(input, { param_1: Integer }, true) end.to raise_error(InvalidParameterError, "'a' is not a valid Integer") @@ -105,10 +101,40 @@ it 'should return a hash when a pemitted is hash' do input = { param_1: { a: 1 } } expect(input).to eq permitted_params(input, { param_1: Hash }) end + it 'should return a hash when a pemitted is shape' do + input = { param_1: { a: 1, b: 2 } } + expect(input).to eq permitted_params( + input, + { param_1: Shape }, + false, + { shape: { a: Integer, b: Integer } } + ) + end + + it 'should return a hash when a pemitted is shape(deep)' do + input = { param_1: { a: { b: 2 } } } + expect(input).to eq permitted_params( + input, + { param_1: Shape }, + false, + { shape: { a: { b: Integer } } } + ) + end + + it 'should return a empty when a pemitted is shape not defined' do + input = { param_1: { a: 1, b: 2 } } + expect({}).to eq permitted_params( + input, + { param_1: Shape }, + false, + { shape: { a: Integer } } + ) + end + it 'should return a hash when a pemitted is hash' do input = { param_1: { a: { b: 1 } } } expect(input).to eq permitted_params(input, { param_1: Hash }) end @@ -131,20 +157,31 @@ } ) end it 'returns the paramter without casting if Any' do + class TestClass + attr_accessor :some_attribute + + def initialize(some_attribute: nil) + @some_attribute = some_attribute + end + end + input = { param_1: '1' } output = { param_1: '1' } expect(output).to eq permitted_params(input, { param_1: Any }) - test_class = TestClass.new - input = { param_1: test_class } - output = { param_1: test_class } - expect(output).to eq permitted_params(input, { param_1: Any }) + input = { + param_1: TestClass.new(some_attribute: 'a string') + } + output = permitted_params(input, { param_1: Any }) + expect(input[:param_1].some_attribute).to eq output[:param_1].some_attribute - test_class = TestClass.new - input = { param_1: test_class, param_2: 2 } - output = { param_1: test_class } - expect(output).to eq permitted_params(input, { param_1: Any }) + input = { + param_1: TestClass.new(some_attribute: 1), + param_2: 2 + } + output = permitted_params(input, { param_1: Any }) + expect(input[:param_1].some_attribute).to eq output[:param_1].some_attribute end end