|
# tmp/shared_examples_spec.rb |
|
|
|
require 'sleeping_king_studios/tools/toolbox/mixin' |
|
|
|
require 'rspec/sleeping_king_studios/concerns/wrap_examples' |
|
|
|
module SharedExamples |
|
extend SleepingKingStudios::Tools::Toolbox::Mixin |
|
|
|
def self.included other |
|
super |
|
|
|
mixin_module = SleepingKingStudios::Tools::Toolbox::Mixin |
|
|
|
return if other.is_a?(Class) || other.ancestors.include?(mixin_module) |
|
|
|
other.extend(mixin_module) |
|
end # method included |
|
|
|
module ClassMethods |
|
def include_examples description, *args, &block |
|
shared = find_shared_examples(description) |
|
|
|
if shared |
|
instance_exec(*args, &shared) |
|
|
|
instance_exec(&block) if block_given? |
|
|
|
return |
|
end # if |
|
|
|
super |
|
end # class method include_examples |
|
|
|
def shared_examples description, &block |
|
shared_example_groups[description] = block |
|
end # class method shared_examples |
|
|
|
protected |
|
|
|
def shared_example_groups |
|
@shared_example_groups ||= {} |
|
end # class method shared_example_groups |
|
|
|
private |
|
|
|
def find_shared_examples description |
|
ancestors. |
|
select { |mod| mod < SharedExamples }. |
|
each do |mod| |
|
next unless mod.shared_example_groups.key?(description) |
|
|
|
return mod.shared_example_groups[description] |
|
end # each |
|
|
|
nil |
|
end # class method find_shared_examples |
|
end # module |
|
end # module |
|
|
|
module ObjectExamples |
|
include SharedExamples |
|
|
|
shared_examples 'should be a' do |klass| |
|
it { expect(instance).to be_a klass } |
|
end # shared_examples |
|
end # module |
|
|
|
module StringExamples |
|
include SharedExamples |
|
|
|
shared_examples 'should be a string' do |
|
it { expect(instance).to be_a String } |
|
end # shared_examples |
|
end # module |
|
|
|
module GreetingExamples |
|
include SharedExamples |
|
include StringExamples |
|
|
|
shared_examples 'should be a greeting' do |
|
it { expect(instance).to start_with('Greetings') } |
|
end # shared_examples |
|
end # module |
|
|
|
RSpec.describe 'default RSpec' do |
|
shared_examples 'should be a greeting' do |
|
it { expect(instance).to start_with('Greetings') } |
|
end # shared_examples |
|
|
|
subject(:instance) { 'Greetings, programs!' } |
|
|
|
include_examples 'should be a greeting' |
|
|
|
describe 'failing examples' do |
|
include_examples 'should be a greeting' do |
|
let(:instance) { 'Hello, world!' } |
|
end # include_examples |
|
end # describe |
|
end # describe |
|
|
|
RSpec.describe SharedExamples do |
|
include ObjectExamples |
|
include GreetingExamples |
|
|
|
subject(:instance) { 'Greetings, programs!' } |
|
|
|
include_examples 'should be a', String |
|
|
|
include_examples 'should be a string' |
|
|
|
include_examples 'should be a greeting' |
|
|
|
describe 'failing examples' do |
|
include_examples 'should be a greeting' do |
|
let(:instance) { 'Hello, world!' } |
|
end # include_examples |
|
end # describe |
|
|
|
describe 'wrapping examples' do |
|
extend RSpec::SleepingKingStudios::Concerns::WrapExamples |
|
|
|
wrap_examples 'should be a greeting' |
|
|
|
wrap_examples 'should be a greeting' do |
|
let(:instance) { 'Hello, world!' } |
|
end # wrap_examples |
|
end # describe |
|
end # describe |