<commit_before>require 'stringio'

require_relative '../../../lib/tchart/main'

# 'require' all project files except ourselves.
me = File.absolute_path(__FILE__)
Dir.glob(File.dirname(me) + '/**/*.rb') {|fn| require fn if fn != me }

module Kernel
  def requirement(description, &block)
    TChart::Requirements::DSL::Runner.run_requirement(description, &block)
  end
end

module TChart
  module Requirements
    module DSL
      class Runner
        Data_filename = "_test_.txt"
        Tex_filename = "_test_.tex"
        
        # Load requirements file(s); each one will then invoke #run_requirement, 
        # via Kernel#requirement (see above), one or more times.
        def self.run(files)
          files.each { |filename| load filename }
        end
        
        # TODO: refactor - too big
        def self.run_requirement(description, &block)
          puts description
          # TODO: refactor - duplicate test
          puts "skip".indent(2) if block.nil?
          return if block.nil?
          requirement = create_requirement(&block)
          return unless requirement_valid?(requirement)
          delete_test_files
          actual_tex, actual_errors = run_tchart_run(requirement)
          Checker.check_output('TeX', requirement.expected_tex, actual_tex)
          Checker.check_output('errors', requirement.expected_errors, actual_errors)
        rescue => e
          $stderr.puts e.message.indent(2)
          $stderr.puts e.backtrace.join("\n    ")
        ensure
          delete_test_files
        end
        
        def self.delete_test_files
          [ Data_filename, Tex_filename ].each { |filename| File.delete(filename) if File.exists?(filename) }
        end
        
        def self.create_requirement(&block)
          requirement = Requirement.new
          requirement.instance_eval(&block)
          requirement
        end
        
        def self.requirement_valid?(requirement)
          case
          when requirement.expected_tex.nil? && requirement.expected_errors.nil?
            $stderr.puts "Error: either 'the_expected_tex_is' or 'the_expected_errors_are' must be given, but neither was found.".indent(2)
            false
          when !requirement.expected_tex.nil? && !requirement.expected_errors.nil?
            $stderr.puts "Error: only one of 'the_expected_tex_is' or 'the_expected_errors_are' must be given, but both were found.".indent(2)<commit_msg>Clean up code a bit.<commit_after>require 'stringio'

require_relative '../../../lib/tchart/main'

# 'require' all project files except ourselves.
me = File.absolute_path(__FILE__)
Dir.glob(File.dirname(me) + '/**/*.rb') {|fn| require fn if fn != me }

module Kernel
  def requirement(description, &block)
    TChart::Requirements::DSL::Runner.run_requirement(description, &block)
  end
end

module TChart
  module Requirements
    module DSL
      class Runner
        
        Data_filename = "_test_.txt"
        Tex_filename = "_test_.tex"
        
        # Load requirements file(s); each one will then invoke #run_requirement, 
        # via Kernel#requirement (see above), one or more times.
        def self.run(files)
          files.each { |filename| load filename }
        end
        
        def self.run_requirement(description, &block)
          puts description
          # TODO: refactor - duplicate test
          puts "skip".indent(2) if block.nil?
          return if block.nil?
          requirement = create_requirement(&block)
          return unless requirement_valid?(requirement)
          delete_test_files
          actual_tex, actual_errors = run_tchart_run(requirement)
          Checker.check_output('TeX', requirement.expected_tex, actual_tex)
          Checker.check_output('errors', requirement.expected_errors, actual_errors)
        rescue => e
          $stderr.puts e.message.indent(2)
          $stderr.puts e.backtrace.join("\n    ")
        ensure
          delete_test_files
        end
        
      private
        
        def self.delete_test_files
          [ Data_filename, Tex_filename ].each { |filename| File.delete(filename) if File.exists?(filename) }
        end
        
        def self.create_requirement(&block)
          requirement = Requirement.new
          requirement.instance_eval(&block)
          requirement
        end
        
        def self.requirement_valid?(requirement)
          case
          when requirement.expected_tex.nil? && requirement.expected_errors.nil?
            $stderr.puts "Error: either 'the_expected_tex_is' or 'the_expected_errors_are' must be given, but neither was found.".indent(2)
            false
          when !requirement.expected_tex.nil? && !requirement.expected_errors.nil?
            $stderr.puts "Error: only one of 'the_expected_tex_is' or 'the_expected_errors_are' must be given, but both were found.".indent(2)
{'fraction_non_alphanumeric': 0.09439406430338004, 'fraction_numerical': 0.0016488046166529267, 'mean_word_length': 2.436968838526912, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '5247908', 'n_tokens_mistral': 1352, 'n_tokens_neox': 1364, 'n_words': 328}