1
+ require 'abstract_unit'
2
+
3
+ class MultibyteUtilsTest < Test ::Unit ::TestCase
4
+
5
+ def test_valid_character_returns_an_expression_for_the_current_encoding
6
+ with_kcode ( 'None' ) do
7
+ assert_nil ActiveSupport ::Multibyte . valid_character
8
+ end
9
+ with_kcode ( 'UTF8' ) do
10
+ assert_equal ActiveSupport ::Multibyte ::VALID_CHARACTER [ 'UTF-8' ] , ActiveSupport ::Multibyte . valid_character
11
+ end
12
+ with_kcode ( 'SJIS' ) do
13
+ assert_equal ActiveSupport ::Multibyte ::VALID_CHARACTER [ 'Shift_JIS' ] , ActiveSupport ::Multibyte . valid_character
14
+ end
15
+ end
16
+
17
+ def test_verify_verifies_ASCII_strings_are_properly_encoded
18
+ with_kcode ( 'None' ) do
19
+ examples . each do |example |
20
+ assert ActiveSupport ::Multibyte . verify ( example )
21
+ end
22
+ end
23
+ end
24
+
25
+ def test_verify_verifies_UTF_8_strings_are_properly_encoded
26
+ with_kcode ( 'UTF8' ) do
27
+ assert ActiveSupport ::Multibyte . verify ( example ( 'valid UTF-8' ) )
28
+ assert !ActiveSupport ::Multibyte . verify ( example ( 'invalid UTF-8' ) )
29
+ end
30
+ end
31
+
32
+ def test_verify_verifies_Shift_JIS_strings_are_properly_encoded
33
+ with_kcode ( 'SJIS' ) do
34
+ assert ActiveSupport ::Multibyte . verify ( example ( 'valid Shift-JIS' ) )
35
+ assert !ActiveSupport ::Multibyte . verify ( example ( 'invalid Shift-JIS' ) )
36
+ end
37
+ end
38
+
39
+ def test_verify_bang_raises_an_exception_when_it_finds_an_invalid_character
40
+ with_kcode ( 'UTF8' ) do
41
+ assert_raises ( ActiveSupport ::Multibyte ::Handlers ::EncodingError ) do
42
+ ActiveSupport ::Multibyte . verify! ( example ( 'invalid UTF-8' ) )
43
+ end
44
+ end
45
+ end
46
+
47
+ def test_verify_bang_doesnt_raise_an_exception_when_the_encoding_is_valid
48
+ with_kcode ( 'UTF8' ) do
49
+ assert_nothing_raised do
50
+ ActiveSupport ::Multibyte . verify! ( example ( 'valid UTF-8' ) )
51
+ end
52
+ end
53
+ end
54
+
55
+ def test_clean_leaves_ASCII_strings_intact
56
+ with_kcode ( 'None' ) do
57
+ [
58
+ 'word' , "\270 \236 \010 \210 \245 "
59
+ ] . each do |string |
60
+ assert_equal string , ActiveSupport ::Multibyte . clean ( string )
61
+ end
62
+ end
63
+ end
64
+
65
+ def test_clean_cleans_invalid_characters_from_UTF_8_encoded_strings
66
+ with_kcode ( 'UTF8' ) do
67
+ cleaned_utf8 = [ 8 ] . pack ( 'C*' )
68
+ assert_equal example ( 'valid UTF-8' ) , ActiveSupport ::Multibyte . clean ( example ( 'valid UTF-8' ) )
69
+ assert_equal cleaned_utf8 , ActiveSupport ::Multibyte . clean ( example ( 'invalid UTF-8' ) )
70
+ end
71
+ end
72
+
73
+ def test_clean_cleans_invalid_characters_from_Shift_JIS_encoded_strings
74
+ with_kcode ( 'SJIS' ) do
75
+ cleaned_sjis = [ 184 , 0 , 136 , 165 ] . pack ( 'C*' )
76
+ assert_equal example ( 'valid Shift-JIS' ) , ActiveSupport ::Multibyte . clean ( example ( 'valid Shift-JIS' ) )
77
+ assert_equal cleaned_sjis , ActiveSupport ::Multibyte . clean ( example ( 'invalid Shift-JIS' ) )
78
+ end
79
+ end
80
+
81
+ private
82
+
83
+ STRINGS = {
84
+ 'valid ASCII' => [ 65 , 83 , 67 , 73 , 73 ] . pack ( 'C*' ) ,
85
+ 'invalid ASCII' => [ 128 ] . pack ( 'C*' ) ,
86
+ 'valid UTF-8' => [ 227 , 129 , 147 , 227 , 129 , 171 , 227 , 129 , 161 , 227 , 130 , 143 ] . pack ( 'C*' ) ,
87
+ 'invalid UTF-8' => [ 184 , 158 , 8 , 136 , 165 ] . pack ( 'C*' ) ,
88
+ 'valid Shift-JIS' => [ 131 , 122 , 129 , 91 , 131 , 128 ] . pack ( 'C*' ) ,
89
+ 'invalid Shift-JIS' => [ 184 , 158 , 8 , 0 , 255 , 136 , 165 ] . pack ( 'C*' )
90
+ }
91
+
92
+ def example ( key )
93
+ STRINGS [ key ]
94
+ end
95
+
96
+ def examples
97
+ STRINGS . values
98
+ end
99
+
100
+ def with_kcode ( code )
101
+ before = $KCODE
102
+ $KCODE = code
103
+ yield
104
+ $KCODE = before
105
+ end
106
+ end
0 commit comments