<commit_before>
require_relative '../all'

class OneLanguageChecker

  include ExternalSetter

  def initialize(root_path, verbose)
    @root_path = root_path
    @root_path += '/' if !root_path.end_with?('/')
    @verbose = verbose
  end

  def dojo
    reset_external(:disk, Disk.new)
    reset_external(:git, Git.new)
    reset_external(:runner, runner)
    reset_external(:exercises_path, @root_path + 'exercises/')
    reset_external(:languages_path, @root_path + 'languages/')
    reset_external(:katas_path, @root_path + 'test/cyber-dojo/katas/')
    Dojo.new
  end

  def runner
    return DockerTestRunner.new if Docker.installed?
    return HostTestRunner.new unless ENV['CYBERDOJO_USE_HOST'].nil?
    return DummyTestRunner.new
  end

  def check(name,test)
    language_name = [name,test].join('-')
    # if running on a Docker server
    #    return [red,amber,green] state
    # else
    #    return nil
    @language = dojo.languages[language_name]
    if true #@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
    else
      vputs " #{language_name} is not runnable"
    end
  end

private

  include TimeNow

  def red_amber_green
    # creates a new *dojo* for each red/amber/green
    [:red,:amber,:green].map{ |colour|
      begin
        language_test(colour)
      rescue Exception => e
        e.message
      end
    }
  end

  def language_test(colour)
    exercise = dojo.exercises['Fizz_Buzz']
    kata = dojo.katas.create_kata(@language, exercise)
    avatar = kata.start_avatar

    pattern = pattern_6times9

    filename = filename_6times9(pattern[:red])
    from = pattern[:red]
    to = pattern[colour]

    if @language.name === 'Java1.8-Cucumber' && colour === :amber
      filename = 'Hiker.java'
      from = '}'
      to = '}typo'
    end

    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}' expected_colour='#{colour}'>",
      visible_files[filename],
      "</test_code>"
    ].join("\n")

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

    now = time_now
    limit = 60
    traffic_lights,_,_ = avatar.test(delta, visible_files, now, limit)

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

    rag = traffic_lights.last['colour']

    vputs [
      "<test_code actual_colour='#{rag}'>",
      "</test_code>"
    ].join("\n")

    print '.'
    rag
  end

  def pattern_6times9
    case (@language.name)
      when 'Clojure-.test'
        then make_pattern('* 6 9')
      when 'Java1.8-Cucumber',
           'Ruby1.9.3-Cucumber'
        then make_pattern('6 times 9')
      when 'Java1.8-Mockito',
           'Java1.8-Powermockito'
        then make_pattern('thenReturn(9)')
      when 'Asm-assert'
        then make_pattern('mov ebx, 9')
      else
        make_pattern('6 * 9')
    end
  end<commit_msg>fixed language test 6*9 pattern matcher now language.name is decoupled from folder names<commit_after>
require_relative '../all'

class OneLanguageChecker

  include ExternalSetter

  def initialize(root_path, verbose)
    @root_path = root_path
    @root_path += '/' if !root_path.end_with?('/')
    @verbose = verbose
  end

  def dojo
    reset_external(:disk, Disk.new)
    reset_external(:git, Git.new)
    reset_external(:runner, runner)
    reset_external(:exercises_path, @root_path + 'exercises/')
    reset_external(:languages_path, @root_path + 'languages/')
    reset_external(:katas_path, @root_path + 'test/cyber-dojo/katas/')
    Dojo.new
  end

  def runner
    return DockerTestRunner.new if Docker.installed?
    return HostTestRunner.new unless ENV['CYBERDOJO_USE_HOST'].nil?
    return DummyTestRunner.new
  end

  def check(name,test)
    language_name = [name,test].join('-')
    # if running on a Docker server
    #    return [red,amber,green] state
    # else
    #    return nil
    @language = dojo.languages[language_name]
    if true #@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
    else
      vputs " #{language_name} is not runnable"
    end
  end

private

  include TimeNow

  def red_amber_green
    # creates a new *dojo* for each red/amber/green
    [:red,:amber,:green].map{ |colour|
      begin
        language_test(colour)
      rescue Exception => e
        e.message
      end
    }
  end

  def language_test(colour)
    exercise = dojo.exercises['Fizz_Buzz']
    kata = dojo.katas.create_kata(@language, exercise)
    avatar = kata.start_avatar

    pattern = pattern_6times9

    filename = filename_6times9(pattern[:red])
    from = pattern[:red]
    to = pattern[colour]

    if @language.name === 'Java1.8-Cucumber' && colour === :amber
      filename = 'Hiker.java'
      from = '}'
      to = '}typo'
    end

    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}' expected_colour='#{colour}'>",
      visible_files[filename],
      "</test_code>"
    ].join("\n")

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

    now = time_now
    limit = 60
    traffic_lights,_,_ = avatar.test(delta, visible_files, now, limit)

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

    rag = traffic_lights.last['colour']

    vputs [
      "<test_code actual_colour='#{rag}'>",
      "</test_code>"
    ].join("\n")

    print '.'
    rag
  end

  def pattern_6times9
    case (@language.name)
      when 'Clojure-.test'
        then make_pattern('* 6 9')
      when 'Java-Cucumber',
           'Ruby-Cucumber'
        then make_pattern('6 times 9')
      when 'Java-Mockito',
           'Java-Powermockito'
        then make_pattern('thenReturn(9)')
      when 'Asm-assert'
        then make_pattern('mov ebx, 9')
      else
        make_pattern('6 * 9')
    end
  end
{'fraction_non_alphanumeric': 0.13523402957767952, 'fraction_numerical': 0.0096051227321238, 'mean_word_length': 2.598464070213933, 'pattern_counts': {'":': 0, '<': 15, '<?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': '4426000', 'n_tokens_mistral': 2315, 'n_tokens_neox': 2228, 'n_words': 463}