-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Deferred::Dsl::MemoizedHelpers#let, #let!
- Loading branch information
1 parent
b29a9d9
commit 00bb9dd
Showing
5 changed files
with
322 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
lib/rspec/sleeping_king_studios/deferred/dsl/memoized_helpers.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rspec/sleeping_king_studios/deferred/dsl' | ||
|
||
module RSpec::SleepingKingStudios::Deferred::Dsl | ||
# DSL for defining memoized helpers for deferred examples. | ||
module MemoizedHelpers | ||
# Callback invoked when the module is extended into another module or class. | ||
# | ||
# Defines a HelperImplementations module on the module and includes it in | ||
# the module. | ||
# | ||
# @param other [Module] the other module or class. | ||
def self.extended(other) | ||
super | ||
|
||
return if other.const_defined?(:HelperImplementations, true) | ||
|
||
other.const_set(:HelperImplementations, Module.new) | ||
end | ||
|
||
def call(example_group) | ||
super | ||
|
||
include self::HelperImplementations | ||
end | ||
|
||
# Defines a memoized helper. | ||
# | ||
# @param helper_name [String, Symbol] the name of the helper method. | ||
# @param block [Block] the implementation of the helper method. | ||
# | ||
# @return [void] | ||
def let(helper_name, &block) | ||
helper_name = helper_name.to_sym | ||
|
||
self::HelperImplementations.define_method(helper_name, &block) | ||
|
||
define_method(helper_name) do | ||
helper_values = @memoized_helper_values ||= {} | ||
|
||
helper_values.fetch(helper_name) do | ||
helper_values[helper_name] = super() | ||
end | ||
end | ||
end | ||
|
||
# Defines a memoized helper and adds a hook to evaluate it before examples. | ||
# | ||
# @param helper_name [String, Symbol] the name of the helper method. | ||
# @param block [Block] the implementation of the helper method. | ||
# | ||
# @return [void] | ||
def let!(helper_name, &block) | ||
let(helper_name, &block) | ||
|
||
before(:example) { send(helper_name) } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
spec/rspec/sleeping_king_studios/deferred/dsl/memoized_helpers_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# frozen_string_literal: true | ||
|
||
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' | ||
|
||
require 'support/shared_examples/deferred_examples' | ||
|
||
RSpec.describe RSpec::SleepingKingStudios::Deferred::Dsl::MemoizedHelpers do | ||
extend RSpec::SleepingKingStudios::Concerns::ExampleConstants | ||
include Spec::Support::SharedExamples::DeferredExamples | ||
|
||
subject(:definitions) { described_class } | ||
|
||
let(:described_class) { Spec::DeferredExamples } | ||
let(:ancestor_class) { Spec::InheritedExamples } | ||
let(:ancestor_examples) { ancestor_class } | ||
|
||
example_constant 'Spec::InheritedExamples' do | ||
Module.new do | ||
extend RSpec::SleepingKingStudios::Deferred::Definitions | ||
extend RSpec::SleepingKingStudios::Deferred::Dsl::Hooks | ||
extend RSpec::SleepingKingStudios::Deferred::Dsl::MemoizedHelpers | ||
end | ||
end | ||
|
||
example_constant 'Spec::DeferredExamples' do | ||
Module.new do | ||
extend RSpec::SleepingKingStudios::Deferred::Definitions | ||
extend RSpec::SleepingKingStudios::Deferred::Dsl::Hooks | ||
extend RSpec::SleepingKingStudios::Deferred::Dsl::MemoizedHelpers | ||
include Spec::InheritedExamples | ||
end | ||
end | ||
|
||
include_examples 'should define memoized helpers' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters