<commit_before>
require_relative '../all'

class OneLanguageChecker

  def initialize(root_path, option)
    ENV['CYBERDOJO_TEST_ROOT_DIR'] = 'true'
    @root_path = root_path
    @verbose = (option == 'noisy')
  end

  def check(language_name)
    # if running on a Docker server
    #    return [red,amber,green] state
    # else
    #    return nil
    @language = dojo.languages[language_name]
    if @language.runnable?
      vputs "  #{language_name} " + ('.' * (35-language_name.to_s.length))
      t1 = Time.now
      rag = red_amber_green
      t2 = Time.now
      took = ((t2 - t1) / 3).round(2)
      vputs " (~ #{took} seconds)\n"
      rag
    end
  end

private

  def red_amber_green
    # NB: This works by creating a new kata for each
    #     red/amber/green test. This is needlessly slow.
    #     Just make one kata, and then verify
    #     o) its starts tests red
    #     o) s/6*9/6*7/ tests green
    #     o) s/6*9/s345 * 9345/ tests amber
    [
      language_test(:red),
      language_test(:amber),
      language_test(:green),
    ]
  end

  def language_test(colour)
    pattern = pattern_6times9
    filename = filename_6times9(pattern[:red])
    from = pattern[:red]
    to = pattern[colour]
    exercise = dojo.exercises['Fizz_Buzz']
    kata = dojo.katas.create_kata(@language, exercise)
    avatar = kata.start_avatar
    visible_files = @language.visible_files
    test_code = visible_files[filename]
    visible_files[filename] = test_code.sub(from, to)

    vputs [
      "<test_code id='#{kata.id}' avatar='#{avatar.name}' colour='#{colour}'>",
      visible_files[filename],
      "</test_code>"
    ].join("\n")

    delta = {
      :changed => [filename],
      :unchanged => visible_files.keys - [filename],
      :deleted => [ ],
      :new => [ ]
    }

    traffic_lights,_,_ = avatar.test(delta, visible_files)

    vputs [
      "<output>",
      visible_files['output'],
      "</output>"
    ].join("\n")

    traffic_lights.last['colour']
  end

  def pattern_6times9
    case (@language.name)
      when 'Clojure-.test'
        then make_pattern('* 6 9')
      when 'Java-1.8_Cucumber'
        then make_pattern('6 times 9')
      when 'Java-1.8_Mockito',
           'Java-1.8_Powermockito'
        then make_pattern('thenReturn(9)')
      else
        make_pattern('6 * 9')
    end
  end

  def make_pattern(base)
    { :red => base,
      :amber => base.sub('9', '9typo'),
      :green => base.sub('9', '7')
    }
  end

  def filename_6times9(pattern)
    filenames = @language.visible_filenames.select { |visible_filename|
      IO.read(@language.path + visible_filename).include? pattern
    }
    if filenames == [ ]
      message = alert + " no '#{pattern}' file found"<commit_msg>language test now prints . for each red/amber/green test so test run provides feedback when it runs<commit_after>
require_relative '../all'

class OneLanguageChecker

  def initialize(root_path, option)
    ENV['CYBERDOJO_TEST_ROOT_DIR'] = 'true'
    @root_path = root_path
    @verbose = (option == 'noisy')
  end

  def check(language_name)
    # if running on a Docker server
    #    return [red,amber,green] state
    # else
    #    return nil
    @language = dojo.languages[language_name]
    if @language.runnable?
      vputs "  #{language_name} " + ('.' * (35-language_name.to_s.length))
      t1 = Time.now
      rag = red_amber_green
      t2 = Time.now
      took = ((t2 - t1) / 3).round(2)
      vputs " (~ #{took} seconds)\n"
      rag
    end
  end

private

  def red_amber_green
    # NB: This works by creating a new kata for each
    #     red/amber/green test. This is needlessly slow.
    #     Just make one kata, and then verify
    #     o) its starts tests red
    #     o) s/6*9/6*7/ tests green
    #     o) s/6*9/s345 * 9345/ tests amber
    [
      language_test(:red),
      language_test(:amber),
      language_test(:green),
    ]
  end

  def language_test(colour)
    pattern = pattern_6times9
    filename = filename_6times9(pattern[:red])
    from = pattern[:red]
    to = pattern[colour]
    exercise = dojo.exercises['Fizz_Buzz']
    kata = dojo.katas.create_kata(@language, exercise)
    avatar = kata.start_avatar
    visible_files = @language.visible_files
    test_code = visible_files[filename]
    visible_files[filename] = test_code.sub(from, to)

    vputs [
      "<test_code id='#{kata.id}' avatar='#{avatar.name}' colour='#{colour}'>",
      visible_files[filename],
      "</test_code>"
    ].join("\n")

    delta = {
      :changed => [filename],
      :unchanged => visible_files.keys - [filename],
      :deleted => [ ],
      :new => [ ]
    }

    traffic_lights,_,_ = avatar.test(delta, visible_files)

    vputs [
      "<output>",
      visible_files['output'],
      "</output>"
    ].join("\n")

    print '.'
    traffic_lights.last['colour']
  end

  def pattern_6times9
    case (@language.name)
      when 'Clojure-.test'
        then make_pattern('* 6 9')
      when 'Java-1.8_Cucumber'
        then make_pattern('6 times 9')
      when 'Java-1.8_Mockito',
           'Java-1.8_Powermockito'
        then make_pattern('thenReturn(9)')
      else
        make_pattern('6 * 9')
    end
  end

  def make_pattern(base)
    { :red => base,
      :amber => base.sub('9', '9typo'),
      :green => base.sub('9', '7')
    }
  end

  def filename_6times9(pattern)
    filenames = @language.visible_filenames.select { |visible_filename|
      IO.read(@language.path + visible_filename).include? pattern
    }
    if filenames == [ ]
      message = alert + " no '#{pattern}' file found"
{'fraction_non_alphanumeric': 0.13600572655690765, 'fraction_numerical': 0.0164638511095204, 'mean_word_length': 2.5217391304347827, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 25, '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': '5280800', 'n_tokens_mistral': 1958, 'n_tokens_neox': 1907, 'n_words': 415}