Skip to content

Commit

Permalink
Add integration specs for deferred helpers.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepingkingstudios committed Jun 9, 2024
1 parent 3a21e47 commit 81fd0fb
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 6 deletions.
13 changes: 12 additions & 1 deletion lib/rspec/sleeping_king_studios/deferred/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,18 @@

module RSpec::SleepingKingStudios::Deferred
# Domain-specific language for defining deferred examples.
module Dsl; end
module Dsl
# Callback invoked when the module is extended into another module or class.
#
# Delegates to child module #extended methods.
#
# @param other [Module] the other module or class.
def self.extended(other)
super

other.extend(RSpec::SleepingKingStudios::Deferred::Dsl::MemoizedHelpers)
end
end
end

require 'rspec/sleeping_king_studios/deferred/dsl/examples'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'rspec/sleeping_king_studios/deferred/dsl'

module RSpec::SleepingKingStudios::Deferred::Dsl
module RSpec::SleepingKingStudios::Deferred::Dsl # rubocop:disable Style/Documentation
# DSL for defining memoized helpers for deferred examples.
module MemoizedHelpers
# Callback invoked when the module is extended into another module or class.
Expand Down Expand Up @@ -94,4 +94,6 @@ def subject!(helper_name = nil, &)
before(:example) { send(helper_name) }
end
end

include MemoizedHelpers
end
9 changes: 9 additions & 0 deletions spec/integration/deferred/helpers_spec.fixture.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

require 'support/integration/deferred/payload_examples'

RSpec.describe Spec::Models::Rocket do
subject(:rocket) { described_class.new('Imp IV') }

include Spec::Integration::Deferred::PayloadExamples
end
28 changes: 28 additions & 0 deletions spec/integration/deferred/helpers_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require 'rspec/sleeping_king_studios/deferred/examples'

require 'support/sandbox'

RSpec.describe RSpec::SleepingKingStudios::Deferred::Examples do
let(:fixture_file) do
%w[spec/integration/deferred/helpers_spec.fixture.rb]
end
let(:result) do
Spec::Support::Sandbox.run(fixture_file)
end
let(:expected_examples) do
<<~EXAMPLES.lines.map(&:strip)
Spec::Models::Rocket#launch when the rocket has a basic payload should not change the previous payload
Spec::Models::Rocket#launch when the rocket has a basic payload should set the payload
Spec::Models::Rocket#launch when the rocket has a complex payload should set the payload
Spec::Models::Rocket#payload is expected to be == {}
EXAMPLES
end

it 'should apply the deferred examples', :aggregate_failures do
expect(result.summary).to be == '4 examples, 0 failures'

expect(result.example_descriptions).to be == expected_examples
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
require 'rspec/sleeping_king_studios/concerns/example_constants'
require 'rspec/sleeping_king_studios/deferred/dsl/hooks'
require 'rspec/sleeping_king_studios/deferred/dsl/memoized_helpers'
require 'rspec/sleeping_king_studios/matchers/built_in/respond_to'
require 'rspec/sleeping_king_studios/matchers/core/have_constant'

require 'support/isolated_example_group'

Expand Down
2 changes: 2 additions & 0 deletions spec/rspec/sleeping_king_studios/deferred/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@
include_examples 'should define deferred example groups'

include_examples 'should define deferred hooks'

include_examples 'should define memoized helpers'
end
2 changes: 2 additions & 0 deletions spec/rspec/sleeping_king_studios/deferred/examples_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@
include_examples 'should define deferred example groups'

include_examples 'should define deferred hooks'

include_examples 'should define memoized helpers'
end
81 changes: 81 additions & 0 deletions spec/support/integration/deferred/payload_examples.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# frozen_string_literal: true

require 'support/integration/deferred'

module Spec::Integration::Deferred
module ShouldNotChangeThePreviousPayload
include RSpec::SleepingKingStudios::Deferred::Examples

let!(:previous_payload) { subject.payload }

it 'should not change the previous payload' do
launch_rocket(payload:)

expect(previous_payload).to be == {}
end
end

module WhenThePayloadIncludesABattery
include RSpec::SleepingKingStudios::Deferred::Examples

let(:payload) { super().merge(battery: true) }
end

module WhenThePayloadIncludesASolarArray
include RSpec::SleepingKingStudios::Deferred::Examples

let(:payload) { super().merge(solar_array: true) }
end

module WhenThePayloadIncludesExperiments
include RSpec::SleepingKingStudios::Deferred::Examples

let(:payload) { super().merge(experiments: true) }
end

module PayloadExamples
include RSpec::SleepingKingStudios::Deferred::Examples

describe '#launch' do
let(:launch_site) { 'KSC' }
let(:payload) { {} }

def launch_rocket(**options)
subject.launch(launch_site:, **options)
end

context 'when the rocket has a basic payload' do
include Spec::Integration::Deferred::WhenThePayloadIncludesABattery
include Spec::Integration::Deferred::ShouldNotChangeThePreviousPayload

let(:expected) { { battery: true } }

it 'should set the payload' do
expect { launch_rocket(payload:) }
.to change(rocket, :payload)
.to be == expected
end
end

context 'when the rocket has a complex payload' do
include Spec::Integration::Deferred::WhenThePayloadIncludesABattery
include Spec::Integration::Deferred::WhenThePayloadIncludesASolarArray
include Spec::Integration::Deferred::WhenThePayloadIncludesExperiments

let(:expected) do
{ battery: true, experiments: true, solar_array: true }
end

it 'should set the payload' do
expect { launch_rocket(payload:) }
.to change(rocket, :payload)
.to be == expected
end
end
end

describe '#payload' do
it { expect(subject.payload).to be == {} }
end
end
end
4 changes: 2 additions & 2 deletions spec/support/models/rocket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ def initialize(name)

@launched = false
@launch_site = nil
@payload = []
@payload = {}
end

attr_reader \
:launch_site,
:payload

def launch(launch_site:, payload: [])
def launch(launch_site:, payload: {})
@launched = true
@launch_site = launch_site
@payload = payload
Expand Down
1 change: 1 addition & 0 deletions spec/support/shared_examples/deferred_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'rspec/sleeping_king_studios/concerns/shared_example_group'
require 'rspec/sleeping_king_studios/matchers/built_in/respond_to'
require 'rspec/sleeping_king_studios/matchers/core/have_constant'

require 'support/isolated_example_group'
require 'support/shared_examples'
Expand Down

0 comments on commit 81fd0fb

Please sign in to comment.