<commit_before>require File.expand_path(File.dirname(__FILE__)) + '/../../../test_helper'

require 'holidays/definition/context/generator'

class GeneratorTests < Test::Unit::TestCase
  def setup
    @custom_method_parser = mock()
    @custom_method_source_decorator = mock()
    @custom_methods_repository = mock()

    @parsed_custom_method = Holidays::Definition::Entity::CustomMethod.new(
      name: 'custom_method',
      arguments: [:year, :month],
      source: "some source",
    )

    @generator = Holidays::Definition::Context::Generator.new(
      @custom_method_parser,
      @custom_method_source_decorator,
      @custom_methods_repository,
    )
  end

  def test_parse_definition_files_raises_error_if_argument_is_nil
    assert_raises ArgumentError do
      @generator.parse_definition_files(nil)
    end
  end

  def test_parse_definition_files_raises_error_if_files_are_empty
    assert_raises ArgumentError do
      @generator.parse_definition_files([])
    end
  end

  def test_parse_definition_files_correctly_parse_regions
    files = ['test/data/test_single_custom_holiday_defs.yaml']
    @custom_method_parser.expects(:call).with(nil).returns({})

    regions = @generator.parse_definition_files(files)[0]

    assert_equal [:custom_single_file], regions
  end

  def test_parse_definitions_files_correctly_parse_rules_by_month
    files = ['test/data/test_single_custom_holiday_defs.yaml']
    @custom_method_parser.expects(:call).with(nil).returns({})

    rules_by_month = @generator.parse_definition_files(files)[1]

    expected_rules_by_month = {
      6 => [
        {
          :mday    => 20,
          :name    => "Company Founding",
          :regions => [:custom_single_file]
        }
      ]
    }

    assert_equal expected_rules_by_month, rules_by_month
  end

  def test_parse_definition_files_correctly_parse_custom_methods
    files = ['test/data/test_single_custom_holiday_defs.yaml']
    @custom_method_parser.expects(:call).with(nil).returns({})

    custom_methods = @generator.parse_definition_files(files)[2]

    expected_custom_methods = {}
    assert_equal expected_custom_methods, custom_methods
  end

  def test_parse_definition_files_correctly_parse_tests
    files = ['test/data/test_single_custom_holiday_defs.yaml']
    @custom_method_parser.expects(:call).with(nil).returns({})

    tests = @generator.parse_definition_files(files)[3]

    expected_tests = [[
        "{Date.civil(2013,6,20) => 'Company Founding'}.each do |date, name|\n  assert_equal name, (Holidays.on(date, :custom_single_file)[0] || {})[:name]\nend"
    ]]

    assert_equal expected_tests, tests
  end

  def test_generate_definition_source_correctly_generate_module_src
    files = ['test/data/test_single_custom_holiday_defs.yaml']
    @custom_method_parser.expects(:call).with(nil).returns({})

    regions, rules_by_month, custom_methods, tests = @generator.parse_definition_files(files)
    module_src = @generator.generate_definition_source("test", files, regions, rules_by_month, custom_methods, tests)[0]

    expected_module_src = "# encoding: utf-8\nmodule Holidays\n  # This file is generated by the Ruby Holidays gem.\n  #\n  # Definitions loaded: test/data/test_single_custom_holiday_defs.yaml\n  #\n  # To use the definitions in this file, load it right after you load the\n  # Holiday gem:\n  #\n  #   require 'holidays'\n  #   require 'generated_definitions/test'\n  #\n  # All the definitions are available at https://github.com/holidays/holidays\n  module TEST # :nodoc:\n    def self.defined_regions\n      [:custom_single_file]\n    end\n\n    def self.holidays_by_month\n      {\n              6 => [{:mday => 20, :name => \"Company Founding\", :regions => [:custom_single_file]}]\n      }\n    end\n\n    def self.custom_methods\n      {\n        \n      }\n    end\n  end\nend\n"

    assert_equal expected_module_src, module_src
  end

  def test_generate_definition_source_correctly_generate_test_src
    files = ['test/data/test_single_custom_holiday_defs.yaml']
    @custom_method_parser.expects(:call).with(nil).returns({})

    regions, rules_by_month, custom_methods, tests = @generator.parse_definition_files(files)
    test_src = @generator.generate_definition_source("test", files, regions, rules_by_month, custom_methods, tests)[1]

    expected_test_src = "# encoding: utf-8\nrequire File.expand_path(File.dirname(__FILE__)) + '/../test_helper'\n\n# This file is generated by the Ruby Holiday gem.\n#\n# Definitions loaded: test/data/test_single_custom_holiday_defs.yaml\nclass TestDefinitionTests < Test::Unit::TestCase  # :nodoc:\n\n  def test_test\n{Date.civil(2013,6,20) => 'Company Founding'}.each do |date, name|\n  assert_equal name, (Holidays.on(date, :custom_single_file)[0] || {})[:name]\nend\n  end\nend\n"

    assert_equal expected_test_src, test_src
  end

  def test_parse_definitions_files_correctly_parse_year_range_by_month
    files = ['test/data/test_custom_year_range_holiday_defs.yaml']
    @custom_method_parser.expects(:call).with(nil).returns({})

    rules_by_month = @generator.parse_definition_files(files)[1]

    expected_rules_by_month = {
      6 => [
        {
          :name => "after_year",
          :regions => [:custom_year_range_file],
          :mday => 1,
          :year_ranges => [{"after" => 2016}]
        },
        {
          :name => "before_year",
          :regions => [:custom_year_range_file],
          :mday => 2,
          :year_ranges => [{"before" => 2017}]
        },
        {
          :name => "between_year",
          :regions => [:custom_year_range_file],
          :mday => 3,
          :year_ranges => [{"between" => 2016..2018}]
        },
        {
          :name => "limited_year",
          :regions => [:custom_year_range_file],
          :mday => 4,
          :year_ranges => [{"limited" => [2016,2018,2019]}]
        },
        {
          :name => "multiple_conditions",
          :regions => [:custom_year_range_file],
          :mday => 5,
          :year_ranges => [{"before" => 2015}, {"after" => 2017}]
        }
      ]
    }

    assert_equal expected_rules_by_month, rules_by_month
  end

  def test_generate_definition_source_correctly_generate_yearrange_test_src
    files = ['test/data/test_custom_year_range_holiday_defs.yaml']
    @custom_method_parser.expects(:call).with(nil).returns({})

    regions, rules_by_month, custom_methods, tests = @generator.parse_definition_files(files)
    module_src = @generator.generate_definition_source("test", files, regions, rules_by_month, custom_methods, tests)[0]
    expected_module_src = "# encoding: utf-8\nmodule Holidays\n  # This file is generated by the Ruby Holidays gem.\n  #\n  # Definitions loaded: test/data/test_custom_year_range_holiday_defs.yaml\n  #\n  # To use the definitions in this file, load it right after you load the\n  # Holiday gem:\n  #\n  #   require 'holidays'\n  #   require 'generated_definitions/test'\n  #\n  # All the definitions are available at https://github.com/holidays/holidays\n  module TEST # :nodoc:\n    def self.defined_regions\n      [:custom_year_range_file]\n    end\n\n    def self.holidays_by_month\n      {\n              6 => [{:mday => 1,  :year_ranges => [{:after => 2016}],:name => \"after_year\", :regions => [:custom_year_range_file]},\n            {:mday => 2,  :year_ranges => [{:before => 2017}],:name => \"before_year\", :regions => [:custom_year_range_file]},\n            {:mday => 3,  :year_ranges => [{:between => 2016..2018}],:name => \"between_year\", :regions => [:custom_year_range_file]},\n            {:mday => 4,  :year_ranges => [{:limited => [2016, 2018, 2019]}],:name => \"limited_year\", :regions => [:custom_year_range_file]},\n            {:mday => 5,  :year_ranges => [{:before => 2015},{:after => 2017}],:name => \"multiple_conditions\", :regions => [:custom_year_range_file]}]\n      }\n    end\n\n    def self.custom_methods\n      {\n        \n      }\n    end\n  end\nend\n"

    assert_equal expected_module_src, module_src
  end

  def test_generate_definition_source_correctly_generate_module_src_with_custom_methods
    files = ['test/data/test_single_custom_holiday_with_custom_procs.yaml']

    @custom_method_parser.expects(:call).with('custom_method' => {'arguments' => 'year, month', 'source' => "d = Date.civil(year, month, 1)\nd + 2\n"}).returns({"custom_method(year, month)" => @parsed_custom_method})
    @custom_methods_repository.expects(:find).twice.with('custom_method(year, month)').returns(nil)
    @custom_method_source_decorator.expects(:call).once.with(@parsed_custom_method).returns("\"custom_method(year, month)\" => Proc.new { |year, month|\nsource_stuff\n}")

    regions, rules_by_month, custom_methods, tests = @generator.parse_definition_files(files)
    module_src = @generator.generate_definition_source("test", files, regions, rules_by_month, custom_methods, tests)[0]

    expected_module_src = "# encoding: utf-8\nmodule Holidays\n  # This file is generated by the Ruby Holidays gem.\n  #\n  # Definitions loaded: test/data/test_single_custom_holiday_with_custom_procs.yaml\n  #\n  # To use the definitions in this file, load it right after you load the\n  # Holiday gem:\n  #\n  #   require 'holidays'\n  #   require 'generated_definitions/test'\n  #\n  # All the definitions are available at https://github.com/holidays/holidays\n  module TEST # :nodoc:\n    def self.defined_regions\n      [:custom_single_file]\n    end\n\n    def self.holidays_by_month\n      {\n              0 => [{:function => \"custom_method(year, month)\", :function_arguments => [:year, :month], :function_modifier => 5, :name => \"Custom Holiday\", :regions => [:custom_single_file]}],\n      6 => [{:mday => 20, :name => \"Company Founding\", :regions => [:custom_single_file]}]\n      }\n    end\n\n    def self.custom_methods\n      {\n        \"custom_method(year, month)\" => Proc.new { |year, month|\nsource_stuff\n},\n\n\n      }\n    end\n  end\nend\n"

    assert_equal expected_module_src, module_src
  end

  def test_generate_definition_source_correctly_generate_test_src_with_custom_methods
    files = ['test/data/test_single_custom_holiday_with_custom_procs.yaml']
<commit_msg>Removed incorrect lines from test<commit_after>require File.expand_path(File.dirname(__FILE__)) + '/../../../test_helper'

require 'holidays/definition/context/generator'

class GeneratorTests < Test::Unit::TestCase
  def setup
    @custom_method_parser = mock()
    @custom_method_source_decorator = mock()
    @custom_methods_repository = mock()

    @parsed_custom_method = Holidays::Definition::Entity::CustomMethod.new(
      name: 'custom_method',
      arguments: [:year, :month],
      source: "some source",
    )

    @generator = Holidays::Definition::Context::Generator.new(
      @custom_method_parser,
      @custom_method_source_decorator,
      @custom_methods_repository,
    )
  end

  def test_parse_definition_files_raises_error_if_argument_is_nil
    assert_raises ArgumentError do
      @generator.parse_definition_files(nil)
    end
  end

  def test_parse_definition_files_raises_error_if_files_are_empty
    assert_raises ArgumentError do
      @generator.parse_definition_files([])
    end
  end

  def test_parse_definition_files_correctly_parse_regions
    files = ['test/data/test_single_custom_holiday_defs.yaml']
    @custom_method_parser.expects(:call).with(nil).returns({})

    regions = @generator.parse_definition_files(files)[0]

    assert_equal [:custom_single_file], regions
  end

  def test_parse_definitions_files_correctly_parse_rules_by_month
    files = ['test/data/test_single_custom_holiday_defs.yaml']
    @custom_method_parser.expects(:call).with(nil).returns({})

    rules_by_month = @generator.parse_definition_files(files)[1]

    expected_rules_by_month = {
      6 => [
        {
          :mday    => 20,
          :name    => "Company Founding",
          :regions => [:custom_single_file]
        }
      ]
    }

    assert_equal expected_rules_by_month, rules_by_month
  end

  def test_parse_definition_files_correctly_parse_custom_methods
    files = ['test/data/test_single_custom_holiday_defs.yaml']
    @custom_method_parser.expects(:call).with(nil).returns({})

    custom_methods = @generator.parse_definition_files(files)[2]

    expected_custom_methods = {}
    assert_equal expected_custom_methods, custom_methods
  end

  def test_parse_definition_files_correctly_parse_tests
    files = ['test/data/test_single_custom_holiday_defs.yaml']
    @custom_method_parser.expects(:call).with(nil).returns({})

    tests = @generator.parse_definition_files(files)[3]

    expected_tests = [[
        "{Date.civil(2013,6,20) => 'Company Founding'}.each do |date, name|\n  assert_equal name, (Holidays.on(date, :custom_single_file)[0] || {})[:name]\nend"
    ]]

    assert_equal expected_tests, tests
  end

  def test_generate_definition_source_correctly_generate_module_src
    files = ['test/data/test_single_custom_holiday_defs.yaml']
    @custom_method_parser.expects(:call).with(nil).returns({})

    regions, rules_by_month, custom_methods, tests = @generator.parse_definition_files(files)
    module_src = @generator.generate_definition_source("test", files, regions, rules_by_month, custom_methods, tests)[0]

    expected_module_src = "# encoding: utf-8\nmodule Holidays\n  # This file is generated by the Ruby Holidays gem.\n  #\n  # Definitions loaded: test/data/test_single_custom_holiday_defs.yaml\n  #\n  # All the definitions are available at https://github.com/holidays/holidays\n  module TEST # :nodoc:\n    def self.defined_regions\n      [:custom_single_file]\n    end\n\n    def self.holidays_by_month\n      {\n              6 => [{:mday => 20, :name => \"Company Founding\", :regions => [:custom_single_file]}]\n      }\n    end\n\n    def self.custom_methods\n      {\n        \n      }\n    end\n  end\nend\n"

    assert_equal expected_module_src, module_src
  end

  def test_generate_definition_source_correctly_generate_test_src
    files = ['test/data/test_single_custom_holiday_defs.yaml']
    @custom_method_parser.expects(:call).with(nil).returns({})

    regions, rules_by_month, custom_methods, tests = @generator.parse_definition_files(files)
    test_src = @generator.generate_definition_source("test", files, regions, rules_by_month, custom_methods, tests)[1]

    expected_test_src = "# encoding: utf-8\nrequire File.expand_path(File.dirname(__FILE__)) + '/../test_helper'\n\n# This file is generated by the Ruby Holiday gem.\n#\n# Definitions loaded: test/data/test_single_custom_holiday_defs.yaml\nclass TestDefinitionTests < Test::Unit::TestCase  # :nodoc:\n\n  def test_test\n{Date.civil(2013,6,20) => 'Company Founding'}.each do |date, name|\n  assert_equal name, (Holidays.on(date, :custom_single_file)[0] || {})[:name]\nend\n  end\nend\n"

    assert_equal expected_test_src, test_src
  end

  def test_parse_definitions_files_correctly_parse_year_range_by_month
    files = ['test/data/test_custom_year_range_holiday_defs.yaml']
    @custom_method_parser.expects(:call).with(nil).returns({})

    rules_by_month = @generator.parse_definition_files(files)[1]

    expected_rules_by_month = {
      6 => [
        {
          :name => "after_year",
          :regions => [:custom_year_range_file],
          :mday => 1,
          :year_ranges => [{"after" => 2016}]
        },
        {
          :name => "before_year",
          :regions => [:custom_year_range_file],
          :mday => 2,
          :year_ranges => [{"before" => 2017}]
        },
        {
          :name => "between_year",
          :regions => [:custom_year_range_file],
          :mday => 3,
          :year_ranges => [{"between" => 2016..2018}]
        },
        {
          :name => "limited_year",
          :regions => [:custom_year_range_file],
          :mday => 4,
          :year_ranges => [{"limited" => [2016,2018,2019]}]
        },
        {
          :name => "multiple_conditions",
          :regions => [:custom_year_range_file],
          :mday => 5,
          :year_ranges => [{"before" => 2015}, {"after" => 2017}]
        }
      ]
    }

    assert_equal expected_rules_by_month, rules_by_month
  end

  def test_generate_definition_source_correctly_generate_yearrange_test_src
    files = ['test/data/test_custom_year_range_holiday_defs.yaml']
    @custom_method_parser.expects(:call).with(nil).returns({})

    regions, rules_by_month, custom_methods, tests = @generator.parse_definition_files(files)
    module_src = @generator.generate_definition_source("test", files, regions, rules_by_month, custom_methods, tests)[0]
    expected_module_src = "# encoding: utf-8\nmodule Holidays\n  # This file is generated by the Ruby Holidays gem.\n  #\n  # Definitions loaded: test/data/test_custom_year_range_holiday_defs.yaml\n  #\n  # All the definitions are available at https://github.com/holidays/holidays\n  module TEST # :nodoc:\n    def self.defined_regions\n      [:custom_year_range_file]\n    end\n\n    def self.holidays_by_month\n      {\n              6 => [{:mday => 1,  :year_ranges => [{:after => 2016}],:name => \"after_year\", :regions => [:custom_year_range_file]},\n            {:mday => 2,  :year_ranges => [{:before => 2017}],:name => \"before_year\", :regions => [:custom_year_range_file]},\n            {:mday => 3,  :year_ranges => [{:between => 2016..2018}],:name => \"between_year\", :regions => [:custom_year_range_file]},\n            {:mday => 4,  :year_ranges => [{:limited => [2016, 2018, 2019]}],:name => \"limited_year\", :regions => [:custom_year_range_file]},\n            {:mday => 5,  :year_ranges => [{:before => 2015},{:after => 2017}],:name => \"multiple_conditions\", :regions => [:custom_year_range_file]}]\n      }\n    end\n\n    def self.custom_methods\n      {\n        \n      }\n    end\n  end\nend\n"

    assert_equal expected_module_src, module_src
  end

  def test_generate_definition_source_correctly_generate_module_src_with_custom_methods
    files = ['test/data/test_single_custom_holiday_with_custom_procs.yaml']

    @custom_method_parser.expects(:call).with('custom_method' => {'arguments' => 'year, month', 'source' => "d = Date.civil(year, month, 1)\nd + 2\n"}).returns({"custom_method(year, month)" => @parsed_custom_method})
    @custom_methods_repository.expects(:find).twice.with('custom_method(year, month)').returns(nil)
    @custom_method_source_decorator.expects(:call).once.with(@parsed_custom_method).returns("\"custom_method(year, month)\" => Proc.new { |year, month|\nsource_stuff\n}")

    regions, rules_by_month, custom_methods, tests = @generator.parse_definition_files(files)
    module_src = @generator.generate_definition_source("test", files, regions, rules_by_month, custom_methods, tests)[0]

    expected_module_src = "# encoding: utf-8\nmodule Holidays\n  # This file is generated by the Ruby Holidays gem.\n  #\n  # Definitions loaded: test/data/test_single_custom_holiday_with_custom_procs.yaml\n  #\n  # All the definitions are available at https://github.com/holidays/holidays\n  module TEST # :nodoc:\n    def self.defined_regions\n      [:custom_single_file]\n    end\n\n    def self.holidays_by_month\n      {\n              0 => [{:function => \"custom_method(year, month)\", :function_arguments => [:year, :month], :function_modifier => 5, :name => \"Custom Holiday\", :regions => [:custom_single_file]}],\n      6 => [{:mday => 20, :name => \"Company Founding\", :regions => [:custom_single_file]}]\n      }\n    end\n\n    def self.custom_methods\n      {\n        \"custom_method(year, month)\" => Proc.new { |year, month|\nsource_stuff\n},\n\n\n      }\n    end\n  end\nend\n"

    assert_equal expected_module_src, module_src
  end

  def test_generate_definition_source_correctly_generate_test_src_with_custom_methods
    files = ['test/data/test_single_custom_holiday_with_custom_procs.yaml']

{'fraction_non_alphanumeric': 0.1348252451596681, 'fraction_numerical': 0.012672868996731205, 'mean_word_length': 3.7894990366088632, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 163, 'https://': 6, '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': '2135519', 'n_tokens_mistral': 6748, 'n_tokens_neox': 6451, 'n_words': 1116}