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