From cb71e1e89785d10864c0c1929c8e4e9656e962c4 Mon Sep 17 00:00:00 2001 From: Markus Schirp Date: Sat, 29 Jun 2024 23:31:45 +0000 Subject: [PATCH] Change to verified mutation generation --- lib/mutant/cli/command/util.rb | 2 +- lib/mutant/meta/example.rb | 2 +- lib/mutant/meta/example/verification.rb | 2 +- lib/mutant/mutation.rb | 15 ++++++--------- lib/mutant/subject.rb | 4 ++-- spec/support/shared_context.rb | 4 ++-- .../unit/mutant/meta/example/verification_spec.rb | 2 +- spec/unit/mutant/meta/example_spec.rb | 2 +- spec/unit/mutant/mutation_spec.rb | 2 +- .../reporter/cli/printer/mutation_result_spec.rb | 4 ++-- spec/unit/mutant/subject/method/instance_spec.rb | 12 ++++++------ spec/unit/mutant/subject_spec.rb | 6 +++--- 12 files changed, 27 insertions(+), 30 deletions(-) diff --git a/lib/mutant/cli/command/util.rb b/lib/mutant/cli/command/util.rb index 0c1ed1515..3acecd103 100644 --- a/lib/mutant/cli/command/util.rb +++ b/lib/mutant/cli/command/util.rb @@ -78,7 +78,7 @@ def print_mutations(target) node: target.node ).each do |mutation| Reporter::CLI::Printer::Mutation.call( - object: Mutant::Mutation::Evil.new(subject: target, node: mutation), + object: Mutant::Mutation::Evil.build(subject: target, node: mutation).from_right, output: world.stdout ) end diff --git a/lib/mutant/meta/example.rb b/lib/mutant/meta/example.rb index 96520f49c..4fcb6ddf1 100644 --- a/lib/mutant/meta/example.rb +++ b/lib/mutant/meta/example.rb @@ -61,7 +61,7 @@ def generated config: Mutation::Config::DEFAULT.with(operators:), node: ).map do |node| - Mutation::Evil.new(subject: self, node:) + Mutation::Evil.build(subject: self, node:).from_right end end memoize :generated diff --git a/lib/mutant/meta/example/verification.rb b/lib/mutant/meta/example/verification.rb index 56c50f70f..5de659307 100644 --- a/lib/mutant/meta/example/verification.rb +++ b/lib/mutant/meta/example/verification.rb @@ -94,7 +94,7 @@ def unexpected def missing (example.expected.map(&:node) - mutations.map(&:node)).map do |node| - Mutation::Evil.new(subject: example, node:) + Mutation::Evil.build(subject: example, node:).from_right end end memoize :missing diff --git a/lib/mutant/mutation.rb b/lib/mutant/mutation.rb index 0d1400e0b..14231a641 100644 --- a/lib/mutant/mutation.rb +++ b/lib/mutant/mutation.rb @@ -4,7 +4,7 @@ module Mutant # Represent a mutated node with its subject class Mutation include AbstractType, Adamantium - include Anima.new(:subject, :node) + include Anima.new(:subject, :node, :source) CODE_DELIMITER = "\0" CODE_RANGE = (..4) @@ -17,14 +17,6 @@ def code end memoize :code - # Normalized mutation source - # - # @return [String] - def source - Unparser.unparse(node) - end - memoize :source - # Identification string # # @return [String] @@ -84,6 +76,11 @@ def diff end memoize :diff + def self.build(node:, subject:) + Unparser.unparse_either(node) + .fmap { |source| new(node:, subject:, source:) } + end + private def sha1 diff --git a/lib/mutant/subject.rb b/lib/mutant/subject.rb index 3870ab60d..1b8c47790 100644 --- a/lib/mutant/subject.rb +++ b/lib/mutant/subject.rb @@ -16,7 +16,7 @@ def mutations config: config.mutation, node: ).map do |mutant| - Mutation::Evil.new(subject: self, node: wrap_node(mutant)) + Mutation::Evil.build(subject: self, node: wrap_node(mutant)).from_right end ) end @@ -92,7 +92,7 @@ def source private def neutral_mutation - Mutation::Neutral.new(subject: self, node: wrap_node(node)) + Mutation::Neutral.build(subject: self, node: wrap_node(node)).from_right end def wrap_node(node) diff --git a/spec/support/shared_context.rb b/spec/support/shared_context.rb index 364f31b3b..91ae021d0 100644 --- a/spec/support/shared_context.rb +++ b/spec/support/shared_context.rb @@ -38,11 +38,11 @@ def setup_shared_context let(:subjects) { [subject_a] } let(:mutation_a) do - Mutant::Mutation::Evil.new(subject: subject_a, node: mutation_a_node) + Mutant::Mutation::Evil.build(subject: subject_a, node: mutation_a_node).from_right end let(:mutation_b) do - Mutant::Mutation::Evil.new(subject: subject_a, node: mutation_b_node) + Mutant::Mutation::Evil.build(subject: subject_a, node: mutation_b_node).from_right end let(:job_a) do diff --git a/spec/unit/mutant/meta/example/verification_spec.rb b/spec/unit/mutant/meta/example/verification_spec.rb index 20ac8dbe2..979f266ef 100644 --- a/spec/unit/mutant/meta/example/verification_spec.rb +++ b/spec/unit/mutant/meta/example/verification_spec.rb @@ -26,7 +26,7 @@ let(:mutations) do generated_nodes.map do |node| - Mutant::Mutation::Evil.new(subject: example, node:) + Mutant::Mutation::Evil.build(subject: example, node:).from_right end end diff --git a/spec/unit/mutant/meta/example_spec.rb b/spec/unit/mutant/meta/example_spec.rb index 24fc1f7ef..2ee0e227d 100644 --- a/spec/unit/mutant/meta/example_spec.rb +++ b/spec/unit/mutant/meta/example_spec.rb @@ -26,7 +26,7 @@ let(:mutations) do mutation_nodes.map do |node| - Mutant::Mutation::Evil.new(subject: object, node:) + Mutant::Mutation::Evil.build(subject: object, node:).from_right end end diff --git a/spec/unit/mutant/mutation_spec.rb b/spec/unit/mutant/mutation_spec.rb index 5125b48b8..c52f3d6be 100644 --- a/spec/unit/mutant/mutation_spec.rb +++ b/spec/unit/mutant/mutation_spec.rb @@ -14,7 +14,7 @@ let(:root_node) { s(:int, 1) } let(:object) do - mutation_class.new(subject: mutation_subject, node:) + mutation_class.build(subject: mutation_subject, node:).from_right end let(:mutation_subject) do diff --git a/spec/unit/mutant/reporter/cli/printer/mutation_result_spec.rb b/spec/unit/mutant/reporter/cli/printer/mutation_result_spec.rb index b87703a0a..30db3f47f 100644 --- a/spec/unit/mutant/reporter/cli/printer/mutation_result_spec.rb +++ b/spec/unit/mutant/reporter/cli/printer/mutation_result_spec.rb @@ -84,7 +84,7 @@ with(:mutation_a_test_result) { { passed: false } } let(:mutation_a) do - Mutant::Mutation::Neutral.new(subject: subject_a, node: s(:true)) + Mutant::Mutation::Neutral.build(subject: subject_a, node: s(:true)).from_right end it_reports(<<~REPORT) @@ -106,7 +106,7 @@ with(:mutation_a_test_result) { { passed: false } } let(:mutation_a) do - Mutant::Mutation::Noop.new(subject: subject_a, node: s(:true)) + Mutant::Mutation::Noop.build(subject: subject_a, node: s(:true)).from_right end it_reports(<<~REPORT) diff --git a/spec/unit/mutant/subject/method/instance_spec.rb b/spec/unit/mutant/subject/method/instance_spec.rb index e8cc903da..4eafde22a 100644 --- a/spec/unit/mutant/subject/method/instance_spec.rb +++ b/spec/unit/mutant/subject/method/instance_spec.rb @@ -155,18 +155,18 @@ def foo; end let(:expected) do [ - Mutant::Mutation::Neutral.new( + Mutant::Mutation::Neutral.build( subject: object, node: s(:begin, s(:def, :foo, s(:args), nil), memoize_node) - ), - Mutant::Mutation::Evil.new( + ).from_right, + Mutant::Mutation::Evil.build( subject: object, node: s(:begin, s(:def, :foo, s(:args), s(:send, nil, :raise)), memoize_node) - ), - Mutant::Mutation::Evil.new( + ).from_right, + Mutant::Mutation::Evil.build( subject: object, node: s(:begin, s(:def, :foo, s(:args), s(:zsuper)), memoize_node) - ) + ).from_right ] end diff --git a/spec/unit/mutant/subject_spec.rb b/spec/unit/mutant/subject_spec.rb index 70469029f..e4d899595 100644 --- a/spec/unit/mutant/subject_spec.rb +++ b/spec/unit/mutant/subject_spec.rb @@ -100,9 +100,9 @@ def foo it 'generates neutral and evil mutations' do should eql([ - Mutant::Mutation::Neutral.new(subject: object, node:), - Mutant::Mutation::Evil.new(subject: object, node: mutation_a), - Mutant::Mutation::Evil.new(subject: object, node: mutation_b) + Mutant::Mutation::Neutral.build(subject: object, node:).from_right, + Mutant::Mutation::Evil.build(subject: object, node: mutation_a).from_right, + Mutant::Mutation::Evil.build(subject: object, node: mutation_b).from_right ]) end end