Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Organization and clean up.

  • Loading branch information...
commit 81c1f9f7a44f88217c256ae1eb2094411e2b0820 1 parent b727cb2
@justinko justinko authored
View
1  lib/mutant.rb
@@ -1,4 +1,5 @@
require 'mutant/version'
+require 'mutant/implementation'
require 'mutant/rbx'
module Mutant
View
19 lib/mutant/implementation.rb
@@ -0,0 +1,19 @@
+module Mutant
+ class Implementation
+ def initialize(str)
+ @str = str
+ end
+
+ def details
+ @str.match(/(\w+)(\.|#)(\w+\?*)/).captures
+ end
+
+ def mutatees
+ [].tap do |ary|
+ if @str =~ /#|\./
+ ary << Rbx::Mutatee.new(*details)
+ end
+ end
+ end
+ end
+end
View
6 lib/mutant/rbx.rb
@@ -1,12 +1,12 @@
-require 'mutant/rbx/matcher'
require 'mutant/rbx/method'
+require 'mutant/rbx/mutatee'
require 'mutant/rbx/mutator'
module Mutant
module Rbx
def self.mutate(implementation)
- Matcher.match(implementation) do |match|
- Mutator.new(match).mutate
+ implementation.mutatees.each do |mutatee|
+ Mutator.new(mutatee).mutate
end
end
end
View
29 lib/mutant/rbx/matcher.rb
@@ -1,29 +0,0 @@
-module Mutant
- module Rbx
- class Matcher
- attr_reader :method
-
- def self.match(implementation)
- yield new(implementation)
- end
-
- def initialize(implementation)
- @implementation = implementation
- @method = instantiate_method
- end
-
- def method_class
- @implementation.split(/\.|#/)[0].to_sym
- end
-
- def instantiate_method
- case @implementation
- when /(.+)#(.+)/
- InstanceMethod.new(Object.const_get($1).instance_method($2))
- when /(.+)\.(.+)/
- SingletonMethod.new(Object.const_get($1).method($2))
- end
- end
- end
- end
-end
View
4 lib/mutant/rbx/method.rb
@@ -22,16 +22,12 @@ class InstanceMethod < Method
def process_defn(*)
super.tap { |ast| @ast = ast if match?(ast) }
end
-
- def instance?() true end
end
class SingletonMethod < Method
def process_defs(*)
super.tap { |ast| @ast = ast if match?(ast.body) }
end
-
- def singleton?() true end
end
end
end
View
21 lib/mutant/rbx/mutatee.rb
@@ -0,0 +1,21 @@
+module Mutant
+ module Rbx
+ class Mutatee
+ def initialize(_class, scope, _method)
+ @_class, @scope, @_method = _class, scope, _method
+ end
+
+ def _class
+ Object.const_get(@_class)
+ end
+
+ def _method
+ @rbx_method ||=
+ case @scope
+ when /#/ then InstanceMethod.new(_class.instance_method(@_method))
+ when /\./ then SingletonMethod.new(_class.method(@_method))
+ end
+ end
+ end
+ end
+end
View
16 lib/mutant/rbx/mutator.rb
@@ -1,25 +1,27 @@
module Mutant
module Rbx
class Mutator
- def initialize(match)
- @match = match
+ def initialize(mutatee)
+ @mutatee = mutatee
end
def mutate
- @match.method.parse_file
+ @mutatee._method.parse_file
- @ast = Marshal.load(Marshal.dump(@match.method.ast))
- body = @match.method.singleton? ? @ast.body.body : @ast.body
+ @ast = Marshal.load(Marshal.dump(@mutatee._method.ast))
+ body = @mutatee._method.is_a?(SingletonMethod) ? @ast.body.body : @ast.body
body.array = [ swap(body.array[0]) ]
+ block = Rubinius::AST::Block.new(1, [ @ast ])
+
# wrap the method in an AST for the class
class_ast = Rubinius::AST::Class.new(
- 1, @match.method_class, nil, Rubinius::AST::Block.new(1, [ @ast ])
+ 1, @mutatee._class.name.to_sym, nil, block
)
# create a script to contain the class AST
root = Rubinius::AST::Script.new(class_ast)
- root.file = @match.method.source_file
+ root.file = @mutatee._method.source_file
# setup the compiler
compiler = Rubinius::Compiler.new(:bytecode, :compiled_method)
View
2  lib/mutant/runners/rspec.rb
@@ -11,7 +11,7 @@ def self.run(args)
end
def initialize(args)
- @implementation = args.shift
+ @implementation = Implementation.new(args.shift)
@args = args
end
Please sign in to comment.
Something went wrong with that request. Please try again.