annotate test/unit/lib/redmine/codeset_util_test.rb @ 1472:f0b798dad2d6 feature_526

Close obsolete branch feature_526
author Chris Cannam
date Tue, 20 Nov 2012 19:45:51 +0000
parents cbb26bc654de
children 433d4f72a19b
rev   line source
Chris@909 1 # Redmine - project management software
Chris@909 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@909 3 #
Chris@909 4 # This program is free software; you can redistribute it and/or
Chris@909 5 # modify it under the terms of the GNU General Public License
Chris@909 6 # as published by the Free Software Foundation; either version 2
Chris@909 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@909 9 # This program is distributed in the hope that it will be useful,
Chris@909 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@909 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@909 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@909 14 # You should have received a copy of the GNU General Public License
Chris@909 15 # along with this program; if not, write to the Free Software
Chris@909 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@909 17
Chris@909 18 require File.expand_path('../../../../test_helper', __FILE__)
Chris@909 19
Chris@909 20 class Redmine::CodesetUtilTest < ActiveSupport::TestCase
Chris@909 21
Chris@909 22 def test_to_utf8_by_setting_from_latin1
Chris@909 23 with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
Chris@909 24 s1 = "Texte encod\xc3\xa9"
Chris@909 25 s2 = "Texte encod\xe9"
Chris@909 26 s3 = s2.dup
Chris@909 27 if s1.respond_to?(:force_encoding)
Chris@909 28 s1.force_encoding("UTF-8")
Chris@909 29 s2.force_encoding("ASCII-8BIT")
Chris@909 30 s3.force_encoding("UTF-8")
Chris@909 31 end
Chris@909 32 assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s2)
Chris@909 33 assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s3)
Chris@909 34 end
Chris@909 35 end
Chris@909 36
Chris@909 37 def test_to_utf8_by_setting_from_euc_jp
Chris@909 38 with_settings :repositories_encodings => 'UTF-8,EUC-JP' do
Chris@909 39 s1 = "\xe3\x83\xac\xe3\x83\x83\xe3\x83\x89\xe3\x83\x9e\xe3\x82\xa4\xe3\x83\xb3"
Chris@909 40 s2 = "\xa5\xec\xa5\xc3\xa5\xc9\xa5\xde\xa5\xa4\xa5\xf3"
Chris@909 41 s3 = s2.dup
Chris@909 42 if s1.respond_to?(:force_encoding)
Chris@909 43 s1.force_encoding("UTF-8")
Chris@909 44 s2.force_encoding("ASCII-8BIT")
Chris@909 45 s3.force_encoding("UTF-8")
Chris@909 46 end
Chris@909 47 assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s2)
Chris@909 48 assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s3)
Chris@909 49 end
Chris@909 50 end
Chris@909 51
Chris@909 52 def test_to_utf8_by_setting_should_be_converted_all_latin1
Chris@909 53 with_settings :repositories_encodings => 'ISO-8859-1' do
Chris@909 54 s1 = "\xc3\x82\xc2\x80"
Chris@909 55 s2 = "\xC2\x80"
Chris@909 56 s3 = s2.dup
Chris@909 57 if s1.respond_to?(:force_encoding)
Chris@909 58 s1.force_encoding("UTF-8")
Chris@909 59 s2.force_encoding("ASCII-8BIT")
Chris@909 60 s3.force_encoding("UTF-8")
Chris@909 61 end
Chris@909 62 assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s2)
Chris@909 63 assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s3)
Chris@909 64 end
Chris@909 65 end
Chris@909 66
Chris@909 67 def test_to_utf8_by_setting_blank_string
Chris@909 68 assert_equal "", Redmine::CodesetUtil.to_utf8_by_setting("")
Chris@909 69 assert_equal nil, Redmine::CodesetUtil.to_utf8_by_setting(nil)
Chris@909 70 end
Chris@909 71
Chris@909 72 def test_to_utf8_by_setting_returns_ascii_as_utf8
Chris@909 73 s1 = "ASCII"
Chris@909 74 s2 = s1.dup
Chris@909 75 if s1.respond_to?(:force_encoding)
Chris@909 76 s1.force_encoding("UTF-8")
Chris@909 77 s2.force_encoding("ISO-8859-1")
Chris@909 78 end
Chris@909 79 str1 = Redmine::CodesetUtil.to_utf8_by_setting(s1)
Chris@909 80 str2 = Redmine::CodesetUtil.to_utf8_by_setting(s2)
Chris@909 81 assert_equal s1, str1
Chris@909 82 assert_equal s1, str2
Chris@909 83 if s1.respond_to?(:force_encoding)
Chris@909 84 assert_equal "UTF-8", str1.encoding.to_s
Chris@909 85 assert_equal "UTF-8", str2.encoding.to_s
Chris@909 86 end
Chris@909 87 end
Chris@909 88
Chris@909 89 def test_to_utf8_by_setting_invalid_utf8_sequences_should_be_stripped
Chris@909 90 with_settings :repositories_encodings => '' do
Chris@909 91 # s1 = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
Chris@909 92 s1 = "Texte encod\xe9 en ISO-8859-1."
Chris@909 93 s1.force_encoding("ASCII-8BIT") if s1.respond_to?(:force_encoding)
Chris@909 94 str = Redmine::CodesetUtil.to_utf8_by_setting(s1)
Chris@909 95 if str.respond_to?(:force_encoding)
Chris@909 96 assert str.valid_encoding?
Chris@909 97 assert_equal "UTF-8", str.encoding.to_s
Chris@909 98 end
Chris@909 99 assert_equal "Texte encod? en ISO-8859-1.", str
Chris@909 100 end
Chris@909 101 end
Chris@909 102
Chris@909 103 def test_to_utf8_by_setting_invalid_utf8_sequences_should_be_stripped_ja_jis
Chris@909 104 with_settings :repositories_encodings => 'ISO-2022-JP' do
Chris@909 105 s1 = "test\xb5\xfetest\xb5\xfe"
Chris@909 106 s1.force_encoding("ASCII-8BIT") if s1.respond_to?(:force_encoding)
Chris@909 107 str = Redmine::CodesetUtil.to_utf8_by_setting(s1)
Chris@909 108 if str.respond_to?(:force_encoding)
Chris@909 109 assert str.valid_encoding?
Chris@909 110 assert_equal "UTF-8", str.encoding.to_s
Chris@909 111 end
Chris@909 112 assert_equal "test??test??", str
Chris@909 113 end
Chris@909 114 end
Chris@909 115 end