require 'test_helper'

class CountryTest < Test::Unit::TestCase
  def test_country_from_hash
    country = Country.new(:name => 'Canada', :alpha2 => 'CA', :alpha3 => 'CAN', :numeric => '124')
    assert_equal 'CA', country.code(:alpha2).value
    assert_equal 'CAN', country.code(:alpha3).value
    assert_equal '124', country.code(:numeric).value
    assert_equal 'Canada', country.to_s
  end

  def test_country_for_alpha2_code
    country = Country.find('CA')
    assert_equal 'CA', country.code(:alpha2).value
    assert_equal 'CAN', country.code(:alpha3).value
    assert_equal '124', country.code(:numeric).value
    assert_equal 'Canada', country.to_s
  end

  def test_country_for_alpha3_code
    country = Country.find('CAN')
    assert_equal 'Canada', country.to_s
  end

  def test_country_for_numeric_code
    country = Country.find('124')
    assert_equal 'Canada', country.to_s
  end

  def test_find_country_by_name
    country = Country.find('Canada')
    assert_equal 'Canada', country.to_s
  end

  def test_find_unknown_country_name
    assert_raise(InvalidCountryCodeError) do
      Country.find('Asskickistan')
    end
  end

  def test_find_australia
    country = Country.find('AU')
    assert_equal 'AU', country.code(:alpha2).value

    country = Country.find('Australia')
    assert_equal 'AU', country.code(:alpha2).value
  end

  def test_find_united_kingdom
    country = Country.find('GB')
    assert_equal 'GB', country.code(:alpha2).value

    country = Country.find('United Kingdom')
    assert_equal 'GB', country.code(:alpha2).value
  end

  def test_raise_on_nil_name
    assert_raise(InvalidCountryCodeError) do
      Country.find(nil)
    end
  end

  def test_country_names_are_alphabetized
    country_names = Country::COUNTRIES.map { | each | each[:name] }
    assert_equal(country_names.sort, country_names)
  end

end
