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