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

require 'holidays/definition/generator/module'

class GeneratorModuleTests < Test::Unit::TestCase
  def setup
    @generator = Holidays::Definition::Generator::Module.new

    @module_name = "TEST"
    @files = ["file1.rb", "file2.rb"]
    @regions = [:test, :test2]
    @month_strings = ["first-string", "second-string"]
    @custom_methods = "custom-methods"
  end

  def subject
    @generator.call(
      @module_name,
      @files,
      @regions,
      @month_strings,
      @custom_methods,
    )
  end

  def test_generates_source
    expected = <<-EOF
# encoding: utf-8
module Holidays
  # This file is generated by the Ruby Holidays gem.
  #
  # Definitions loaded: file1.rb, file2.rb
  #
  # All the definitions are available at https://github.com/holidays/holidays
  module TEST # :nodoc:
    def self.defined_regions
      [:test, :test2]
    end

    def self.holidays_by_month
      {
          first-string,
second-string
      }
    end

    def self.custom_methods
      {
          custom-methods
      }
    end
  end
end
EOF

    assert_equal expected, subject
  end

  def test_module_name_more_than_one_word
    @module_name = "TESTWITHHYPHEN"

    expected = <<-EOF
# encoding: utf-8
module Holidays
  # This file is generated by the Ruby Holidays gem.
  #
  # Definitions loaded: file1.rb, file2.rb
  #
  # All the definitions are available at https://github.com/holidays/holidays
  module TESTWITHHYPHEN # :nodoc:
    def self.defined_regions
      [:test, :test2]
    end

    def self.holidays_by_month
      {
          first-string,
second-string
      }
    end

    def self.custom_methods
      {
          custom-methods
      }
    end
  end
end
EOF

    assert_equal expected, subject
  end

  def test_generates_source_with_blank_custom_methods
    @custom_methods = ""

    expected = <<-EOF
# encoding: utf-8
module Holidays
  # This file is generated by the Ruby Holidays gem.
  #
  # Definitions loaded: file1.rb, file2.rb
  #
  # All the definitions are available at https://github.com/holidays/holidays
  module TEST # :nodoc:
    def self.defined_regions
      [:test, :test2]
    end

    def self.holidays_by_month
      {
          first-string,
second-string
      }
    end

    def self.custom_methods
      {
          
      }
    end
  end
end
EOF

    assert_equal expected, subject
  end

  def test_generates_source_with_nil_custom_methods
    @custom_methods = nil

    expected = <<-EOF
# encoding: utf-8
module Holidays
  # This file is generated by the Ruby Holidays gem.
  #
  # Definitions loaded: file1.rb, file2.rb
  #
  # All the definitions are available at https://github.com/holidays/holidays
  module TEST # :nodoc:
    def self.defined_regions
      [:test, :test2]
    end

    def self.holidays_by_month
      {
          first-string,
second-string
      }
    end

    def self.custom_methods
      {
          
      }
    end
  end
end
EOF

    assert_equal expected, subject
  end

  def test_generates_source_with_symbol_module_name
    @module_name = :test

    expected = <<-EOF
# encoding: utf-8
module Holidays
  # This file is generated by the Ruby Holidays gem.
  #
  # Definitions loaded: file1.rb, file2.rb
  #
  # All the definitions are available at https://github.com/holidays/holidays
  module TEST # :nodoc:
    def self.defined_regions
      [:test, :test2]
    end

    def self.holidays_by_month
      {
          first-string,
second-string
      }
    end

    def self.custom_methods
      {
          custom-methods
      }
    end
  end
end
EOF

    assert_equal expected, subject
  end

  def test_raise_error_if_module_name_is_blank
    @module_name = ""
    assert_raises ArgumentError do
      subject
    end
  end

  def test_raise_error_if_module_name_is_nil
    @module_name = nil
    assert_raises ArgumentError do
      subject
    end
  end

  def test_raise_error_if_files_are_empty
    @files = []

    assert_raises ArgumentError do
      subject
    end
  end

  def test_raise_error_if_files_are_not_all_strings
    @files = [:test, 1]

    assert_raises ArgumentError do
      subject
    end
  end

  def test_raise_error_if_files_is_nil
    @files = nil

    assert_raises ArgumentError do
      subject
    end
  end

  def test_raise_error_if_regions_are_empty
    @regions = []

    assert_raises ArgumentError do
      subject
    end
  end

  def test_raise_error_if_regions_is_nil
    @regions = nil

    assert_raises ArgumentError do
      subject
    end
  end

  def test_raise_error_if_month_strings_are_empty
    @month_strings = []

    assert_raises ArgumentError do
      subject
    end
  end

  def test_raise_error_if_month_strings_is_nil
    @month_strings = nil

    assert_raises ArgumentError do
      subject
    end
  end
end
