crispr 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d070d677dfe3f0926fff8ddf0a2f31aae859ed65a5fdeb76164eaf180a2fc921
4
- data.tar.gz: 1a76d002b52a92f4ff756eb11cac032dbaccda83e23fec8bbbfa4ababbfe64a9
3
+ metadata.gz: 205d5b57e819c5c2e07a085ea24e98907d2f4b669036455840e03381a23e7e69
4
+ data.tar.gz: 23151bd8e26ad94e8e2cf2b914e62aace2e9e851b9b418b54a18341981db7fec
5
5
  SHA512:
6
- metadata.gz: 7bdf7c8476653270d00188101b87ae5b3de8fbcd6c6d5c572211ed35833dbbd88ad3f9e69fe9abd3921567e7914a50899bc071071f50547dc273b560b0823a5e
7
- data.tar.gz: cf43c052481ebd7f10d4eaa67056d46e8e85e76419af7675e5b960b743a096e22cbd967bffebfbf86099e693e9f092847f9344e44748c1e5101dac298255ce97
6
+ metadata.gz: 2c48c9bbe4748591fc70b8b9c455f91b3c90bffc912843534dbe47ea7359f77fad1a19af270681e47659d5d485f168fb42236bfa78b71e82f9f4393a722b6cc3
7
+ data.tar.gz: e541dc13fc8fe3da01a1fc550960ba3c922cc65af0c6b8f59d9afa356c1e182bc5e28e8af5537d1ada229064ba9c5a4941217afaade3f8f0766a38ea5de1c872
data/.rubocop.yml CHANGED
@@ -6,6 +6,9 @@ AllCops:
6
6
  NewCops: enable
7
7
  TargetRubyVersion: 3.1
8
8
 
9
+ Layout/LineLength:
10
+ Enabled: false
11
+
9
12
  Metrics/AbcSize:
10
13
  Enabled: false
11
14
 
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Crispr
4
+ module Mutations
5
+ # Abstract base class for all mutation strategies.
6
+ # Subclasses must implement the `#mutations_for` method.
7
+ class Base
8
+ # Returns an array of mutated AST nodes for a given node.
9
+ #
10
+ # @param node [Parser::AST::Node] the node to mutate
11
+ # @return [Array<Parser::AST::Node>] mutations
12
+ def mutations_for(node)
13
+ raise NotImplementedError, "#{self.class} must implement #mutations_for"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,16 +1,18 @@
1
1
  # rubocop:disable Lint/BooleanSymbol
2
2
  # frozen_string_literal: true
3
3
 
4
+ require_relative "base"
5
+
4
6
  module Crispr
5
7
  module Mutations
6
8
  # Provides boolean-specific AST mutations.
7
9
  # Currently supports toggling `true` to `false` and `false` to `true`.
8
- module BooleanMutations
10
+ class Boolean < Base
9
11
  # Returns a list of stringified mutated forms for the given boolean AST node.
10
12
  #
11
13
  # @param node [Parser::AST::Node] the AST node to inspect
12
14
  # @return [Array<String>] mutated Ruby source code strings
13
- def self.mutations_for(node)
15
+ def mutations_for(node)
14
16
  return [] unless node.is_a?(Parser::AST::Node)
15
17
 
16
18
  case node.type
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "base"
4
+
5
+ module Crispr
6
+ module Mutations
7
+ # The Numeric class defines mutations for integer literals.
8
+ # It generates alternative integer nodes by applying small transformations,
9
+ # such as incrementing, decrementing, zeroing, or negating the value.
10
+ class Numeric < Base
11
+ # Applies numeric mutations to the given AST node.
12
+ #
13
+ # @param node [Parser::AST::Node] the node to mutate
14
+ # @return [Array<Parser::AST::Node>] an array of mutated nodes
15
+ def mutations_for(node)
16
+ return [] unless node.type == :int
17
+
18
+ value = node.children[0]
19
+
20
+ # Generate a set of numeric mutations
21
+ mutations = []
22
+ mutations << replace(node, value + 1)
23
+ mutations << replace(node, value - 1)
24
+ mutations << replace(node, 0) unless value.zero?
25
+ mutations << replace(node, -value) unless value.zero?
26
+
27
+ mutations
28
+ end
29
+
30
+ private
31
+
32
+ # Creates a new AST node with the given integer value.
33
+ #
34
+ # @param node [Parser::AST::Node] the original node
35
+ # @param new_value [Integer] the new integer value
36
+ # @return [Parser::AST::Node] the mutated node
37
+ def replace(node, new_value)
38
+ Parser::AST::Node.new(:int, [new_value], location: node.location)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -2,12 +2,18 @@
2
2
 
3
3
  require "parser/current"
4
4
  require "unparser"
5
- require_relative "mutations/boolean_mutations"
5
+ require_relative "mutations/boolean"
6
+ require_relative "mutations/numeric"
6
7
 
7
8
  module Crispr
8
9
  # Mutator performs simple AST mutations on Ruby source code.
9
- # Currently, it supports changing `true` literals to `false` and vice versa.
10
+ # It delegates to multiple mutation strategies (Boolean, Numeric, etc.)
10
11
  class Mutator
12
+ MUTATORS = [
13
+ Crispr::Mutations::Boolean.new,
14
+ Crispr::Mutations::Numeric.new
15
+ ].freeze
16
+
11
17
  def initialize(source_code)
12
18
  @source_code = source_code
13
19
  end
@@ -24,8 +30,11 @@ module Crispr
24
30
  def find_mutations(node)
25
31
  return [] unless node.is_a?(Parser::AST::Node)
26
32
 
27
- local_mutations = Crispr::Mutations::BooleanMutations.mutations_for(node)
28
- child_mutations = node.children.flat_map { |child| find_mutations(child) }
33
+ local_mutations =
34
+ MUTATORS.flat_map { |mutator| mutator.mutations_for(node) }
35
+
36
+ child_mutations =
37
+ node.children.flat_map { |child| find_mutations(child) }
29
38
 
30
39
  local_mutations + child_mutations
31
40
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Crispr
4
- VERSION = "0.1.3"
4
+ VERSION = "0.1.4"
5
5
  end
data/lib/crispr.rb CHANGED
@@ -5,6 +5,10 @@ require_relative "crispr/mutator"
5
5
  require_relative "crispr/runner"
6
6
  require_relative "crispr/cli"
7
7
 
8
+ require_relative "crispr/mutations/base"
9
+ require_relative "crispr/mutations/boolean"
10
+ require_relative "crispr/mutations/numeric"
11
+
8
12
  module Crispr
9
13
  class Error < StandardError; end
10
14
  # Your code goes here...
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crispr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron F Stanton
@@ -54,7 +54,9 @@ files:
54
54
  - bin/crispr
55
55
  - lib/crispr.rb
56
56
  - lib/crispr/cli.rb
57
- - lib/crispr/mutations/boolean_mutations.rb
57
+ - lib/crispr/mutations/base.rb
58
+ - lib/crispr/mutations/boolean.rb
59
+ - lib/crispr/mutations/numeric.rb
58
60
  - lib/crispr/mutator.rb
59
61
  - lib/crispr/reporter.rb
60
62
  - lib/crispr/runner.rb