<commit_before>require_relative 'source_code_matcher_extractor'
require 'ostruct'
require 'dotenv'

module RubyAtdd

  class Base

    def self.scenarios(attrs)
      load_dot_env
      called_by = called_by(caller)

      puts '', '-'*30, "TEST RUN STARTED", File.basename(called_by.file), "#{called_by.file}", "URL_BASE=#{ENV['BASE_URL']}", Time.now.to_s, '-'*30, ''

      failing_tests_count = handle_multiple_scenarios(get_scenarios_str(attrs))
      exit(failing_tests_count)
    end

    def initialize(*args)
    end

    def scenario(str)
      response_code = 0
      line_results = []
      begin
        ScenarioParser.new(str).each_line do |line, multiline_text|
          line_results << execute_line(line.to_s, multiline_text)
        end
      rescue Exception => e # must rescue Exception because Minitest::Assertion subclasses Exception
        puts "FAIL: #{e.message}"
        puts e.backtrace unless e.class.to_s == 'Minitest::Assertion'
        response_code = 1
      end
      puts
      {response_code: response_code, line_results: line_results}
    end

    private

    def self.handle_multiple_scenarios(str)
      results = ScenariosParser.new(str).map do |scenario_str|
        subject = new({uri_base: ENV['BASE_URL']})
        subject.scenario(scenario_str)
        .tap{|r| puts } # blank line between scenario results
      end

      failing_test_cnt = results.map{|r| r[:response_code] }.reduce(:+)
      pending_test_cnt = results.map{|r| r[:line_results].compact.size }.delete_if{|r| r > 0 }.size
      puts "#{results.size} scenarios. #{failing_test_cnt} scenarios failed. #{pending_test_cnt} pending scenarios."
      failing_test_cnt
    end

    def self.load_dot_env
      Dotenv.load ".env.#{ENV['ENV']}", '.env'<commit_msg>Reorder failing, pending, total scenarios count on exit.<commit_after>require_relative 'source_code_matcher_extractor'
require 'ostruct'
require 'dotenv'

module RubyAtdd

  class Base

    def self.scenarios(attrs)
      load_dot_env
      called_by = called_by(caller)

      puts '', '-'*30, "TEST RUN STARTED", File.basename(called_by.file), "#{called_by.file}", "URL_BASE=#{ENV['BASE_URL']}", Time.now.to_s, '-'*30, ''

      failing_tests_count = handle_multiple_scenarios(get_scenarios_str(attrs))
      exit(failing_tests_count)
    end

    def initialize(*args)
    end

    def scenario(str)
      response_code = 0
      line_results = []
      begin
        ScenarioParser.new(str).each_line do |line, multiline_text|
          line_results << execute_line(line.to_s, multiline_text)
        end
      rescue Exception => e # must rescue Exception because Minitest::Assertion subclasses Exception
        puts "FAIL: #{e.message}"
        puts e.backtrace unless e.class.to_s == 'Minitest::Assertion'
        response_code = 1
      end
      puts
      {response_code: response_code, line_results: line_results}
    end

    private

    def self.handle_multiple_scenarios(str)
      results = ScenariosParser.new(str).map do |scenario_str|
        subject = new({uri_base: ENV['BASE_URL']})
        subject.scenario(scenario_str)
        .tap{|r| puts } # blank line between scenario results
      end

      failing_test_cnt = results.map{|r| r[:response_code] }.reduce(:+)
      pending_test_cnt = results.map{|r| r[:line_results].compact.size }.delete_if{|r| r > 0 }.size
      puts "#{failing_test_cnt} failed scenarios. #{pending_test_cnt} pending scenarios. #{results.size} total scenarios."
      failing_test_cnt
    end

    def self.load_dot_env
      Dotenv.load ".env.#{ENV['ENV']}", '.env'
{'fraction_non_alphanumeric': 0.11687587168758717, 'fraction_numerical': 0.003905160390516039, 'mean_word_length': 3.279236276849642, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1485864', 'n_tokens_mistral': 1192, 'n_tokens_neox': 1161, 'n_words': 245}