Skip to content

Commit

Permalink
Improve HashChecker. Reuse ClassChecker.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcelicalderon committed Apr 4, 2020
1 parent fce8114 commit 0753bb2
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 19 deletions.
Expand Up @@ -3,7 +3,7 @@ module MountMethod
module OptionSanitizers
class ClassChecker
def initialize(klass)
@klass = klass
@klass_array = Array(klass)
end

def call!(value, key)
Expand All @@ -13,8 +13,9 @@ def call!(value, key)
raise GraphqlDevise::InvalidMountOptionsError, "`#{key}` option has an invalid value. Class expected."
end

unless value.ancestors.include?(@klass)
raise GraphqlDevise::InvalidMountOptionsError, "`#{key}` option has an invalid value. #{@klass} expected. Got #{value}."
unless @klass_array.any? { |klass| value.ancestors.include?(klass) }
raise GraphqlDevise::InvalidMountOptionsError,
"`#{key}` option has an invalid value. #{@klass_array.join(', ')} or descendants expected. Got #{value}."
end

value
Expand Down
Expand Up @@ -14,9 +14,7 @@ def call!(value, key)
raise GraphqlDevise::InvalidMountOptionsError, "`#{key}` option has an invalid value. Hash expected. Got #{value.class}."
end

unless value.all? { |_, element| element.instance_of?(Class) && @element_type_array.any? { |type| element.ancestors.include?(type) } }
raise GraphqlDevise::InvalidMountOptionsError, "`#{key}` option has invalid elements. [#{@element_type_array.join(', ')}] or descendants expected. Got #{value}."
end
value.each { |internal_key, klass| ClassChecker.new(@element_type_array).call!(klass, "#{key} -> #{internal_key}") }

value
end
Expand Down
Expand Up @@ -16,15 +16,15 @@
context 'when provided value is not an array' do
let(:value) { 'not an array' }

it 'railses an error' do
it 'raises an error' do
expect { clean_value }.to raise_error(GraphqlDevise::InvalidMountOptionsError, "`#{key}` option has an invalid value. Array expected.")
end
end

context 'when provided array contains invalid elements' do
let(:value) { [:valid, 'invalid'] }

it 'railses an error' do
it 'raises an error' do
expect { clean_value }.to raise_error(GraphqlDevise::InvalidMountOptionsError, "`#{key}` option has invalid elements. #{element_type} expected.")
end
end
Expand Down
Expand Up @@ -16,16 +16,16 @@
context 'when provided value is not a class' do
let(:value) { 'I\'m not a class' }

it 'railses an error' do
it 'raises an error' do
expect { clean_value }.to raise_error(GraphqlDevise::InvalidMountOptionsError, "`#{key}` option has an invalid value. Class expected.")
end
end

context 'when provided class is not of the expected type' do
let(:value) { String }

it 'railses an error' do
expect { clean_value }.to raise_error(GraphqlDevise::InvalidMountOptionsError, "`#{key}` option has an invalid value. #{expected_class} expected. Got String.")
it 'raises an error' do
expect { clean_value }.to raise_error(GraphqlDevise::InvalidMountOptionsError, "`#{key}` option has an invalid value. #{expected_class} or descendants expected. Got String.")
end
end

Expand Down
26 changes: 19 additions & 7 deletions spec/services/mount_method/option_sanitizers/hash_checker_spec.rb
Expand Up @@ -7,7 +7,7 @@
let(:key) { :any_option }

context 'when a single valid type is provided' do
let(:element_type) { String }
let(:element_type) { Numeric }

context 'when no value is provided' do
let(:value) { nil }
Expand All @@ -24,22 +24,28 @@
end

context 'when provided hash contains invalid elements' do
let(:value) { { valid: String, invalid: Float } }
let(:value) { { valid: Float, invalid: String } }

it 'raises an error' do
expect { clean_value }.to raise_error(GraphqlDevise::InvalidMountOptionsError, "`#{key}` option has invalid elements. [#{element_type}] or descendants expected. Got #{value}.")
expect { clean_value }.to raise_error(GraphqlDevise::InvalidMountOptionsError, "`#{key} -> invalid` option has an invalid value. #{element_type} or descendants expected. Got #{String}.")
end
end

context 'when provided array contains all valid elements' do
let(:value) { { valid1: String, valid2: String } }
let(:value) { { valid1: Numeric, valid2: Numeric } }

it { is_expected.to eq(value) }
end

context 'when provided class has the expected type as an acestor' do
let(:value) { { valid: Float } }

it { is_expected.to eq(value) }
end
end

context 'when multiple value types are allowed' do
let(:element_type) { [String, Float] }
let(:element_type) { [String, Numeric] }

context 'when no value is provided' do
let(:value) { nil }
Expand All @@ -48,7 +54,13 @@
end

context 'when provided array contains all valid elements' do
let(:value) { { valid1: String, valid2: Float } }
let(:value) { { valid1: String, valid2: Numeric } }

it { is_expected.to eq(value) }
end

context 'when provided class has the expected type as an acestor' do
let(:value) { { valid: Float } }

it { is_expected.to eq(value) }
end
Expand All @@ -65,7 +77,7 @@
let(:value) { { valid: String, invalid: StandardError } }

it 'raises an error' do
expect { clean_value }.to raise_error(GraphqlDevise::InvalidMountOptionsError, "`#{key}` option has invalid elements. [#{element_type.join(', ')}] or descendants expected. Got #{value}.")
expect { clean_value }.to raise_error(GraphqlDevise::InvalidMountOptionsError, "`#{key} -> invalid` option has an invalid value. #{element_type.join(', ')} or descendants expected. Got #{StandardError}.")
end
end
end
Expand Down
Expand Up @@ -16,7 +16,7 @@
context 'when provided value is not a String' do
let(:value) { 1000 }

it 'railses an error' do
it 'raises an error' do
expect { clean_value }.to raise_error(GraphqlDevise::InvalidMountOptionsError, "`#{key}` option has an invalid value. String expected.")
end
end
Expand Down

0 comments on commit 0753bb2

Please sign in to comment.