Skip to content

Instantly share code, notes, and snippets.

@Sam-Serpoosh
Created January 10, 2013 00:57
Show Gist options
  • Save Sam-Serpoosh/4498464 to your computer and use it in GitHub Desktop.
Save Sam-Serpoosh/4498464 to your computer and use it in GitHub Desktop.
Very simple Mutator for the concept of Mutation-Testing in Ruby! Just for fun :)
class Mutator
def initialize(ruby_file)
@ruby_file = ruby_file
end
def run_test
spec_file_name = @ruby_file.split(/\./)[0]
system("rspec #{spec_file_name}_spec.rb")
end
def mutate_code
mutated = mutate_bigger_than_comparisons
File.open(@ruby_file, "w") { |f| f.print(mutated) }
end
def unmutate_code
unmutated = unmutate_bigger_than_comparisons
File.open(@ruby_file, "w") { |f| f.print(unmutated) }
end
private
def mutate_bigger_than_comparisons
ruby_code = get_content
ruby_code.gsub(">", "<")
end
def unmutate_bigger_than_comparisons
ruby_code = get_content
ruby_code.gsub("<", ">")
end
def get_content
File.open(@ruby_file, "r") { |f| f.read }
end
end
mutator = Mutator.new("number_descriptor.rb")
mutator.mutate_code
mutator.run_test
mutator.unmutate_code
mutator.run_test
def describe_number(num)
return "Big" if (num > 1000)
"Small"
end
describe "Number Description" do
it "says Big for number bigger than 1000" do
describe_number(2000).should == "Big"
end
it "says Small for number smaller than 1000" do
describe_number(500).should == "Small"
end
end
@dkubb
Copy link

dkubb commented Jan 14, 2013

If you're interested in mutation testing you should check out https://github.com/mbj/mutant/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment