spec/permit_params_spec.rb in sinatra-my-params-0.0.9 vs spec/permit_params_spec.rb in sinatra-my-params-0.0.10
- old
+ new
@@ -5,144 +5,132 @@
require 'rspec'
require 'rack/test'
include PermitParams
-describe 'exceptions' do
+describe 'behaviours' do
it 'should raise error when at least one param is invalid' do
- input = { param_1: 'a' }
+ input = { param: 'a' }
expect do
- permitted_params(input, { param_1: Integer }, true)
+ permitted_params(input, { param: 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' }
+ input = { param: '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' }
+ it 'should remove a string when permitted param is integer' do
+ input = { param: 'a string' }
output = {}
- expect(output).to eq permitted_params(input, { param_1: Integer })
+ expect(output).to eq permitted_params(input, { param: 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 })
+ it 'should return an integer when permitted param is integer' do
+ input = { param: 1 }
+ expect(input).to eq permitted_params(input, { param: 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 })
+ it 'should return an integer when permitted param can be cast into integer' do
+ input = { param: '1' }
+ output = { param: 1 }
+ expect(output).to eq permitted_params(input, { param: 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 })
+ it 'should return a string when permitted param is string' do
+ input = { param: 'a string' }
+ expect(input).to eq permitted_params(input, { param: String })
end
- it 'should return a float when a pemitted is float' do
- input = { param_1: 10.0 }
- expect(input).to eq permitted_params(input, { param_1: Float })
+ it 'should return a float when permitted param is float' do
+ input = { param: 10.0 }
+ expect(input).to eq permitted_params(input, { param: Float })
end
- it 'should return a date when a pemitted is date' do
- input = { param_1: Date.new }
- expect(input).to eq permitted_params(input, { param_1: Date })
+ it 'should return a date when permitted param is date' do
+ input = { param: Date.new }
+ expect(input).to eq permitted_params(input, { param: Date })
end
- 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 })
+ it 'should return a time when permitted param is time' do
+ input = { param: Time.new }
+ expect(input).to eq permitted_params(input, { param: Time })
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 })
+ it 'should return a false(boolean) when permitted param is boolean' do
+ input = { param: 'false' }
+ output = { param: false }
+ expect(output).to eq permitted_params(input, { param: Boolean })
end
- it 'should return a true(boolean) when a pemitted is boolean' do
- input = { param_1: 'true' }
- output = { param_1: true }
- expect(output).to eq permitted_params(input, { param_1: Boolean })
+ it 'should return a true(boolean) when permitted param is boolean' do
+ input = { param: 'true' }
+ output = { param: true }
+ expect(output).to eq permitted_params(input, { param: Boolean })
end
- it 'should return an array when a pemitted is array' do
- input = { param_1: [1, 2] }
- expect(input).to eq permitted_params(input, { param_1: Array })
+ it 'should return an array when permitted param is array' do
+ input = { param: [1, 2] }
+ expect(input).to eq permitted_params(input, { param: 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 })
+ it 'should return an array when permitted param is array' do
+ input = { param: '1, 2' }
+ output = { param: %w[1 2] }
+ expect(output).to eq permitted_params(input, { param: 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: ';' })
+ it 'should return an array when permitted param is array' do
+ input = { param: '1; 2' }
+ output = { param: %w[1 2] }
+ expect(output).to eq permitted_params(input, { param: 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 })
+ it 'should return an empty hash when permitted param is array with wrong delimiter' do
+ input = { param: '1; 2' }
+ output = {}
+ expect(output).to eq permitted_params(input, { param: 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 }, false, { delimiter: ';' })
+ it 'should return a hash when permitted param is hash' do
+ input = { param: 'a: 1, b: 2' }
+ output = { param: { 'a' => '1', 'b' => '2' } }
+ expect(output).to eq permitted_params(input, { param: Hash })
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 })
+ it 'should return a hash when permitted param is hash' do
+ input = { param: 'a: 1; b: 2' }
+ output = { param: { 'a' => '1', 'b' => '2' } }
+ expect(output).to eq permitted_params(input, { param: Hash }, false, { delimiter: ';' })
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 } }
- )
+ it 'should return an empty hash when permitted param is hash with wrong separator' do
+ input = { param: 'a: 1; b: 2' }
+ output = {}
+ expect(output).to eq permitted_params(input, { param: Hash }, false, { separator: '.' })
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 } } }
- )
+ it 'should return an empty hash when permitted param is hash with wrong delimiter' do
+ input = { param: 'a: 1; b: 2' }
+ output = {}
+ expect(output).to eq permitted_params(input, { param: Hash }, false, { delimiter: ',' })
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 } }
- )
+ it 'should return a hash when permitted param is hash' do
+ input = { param: { a: 1 } }
+ expect(input).to eq permitted_params(input, { param: Hash })
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 })
+ it 'should return a hash when permitted param is hash' do
+ input = { param: { a: { b: 1 } } }
+ expect(input).to eq permitted_params(input, { param: Hash })
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 })
+ it 'should return a hash when permitted param is hash' do
+ input = { param: { "a": 1 } }
+ expect(input).to eq permitted_params(input, { param: Hash })
end
it 'should return a several types for several inputs' do
input = { number: 1, string: 'string', bol: 'true', array: [1, 2], hsh: { a: 3 } }
output = { number: 1, string: 'string', bol: true, array: [1, 2], hsh: { a: 3 } }
@@ -165,23 +153,23 @@
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 })
+ input = { param: '1' }
+ output = { param: '1' }
+ expect(output).to eq permitted_params(input, { param: Any })
input = {
- param_1: TestClass.new(some_attribute: 'a string')
+ param: 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
+ output = permitted_params(input, { param: Any })
+ expect(input[:param].some_attribute).to eq output[:param].some_attribute
input = {
- param_1: TestClass.new(some_attribute: 1),
- param_2: 2
+ param: TestClass.new(some_attribute: 1),
+ antoher_param: 2
}
- output = permitted_params(input, { param_1: Any })
- expect(input[:param_1].some_attribute).to eq output[:param_1].some_attribute
+ output = permitted_params(input, { param: Any })
+ expect(input[:param].some_attribute).to eq output[:param].some_attribute
end
end