Logic & Math Mutators
Relevant source files
The following files were used as context for generating this wiki page:
Logic and Math Mutators represent the core "Tier 1" mutation suite within Mutineer. These components traverse the Ruby Abstract Syntax Tree (AST) to identify and replace mathematical operators, logical connectors, and comparison signs with their functional opposites or alternatives. By intentionally breaking the logic of the source code, these mutators force the test suite to prove its efficacy in detecting changes.
Sources: lib/mutineer.rb:14-17, test/mutator_registry_test.rb:10-12
Mutator Architecture
All mutators in this category inherit from a common base class and leverage the Prism parser to interact with Ruby source code. The mutators register themselves under specific names (e.g., arithmetic, comparison) and are grouped into tiers based on their impact and stability.
This diagram illustrates the inheritance structure where specialized logic/math mutators extend the Base mutator class.
Sources: lib/mutineer.rb:14-17, test/mutator_registry_test.rb:10-12, lib/mutineer/mutators/base.rb
Arithmetic Mutators
The arithmetic mutator targets standard mathematical operations. It swaps binary operators to verify that tests can distinguish between addition and subtraction, or multiplication and division.
| Original Operator | Replacement | Description |
|---|---|---|
+ | - | Swaps addition for subtraction |
- | + | Swaps subtraction for addition |
* | / | Swaps multiplication for division |
/ | * | Swaps division for multiplication |
% | * | Swaps modulo for multiplication |
** | * | Swaps exponentiation for multiplication |
Sources: test/mutators/arithmetic_test.rb:27-52
Implementation Logic
The mutator scans for CallNode instances in the Prism AST that match these specific mathematical symbols. If a match occurs, the mutator generates a Mutineer::Mutation object containing the byte offsets and the replacement character.
# Example of Arithmetic Mutation Transformation
def m
a + b # Original
a - b # Mutated
end
Sources: test/mutators/arithmetic_test.rb:27-30
Comparison Mutators
Comparison mutators identify relational operators and replace them with alternatives to test boundary conditions and equality checks. These are critical for detecting "off-by-one" errors in test suites.
| Operator Type | Original | Replacement |
|---|---|---|
| Inequality | > | >= (or vice versa) |
| Inequality | < | <= (or vice versa) |
| Equality | == | != |
| Equality | != | == |
Sources: lib/mutineer.rb:16, test/mutator_registry_test.rb:10-12
Boolean Connector Mutators
The boolean_connector mutator targets logical short-circuiting operators. It validates that the test suite exercises both branches of a logical condition by swapping the connectors.
The flow shows how logical nodes are identified and swapped during the AST traversal.
Sources: lib/mutineer/mutators/boolean_connector.rb, test/mutator_registry_test.rb:10-12
Operator Assignment Mutators
Operator assignment mutators handle compound assignments. These mutators visit the target node and the value node to ensure nested operations are also mutated.
Supported Assignments
+=replaces with-=-=replaces with+=*=replaces with/=/=replaces with*=%=replaces with*=**=replaces with*=
Sources: test/mutators/operator_assignment_test.rb:25-45
Exclusions
Specific bitwise and logical assignments are excluded from mutation in this category:
||=(Or-assignment)&&=(And-assignment)|=,&=,^=,<<=,>>=(Bitwise assignments)
Sources: test/mutators/operator_assignment_test.rb:83-96
Mutation Registration and Tiers
Mutineer organizes mutators into tiers. Tier 1 mutators are enabled by default, while Tier 2 mutators must be explicitly requested or are considered more experimental.
| Mutator Name | Tier | Default | Description |
|---|---|---|---|
arithmetic | 1 | Yes | Standard math operations |
comparison | 1 | Yes | Relational operators |
boolean_connector | 1 | Yes | Logical AND/OR |
operator_assignment | 2 | No | Compound math assignments |
Sources: test/mutator_registry_test.rb:10-12, 79-88
Summary of Logic & Math Mutators
Logic and Math Mutators provide the foundational checks for Ruby code by targeting arithmetic, comparison, and boolean logic. They use Prism AST traversal to find specific nodes and replace them with functional alternatives. These mutators are primary tools for ensuring that test suites do more than just execute code—they must actively validate the correctness of the operations performed.