Skip to content
Browse files

Add mutation for Floats.

  • Loading branch information...
1 parent c140646 commit 4085758326919ab2fb539e483e439a9260657a5d @txus committed Feb 11, 2012
View
7 lib/mutant/literal.rb
@@ -52,6 +52,13 @@ def swap
end
end
+ class FloatLiteral < BaseLiteral
+ def swap
+ @node.value = Random.float
+ @node
+ end
+ end
+
class Range < BaseLiteral
def swap
range = Random.range
View
5 lib/mutant/random.rb
@@ -18,6 +18,11 @@ def self.fixnum
rand(100)
end
+ def self.float
+ ENV['RANDOM_FLOAT'] or
@dkubb
dkubb added a note Feb 12, 2012

what about ENV.fetch('RANDOM_FLOAT') { rand(100) + 0.5 } ?

@txus
Owner
txus added a note Feb 12, 2012

Good point, I think this helps readability!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ (rand(100) + 0.5)
+ end
+
def self.range
if ENV['RANDOM_RANGE_MIN'] and ENV['RANDOM_RANGE_MAX']
min, max = ENV['RANDOM_RANGE_MIN'].to_i, ENV['RANDOM_RANGE_MAX'].to_i
View
51 spec/functional/instance_method/float_spec.rb
@@ -0,0 +1,51 @@
+require 'spec_helper'
+
+describe 'Mutating floats' do
+ context 'for an instance method' do
+ context 'that contains 42.5' do
+ before do
+ write_file 'life.rb', """
+ class Life
+ def answer
+ 42.5
+ end
+ end
+ """
+ end
+
+ context 'with an expectation that the return value is 42.5' do
+ before do
+ write_file 'spec/life_spec.rb', """
+ require 'life'
+
+ describe 'Life#answer' do
+ specify { Life.new.answer.should eq(42.5) }
+ end
+ """
+ run_simple '../../bin/mutate Life#answer spec/life_spec.rb'
+ end
+
+ specify 'the mutation passes' do
+ all_output.should include('passed')
+ end
+ end
+
+ context 'with an expectation that the return value is a Float' do
+ before do
+ write_file 'spec/life_spec.rb', """
+ require 'life'
+
+ describe 'Life#answer' do
+ specify { Life.new.answer.should be_a(Float) }
+ end
+ """
+ run_simple '../../bin/mutate Life#answer spec/life_spec.rb'
+ end
+
+ specify 'the mutation fails' do
+ all_output.should include('failed')
+ end
+ end
+ end
+ end
+end
View
51 spec/functional/singleton_method/float_spec.rb
@@ -0,0 +1,51 @@
+require 'spec_helper'
+
+describe 'Mutating floats' do
+ context 'for a singleton method' do
+ context 'that contains 42.05' do
+ before do
+ write_file 'life.rb', """
+ class Life
+ def self.answer
+ 42.05
+ end
+ end
+ """
+ end
+
+ context 'with an expectation that the return value is 42.05' do
+ before do
+ write_file 'spec/life_spec.rb', """
+ require 'life'
+
+ describe 'Life#answer' do
+ specify { Life.answer.should eq(42.05) }
+ end
+ """
+ run_simple '../../bin/mutate Life#answer spec/life_spec.rb'
+ end
+
+ specify 'the mutation passes' do
+ all_output.should include('passed')
+ end
+ end
+
+ context 'with an expectation that the return value is a Float' do
+ before do
+ write_file 'spec/life_spec.rb', """
+ require 'life'
+
+ describe 'Life#answer' do
+ specify { Life.answer.should be_a(Float) }
+ end
+ """
+ run_simple '../../bin/mutate Life#answer spec/life_spec.rb'
+ end
+
+ specify 'the mutation fails' do
+ all_output.should include('failed')
+ end
+ end
+ end
+ end
+end
View
11 spec/mutant/literal_spec.rb
@@ -83,6 +83,17 @@
end
end
+ describe Mutant::Literal::FloatLiteral do
+ describe '#swap' do
+ let(:node) { double('node') }
+
+ it "sets the node's value to a random float" do
+ node.should_receive(:value=).with(instance_of(Float))
+ Mutant::Literal::FloatLiteral.new(node).swap.should eq(node)
+ end
+ end
+ end
+
describe Mutant::Literal::Range do
describe '#swap' do
let(:node) { double('node', :line => 1) }
View
6 spec/mutant/random_spec.rb
@@ -19,6 +19,12 @@
end
end
+ describe '.float' do
+ it 'returns a random float' do
+ Mutant::Random.float.should be_a(Float)
+ end
+ end
+
describe '.range' do
it 'returns a random range' do
Mutant::Random.range.should be_a(Range)

0 comments on commit 4085758

Please sign in to comment.
Something went wrong with that request. Please try again.