-
Notifications
You must be signed in to change notification settings - Fork 794
/
Copy pathtest_encoding_utils.rb
251 lines (217 loc) · 8.35 KB
/
test_encoding_utils.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# encoding: utf-8
# frozen_string_literal: true
require 'minitest/autorun'
require 'sprockets/encoding_utils'
class TestDigestUtils < Minitest::Test
include Sprockets::EncodingUtils
def test_deflate
output = deflate("foobar")
assert_equal 8, output.length
assert_equal [75, 203, 207, 79, 74, 44, 2, 0], output.bytes.take(8)
end
def test_gzip
output = gzip("foobar")
assert_equal 26, output.length
assert_equal [31, 139, 8, 0, 1, 0, 0, 0], output.bytes.take(8)
end
def test_base64
assert_equal "Zm9vYmFy", base64("foobar")
end
def test_unmarshal
str = Marshal.dump("abc")
assert_equal Marshal.load(str), unmarshaled_deflated(str)
end
def test_unmarshal_older_minor_version
old_verbose, $VERBOSE = $VERBOSE, false
begin
str = Marshal.dump("abc")
str[1] = "\0"
assert_equal Marshal.load(str), unmarshaled_deflated(str)
ensure
$VERBOSE = old_verbose
end
end
def test_fail_to_unmarshal_older_major_version
str = Marshal.dump("abc")
str[0] = "\1"
assert_raises TypeError do
Marshal.load(str)
end
assert_raises TypeError do
unmarshaled_deflated(str)
end
end
def test_fail_to_unmarshal_not_enough_data
if RUBY_ENGINE == 'truffleruby' and Gem::Version.new(RUBY_ENGINE_VERSION) < Gem::Version.new('23.0.0.a')
skip "TypeError on TruffleRuby < 23.0"
end
assert_raises ArgumentError do
Marshal.load("")
end
assert_raises ArgumentError do
unmarshaled_deflated("")
end
end
def test_unmarshal_deflated
str = deflate(Marshal.dump("abc"))
assert_equal "abc", unmarshaled_deflated(str)
end
def test_detect_unicode_bom
path = File.expand_path("../fixtures/encoding/ascii.js", __FILE__)
str = File.binread(path)
assert_equal Encoding::BINARY, str.encoding
assert_equal 17, str.bytesize
str = detect_unicode_bom(str)
assert_equal Encoding::BINARY, str.encoding
assert_equal 17, str.bytesize
path = File.expand_path("../fixtures/encoding/utf8.js", __FILE__)
str = File.binread(path)
assert_equal Encoding::BINARY, str.encoding
assert_equal 20, str.bytesize
str = detect_unicode_bom(str)
assert_equal Encoding::BINARY, str.encoding
assert_equal 20, str.bytesize
path = File.expand_path("../fixtures/encoding/utf8_bom.js", __FILE__)
str = File.binread(path)
assert_equal Encoding::BINARY, str.encoding
assert_equal 23, str.bytesize
str = detect_unicode_bom(str)
assert_equal Encoding::UTF_8, str.encoding
assert_equal 20, str.bytesize
path = File.expand_path("../fixtures/encoding/utf8_bom.js", __FILE__)
str = File.binread(path)
str.force_encoding(Encoding::UTF_8)
assert_equal 23, str.bytesize
str = detect_unicode_bom(str)
assert_equal Encoding::UTF_8, str.encoding
assert_equal 20, str.bytesize
path = File.expand_path("../fixtures/encoding/utf16le.js", __FILE__)
str = File.binread(path)
assert_equal Encoding::BINARY, str.encoding
assert_equal 38, str.bytesize
str = detect_unicode_bom(str)
assert_equal Encoding::UTF_16LE, str.encoding
assert_equal 36, str.bytesize
path = File.expand_path("../fixtures/encoding/utf16le.js", __FILE__)
str = File.binread(path)
str.force_encoding(Encoding::UTF_16LE)
assert_equal 38, str.bytesize
str = detect_unicode_bom(str)
assert_equal Encoding::UTF_16LE, str.encoding
assert_equal 36, str.bytesize
path = File.expand_path("../fixtures/encoding/utf16be.js", __FILE__)
str = File.binread(path)
assert_equal Encoding::BINARY, str.encoding
assert_equal 38, str.bytesize
str = detect_unicode_bom(str)
assert_equal Encoding::UTF_16BE, str.encoding
assert_equal 36, str.bytesize
path = File.expand_path("../fixtures/encoding/utf32le.js", __FILE__)
str = File.binread(path)
assert_equal Encoding::BINARY, str.encoding
assert_equal 76, str.bytesize
str = detect_unicode_bom(str)
assert_equal Encoding::UTF_32LE, str.encoding
assert_equal 72, str.bytesize
path = File.expand_path("../fixtures/encoding/utf32be.js", __FILE__)
str = File.binread(path)
assert_equal Encoding::BINARY, str.encoding
assert_equal 76, str.bytesize
str = detect_unicode_bom(str)
assert_equal Encoding::UTF_32BE, str.encoding
assert_equal 72, str.bytesize
end
def test_detect_css_charset
path = File.expand_path("../fixtures/encoding/utf8-charset.css", __FILE__)
str = File.binread(path)
assert_equal Encoding::BINARY, str.encoding
assert_equal 38, str.bytesize
str = detect_css(str)
assert_equal Encoding::UTF_8, str.encoding
assert_equal 21, str.bytesize
assert_equal "\n\nh1 { color: red; }\n", str.encode(Encoding::UTF_8)
path = File.expand_path("../fixtures/encoding/utf8-charset.css", __FILE__)
str = File.binread(path)
str.force_encoding(Encoding::UTF_8)
assert_equal 38, str.bytesize
str = detect_css(str)
assert_equal Encoding::UTF_8, str.encoding
assert_equal 21, str.bytesize
assert_equal "\n\nh1 { color: red; }\n", str.encode(Encoding::UTF_8)
path = File.expand_path("../fixtures/encoding/utf16le-charset.css", __FILE__)
str = File.binread(path)
assert_equal Encoding::BINARY, str.encoding
assert_equal 82, str.bytesize
str = detect_css(str)
assert_equal Encoding::UTF_16LE, str.encoding
assert_equal 42, str.bytesize
assert_equal "\n\nh1 { color: red; }\n", str.encode(Encoding::UTF_8)
path = File.expand_path("../fixtures/encoding/utf16le-charset.css", __FILE__)
str = File.binread(path)
str.force_encoding(Encoding::UTF_16LE)
assert_equal 82, str.bytesize
str = detect_css(str)
assert_equal Encoding::UTF_16LE, str.encoding
assert_equal 42, str.bytesize
assert_equal "\n\nh1 { color: red; }\n", str.encode(Encoding::UTF_8)
path = File.expand_path("../fixtures/encoding/utf16le-bom-charset.css", __FILE__)
str = File.binread(path)
assert_equal Encoding::BINARY, str.encoding
assert_equal 84, str.bytesize
str = detect_css(str)
assert_equal Encoding::UTF_16LE, str.encoding
assert_equal 42, str.bytesize
assert_equal "\n\nh1 { color: red; }\n", str.encode(Encoding::UTF_8)
end
def test_detect_html_charset
assert_equal Encoding.default_external, Encoding::UTF_8
path = File.expand_path("../fixtures/encoding/utf8-charset.html", __FILE__)
str = File.binread(path)
assert_equal Encoding::BINARY, str.encoding
assert_equal 10, str.bytesize
str = detect_html(str)
assert_equal Encoding::UTF_8, str.encoding
assert_equal 10, str.bytesize
assert_equal "<p>☃</p>", str.encode(Encoding::UTF_8)
path = File.expand_path("../fixtures/encoding/utf8_bom.html", __FILE__)
str = File.binread(path)
str.force_encoding(Encoding::UTF_8)
assert_equal 13, str.bytesize
str = detect_html(str)
assert_equal Encoding::UTF_8, str.encoding
assert_equal 10, str.bytesize
assert_equal "<p>☃</p>", str.encode(Encoding::UTF_8)
path = File.expand_path("../fixtures/encoding/utf16le.html", __FILE__)
str = File.binread(path)
assert_equal Encoding::BINARY, str.encoding
assert_equal 18, str.bytesize
str = detect_html(str)
assert_equal Encoding::UTF_16LE, str.encoding
assert_equal 16, str.bytesize
assert_equal "<p>☃</p>", str.encode(Encoding::UTF_8)
path = File.expand_path("../fixtures/encoding/utf16be.html", __FILE__)
str = File.binread(path)
str.force_encoding(Encoding::UTF_16BE)
assert_equal 18, str.bytesize
str = detect_html(str)
assert_equal Encoding::UTF_16BE, str.encoding
assert_equal 16, str.bytesize
assert_equal "<p>☃</p>", str.encode(Encoding::UTF_8)
path = File.expand_path("../fixtures/encoding/utf32be.html", __FILE__)
str = File.binread(path)
assert_equal Encoding::BINARY, str.encoding
assert_equal 36, str.bytesize
str = detect_html(str)
assert_equal Encoding::UTF_32BE, str.encoding
assert_equal 32, str.bytesize
assert_equal "<p>☃</p>", str.encode(Encoding::UTF_8)
path = File.expand_path("../fixtures/encoding/utf32le.html", __FILE__)
str = File.binread(path)
assert_equal Encoding::BINARY, str.encoding
assert_equal 36, str.bytesize
str = detect_html(str)
assert_equal Encoding::UTF_32LE, str.encoding
assert_equal 32, str.bytesize
assert_equal "<p>☃</p>", str.encode(Encoding::UTF_8)
end
end