Permalink
Browse files

Add regex mutation

  • Loading branch information...
1 parent 4096fb1 commit 0e279ddbf02a251e764401e45aff6d39f89f7fcb @txus committed Feb 22, 2012
@@ -30,9 +30,11 @@ def item_value(value = item.value)
case value
when Rubinius::AST::Range
Range.new(item_value(value.start), item_value(value.finish))
+ when Rubinius::AST::RegexLiteral
+ Regexp.new(value.source)
else
value.respond_to?(:string) ? value.string.inspect : value.value.inspect
end
end
end
-end
+end
@@ -68,6 +68,13 @@ def swap
end
end
+ class RegexLiteral < BaseLiteral
+ def swap
+ @node.source = Regexp.escape(Random.string)
+ @node
+ end
+ end
+
class LocalVariableAssignment < BaseLiteral
def swap
@node.value = literal_class.new(@node.value.clone).swap
@@ -0,0 +1,53 @@
+require 'spec_helper'
+
+describe 'Mutating regexen' do
+ context 'for an instance method' do
+ context 'that contains `.*`' do
+ before do
+ write_file 'thing.rb', """
+ class Thing
+ def regex
+ /.*/
+ end
+ end
+ """
+ end
+
+ context 'with an expectation that a string matches the regex' do
+ before do
+ write_file 'spec/thing_spec.rb', """
+ require 'thing'
+
+ describe 'Thing#regex' do
+ specify do
+ 'hello'.should match(Thing.new.regex)
+ end
+ end
+ """
+ run_simple '../../bin/mutate Thing#regex spec/thing_spec.rb'
+ end
+
+ specify 'the mutation passes' do
+ all_output.should include('passed')
+ end
+ end
+
+ context 'with an expectation that the regex is a Regex' do
+ before do
+ write_file 'spec/thing_spec.rb', """
+ require 'thing'
+
+ describe 'Thing#regex' do
+ specify { Thing.new.regex.should be_kind_of(Regexp) }
+ end
+ """
+ run_simple '../../bin/mutate Thing#regex spec/thing_spec.rb'
+ end
+
+ specify 'the mutation fails' do
+ all_output.should include('failed')
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,53 @@
+require 'spec_helper'
+
+describe 'Mutating regexen' do
+ context 'for a singleton method' do
+ context 'that contains `.*`' do
+ before do
+ write_file 'thing.rb', """
+ class Thing
+ def self.regex
+ /.*/
+ end
+ end
+ """
+ end
+
+ context 'with an expectation that a string matches the regex' do
+ before do
+ write_file 'spec/thing_spec.rb', """
+ require 'thing'
+
+ describe 'Thing.regex' do
+ specify do
+ 'hello'.should match(Thing.regex)
+ end
+ end
+ """
+ run_simple '../../bin/mutate Thing.regex spec/thing_spec.rb'
+ end
+
+ specify 'the mutation passes' do
+ all_output.should include('passed')
+ end
+ end
+
+ context 'with an expectation that the regex is a Regexp' do
+ before do
+ write_file 'spec/thing_spec.rb', """
+ require 'thing'
+
+ describe 'Thing.regex' do
+ specify { Thing.regex.should be_kind_of(Regexp) }
+ end
+ """
+ run_simple '../../bin/mutate Thing.regex spec/thing_spec.rb'
+ end
+
+ specify 'the mutation fails' do
+ all_output.should include('failed')
+ end
+ end
+ end
+ end
+end

0 comments on commit 0e279dd

Please sign in to comment.