1
+ # encoding: utf-8
2
+
3
+ require 'abstract_unit'
4
+ require 'multibyte_test_helpers'
5
+
6
+ class MultibyteUtilsTest < ActiveSupport ::TestCase
7
+ include MultibyteTestHelpers
8
+
9
+ test "valid_character returns an expression for the current encoding" do
10
+ with_encoding ( 'None' ) do
11
+ assert_nil ActiveSupport ::Multibyte . valid_character
12
+ end
13
+ with_encoding ( 'UTF8' ) do
14
+ assert_equal ActiveSupport ::Multibyte ::VALID_CHARACTER [ 'UTF-8' ] , ActiveSupport ::Multibyte . valid_character
15
+ end
16
+ with_encoding ( 'SJIS' ) do
17
+ assert_equal ActiveSupport ::Multibyte ::VALID_CHARACTER [ 'Shift_JIS' ] , ActiveSupport ::Multibyte . valid_character
18
+ end
19
+ end
20
+
21
+ test "verify verifies ASCII strings are properly encoded" do
22
+ with_encoding ( 'None' ) do
23
+ examples . each do |example |
24
+ assert ActiveSupport ::Multibyte . verify ( example )
25
+ end
26
+ end
27
+ end
28
+
29
+ test "verify verifies UTF-8 strings are properly encoded" do
30
+ with_encoding ( 'UTF8' ) do
31
+ assert ActiveSupport ::Multibyte . verify ( example ( 'valid UTF-8' ) )
32
+ assert !ActiveSupport ::Multibyte . verify ( example ( 'invalid UTF-8' ) )
33
+ end
34
+ end
35
+
36
+ test "verify verifies Shift-JIS strings are properly encoded" do
37
+ with_encoding ( 'SJIS' ) do
38
+ assert ActiveSupport ::Multibyte . verify ( example ( 'valid Shift-JIS' ) )
39
+ assert !ActiveSupport ::Multibyte . verify ( example ( 'invalid Shift-JIS' ) )
40
+ end
41
+ end
42
+
43
+ test "verify! raises an exception when it finds an invalid character" do
44
+ with_encoding ( 'UTF8' ) do
45
+ assert_raises ( ActiveSupport ::Multibyte ::EncodingError ) do
46
+ ActiveSupport ::Multibyte . verify! ( example ( 'invalid UTF-8' ) )
47
+ end
48
+ end
49
+ end
50
+
51
+ test "verify! doesn't raise an exception when the encoding is valid" do
52
+ with_encoding ( 'UTF8' ) do
53
+ assert_nothing_raised do
54
+ ActiveSupport ::Multibyte . verify! ( example ( 'valid UTF-8' ) )
55
+ end
56
+ end
57
+ end
58
+
59
+ if RUBY_VERSION < '1.9'
60
+ test "clean leaves ASCII strings intact" do
61
+ with_encoding ( 'None' ) do
62
+ [
63
+ 'word' , "\270 \236 \010 \210 \245 "
64
+ ] . each do |string |
65
+ assert_equal string , ActiveSupport ::Multibyte . clean ( string )
66
+ end
67
+ end
68
+ end
69
+
70
+ test "clean cleans invalid characters from UTF-8 encoded strings" do
71
+ with_encoding ( 'UTF8' ) do
72
+ cleaned_utf8 = [ 8 ] . pack ( 'C*' )
73
+ assert_equal example ( 'valid UTF-8' ) , ActiveSupport ::Multibyte . clean ( example ( 'valid UTF-8' ) )
74
+ assert_equal cleaned_utf8 , ActiveSupport ::Multibyte . clean ( example ( 'invalid UTF-8' ) )
75
+ end
76
+ end
77
+
78
+ test "clean cleans invalid characters from Shift-JIS encoded strings" do
79
+ with_encoding ( 'SJIS' ) do
80
+ cleaned_sjis = [ 184 , 0 , 136 , 165 ] . pack ( 'C*' )
81
+ assert_equal example ( 'valid Shift-JIS' ) , ActiveSupport ::Multibyte . clean ( example ( 'valid Shift-JIS' ) )
82
+ assert_equal cleaned_sjis , ActiveSupport ::Multibyte . clean ( example ( 'invalid Shift-JIS' ) )
83
+ end
84
+ end
85
+ else
86
+ test "clean is a no-op" do
87
+ with_encoding ( 'UTF8' ) do
88
+ assert_equal example ( 'invalid Shift-JIS' ) , ActiveSupport ::Multibyte . clean ( example ( 'invalid Shift-JIS' ) )
89
+ end
90
+ end
91
+ end
92
+
93
+ private
94
+
95
+ STRINGS = {
96
+ 'valid ASCII' => [ 65 , 83 , 67 , 73 , 73 ] . pack ( 'C*' ) ,
97
+ 'invalid ASCII' => [ 128 ] . pack ( 'C*' ) ,
98
+ 'valid UTF-8' => [ 227 , 129 , 147 , 227 , 129 , 171 , 227 , 129 , 161 , 227 , 130 , 143 ] . pack ( 'C*' ) ,
99
+ 'invalid UTF-8' => [ 184 , 158 , 8 , 136 , 165 ] . pack ( 'C*' ) ,
100
+ 'valid Shift-JIS' => [ 131 , 122 , 129 , 91 , 131 , 128 ] . pack ( 'C*' ) ,
101
+ 'invalid Shift-JIS' => [ 184 , 158 , 8 , 0 , 255 , 136 , 165 ] . pack ( 'C*' )
102
+ }
103
+
104
+ if Kernel . const_defined? ( :Encoding )
105
+ def example ( key )
106
+ STRINGS [ key ] . force_encoding ( Encoding . default_internal )
107
+ end
108
+
109
+ def examples
110
+ STRINGS . values . map { |s | s . force_encoding ( Encoding . default_internal ) }
111
+ end
112
+ else
113
+ def example ( key )
114
+ STRINGS [ key ]
115
+ end
116
+
117
+ def examples
118
+ STRINGS . values
119
+ end
120
+ end
121
+
122
+ if 'string' . respond_to? ( :encoding )
123
+ def with_encoding ( enc )
124
+ before = Encoding . default_internal
125
+
126
+ case enc
127
+ when 'UTF8'
128
+ Encoding . default_internal = Encoding ::UTF_8
129
+ when 'SJIS'
130
+ Encoding . default_internal = Encoding ::Shift_JIS
131
+ else
132
+ Encoding . default_internal = Encoding ::BINARY
133
+ end
134
+ yield
135
+
136
+ Encoding . default_internal = before
137
+ end
138
+ else
139
+ alias with_encoding with_kcode
140
+ end
141
+ end
0 commit comments