require 'holidays/errors'

module Holidays
  module Definition
    module Generator
      class Test
        def initialize(decorator)
          @decorator = decorator
        end

        def call(module_name, file_names, tests)
          validate!(module_name, file_names, tests)

          test_src =<<-EndOfTests
# encoding: utf-8
require File.expand_path(File.dirname(__FILE__)) + '/../test_helper'

# This file is generated by the Ruby Holiday gem.
#
# Definitions loaded: #{file_names.join(', ')}
class #{module_name.to_s.capitalize}DefinitionTests < Test::Unit::TestCase  # :nodoc:

  def test_#{module_name.to_s.downcase}#{decorate(tests)}
  end
end
EndOfTests

        test_src
        end

        private

        def validate!(module_name, file_names, tests)
          raise ArgumentError.new("module_name cannot be missing") if module_name.nil? || module_name.empty?
          raise ArgumentError.new("file_names for '#{module_name}' cannot be missing") if file_names.nil? || file_names.empty?
          raise ArgumentError.new("tests for '#{module_name}' cannot be missing") if tests.nil?
        end

        def decorate(tests)
          out = ""

          tests.each do |t|
            out << "\n    " + @decorator.call(t)
          end

          out
        end
      end
    end
  end
end
