-
Notifications
You must be signed in to change notification settings - Fork 792
/
Copy pathtest_encoding.rb
72 lines (61 loc) · 2.27 KB
/
test_encoding.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
# frozen_string_literal: true
require "sprockets_test"
class AssetEncodingTest < Sprockets::TestCase
def setup
@env = Sprockets::Environment.new
@env.append_path(fixture_path('encoding'))
end
test "read ASCII asset" do
data = @env['ascii.js'].to_s
assert_equal "var foo = \"bar\";\n", data
assert_equal Encoding::UTF_8, data.encoding
end
test "read UTF-8 asset" do
data = @env['utf8.js'].to_s
assert_equal "var snowman = \"☃\";\n", data
assert_equal Encoding::UTF_8, data.encoding
end
test "read UTF-8 asset with BOM" do
data = @env['utf8_bom.js'].to_s
assert_equal "var snowman = \"☃\";\n", data
assert_equal Encoding::UTF_8, data.encoding
end
test "read UTF-16 asset" do
data = @env['utf16le.js'].to_s
assert_equal "var snowman = \"☃\";\n", data
assert_equal Encoding::UTF_8, data.encoding
end
test "read ASCII + UTF-8 concatation asset" do
data = @env['ascii_utf8.js'].to_s
assert_equal "var foo = \"bar\";\nvar snowman = \"\342\230\203\";\n\n\n",
data
assert_equal Encoding::UTF_8, data.encoding
end
test "read static BINARY asset" do
data = @env['binary.png'].to_s
assert_equal (+"\x89PNG\r\n\x1A\n\x00\x00\x00").force_encoding("BINARY"),
data[0..10]
assert_equal Encoding::BINARY, data.encoding
end
test "read processed BINARY asset" do
@env.register_postprocessor('image/png') { |input| input[:data] }
data = @env['binary.png'].to_s
assert_equal (+"\x89PNG\r\n\x1A\n\x00\x00\x00").force_encoding("BINARY"),
data[0..10]
assert_equal Encoding::BINARY, data.encoding
end
test "read css asset with charset" do
expected = "\n\nh1 { color: red; }\n"
assert_equal expected, @env['utf8-charset.css'].to_s
assert_equal expected, @env['utf16le-charset.css'].to_s
assert_equal expected, @env['utf16le-bom-charset.css'].to_s
end
test "read file with content type" do
data = @env.read_file(fixture_path('encoding/ascii.js'), 'application/javascript')
assert_equal "var foo = \"bar\";\n", data
assert_equal Encoding::UTF_8, data.encoding
data = @env.read_file(fixture_path('encoding/utf16le-charset.css'), 'text/css')
assert_equal "\n\nh1 { color: red; }\n", data
assert_equal Encoding::UTF_8, data.encoding
end
end