Class: Mutineer::Mutation
- Inherits:
-
Data
- Object
- Data
- Mutineer::Mutation
- Defined in:
- lib/mutineer/mutation.rb
Overview
One atomic byte-range edit.
Immutable. One mutation per mutant — never combine. Source is mutated textually, never regenerated from the AST.
Instance Attribute Summary collapse
-
#end_offset ⇒ Object
readonly
Returns the value of attribute end_offset.
-
#operator ⇒ Object
readonly
Returns the value of attribute operator.
-
#replacement ⇒ Object
readonly
Returns the value of attribute replacement.
-
#start_offset ⇒ Object
readonly
Returns the value of attribute start_offset.
Instance Method Summary collapse
-
#apply(source) ⇒ String
Applies the mutation to source text.
-
#valid?(source) ⇒ Boolean
Checks whether the running Ruby can still load the mutated source (see Parser.invalid?).
Instance Attribute Details
#end_offset ⇒ Object (readonly)
Returns the value of attribute end_offset
10 11 12 |
# File 'lib/mutineer/mutation.rb', line 10 def end_offset @end_offset end |
#operator ⇒ Object (readonly)
Returns the value of attribute operator
10 11 12 |
# File 'lib/mutineer/mutation.rb', line 10 def operator @operator end |
#replacement ⇒ Object (readonly)
Returns the value of attribute replacement
10 11 12 |
# File 'lib/mutineer/mutation.rb', line 10 def replacement @replacement end |
#start_offset ⇒ Object (readonly)
Returns the value of attribute start_offset
10 11 12 |
# File 'lib/mutineer/mutation.rb', line 10 def start_offset @start_offset end |
Instance Method Details
#apply(source) ⇒ String
Applies the mutation to source text.
15 16 17 18 19 20 21 22 |
# File 'lib/mutineer/mutation.rb', line 15 def apply(source) # An ASCII replacement is retagged so it concatenates with a source # whose magic comment names another encoding (EUC-JP, Shift_JIS). # The bytes do not change, so the file Ruby loads stays in that encoding. repl = replacement repl = repl.dup.force_encoding(source.encoding) if repl.ascii_only? && repl.encoding != source.encoding source.byteslice(0, start_offset) + repl + source.byteslice(end_offset..) end |
#valid?(source) ⇒ Boolean
Checks whether the running Ruby can still load the mutated source (see Parser.invalid?).
29 30 31 |
# File 'lib/mutineer/mutation.rb', line 29 def valid?(source) !Parser.invalid?(apply(source)) end |