Permalink
Browse files

Add specs.

  • Loading branch information...
1 parent efab511 commit a433d9704532fcac384b3e231ef4746ce4b3ae00 @justinko justinko committed Feb 8, 2012
Showing with 114 additions and 41 deletions.
  1. +2 −1 lib/mutant.rb
  2. +38 −0 lib/mutant/formatter.rb
  3. +5 −40 lib/mutant/node.rb
  4. +41 −0 spec/mutant/node_spec.rb
  5. +28 −0 spec/mutant_spec.rb
View
3 lib/mutant.rb
@@ -1,4 +1,4 @@
-require 'mutant/version'
+require 'mutant/formatter'
require 'mutant/implementation'
require 'mutant/literal'
require 'mutant/method'
@@ -8,6 +8,7 @@
require 'mutant/node'
require 'mutant/random'
require 'mutant/reporter'
+require 'mutant/version'
module Mutant
module Runners
View
38 lib/mutant/formatter.rb
@@ -0,0 +1,38 @@
+module Mutant
+ class Formatter
+ attr_reader :item
+
+ def initialize(item)
+ @item = item
+ end
+
+ def nested?
+ item.is_a?(Rubinius::AST::LocalVariableAssignment)
+ end
+
+ def to_s
+ if item.is_a?(Rubinius::AST::LocalVariableAssignment)
+ [item.name.to_s, item_value].join(' = ')
+ else
+ if item.is_a?(Rubinius::AST::TrueLiteral)
+ 'true'
+ elsif item.is_a?(Rubinius::AST::FalseLiteral)
+ 'false'
+ else
+ item_value(item)
+ end
+ end
+ end
+
+ private
+
+ def item_value(value = item.value)
+ case value
+ when Rubinius::AST::Range
+ Range.new(item_value(value.start), item_value(value.finish))
+ else
+ value.respond_to?(:string) ? value.string.inspect : value.value.inspect
+ end
+ end
+ end
+end
View
45 lib/mutant/node.rb
@@ -1,61 +1,26 @@
module Mutant
class Node
- attr_reader :item
+ attr_reader :item, :copy
def initialize(item)
@item = item
@copy = item.clone
end
def line
- @item.line
+ item.line
end
def from
- @from ||= Formatter.new(@item)
+ @from ||= Formatter.new(item)
end
def to
- @to ||= Formatter.new(@copy)
+ @to ||= Formatter.new(copy)
end
def swap
- @copy = Literal.new(@copy).swap
- end
-
- class Formatter
- def initialize(item)
- @item = item
- end
-
- def nested?
- @item.is_a?(Rubinius::AST::LocalVariableAssignment)
- end
-
- def to_s
- if @item.is_a?(Rubinius::AST::LocalVariableAssignment)
- [@item.name.to_s, item_value].join(' = ')
- else
- if @item.is_a?(Rubinius::AST::TrueLiteral)
- 'true'
- elsif @item.is_a?(Rubinius::AST::FalseLiteral)
- 'false'
- else
- item_value(@item)
- end
- end
- end
-
- private
-
- def item_value(value = @item.value)
- case value
- when Rubinius::AST::Range
- Range.new(item_value(value.start), item_value(value.finish))
- else
- value.respond_to?(:string) ? value.string.inspect : value.value.inspect
- end
- end
+ @copy = Literal.new(copy).swap
end
end
end
View
41 spec/mutant/node_spec.rb
@@ -0,0 +1,41 @@
+require 'spec_helper'
+
+describe Mutant::Node do
+ let(:item) { double('item', :line => 1) }
+ let(:node) { Mutant::Node.new(item) }
+
+ describe '#line' do
+ it "returns the item's line" do
+ Mutant::Node.new(item).line.should eq 1
+ end
+ end
+
+ describe '#from' do
+ it "returns a Formatter instance with its item set to the node's item" do
+ formatter = node.from
+ formatter.should be_an_instance_of(Mutant::Formatter)
+ formatter.item.should eq(item)
+ end
+ end
+
+ describe '#to' do
+ it "returns a Formatter instance with its item set to the node's copy" do
+ formatter = node.to
+ formatter.should be_an_instance_of(Mutant::Formatter)
+ formatter.item.should eq(node.copy)
+ end
+ end
+
+ describe '#swap' do
+ let(:swap) { double('swap') }
+ let(:literal) { double('literal', :swap => swap) }
+
+ before do
+ Mutant::Literal.should_receive(:new).with(node.copy).and_return(literal)
+ end
+
+ it "sets the node's copy to a different Literal" do
+ expect { node.swap }.to change { node.copy }.to swap
+ end
+ end
+end
View
28 spec/mutant_spec.rb
@@ -0,0 +1,28 @@
+require 'spec_helper'
+
+describe Mutant do
+ describe '.run' do
+ it 'runs the RSpec runner with the given arguments' do
+ Mutant::Runners::RSpec.should_receive(:run).with(:foo)
+ Mutant.run(:foo)
+ end
+ end
+
+ describe '.mutate' do
+ context 'given an implementation' do
+ let(:mutatee) { double('mutatee') }
+ let(:implementation) { double('implementation') }
+ let(:mutator) { double('mutator') }
+
+ before do
+ mutator.should_receive(:mutate)
+ implementation.should_receive(:mutatees).and_return([mutatee])
+ end
+
+ it "mutates each of the implementation's mutatees" do
+ Mutant::Mutator.should_receive(:new).with(mutatee).and_return(mutator)
+ Mutant.mutate(implementation)
+ end
+ end
+ end
+end

0 comments on commit a433d97

Please sign in to comment.