tc_country.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
  2. require 'test/unit'
  3. require File.join(File.dirname(__FILE__), 'test_utils')
  4. require 'tzinfo'
  5. include TZInfo
  6. class TCCountry < Test::Unit::TestCase
  7. def test_get_valid
  8. c = Country.get('GB')
  9. assert c
  10. assert_equal('GB', c.code)
  11. end
  12. def test_get_not_exist
  13. assert_raises(InvalidCountryCode) {
  14. Country.get('ZZ')
  15. }
  16. end
  17. def test_get_invalid
  18. assert_raises(InvalidCountryCode) {
  19. Country.get('../Countries/GB')
  20. }
  21. end
  22. def test_get_nil
  23. assert_raises(InvalidCountryCode) {
  24. Country.get(nil)
  25. }
  26. end
  27. def test_get_case
  28. assert_raises(InvalidCountryCode) {
  29. Country.get('gb')
  30. }
  31. end
  32. def test_new_nil
  33. assert_raises(InvalidCountryCode) {
  34. c = Country.new(nil)
  35. }
  36. end
  37. def test_new_arg
  38. c = Country.new('GB')
  39. assert_same(Country.get('GB'), c)
  40. end
  41. def test_new_arg_not_exist
  42. assert_raises(InvalidCountryCode) {
  43. Country.new('ZZ')
  44. }
  45. end
  46. def test_all_codes
  47. all_codes = Country.all_codes
  48. assert_kind_of(Array, all_codes)
  49. end
  50. def test_all
  51. all = Country.all
  52. assert_equal(Country.all_codes, all.collect {|c| c.code})
  53. end
  54. def test_code
  55. assert_equal('US', Country.get('US').code)
  56. end
  57. def test_name
  58. assert_kind_of(String, Country.get('US').name)
  59. end
  60. def test_to_s
  61. assert_equal(Country.get('US').name, Country.get('US').to_s)
  62. assert_equal(Country.get('GB').name, Country.get('GB').to_s)
  63. end
  64. def test_zone_identifiers
  65. zone_names = Country.get('US').zone_names
  66. assert_kind_of(Array, zone_names)
  67. assert_equal(true, zone_names.frozen?)
  68. end
  69. def test_zone_names
  70. assert_equal(Country.get('US').zone_identifiers, Country.get('US').zone_names)
  71. end
  72. def test_zones
  73. zones = Country.get('US').zones
  74. assert_kind_of(Array, zones)
  75. assert_equal(Country.get('US').zone_identifiers, zones.collect {|z| z.identifier})
  76. zones.each {|z| assert_kind_of(TimezoneProxy, z)}
  77. end
  78. def test_zone_info
  79. zones = Country.get('US').zone_info
  80. assert_kind_of(Array, zones)
  81. assert_equal(true, zones.frozen?)
  82. assert_equal(Country.get('US').zone_identifiers, zones.collect {|z| z.identifier})
  83. assert_equal(Country.get('US').zone_identifiers, zones.collect {|z| z.timezone.identifier})
  84. zones.each {|z| assert_kind_of(CountryTimezone, z)}
  85. end
  86. def test_compare
  87. assert_equal(0, Country.get('GB') <=> Country.get('GB'))
  88. assert_equal(-1, Country.get('GB') <=> Country.get('US'))
  89. assert_equal(1, Country.get('US') <=> Country.get('GB'))
  90. assert_equal(-1, Country.get('FR') <=> Country.get('US'))
  91. assert_equal(1, Country.get('US') <=> Country.get('FR'))
  92. end
  93. def test_equality
  94. assert_equal(true, Country.get('GB') == Country.get('GB'))
  95. assert_equal(false, Country.get('GB') == Country.get('US'))
  96. assert(!(Country.get('GB') == Object.new))
  97. end
  98. def test_eql
  99. assert_equal(true, Country.get('GB').eql?(Country.get('GB')))
  100. assert_equal(false, Country.get('GB').eql?(Country.get('US')))
  101. assert(!Country.get('GB').eql?(Object.new))
  102. end
  103. def test_hash
  104. assert_equal('GB'.hash, Country.get('GB').hash)
  105. assert_equal('US'.hash, Country.get('US').hash)
  106. end
  107. def test_marshal
  108. c = Country.get('US')
  109. # Should get back the same instance because load calls Country.get.
  110. assert_same(c, Marshal.load(Marshal.dump(c)))
  111. end
  112. def test_reload
  113. # If country gets reloaded for some reason, it needs to force a reload of
  114. # the country index.
  115. c = Country.get('US')
  116. assert_equal('US', Country.get('US').code)
  117. # Suppress redefined method warnings.
  118. without_warnings do
  119. load 'tzinfo/country.rb'
  120. end
  121. c = Country.get('US')
  122. assert_equal('US', Country.get('US').code)
  123. end
  124. end