annotate .svn/pristine/86/86b9bcb879f7b1ffb317c5c416700af50bfd5f4c.svn-base @ 1327:287f201c2802 redmine-2.2-integration

Add italic
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Wed, 19 Jun 2013 20:56:22 +0100
parents 038ba2d95de8
children
rev   line source
Chris@1296 1 # Redmine - project management software
Chris@1296 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@1296 3 #
Chris@1296 4 # This program is free software; you can redistribute it and/or
Chris@1296 5 # modify it under the terms of the GNU General Public License
Chris@1296 6 # as published by the Free Software Foundation; either version 2
Chris@1296 7 # of the License, or (at your option) any later version.
Chris@1296 8 #
Chris@1296 9 # This program is distributed in the hope that it will be useful,
Chris@1296 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1296 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1296 12 # GNU General Public License for more details.
Chris@1296 13 #
Chris@1296 14 # You should have received a copy of the GNU General Public License
Chris@1296 15 # along with this program; if not, write to the Free Software
Chris@1296 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1296 17
Chris@1296 18 require File.expand_path('../../../../test_helper', __FILE__)
Chris@1296 19
Chris@1296 20 class Redmine::I18nTest < ActiveSupport::TestCase
Chris@1296 21 include Redmine::I18n
Chris@1296 22 include ActionView::Helpers::NumberHelper
Chris@1296 23
Chris@1296 24 def setup
Chris@1296 25 User.current.language = nil
Chris@1296 26 end
Chris@1296 27
Chris@1296 28 def teardown
Chris@1296 29 set_language_if_valid 'en'
Chris@1296 30 end
Chris@1296 31
Chris@1296 32 def test_date_format_default
Chris@1296 33 set_language_if_valid 'en'
Chris@1296 34 today = Date.today
Chris@1296 35 Setting.date_format = ''
Chris@1296 36 assert_equal I18n.l(today), format_date(today)
Chris@1296 37 end
Chris@1296 38
Chris@1296 39 def test_date_format
Chris@1296 40 set_language_if_valid 'en'
Chris@1296 41 today = Date.today
Chris@1296 42 Setting.date_format = '%d %m %Y'
Chris@1296 43 assert_equal today.strftime('%d %m %Y'), format_date(today)
Chris@1296 44 end
Chris@1296 45
Chris@1296 46 def test_date_format_default_with_user_locale
Chris@1296 47 set_language_if_valid 'es'
Chris@1296 48 today = now = Time.parse('2011-02-20 14:00:00')
Chris@1296 49 Setting.date_format = '%d %B %Y'
Chris@1296 50 User.current.language = 'fr'
Chris@1296 51 s1 = "20 f\xc3\xa9vrier 2011"
Chris@1296 52 s1.force_encoding("UTF-8") if s1.respond_to?(:force_encoding)
Chris@1296 53 assert_equal s1, format_date(today)
Chris@1296 54 User.current.language = nil
Chris@1296 55 assert_equal '20 Febrero 2011', format_date(today)
Chris@1296 56 end
Chris@1296 57
Chris@1296 58 def test_date_and_time_for_each_language
Chris@1296 59 Setting.date_format = ''
Chris@1296 60 valid_languages.each do |lang|
Chris@1296 61 set_language_if_valid lang
Chris@1296 62 assert_nothing_raised "#{lang} failure" do
Chris@1296 63 format_date(Date.today)
Chris@1296 64 format_time(Time.now)
Chris@1296 65 format_time(Time.now, false)
Chris@1296 66 assert_not_equal 'default', ::I18n.l(Date.today, :format => :default),
Chris@1296 67 "date.formats.default missing in #{lang}"
Chris@1296 68 assert_not_equal 'time', ::I18n.l(Time.now, :format => :time),
Chris@1296 69 "time.formats.time missing in #{lang}"
Chris@1296 70 end
Chris@1296 71 assert l('date.day_names').is_a?(Array)
Chris@1296 72 assert_equal 7, l('date.day_names').size
Chris@1296 73
Chris@1296 74 assert l('date.month_names').is_a?(Array)
Chris@1296 75 assert_equal 13, l('date.month_names').size
Chris@1296 76 end
Chris@1296 77 end
Chris@1296 78
Chris@1296 79 def test_time_for_each_zone
Chris@1296 80 ActiveSupport::TimeZone.all.each do |zone|
Chris@1296 81 User.current.stubs(:time_zone).returns(zone.name)
Chris@1296 82 assert_nothing_raised "#{zone} failure" do
Chris@1296 83 format_time(Time.now)
Chris@1296 84 end
Chris@1296 85 end
Chris@1296 86 end
Chris@1296 87
Chris@1296 88 def test_time_format
Chris@1296 89 set_language_if_valid 'en'
Chris@1296 90 now = Time.parse('2011-02-20 15:45:22')
Chris@1296 91 with_settings :time_format => '%H:%M' do
Chris@1296 92 with_settings :date_format => '' do
Chris@1296 93 assert_equal '02/20/2011 15:45', format_time(now)
Chris@1296 94 assert_equal '15:45', format_time(now, false)
Chris@1296 95 end
Chris@1296 96 with_settings :date_format => '%Y-%m-%d' do
Chris@1296 97 assert_equal '2011-02-20 15:45', format_time(now)
Chris@1296 98 assert_equal '15:45', format_time(now, false)
Chris@1296 99 end
Chris@1296 100 end
Chris@1296 101 end
Chris@1296 102
Chris@1296 103 def test_time_format_default
Chris@1296 104 set_language_if_valid 'en'
Chris@1296 105 now = Time.parse('2011-02-20 15:45:22')
Chris@1296 106 with_settings :time_format => '' do
Chris@1296 107 with_settings :date_format => '' do
Chris@1296 108 assert_equal '02/20/2011 03:45 pm', format_time(now)
Chris@1296 109 assert_equal '03:45 pm', format_time(now, false)
Chris@1296 110 end
Chris@1296 111 with_settings :date_format => '%Y-%m-%d' do
Chris@1296 112 assert_equal '2011-02-20 03:45 pm', format_time(now)
Chris@1296 113 assert_equal '03:45 pm', format_time(now, false)
Chris@1296 114 end
Chris@1296 115 end
Chris@1296 116 end
Chris@1296 117
Chris@1296 118 def test_time_format_default_with_user_locale
Chris@1296 119 set_language_if_valid 'en'
Chris@1296 120 User.current.language = 'fr'
Chris@1296 121 now = Time.parse('2011-02-20 15:45:22')
Chris@1296 122 with_settings :time_format => '' do
Chris@1296 123 with_settings :date_format => '' do
Chris@1296 124 assert_equal '20/02/2011 15:45', format_time(now)
Chris@1296 125 assert_equal '15:45', format_time(now, false)
Chris@1296 126 end
Chris@1296 127 with_settings :date_format => '%Y-%m-%d' do
Chris@1296 128 assert_equal '2011-02-20 15:45', format_time(now)
Chris@1296 129 assert_equal '15:45', format_time(now, false)
Chris@1296 130 end
Chris@1296 131 end
Chris@1296 132 end
Chris@1296 133
Chris@1296 134 def test_time_format
Chris@1296 135 set_language_if_valid 'en'
Chris@1296 136 now = Time.now
Chris@1296 137 Setting.date_format = '%d %m %Y'
Chris@1296 138 Setting.time_format = '%H %M'
Chris@1296 139 assert_equal now.strftime('%d %m %Y %H %M'), format_time(now)
Chris@1296 140 assert_equal now.strftime('%H %M'), format_time(now, false)
Chris@1296 141 end
Chris@1296 142
Chris@1296 143 def test_utc_time_format
Chris@1296 144 set_language_if_valid 'en'
Chris@1296 145 now = Time.now
Chris@1296 146 Setting.date_format = '%d %m %Y'
Chris@1296 147 Setting.time_format = '%H %M'
Chris@1296 148 assert_equal now.strftime('%d %m %Y %H %M'), format_time(now.utc)
Chris@1296 149 assert_equal now.strftime('%H %M'), format_time(now.utc, false)
Chris@1296 150 end
Chris@1296 151
Chris@1296 152 def test_number_to_human_size_for_each_language
Chris@1296 153 valid_languages.each do |lang|
Chris@1296 154 set_language_if_valid lang
Chris@1296 155 assert_nothing_raised "#{lang} failure" do
Chris@1296 156 size = number_to_human_size(257024)
Chris@1296 157 assert_match /251/, size
Chris@1296 158 end
Chris@1296 159 end
Chris@1296 160 end
Chris@1296 161
Chris@1296 162 def test_day_name
Chris@1296 163 set_language_if_valid 'fr'
Chris@1296 164 assert_equal 'dimanche', day_name(0)
Chris@1296 165 assert_equal 'jeudi', day_name(4)
Chris@1296 166 end
Chris@1296 167
Chris@1296 168 def test_day_letter
Chris@1296 169 set_language_if_valid 'fr'
Chris@1296 170 assert_equal 'd', day_letter(0)
Chris@1296 171 assert_equal 'j', day_letter(4)
Chris@1296 172 end
Chris@1296 173
Chris@1296 174 def test_number_to_currency_for_each_language
Chris@1296 175 valid_languages.each do |lang|
Chris@1296 176 set_language_if_valid lang
Chris@1296 177 assert_nothing_raised "#{lang} failure" do
Chris@1296 178 number_to_currency(-1000.2)
Chris@1296 179 end
Chris@1296 180 end
Chris@1296 181 end
Chris@1296 182
Chris@1296 183 def test_number_to_currency_default
Chris@1296 184 set_language_if_valid 'bs'
Chris@1296 185 assert_equal "KM -1000,20", number_to_currency(-1000.2)
Chris@1296 186 set_language_if_valid 'de'
Chris@1296 187 euro_sign = "\xe2\x82\xac"
Chris@1296 188 euro_sign.force_encoding('UTF-8') if euro_sign.respond_to?(:force_encoding)
Chris@1296 189 assert_equal "-1000,20 #{euro_sign}", number_to_currency(-1000.2)
Chris@1296 190 end
Chris@1296 191
Chris@1296 192 def test_valid_languages
Chris@1296 193 assert valid_languages.is_a?(Array)
Chris@1296 194 assert valid_languages.first.is_a?(Symbol)
Chris@1296 195 end
Chris@1296 196
Chris@1296 197 def test_languages_options
Chris@1296 198 options = languages_options
Chris@1296 199
Chris@1296 200 assert options.is_a?(Array)
Chris@1296 201 assert_equal valid_languages.size, options.size
Chris@1296 202 assert_nil options.detect {|option| !option.is_a?(Array)}
Chris@1296 203 assert_nil options.detect {|option| option.size != 2}
Chris@1296 204 assert_nil options.detect {|option| !option.first.is_a?(String) || !option.last.is_a?(String)}
Chris@1296 205 assert_include ["English", "en"], options
Chris@1296 206 end
Chris@1296 207
Chris@1296 208 def test_locales_validness
Chris@1296 209 lang_files_count = Dir["#{Rails.root}/config/locales/*.yml"].size
Chris@1296 210 assert_equal lang_files_count, valid_languages.size
Chris@1296 211 valid_languages.each do |lang|
Chris@1296 212 assert set_language_if_valid(lang)
Chris@1296 213 end
Chris@1296 214 set_language_if_valid('en')
Chris@1296 215 end
Chris@1296 216
Chris@1296 217 def test_valid_language
Chris@1296 218 to_test = {'fr' => :fr,
Chris@1296 219 'Fr' => :fr,
Chris@1296 220 'zh' => :zh,
Chris@1296 221 'zh-tw' => :"zh-TW",
Chris@1296 222 'zh-TW' => :"zh-TW",
Chris@1296 223 'zh-ZZ' => nil }
Chris@1296 224 to_test.each {|lang, expected| assert_equal expected, find_language(lang)}
Chris@1296 225 end
Chris@1296 226
Chris@1296 227 def test_fallback
Chris@1296 228 ::I18n.backend.store_translations(:en, {:untranslated => "Untranslated string"})
Chris@1296 229 ::I18n.locale = 'en'
Chris@1296 230 assert_equal "Untranslated string", l(:untranslated)
Chris@1296 231 ::I18n.locale = 'fr'
Chris@1296 232 assert_equal "Untranslated string", l(:untranslated)
Chris@1296 233
Chris@1296 234 ::I18n.backend.store_translations(:fr, {:untranslated => "Pas de traduction"})
Chris@1296 235 ::I18n.locale = 'en'
Chris@1296 236 assert_equal "Untranslated string", l(:untranslated)
Chris@1296 237 ::I18n.locale = 'fr'
Chris@1296 238 assert_equal "Pas de traduction", l(:untranslated)
Chris@1296 239 end
Chris@1296 240
Chris@1296 241 def test_utf8
Chris@1296 242 set_language_if_valid 'ja'
Chris@1296 243 str_ja_yes = "\xe3\x81\xaf\xe3\x81\x84"
Chris@1296 244 i18n_ja_yes = l(:general_text_Yes)
Chris@1296 245 if str_ja_yes.respond_to?(:force_encoding)
Chris@1296 246 str_ja_yes.force_encoding('UTF-8')
Chris@1296 247 assert_equal "UTF-8", i18n_ja_yes.encoding.to_s
Chris@1296 248 end
Chris@1296 249 assert_equal str_ja_yes, i18n_ja_yes
Chris@1296 250 end
Chris@1296 251
Chris@1296 252 def test_traditional_chinese_locale
Chris@1296 253 set_language_if_valid 'zh-TW'
Chris@1296 254 str_tw = "Traditional Chinese (\xe7\xb9\x81\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87)"
Chris@1296 255 if str_tw.respond_to?(:force_encoding)
Chris@1296 256 str_tw.force_encoding('UTF-8')
Chris@1296 257 end
Chris@1296 258 assert_equal str_tw, l(:general_lang_name)
Chris@1296 259 end
Chris@1296 260
Chris@1296 261 def test_french_locale
Chris@1296 262 set_language_if_valid 'fr'
Chris@1296 263 str_fr = "Fran\xc3\xa7ais"
Chris@1296 264 if str_fr.respond_to?(:force_encoding)
Chris@1296 265 str_fr.force_encoding('UTF-8')
Chris@1296 266 end
Chris@1296 267 assert_equal str_fr, l(:general_lang_name)
Chris@1296 268 end
Chris@1296 269 end