Class: Botrytis::Formatter

Inherits:
Object
  • Object
show all
Includes:
Cucumber::Formatter::Console
Defined in:
lib/botrytis/formatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Formatter

Returns a new instance of Formatter.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/botrytis/formatter.rb', line 8

def initialize(config)
  @config = config
  
  # Initialize Botrytis configuration if not already done
  unless defined?(Botrytis.configuration)
    Botrytis.configure do |botrytis_config|
      botrytis_config.confidence_threshold = 0.7
      botrytis_config.cache_enabled = false
      botrytis_config.llm_provider = :openai
      botrytis_config.model_name = "gpt-4o"
    end
  end
  
  @semantic_matcher = SemanticMatcher.new
  @step_definitions = []

  # Use the modern event system to collect step definitions
  config.on_event(:step_definition_registered) do |event|
    @step_definitions << event.step_definition
  end

  # Register semantic matcher once all setup is done
  config.on_event(:test_run_started) do |event|
    register_semantic_matcher
  end
end

Instance Method Details

#register_semantic_matcherObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/botrytis/formatter.rb', line 35

def register_semantic_matcher
  # Find the correct StepDefinition class to monkey patch
  step_def_class = if defined?(Cucumber::Glue::StepDefinition)
                     Cucumber::Glue::StepDefinition
                   elsif defined?(Cucumber::StepDefinition)
                     Cucumber::StepDefinition
                   else
                     # Try to find any step definition class
                     Cucumber.constants.select do |c|
                       Cucumber.const_get(c).is_a?(Class) && c.to_s.include?('Step')
                     end.first&.then { |c| Cucumber.const_get(c) }
                   end

  return unless step_def_class

  @original_match_method = step_def_class.instance_method(:match)

  semantic_matcher = @semantic_matcher
  step_definitions = @step_definitions

  step_def_class.define_method(:match) do |step_name|
    result = @original_match_method.bind(self).call(step_name)

    if result.nil?
      puts "\nšŸ„’ Botrytis is looking for a fuzzy match for: \"#{step_name}\""

      match = semantic_matcher.find_match(step_name, step_definitions)

      if match
        puts "āœ… Found a semantic match!"
        return match
      else
        puts "āŒ No semantic match found"
      end
    end

    result
  end
end