diff --git a/CHANGELOG.md b/CHANGELOG.md index 50839e4..5af89d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,17 @@ # Changelog +## 1.4.0 + +The "Signs And Portents" Update + +### Results + +Added `Result.success` and `Result.failure`, which provide singleton results with respective `:success` and `:failure` statuses. + +### RSpec + +Updated `ParameterValidationExamples` to support lazy evaluation of the `message:` parameter, as well as any additional parameters passed along with `type:`. Parameters passed as a `Proc` will be evaluated in the context of the current example group. + ## 1.3.1 Added support for Ruby 4.0. diff --git a/lib/cuprum/rspec/deferred/parameter_validation_examples.rb b/lib/cuprum/rspec/deferred/parameter_validation_examples.rb index aeacea3..79e25ef 100644 --- a/lib/cuprum/rspec/deferred/parameter_validation_examples.rb +++ b/lib/cuprum/rspec/deferred/parameter_validation_examples.rb @@ -57,10 +57,19 @@ module ParameterValidationExamples deferred_examples 'should validate the parameter' \ do |name, type = nil, message: nil, **options| + let(:expected_message) do + message.is_a?(Proc) ? instance_exec(&message) : message + end + let(:expected_options) do + options.transform_values do |value| + value.is_a?(Proc) ? instance_exec(&value) : value + end + end + it 'should return a failing result with InvalidParameters error' do expected_failure = - message || - tools.assertions.error_message_for(type, as: name, **options) + expected_message || + tools.assertions.error_message_for(type, as: name, **expected_options) expected_error = Cuprum::Errors::InvalidParameters.new( command_class: subject.class, failures: [expected_failure] @@ -71,11 +80,5 @@ module ParameterValidationExamples .with_error(expected_error) end end - - private - - def tools - SleepingKingStudios::Tools::Toolbelt.instance - end end end diff --git a/spec/cuprum/rspec/deferred/parameter_validation_examples_spec.fixtures.rb b/spec/cuprum/rspec/deferred/parameter_validation_examples_spec.fixtures.rb index d8803be..280b60b 100644 --- a/spec/cuprum/rspec/deferred/parameter_validation_examples_spec.fixtures.rb +++ b/spec/cuprum/rspec/deferred/parameter_validation_examples_spec.fixtures.rb @@ -27,11 +27,45 @@ klass.validate :quantity, Integer end - def call_command + define_method :call_command do command.call(name, quantity) end + define_method :tools do + SleepingKingStudios::Tools::Toolbelt.instance + end + describe '"should validate the parameter" examples' do + describe 'with message: a Proc' do + context 'when the parameters are valid' do + include_deferred 'should validate the parameter', + :quantity, + message: -> { 'quantity is invalid' } + end + + context 'when the parameters are invalid with non-matching errors' do + let(:quantity) { nil } + + include_deferred 'should validate the parameter', + :quantity, + message: -> { 'quantity is invalid' } + end + + context 'when the parameters are invalid with matching error' do + let(:quantity) { nil } + + include_deferred 'should validate the parameter', + :quantity, + message: lambda { + tools.assertions.error_message_for( + :instance_of, + as: 'quantity', + expected: Integer + ) + } + end + end + describe 'with message: value' do context 'when the parameters are valid' do include_deferred 'should validate the parameter', @@ -73,6 +107,13 @@ def call_command :name, 'sleeping_king_studios.tools.assertions.presence', as: 'item name' + + describe 'with as: a Proc' do + include_deferred 'should validate the parameter', + :name, + 'sleeping_king_studios.tools.assertions.presence', + as: -> { 'item name' } + end end context 'when the parameters are invalid with matching error' do @@ -82,6 +123,13 @@ def call_command :name, 'sleeping_king_studios.tools.assertions.presence', as: 'item name' + + describe 'with as: a Proc' do + include_deferred 'should validate the parameter', + :name, + 'sleeping_king_studios.tools.assertions.presence', + as: -> { 'item name' } + end end end end diff --git a/spec/cuprum/rspec/deferred/parameter_validation_examples_spec.rb b/spec/cuprum/rspec/deferred/parameter_validation_examples_spec.rb index 87a4cad..4413caf 100644 --- a/spec/cuprum/rspec/deferred/parameter_validation_examples_spec.rb +++ b/spec/cuprum/rspec/deferred/parameter_validation_examples_spec.rb @@ -15,22 +15,27 @@ end let(:expected_failing) do <<~EXAMPLES.lines.map(&:strip) + Cuprum::RSpec::Deferred::ParameterValidationExamples "should validate the parameter" examples with message: a Proc when the parameters are valid should return a failing result with InvalidParameters error + Cuprum::RSpec::Deferred::ParameterValidationExamples "should validate the parameter" examples with message: a Proc when the parameters are invalid with non-matching errors should return a failing result with InvalidParameters error Cuprum::RSpec::Deferred::ParameterValidationExamples "should validate the parameter" examples with message: value when the parameters are valid should return a failing result with InvalidParameters error Cuprum::RSpec::Deferred::ParameterValidationExamples "should validate the parameter" examples with message: value when the parameters are invalid with non-matching errors should return a failing result with InvalidParameters error Cuprum::RSpec::Deferred::ParameterValidationExamples "should validate the parameter" examples with type: value when the parameters are valid should return a failing result with InvalidParameters error Cuprum::RSpec::Deferred::ParameterValidationExamples "should validate the parameter" examples with type: value when the parameters are invalid with non-matching errors should return a failing result with InvalidParameters error + Cuprum::RSpec::Deferred::ParameterValidationExamples "should validate the parameter" examples with type: value when the parameters are invalid with non-matching errors with as: a Proc should return a failing result with InvalidParameters error EXAMPLES end let(:expected_passing) do <<~EXAMPLES.lines.map(&:strip) + Cuprum::RSpec::Deferred::ParameterValidationExamples "should validate the parameter" examples with message: a Proc when the parameters are invalid with matching error should return a failing result with InvalidParameters error Cuprum::RSpec::Deferred::ParameterValidationExamples "should validate the parameter" examples with message: value when the parameters are invalid with matching error should return a failing result with InvalidParameters error Cuprum::RSpec::Deferred::ParameterValidationExamples "should validate the parameter" examples with type: value when the parameters are invalid with matching error should return a failing result with InvalidParameters error + Cuprum::RSpec::Deferred::ParameterValidationExamples "should validate the parameter" examples with type: value when the parameters are invalid with matching error with as: a Proc should return a failing result with InvalidParameters error EXAMPLES end - it { expect(result.summary).to be == '6 examples, 4 failures' } + it { expect(result.summary).to be == '11 examples, 7 failures' } - it { expect(result.failing_examples).to be == expected_failing } + it { expect(result.failing_examples).to deep_match(expected_failing) } - it { expect(result.passing_examples).to be == expected_passing } + it { expect(result.passing_examples).to deep_match(expected_passing) } end