1
0
Fork 0

Use more modern spec layout

* This specs still have spec per method granularity
* But one file specs multiple methods
* Compatible with mutant spec selector
* Deduplicates boilerplate unit setup
This commit is contained in:
Markus Schirp 2013-12-29 23:29:58 +01:00
parent a9974741f2
commit 9edb375ef3
27 changed files with 415 additions and 580 deletions

View file

@ -1,41 +0,0 @@
# encoding: utf-8
require 'spec_helper'
describe Mutant, '.singleton_subclass_instance' do
let(:object) { described_class }
subject { object.singleton_subclass_instance(name, superclass, &block) }
before do
subject
end
let(:name) { 'Test' }
let(:block) { proc { def foo; end } }
let(:superclass) { Class.new }
let(:generated) { superclass.const_get(:Test) }
it_should_behave_like 'a command method'
it 'sets expected name' do
name = generated.class.name
name.should eql("::#{self.name}")
name.should be_frozen
end
it 'stores instance of subclass' do
generated.should be_kind_of(superclass)
end
it 'evaluates the context of proc inside subclass' do
generated.should respond_to(:foo)
end
it 'generates nice #inspect' do
inspect = generated.inspect
inspect.should eql("::#{self.name}")
inspect.should be_frozen
end
end

View file

@ -1,14 +0,0 @@
# encoding: utf-8
require 'spec_helper'
describe Mutant::Differ, '.build' do
let(:object) { described_class }
subject { object.build(old_string, new_string) }
let(:old_string) { "foo\nbar" }
let(:new_string) { "bar\nbaz" }
it { should eql(Mutant::Differ.new(%w(foo bar), %w(bar baz))) }
end

View file

@ -1,27 +0,0 @@
# encoding: utf-8
require 'spec_helper'
describe Mutant::Differ, '.colorize_line' do
let(:object) { described_class }
subject { object.colorize_line(line) }
context 'line beginning with "+"' do
let(:line) { '+line' }
it { should eql(Mutant::Color::GREEN.format(line)) }
end
context 'line beginning with "-"' do
let(:line) { '-line' }
it { should eql(Mutant::Color::RED.format(line)) }
end
context 'line beginning in other char' do
let(:line) { ' line' }
it { should eql(line) }
end
end

View file

@ -0,0 +1,42 @@
# encoding: utf-8
require 'spec_helper'
describe Mutant::Differ do
let(:object) { described_class }
describe '.build' do
subject { object.build(old_string, new_string) }
let(:old_string) { "foo\nbar" }
let(:new_string) { "bar\nbaz" }
it { should eql(Mutant::Differ.new(%w(foo bar), %w(bar baz))) }
end
describe '.colorize_line' do
let(:object) { described_class }
subject { object.colorize_line(line) }
context 'line beginning with "+"' do
let(:line) { '+line' }
it { should eql(Mutant::Color::GREEN.format(line)) }
end
context 'line beginning with "-"' do
let(:line) { '-line' }
it { should eql(Mutant::Color::RED.format(line)) }
end
context 'line beginning in other char' do
let(:line) { ' line' }
it { should eql(line) }
end
end
end

View file

@ -1,43 +0,0 @@
# encoding: utf-8
require 'spec_helper'
describe Mutant::Matcher::Chain, '#each' do
subject { object.each { |entry| yields << entry } }
let(:object) { described_class.new(matchers) }
let(:matchers) { [matcher_a, matcher_b] }
let(:matcher_a) { double('Matcher A') }
let(:matcher_b) { double('Matcher B') }
let(:subject_a) { double('Subject A') }
let(:subject_b) { double('Subject B') }
before do
matcher_a.stub(:each).and_yield(subject_a).and_return(matcher_a)
matcher_b.stub(:each).and_yield(subject_b).and_return(matcher_b)
end
# it_should_behave_like 'an #each method'
context 'with no block' do
subject { object.each }
it { should be_instance_of(to_enum.class) }
if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
pending 'FIX RBX rspec? BUG HERE'
else
it 'yields the expected values' do
subject.to_a.should eql(object.to_a)
end
end
end
let(:yields) { [] }
it 'should yield subjects' do
expect { subject }.to change { yields }.from([]).to([subject_a, subject_b])
end
end

View file

@ -1,14 +0,0 @@
# encoding: utf-8
require 'spec_helper'
describe Mutant::Matcher::Chain, '#matchers' do
subject { object.matchers }
let(:object) { described_class.new(matchers) }
let(:matchers) { double('Matchers') }
it { should be(matchers) }
it_should_behave_like 'an idempotent method'
end

View file

@ -0,0 +1,58 @@
# encoding: utf-8
require 'spec_helper'
describe Mutant::Matcher::Chain do
let(:object) { described_class.new(matchers) }
describe '#each' do
subject { object.each { |entry| yields << entry } }
let(:matchers) { [matcher_a, matcher_b] }
let(:matcher_a) { double('Matcher A') }
let(:matcher_b) { double('Matcher B') }
let(:subject_a) { double('Subject A') }
let(:subject_b) { double('Subject B') }
before do
matcher_a.stub(:each).and_yield(subject_a).and_return(matcher_a)
matcher_b.stub(:each).and_yield(subject_b).and_return(matcher_b)
end
# it_should_behave_like 'an #each method'
context 'with no block' do
subject { object.each }
it { should be_instance_of(to_enum.class) }
if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
pending 'FIX RBX rspec? BUG HERE'
else
it 'yields the expected values' do
subject.to_a.should eql(object.to_a)
end
end
end
let(:yields) { [] }
it 'should yield subjects' do
expect { subject }.to change { yields }.from([]).to([subject_a, subject_b])
end
end
describe '#matchers' do
subject { object.matchers }
let(:matchers) { double('Matchers') }
it { should be(matchers) }
it_should_behave_like 'an idempotent method'
end
end

View file

@ -1,21 +0,0 @@
# encoding: utf-8
require 'spec_helper'
# This spec is only present to ensure 100% test coverage.
# The code should not be triggered on runtime.
describe Mutant::Matcher, '#each' do
subject { object.send(:each) }
let(:object) { described_class.allocate }
it 'should raise error' do
expect do
subject
end.to raise_error(
NotImplementedError,
'Mutant::Matcher#each is not implemented'
)
end
end

View file

@ -1,42 +0,0 @@
# encoding: utf-8
require 'spec_helper'
describe Mutant::Matcher::Method::Instance, '.build' do
let(:object) { described_class }
subject { object.build(cache, scope, method) }
let(:cache) { double }
let(:scope) do
Class.new do
include Adamantium
def foo
end
memoize :foo
def bar
end
end
end
let(:method) do
scope.instance_method(method_name)
end
context 'with adamantium infected scope' do
context 'with unmemoized method' do
let(:method_name) { :bar }
it { should eql(described_class.new(cache, scope, method)) }
end
context 'with memoized method' do
let(:method_name) { :foo }
it { should eql(described_class::Memoized.new(cache, scope, method)) }
end
end
end

View file

@ -1,114 +0,0 @@
# encoding: utf-8
require 'spec_helper'
describe Mutant::Matcher::Method::Instance, '#each' do
subject { object.each { |subject| yields << subject } }
let(:cache) { Fixtures::AST_CACHE }
let(:object) { described_class.new(cache, scope, method) }
let(:method) { scope.instance_method(method_name) }
let(:yields) { [] }
let(:namespace) { self.class }
let(:scope) { self.class::Foo }
let(:type) { :def }
let(:method_name) { :bar }
let(:method_arity) { 0 }
def name
node.children[0]
end
def arguments
node.children[1]
end
context 'when method is defined once' do
let(:base) { __LINE__ }
class self::Foo
def bar; end
end
let(:method_line) { 2 }
it_should_behave_like 'a method matcher'
end
context 'when method is defined multiple times' do
context 'on differend lines' do
let(:base) { __LINE__ }
class self::Foo
def bar
end
def bar(arg)
end
end
let(:method_line) { 5 }
let(:method_arity) { 1 }
it_should_behave_like 'a method matcher'
end
context 'on the same line' do
let(:base) { __LINE__ }
class self::Foo
def bar; end; def bar(arg); end
end
let(:method_line) { 2 }
let(:method_arity) { 1 }
it_should_behave_like 'a method matcher'
end
context 'on the same line with differend scope' do
let(:base) { __LINE__ }
class self::Foo
def self.bar; end; def bar(arg); end
end
let(:method_line) { 2 }
let(:method_arity) { 1 }
it_should_behave_like 'a method matcher'
end
context 'when nested' do
let(:pattern) { 'Foo::Bar#baz' }
context 'in class' do
let(:base) { __LINE__ }
class self::Foo
class Bar
def baz
end
end
end
let(:method_line) { 3 }
let(:method_name) { :baz }
let(:scope) { self.class::Foo::Bar }
it_should_behave_like 'a method matcher'
end
context 'in module' do
let(:base) { __LINE__ }
module self::Foo
class Bar
def baz
end
end
end
let(:method_line) { 3 }
let(:method_name) { :baz }
let(:scope) { self.class::Foo::Bar }
it_should_behave_like 'a method matcher'
end
end
end
end

View file

@ -0,0 +1,155 @@
# encoding: utf-8
require 'spec_helper'
describe Mutant::Matcher::Method::Instance do
let(:cache) { Fixtures::AST_CACHE }
describe '#each' do
subject { object.each { |subject| yields << subject } }
let(:object) { described_class.new(cache, scope, method) }
let(:method) { scope.instance_method(method_name) }
let(:yields) { [] }
let(:namespace) { self.class }
let(:scope) { self.class::Foo }
let(:type) { :def }
let(:method_name) { :bar }
let(:method_arity) { 0 }
def name
node.children[0]
end
def arguments
node.children[1]
end
context 'when method is defined once' do
let(:base) { __LINE__ }
class self::Foo
def bar; end
end
let(:method_line) { 2 }
it_should_behave_like 'a method matcher'
end
context 'when method is defined multiple times' do
context 'on differend lines' do
let(:base) { __LINE__ }
class self::Foo
def bar
end
def bar(arg)
end
end
let(:method_line) { 5 }
let(:method_arity) { 1 }
it_should_behave_like 'a method matcher'
end
context 'on the same line' do
let(:base) { __LINE__ }
class self::Foo
def bar; end; def bar(arg); end
end
let(:method_line) { 2 }
let(:method_arity) { 1 }
it_should_behave_like 'a method matcher'
end
context 'on the same line with differend scope' do
let(:base) { __LINE__ }
class self::Foo
def self.bar; end; def bar(arg); end
end
let(:method_line) { 2 }
let(:method_arity) { 1 }
it_should_behave_like 'a method matcher'
end
context 'when nested' do
let(:pattern) { 'Foo::Bar#baz' }
context 'in class' do
let(:base) { __LINE__ }
class self::Foo
class Bar
def baz
end
end
end
let(:method_line) { 3 }
let(:method_name) { :baz }
let(:scope) { self.class::Foo::Bar }
it_should_behave_like 'a method matcher'
end
context 'in module' do
let(:base) { __LINE__ }
module self::Foo
class Bar
def baz
end
end
end
let(:method_line) { 3 }
let(:method_name) { :baz }
let(:scope) { self.class::Foo::Bar }
it_should_behave_like 'a method matcher'
end
end
end
end
describe '.build' do
let(:object) { described_class }
subject { object.build(cache, scope, method) }
let(:scope) do
Class.new do
include Adamantium
def foo
end
memoize :foo
def bar
end
end
end
let(:method) do
scope.instance_method(method_name)
end
context 'with adamantium infected scope' do
context 'with unmemoized method' do
let(:method_name) { :bar }
it { should eql(described_class.new(cache, scope, method)) }
end
context 'with memoized method' do
let(:method_name) { :foo }
it { should eql(described_class::Memoized.new(cache, scope, method)) }
end
end
end
end

View file

@ -1,27 +0,0 @@
# encoding: utf-8
# This file is the sandbox for new mutations.
# Once finished mutation test will be moved to class specfic
# file.
require 'spec_helper'
describe Mutant::Mutator, '.each' do
pending 'interpolated string literal (DynamicString)' do
let(:source) { '"foo#{1}bar"' }
let(:random_string) { 'this-is-random' }
let(:mutations) do
mutations = []
mutations << 'nil'
end
before do
Mutant::Random.stub(hex_string: random_string)
end
it_should_behave_like 'a mutator'
end
end

View file

@ -1,59 +0,0 @@
# encoding: utf-8
require 'spec_helper'
describe Mutant::Mutator, '#emit_new' do
subject { object.send(:emit_new) { generated } }
class Block
attr_reader :arguments
def called?
defined?(@arguments)
end
def call(*arguments)
@arguments = arguments
end
end
let(:object) { class_under_test.new(input, parent, block) }
let(:block) { Block.new }
let(:input) { :input }
let(:parent) { :parent }
let(:class_under_test) do
Class.new(described_class) do
def dispatch
# noop
end
end
end
context 'when new object generated' do
let(:generated) { :generated }
it 'should call block' do
subject
block.should be_called
end
it 'should call block with generated object' do
subject
block.arguments.should eql([generated])
end
end
context 'when new AST could not be generated' do
let(:generated) { input }
it 'should raise error' do
expect do
subject
end.to raise_error(
RuntimeError,
'New AST could not be generated after 3 attempts'
)
end
end
end

View file

@ -1,55 +0,0 @@
# encoding: utf-8
require 'spec_helper'
describe Mutant::Mutator, '#emit' do
subject { object.send(:emit, generated) }
class Block
attr_reader :arguments
def called?
defined?(@arguments)
end
def call(*arguments)
@arguments = arguments
end
end
let(:object) { class_under_test.new(input, parent, block) }
let(:block) { Block.new }
let(:input) { :input }
let(:parent) { :parent }
let(:class_under_test) do
Class.new(described_class) do
def dispatch
# noop
end
end
end
context 'with generated that is not equal to input' do
let(:generated) { :generated }
it 'should call block' do
subject
block.should be_called
end
it 'should call block with generated' do
subject
block.arguments.should eql([generated])
end
end
context 'with generated object that is equal to input' do
let(:generated) { input }
it 'should not call block' do
subject
block.should_not be_called
end
end
end

View file

@ -0,0 +1,29 @@
# encoding: utf-8
# This file is the sandbox for new mutations.
# Once finished mutation test will be moved to class specfic
# file.
require 'spec_helper'
describe Mutant::Mutator do
describe '.each' do
pending 'interpolated string literal (DynamicString)' do
let(:source) { '"foo#{1}bar"' }
let(:random_string) { 'this-is-random' }
let(:mutations) do
mutations = []
mutations << 'nil'
end
before do
Mutant::Random.stub(hex_string: random_string)
end
it_should_behave_like 'a mutator'
end
end
end

View file

@ -1,14 +0,0 @@
# encoding: utf-8
require 'spec_helper'
describe Mutant::NodeHelpers, '#n_not' do
subject { object.n_not(node) }
let(:object) { Object.new.extend(described_class) }
let(:node) { described_class::N_TRUE }
it 'returns the negated node' do
expect(subject).to eq(parse('not true'))
end
end

View file

@ -1,52 +0,0 @@
# encoding: utf-8
require 'spec_helper'
describe Mutant::Runner::Config, '#subjects' do
let(:object) { described_class.run(config) }
subject { object.subjects }
let(:config) do
double(
'Config',
class: Mutant::Config,
subjects: [subject_a, subject_b],
strategy: strategy,
reporter: reporter
)
end
let(:reporter) { double('Reporter') }
let(:strategy) { double('Strategy') }
let(:subject_a) { double('Subject A') }
let(:subject_b) { double('Subject B') }
let(:runner_a) { double('Runner A', stop?: stop_a) }
let(:runner_b) { double('Runner B', stop?: stop_b) }
before do
strategy.stub(:setup)
strategy.stub(:teardown)
reporter.stub(report: reporter)
Mutant::Runner.stub(:run).with(config, subject_a).and_return(runner_a)
Mutant::Runner.stub(:run).with(config, subject_b).and_return(runner_b)
end
context 'without early stop' do
let(:stop_a) { false }
let(:stop_b) { false }
it { should eql([runner_a, runner_b]) }
it_should_behave_like 'an idempotent method'
end
context 'with early stop' do
let(:stop_a) { true }
let(:stop_b) { false }
it { should eql([runner_a]) }
it_should_behave_like 'an idempotent method'
end
end

View file

@ -1,57 +0,0 @@
# encoding: utf-8
require 'spec_helper'
describe Mutant::Runner::Config, '#success?' do
subject { object.success? }
let(:object) { described_class.new(config) }
let(:config) do
double(
'Config',
reporter: reporter,
strategy: strategy,
subjects: [subject_a, subject_b]
)
end
let(:reporter) { double('Reporter') }
let(:strategy) { double('Strategy') }
let(:subject_a) { double('Subject A') }
let(:subject_b) { double('Subject B') }
let(:runner_a) do
double('Runner A', stop?: stop_a, success?: success_a)
end
let(:runner_b) do
double('Runner B', stop?: stop_b, success?: success_b)
end
before do
reporter.stub(report: reporter)
strategy.stub(:setup)
strategy.stub(:teardown)
Mutant::Runner.stub(:run).with(config, subject_a).and_return(runner_a)
Mutant::Runner.stub(:run).with(config, subject_b).and_return(runner_b)
end
context 'without failed subjects' do
let(:stop_a) { false }
let(:stop_b) { false }
let(:success_a) { true }
let(:success_b) { true }
it { should be(true) }
end
context 'with failing subjects' do
let(:stop_a) { false }
let(:stop_b) { false }
let(:success_a) { false }
let(:success_b) { true }
it { should be(false) }
end
end

View file

@ -0,0 +1,88 @@
# encoding: utf-8
require 'spec_helper'
describe Mutant::Runner::Config do
let(:config) do
double(
'Config',
class: Mutant::Config,
subjects: [subject_a, subject_b],
strategy: strategy,
reporter: reporter
)
end
before do
reporter.stub(report: reporter)
strategy.stub(:setup)
strategy.stub(:teardown)
Mutant::Runner.stub(:run).with(config, subject_a).and_return(runner_a)
Mutant::Runner.stub(:run).with(config, subject_b).and_return(runner_b)
end
let(:reporter) { double('Reporter') }
let(:strategy) { double('Strategy') }
let(:subject_a) { double('Subject A') }
let(:subject_b) { double('Subject B') }
describe '#subjects' do
let(:object) { described_class.run(config) }
subject { object.subjects }
let(:runner_a) { double('Runner A', stop?: stop_a) }
let(:runner_b) { double('Runner B', stop?: stop_b) }
context 'without early stop' do
let(:stop_a) { false }
let(:stop_b) { false }
it { should eql([runner_a, runner_b]) }
it_should_behave_like 'an idempotent method'
end
context 'with early stop' do
let(:stop_a) { true }
let(:stop_b) { false }
it { should eql([runner_a]) }
it_should_behave_like 'an idempotent method'
end
end
describe '#success?' do
subject { object.success? }
let(:object) { described_class.new(config) }
let(:runner_a) do
double('Runner A', stop?: stop_a, success?: success_a)
end
let(:runner_b) do
double('Runner B', stop?: stop_b, success?: success_b)
end
context 'without failed subjects' do
let(:stop_a) { false }
let(:stop_b) { false }
let(:success_a) { true }
let(:success_b) { true }
it { should be(true) }
end
context 'with failing subjects' do
let(:stop_a) { false }
let(:stop_b) { false }
let(:success_a) { false }
let(:success_b) { true }
it { should be(false) }
end
end
end

43
spec/unit/mutant_spec.rb Normal file
View file

@ -0,0 +1,43 @@
# encoding: utf-8
require 'spec_helper'
describe Mutant do
describe '.singleton_subclass_instance' do
let(:object) { described_class }
subject { object.singleton_subclass_instance(name, superclass, &block) }
before do
subject
end
let(:name) { 'Test' }
let(:block) { proc { def foo; end } }
let(:superclass) { Class.new }
let(:generated) { superclass.const_get(:Test) }
it_should_behave_like 'a command method'
it 'sets expected name' do
name = generated.class.name
name.should eql("::#{self.name}")
name.should be_frozen
end
it 'stores instance of subclass' do
generated.should be_kind_of(superclass)
end
it 'evaluates the context of proc inside subclass' do
generated.should respond_to(:foo)
end
it 'generates nice #inspect' do
inspect = generated.inspect
inspect.should eql("::#{self.name}")
inspect.should be_frozen
end
end
end