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

- old
+ new

@@ -17,10 +17,32 @@ expect do permitted_params(input, { param_1: Integer }, true) end.to raise_error(InvalidParameterError, "'a' is not a valid Integer") end + it 'should allow all params when no restriction is given' do + input = { param_1: 'a string' } + expect(input).to eq permitted_params(input) + end + + it 'should remove a string when a pemitted is integer' do + input = { param_1: 'a string' } + output = {} + expect(output).to eq permitted_params(input, { param_1: Integer }) + end + + it 'should return an integer when a pemitted is integer' do + input = { param_1: 1 } + expect(input).to eq permitted_params(input, { param_1: Integer }) + end + + it 'should return an integer when a pemitted can be cast into integer' do + input = { param_1: '1' } + output = { param_1: 1 } + expect(output).to eq permitted_params(input, { param_1: Integer }) + end + it 'should return a string when a pemitted is string' do input = { param_1: 'a string' } expect(input).to eq permitted_params(input, { param_1: String }) end @@ -37,21 +59,10 @@ it 'should return a time when a pemitted is time' do input = { param_1: Time.new } expect(input).to eq permitted_params(input, { param_1: Time }) end - it 'should return an integer when a pemitted is integer' do - input = { param_1: 1 } - expect(input).to eq permitted_params(input, { param_1: Integer }) - end - - it 'should return an integer when a pemitted can be cast into integer' do - input = { param_1: '1' } - output = { param_1: 1 } - expect(output).to eq permitted_params(input, { param_1: Integer }) - end - it 'should return a false(boolean) when a pemitted is boolean' do input = { param_1: 'false' } output = { param_1: false } expect(output).to eq permitted_params(input, { param_1: Boolean }) end @@ -71,23 +82,35 @@ input = { param_1: '1, 2' } output = { param_1: %w[1 2] } expect(output).to eq permitted_params(input, { param_1: Array }) end + it 'should return an array when a pemitted is array' do + input = { param_1: '1; 2' } + output = { param_1: %w[1 2] } + expect(output).to eq permitted_params(input, { param_1: Array }, false, { delimiter: ';' }) + end + it 'should return a hash when a pemitted is hash' do input = { param_1: 'a: 1, b: 2' } output = { param_1: { 'a' => '1', 'b' => '2' } } expect(output).to eq permitted_params(input, { param_1: Hash }) end it 'should return a hash when a pemitted is hash' do + input = { param_1: 'a: 1; b: 2' } + output = { param_1: { 'a' => '1', 'b' => '2' } } + expect(output).to eq permitted_params(input, { param_1: Hash }, false, { delimiter: ';' }) + end + + 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 hash' do - input = { param_1: { a: 1 } } + input = { param_1: { a: { b: 1 } } } expect(input).to eq permitted_params(input, { param_1: Hash }) end it 'should return a hash when a pemitted is hash' do input = { param_1: { "a": 1 } } @@ -112,20 +135,16 @@ it 'returns the paramter without casting if Any' do input = { param_1: '1' } output = { param_1: '1' } expect(output).to eq permitted_params(input, { param_1: Any }) - input = { param_1: TestClass.new } - expect(input).to eq permitted_params(input, { param_1: Any }) - end + test_class = TestClass.new + input = { param_1: test_class } + output = { param_1: test_class } + expect(output).to eq permitted_params(input, { param_1: Any }) - it 'should remove a string when a pemitted is integer' do - input = { param_1: 'a string' } - output = {} - expect(output).to eq permitted_params(input, { param_1: Integer }) - end - - it 'should allow all params when no restriction is given' do - input = { param_1: 'a string' } - expect(input).to eq permitted_params(input) + 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 }) end end