<commit_before>#!/usr/bin/env ruby

require_relative 'controller_test_base'

class DifferControllerTest < ControllerTestBase

  def setup
    super
    reset_external(:runner, runner)
  end

  def teardown
    reset_external(:runner, nil)
  end

  #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  test 'no lines different in any files between successive tags' do
    @id = create_kata
    enter # 0
    filename = 'hiker.c'
    kata_run_tests file_content: { filename => '#include...' }, #1
      file_hashes_incoming: { filename => 234234 },
      file_hashes_outgoing: { filename => -4545645678 }
    kata_run_tests file_content: { filename => 'wibble' }, #2
      file_hashes_incoming: { filename => -4545645678 },
      file_hashes_outgoing: { filename => -4545645678 }
    @was_tag,@now_tag = 1,2
    differ
    lights = json['lights']
    info = " " + @id + ":" + @avatar_name
    was_light = lights[@was_tag-1]
    assert_equal 'amber', was_light['colour'], info
    assert_equal @was_tag, was_light['number'], info
    now_light = lights[@now_tag-1]
    assert_equal 'amber', now_light['colour'], info
    assert_equal @now_tag, now_light['number'], info
    diffs = json['diffs']
    assert_equal filename, diffs[0]['filename'], info
    assert_equal 0, diffs[0]['section_count'], info
    assert_equal 0, diffs[0]['deleted_line_count'], info
    assert_equal 0, diffs[0]['added_line_count'], info
    assert_equal '<same>wibble</same>', diffs[0]['content'], info
    assert_equal '<same><ln>1</ln></same>', diffs[0]['line_numbers'], info
  end

  test 'one line different in one file between successive tags' do
    @id = create_kata
    enter # 0
    filename = 'hiker.rb'
    kata_run_tests file_content: { filename => 'tweedledee' },
      file_hashes_incoming: { filename => 234234 },
      file_hashes_outgoing: { filename => -4545645678 } #1
    kata_run_tests file_content: { filename => 'tweedledum' },
      file_hashes_incoming: { filename => -4545645678 },
      file_hashes_outgoing: { filename => 654356 } #2
    @was_tag,@now_tag = 1,2
    differ
    lights = json['lights']
    info = " " + @id + ':' + @avatar_name + ':'
    was_light = lights[@was_tag-1]
    assert_equal 'amber', was_light['colour'], info
    assert_equal @was_tag, was_light['number'], info
    now_light = lights[@now_tag-1]
    assert_equal 'amber', now_light['colour'], info
    assert_equal @now_tag, now_light['number'], info
    diffs = json['diffs']
    assert_equal filename, diffs[0]['filename'], info + "diffs[0]['filename']"
    assert_equal 1, diffs[0]['section_count'], info + "diffs[0]['section_count']"
    assert_equal 1, diffs[0]['deleted_line_count'], info + "diffs[0]['deleted_line_count']"
    assert_equal 1, diffs[0]['added_line_count'], info + "diffs[0]['added_line_count']"
    assert diffs[0]['content'].include?('<deleted>tweedledee</deleted>')
    assert diffs[0]['content'].include?('<added>tweedledum</added>')
    assert_equal '<deleted><ln>1</ln></deleted><added><ln>1</ln></added>', diffs[0]['line_numbers'], info + "diffs[0]['line_numbers']"
  end

  test 'tag -1 gives last traffic-light' do
    @id = create_kata
    enter     # 0
    any_test  # 1
    any_test  # 2
    @was_tag,@now_tag = -1,-1
    differ
    assert_equal 2, json['wasTag']
    assert_equal 2, json['nowTag']
  end

  test 'nextAvatar and prevAvatar are empty string for dojo with one avatar' do
    @id = create_kata
    enter     # 0
    any_test  # 1
    @was_tag,@now_tag = 0,1
    differ
    assert_equal "", json['prevAvatar']
    assert_equal "", json['nextAvatar']
  end

  test 'nextAvatar and prevAvatar for dojo with two avatars' do
    @id = create_kata
    firstAvatar = enter # 0
    any_test  # 1
    secondAvatar = enter # 0
    any_test  # 1
    @was_tag,@now_tag = 0,1
    get 'differ/diff', :format => :json,
                       id:@id,
                       avatar:firstAvatar,
                       was_tag:@was_tag,
                       now_tag:@now_tag
    assert_equal secondAvatar, json['prevAvatar']
    assert_equal secondAvatar, json['nextAvatar']
  end

  #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  def runner
    if Docker.installed?
      DockerTestRunner.new
    else
      HostTestRunner.new<commit_msg>refactoring<commit_after>#!/usr/bin/env ruby

require_relative 'controller_test_base'

class DifferControllerTest < ControllerTestBase

  def setup
    super
    reset_external(:runner, runner)
  end

  def teardown
    reset_external(:runner, nil)
  end

  #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  test 'no lines different in any files between successive tags' do
    @id = create_kata
    enter # 0
    filename = 'hiker.c'
    kata_run_tests file_content: { filename => '#include...' }, #1
      file_hashes_incoming: { filename => 234234 },
      file_hashes_outgoing: { filename => -4545645678 }
    kata_run_tests file_content: { filename => 'wibble' }, #2
      file_hashes_incoming: { filename => -4545645678 },
      file_hashes_outgoing: { filename => -4545645678 }
    @was_tag,@now_tag = 1,2
    differ
    lights = json['lights']
    info = " " + @id + ":" + @avatar_name
    was_light = lights[@was_tag-1]
    assert_equal 'amber', was_light['colour'], info
    assert_equal @was_tag, was_light['number'], info
    now_light = lights[@now_tag-1]
    assert_equal 'amber', now_light['colour'], info
    assert_equal @now_tag, now_light['number'], info
    diffs = json['diffs']
    index = diffs.find_index{|diff| diff['filename'] == filename }    
    assert_equal filename, diffs[index]['filename'], info
    assert_equal 0, diffs[index]['section_count'], info
    assert_equal 0, diffs[index]['deleted_line_count'], info
    assert_equal 0, diffs[index]['added_line_count'], info
    assert_equal '<same>wibble</same>', diffs[index]['content'], info
    assert_equal '<same><ln>1</ln></same>', diffs[index]['line_numbers'], info
  end

  test 'one line different in one file between successive tags' do
    @id = create_kata
    enter # 0
    filename = 'hiker.rb'
    kata_run_tests file_content: { filename => 'def fubar' },
      file_hashes_incoming: { filename => 234234 },
      file_hashes_outgoing: { filename => -4545645678 } #1
    kata_run_tests file_content: { filename => 'def snafu' },
      file_hashes_incoming: { filename => -4545645678 },
      file_hashes_outgoing: { filename => 654356 } #2
    @was_tag,@now_tag = 1,2
    differ
    lights = json['lights']
    info = " " + @id + ':' + @avatar_name + ':'
    was_light = lights[@was_tag-1]
    assert_equal 'amber', was_light['colour'], info
    assert_equal @was_tag, was_light['number'], info
    now_light = lights[@now_tag-1]
    assert_equal 'amber', now_light['colour'], info
    assert_equal @now_tag, now_light['number'], info
    diffs = json['diffs']
    index = diffs.find_index{|diff| diff['filename'] == filename }
    assert_equal filename, diffs[index]['filename'], info + "diffs[#{index}]['filename']"
    assert_equal 1, diffs[index]['section_count'], info + "diffs[#{index}]['section_count']"
    assert_equal 1, diffs[index]['deleted_line_count'], info + "diffs[#{index}]['deleted_line_count']"
    assert_equal 1, diffs[index]['added_line_count'], info + "diffs[#{index}]['added_line_count']"
    assert diffs[index]['content'].include?('<deleted>def fubar</deleted>')
    assert diffs[index]['content'].include?('<added>def snafu</added>')
    assert_equal '<deleted><ln>1</ln></deleted><added><ln>1</ln></added>', 
        diffs[index]['line_numbers'], info + "diffs[0]['line_numbers']"
  end

  test 'tag -1 gives last traffic-light' do
    @id = create_kata
    enter     # 0
    any_test  # 1
    any_test  # 2
    @was_tag,@now_tag = -1,-1
    differ
    assert_equal 2, json['wasTag']
    assert_equal 2, json['nowTag']
  end

  test 'nextAvatar and prevAvatar are empty string for dojo with one avatar' do
    @id = create_kata
    enter     # 0
    any_test  # 1
    @was_tag,@now_tag = 0,1
    differ
    assert_equal "", json['prevAvatar']
    assert_equal "", json['nextAvatar']
  end

  test 'nextAvatar and prevAvatar for dojo with two avatars' do
    @id = create_kata
    firstAvatar = enter # 0
    any_test  # 1
    secondAvatar = enter # 0
    any_test  # 1
    @was_tag,@now_tag = 0,1
    @avatar_name = firstAvatar
    differ
    assert_equal secondAvatar, json['prevAvatar']
    assert_equal secondAvatar, json['nextAvatar']
  end

  #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  def runner
    if Docker.installed?
      DockerTestRunner.new
    else
      HostTestRunner.new
{'fraction_non_alphanumeric': 0.14917254947344058, 'fraction_numerical': 0.027427381090151602, 'mean_word_length': 3.1270296084049667, 'pattern_counts': {'":': 2, '<': 41, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4805725', 'n_tokens_mistral': 3147, 'n_tokens_neox': 2986, 'n_words': 624}