Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
19 changes: 11 additions & 8 deletions lib/cuprum/rspec/deferred/parameter_validation_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -71,11 +80,5 @@ module ParameterValidationExamples
.with_error(expected_error)
end
end

private

def tools
SleepingKingStudios::Tools::Toolbelt.instance
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
11 changes: 8 additions & 3 deletions spec/cuprum/rspec/deferred/parameter_validation_examples_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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