Created
January 10, 2013 00:57
-
-
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 :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you're interested in mutation testing you should check out https://github.com/mbj/mutant/