From 0753bb2b8584d562717c743d85ea8507e39d7a83 Mon Sep 17 00:00:00 2001 From: Mario Celi Date: Sun, 5 Apr 2020 00:43:17 +0200 Subject: [PATCH] Improve HashChecker. Reuse ClassChecker. --- .../option_sanitizers/class_checker.rb | 7 ++--- .../option_sanitizers/hash_checker.rb | 4 +-- .../option_sanitizers/array_checker_spec.rb | 4 +-- .../option_sanitizers/class_checker_spec.rb | 6 ++--- .../option_sanitizers/hash_checker_spec.rb | 26 ++++++++++++++----- .../option_sanitizers/string_checker_spec.rb | 2 +- 6 files changed, 30 insertions(+), 19 deletions(-) diff --git a/lib/graphql_devise/mount_method/option_sanitizers/class_checker.rb b/lib/graphql_devise/mount_method/option_sanitizers/class_checker.rb index a849a628..6cc8802e 100644 --- a/lib/graphql_devise/mount_method/option_sanitizers/class_checker.rb +++ b/lib/graphql_devise/mount_method/option_sanitizers/class_checker.rb @@ -3,7 +3,7 @@ module MountMethod module OptionSanitizers class ClassChecker def initialize(klass) - @klass = klass + @klass_array = Array(klass) end def call!(value, key) @@ -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 diff --git a/lib/graphql_devise/mount_method/option_sanitizers/hash_checker.rb b/lib/graphql_devise/mount_method/option_sanitizers/hash_checker.rb index ff510bcc..5d298932 100644 --- a/lib/graphql_devise/mount_method/option_sanitizers/hash_checker.rb +++ b/lib/graphql_devise/mount_method/option_sanitizers/hash_checker.rb @@ -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 diff --git a/spec/services/mount_method/option_sanitizers/array_checker_spec.rb b/spec/services/mount_method/option_sanitizers/array_checker_spec.rb index 15ebd70b..2096ac8b 100644 --- a/spec/services/mount_method/option_sanitizers/array_checker_spec.rb +++ b/spec/services/mount_method/option_sanitizers/array_checker_spec.rb @@ -16,7 +16,7 @@ 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 @@ -24,7 +24,7 @@ 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 diff --git a/spec/services/mount_method/option_sanitizers/class_checker_spec.rb b/spec/services/mount_method/option_sanitizers/class_checker_spec.rb index 2c518b14..5ae9e07b 100644 --- a/spec/services/mount_method/option_sanitizers/class_checker_spec.rb +++ b/spec/services/mount_method/option_sanitizers/class_checker_spec.rb @@ -16,7 +16,7 @@ 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 @@ -24,8 +24,8 @@ 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 diff --git a/spec/services/mount_method/option_sanitizers/hash_checker_spec.rb b/spec/services/mount_method/option_sanitizers/hash_checker_spec.rb index 10396244..e0f24501 100644 --- a/spec/services/mount_method/option_sanitizers/hash_checker_spec.rb +++ b/spec/services/mount_method/option_sanitizers/hash_checker_spec.rb @@ -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 } @@ -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 } @@ -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 @@ -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 diff --git a/spec/services/mount_method/option_sanitizers/string_checker_spec.rb b/spec/services/mount_method/option_sanitizers/string_checker_spec.rb index c0afcdac..09869eac 100644 --- a/spec/services/mount_method/option_sanitizers/string_checker_spec.rb +++ b/spec/services/mount_method/option_sanitizers/string_checker_spec.rb @@ -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