<commit_before>require_relative '../test_helper'
require 'trackler/problem'
require 'trackler/implementation'

class ImplementationTest < Minitest::Test
  PATH     = FIXTURE_PATH
  TRACK_ID = 'fake'.freeze
  URL      = '[REPO_URL]'.freeze
  PROBLEM  = Trackler::Problem.new('hello-world', PATH)

  def test_zip
    implementation = Trackler::Implementation.new(TRACK_ID, URL, PROBLEM, PATH)
    # Our archive is not binary identically reproducable :(
    archive = implementation.zip
    assert_instance_of StringIO, archive
    expected_files = ['hello_test.ext', 'world_test.ext', 'README.md']
    assert_equal expected_files, archive_filenames(implementation.zip)
  end

  def test_implementation_with_extra_files
    problem = Trackler::Problem.new('one', PATH)
    implementation = Trackler::Implementation.new(TRACK_ID, URL, problem, PATH)

    expected = {
      "Fakefile" => "Autorun fake code\n",
      "one_test.ext" => "assert 'one'\n",

      # includes dotfiles
      ".dot" => "dot\n",

      # includes files in subdirectory
      "sub/src/stubfile.ext" => "stub\n",

      # contains implementation-specific hint, but not language-specific hint
      "README.md" => "# One\n\nThis is one.\n\n* one\n* one again\n\n* one hint\n* one more hint\n\n## Source\n\nThe internet. [http://example.com](http://example.com)\n\n## Submitting Incomplete Problems\nIt's possible to submit an incomplete solution so you can see how others have completed the exercise.\n\n"
    }
    assert_equal expected, implementation.files
  end

  def test_implementation_in_subdirectory
    problem = Trackler::Problem.new('apple', PATH)
    implementation = Trackler::Implementation.new('fruit', URL, problem, PATH)
    assert_equal "[REPO_URL]/tree/master/exercises/apple", implementation.git_url
    assert_match Regexp.new('exercises[\\/]apple'), implementation.exercise_dir

    expected = {
      "apple.ext" => "an apple a day keeps the doctor away\n",
      "apple.tst" => "assert 'apple'\n",
      "README.md" => "# Apple\n\nThis is apple.\n\n* apple\n* apple again\n\nThe SETUP.md file is deprecated, and exercises/TRACK_HINTS.md should be used.\n\n## Source\n\nThe internet.\n\n## Submitting Incomplete Problems\nIt's possible to submit an incomplete solution so you can see how others have completed the exercise.\n\n"
    }
    assert_equal expected, implementation.files
  end

  def test_language_and_implementation_specific_readme
    problem = Trackler::Problem.new('banana', PATH)
    implementation = Trackler::Implementation.new('fruit', URL, problem, PATH)

    expected = "# Banana\n\nThis is banana.\n\n* banana\n* banana again\n\n* banana specific hints.\n* a hint\n* another hint\n\nThe SETUP.md file is deprecated, and exercises/TRACK_HINTS.md should be used.\n\n## Source\n\n[http://example.com](http://example.com)\n\n## Submitting Incomplete Problems\nIt's possible to submit an incomplete solution so you can see how others have completed the exercise.\n\n"

    assert_equal expected, implementation.readme
  end

  def test_symlinked_file
    problem = Trackler::Problem.new('fish', PATH)
    implementation = Trackler::Implementation.new('animal', URL, problem, PATH)

    expected = "This should get included in fish.\n"
    assert_equal expected, implementation.files['included-via-symlink.txt']
  end

  def test_missing_implementation
    problem = Trackler::Problem.new('apple', PATH)
    implementation = Trackler::Implementation.new(TRACK_ID, URL, problem, PATH)
    refute implementation.exists?, "Not expecting apple to be implemented for track TRACK_ID"
  end

  def test_override_implementation_files
    implementation = Trackler::Implementation.new(TRACK_ID, URL, PROBLEM, PATH)
    files = { "filename" => "contents" }
    implementation.files = files
    assert_equal files, implementation.files
  end

  def test_ignores_example_files
    track_id = 'fruit'
    problem = Trackler::Problem.new('imbe', PATH)
    implementation = Trackler::Implementation.new(track_id, URL, problem, PATH)
    expected = ['imbe.txt', 'README.md']
    assert_equal expected, implementation.files.keys
  end

  def test_readme_has_empty_string_for_track_hint_when_setup_file_does_not_exist
    track_id = 'fake'
    problem = Trackler::Problem.new('apple', PATH)
    implementation = Trackler::Implementation.new(track_id, URL, problem, PATH)

    expected = "# Apple\n\nThis is apple.\n\n* apple\n* apple again\n\n## Source\n\nThe internet.\n\n## Submitting Incomplete Problems\nIt's possible to submit an incomplete solution so you can see how others have completed the exercise.\n\n"
    assert_equal expected, implementation.readme
  end

  def test_readme_uses_track_hint_in_precedence_of_setup
    track_id = 'animal'
    problem = Trackler::Problem.new('dog', PATH)
    implementation = Trackler::Implementation.new(track_id, URL, problem, PATH)

    assert_match /This is the content of the track hints file/, implementation.readme
  end

  def test_readme_uses_setup_when_track_hints_is_missing
    track_id = 'fruit'
    problem = Trackler::Problem.new('apple', PATH)
    implementation = Trackler::Implementation.new(track_id, URL, problem, PATH)

    assert_match %r(The SETUP.md file is deprecated, and exercises/TRACK_HINTS.md should be used.), implementation.readme
  end

  def test_readme_uses_track_hint_instead_of_setup
    track_id = 'jewels'
    problem = Trackler::Problem.new('hello-world', PATH)
    implementation = Trackler::Implementation.new(track_id, URL, problem, PATH)

    assert_match /This is the content of the track hints file/, implementation.readme
  end

  private

  def archive_filenames(zip)
    files = []
    Zip::InputStream.open(zip) do |io|
      while (entry = io.get_next_entry)
        files << entry.name
      end
    end
    files
  end
end
<commit_msg>Inline constants in Implementation tests<commit_after>require_relative '../test_helper'
require 'trackler/problem'
require 'trackler/implementation'

class ImplementationTest < Minitest::Test
  def test_zip
    problem = Trackler::Problem.new('hello-world', FIXTURE_PATH)
    implementation = Trackler::Implementation.new("fake", "repo.url", problem, FIXTURE_PATH)
    # Our archive is not binary identically reproducable :(
    archive = implementation.zip
    assert_instance_of StringIO, archive
    expected_files = ['hello_test.ext', 'world_test.ext', 'README.md']
    assert_equal expected_files, archive_filenames(implementation.zip)
  end

  def test_implementation_with_extra_files
    problem = Trackler::Problem.new('one', FIXTURE_PATH)
    implementation = Trackler::Implementation.new("fake", "repo.url", problem, FIXTURE_PATH)

    expected = {
      "Fakefile" => "Autorun fake code\n",
      "one_test.ext" => "assert 'one'\n",

      # includes dotfiles
      ".dot" => "dot\n",

      # includes files in subdirectory
      "sub/src/stubfile.ext" => "stub\n",

      # contains implementation-specific hint, but not language-specific hint
      "README.md" => "# One\n\nThis is one.\n\n* one\n* one again\n\n* one hint\n* one more hint\n\n## Source\n\nThe internet. [http://example.com](http://example.com)\n\n## Submitting Incomplete Problems\nIt's possible to submit an incomplete solution so you can see how others have completed the exercise.\n\n"
    }
    assert_equal expected, implementation.files
  end

  def test_implementation_in_subdirectory
    problem = Trackler::Problem.new('apple', FIXTURE_PATH)
    implementation = Trackler::Implementation.new('fruit', "repo.url", problem, FIXTURE_PATH)
    assert_equal "repo.url/tree/master/exercises/apple", implementation.git_url
    assert_match Regexp.new('exercises[\\/]apple'), implementation.exercise_dir

    expected = {
      "apple.ext" => "an apple a day keeps the doctor away\n",
      "apple.tst" => "assert 'apple'\n",
      "README.md" => "# Apple\n\nThis is apple.\n\n* apple\n* apple again\n\nThe SETUP.md file is deprecated, and exercises/TRACK_HINTS.md should be used.\n\n## Source\n\nThe internet.\n\n## Submitting Incomplete Problems\nIt's possible to submit an incomplete solution so you can see how others have completed the exercise.\n\n"
    }
    assert_equal expected, implementation.files
  end

  def test_language_and_implementation_specific_readme
    problem = Trackler::Problem.new('banana', FIXTURE_PATH)
    implementation = Trackler::Implementation.new('fruit', "repo.url", problem, FIXTURE_PATH)

    expected = "# Banana\n\nThis is banana.\n\n* banana\n* banana again\n\n* banana specific hints.\n* a hint\n* another hint\n\nThe SETUP.md file is deprecated, and exercises/TRACK_HINTS.md should be used.\n\n## Source\n\n[http://example.com](http://example.com)\n\n## Submitting Incomplete Problems\nIt's possible to submit an incomplete solution so you can see how others have completed the exercise.\n\n"

    assert_equal expected, implementation.readme
  end

  def test_symlinked_file
    problem = Trackler::Problem.new('fish', FIXTURE_PATH)
    implementation = Trackler::Implementation.new('animal', "repo.url", problem, FIXTURE_PATH)

    expected = "This should get included in fish.\n"
    assert_equal expected, implementation.files['included-via-symlink.txt']
  end

  def test_missing_implementation
    problem = Trackler::Problem.new('apple', FIXTURE_PATH)
    implementation = Trackler::Implementation.new("fake", "repo.url", problem, FIXTURE_PATH)
    refute implementation.exists?, "Not expecting apple to be implemented for track TRACK_ID"
  end

  def test_override_implementation_files
    problem = Trackler::Problem.new('hello-world', FIXTURE_PATH)
    implementation = Trackler::Implementation.new("fake", "repo.url", problem, FIXTURE_PATH)
    files = { "filename" => "contents" }
    implementation.files = files
    assert_equal files, implementation.files
  end

  def test_ignores_example_files
    track_id = 'fruit'
    problem = Trackler::Problem.new('imbe', FIXTURE_PATH)
    implementation = Trackler::Implementation.new(track_id, "repo.url", problem, FIXTURE_PATH)
    expected = ['imbe.txt', 'README.md']
    assert_equal expected, implementation.files.keys
  end

  def test_readme_has_empty_string_for_track_hint_when_setup_file_does_not_exist
    track_id = 'fake'
    problem = Trackler::Problem.new('apple', FIXTURE_PATH)
    implementation = Trackler::Implementation.new(track_id, "repo.url", problem, FIXTURE_PATH)

    expected = "# Apple\n\nThis is apple.\n\n* apple\n* apple again\n\n## Source\n\nThe internet.\n\n## Submitting Incomplete Problems\nIt's possible to submit an incomplete solution so you can see how others have completed the exercise.\n\n"
    assert_equal expected, implementation.readme
  end

  def test_readme_uses_track_hint_in_precedence_of_setup
    track_id = 'animal'
    problem = Trackler::Problem.new('dog', FIXTURE_PATH)
    implementation = Trackler::Implementation.new(track_id, "repo.url", problem, FIXTURE_PATH)

    assert_match /This is the content of the track hints file/, implementation.readme
  end

  def test_readme_uses_setup_when_track_hints_is_missing
    track_id = 'fruit'
    problem = Trackler::Problem.new('apple', FIXTURE_PATH)
    implementation = Trackler::Implementation.new(track_id, "repo.url", problem, FIXTURE_PATH)

    assert_match %r(The SETUP.md file is deprecated, and exercises/TRACK_HINTS.md should be used.), implementation.readme
  end

  def test_readme_uses_track_hint_instead_of_setup
    track_id = 'jewels'
    problem = Trackler::Problem.new('hello-world', FIXTURE_PATH)
    implementation = Trackler::Implementation.new(track_id, "repo.url", problem, FIXTURE_PATH)

    assert_match /This is the content of the track hints file/, implementation.readme
  end

  private

  def archive_filenames(zip)
    files = []
    Zip::InputStream.open(zip) do |io|
      while (entry = io.get_next_entry)
        files << entry.name
      end
    end
    files
  end
end

{'fraction_non_alphanumeric': 0.11010406176569319, 'fraction_numerical': 0.0, 'mean_word_length': 4.213035870516186, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, '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': '3295139', 'n_tokens_mistral': 3810, 'n_tokens_neox': 3609, 'n_words': 915}