-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathblock_test.rb
100 lines (90 loc) · 3.13 KB
/
block_test.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
# frozen_string_literal: true
require 'test_helper'
class BlockTest < MiniTest::Test
def test_no_allocation_of_trimmed_strings
template = Liquid::Template.parse("{{ a -}} {{- b }}")
assert_equal(2, template.root.nodelist.size)
template = Liquid::Template.parse("{{ a -}} foo {{- b }}")
assert_equal(3, template.root.nodelist.size)
end
def test_raise_on_output_with_non_utf8_encoding
output = String.new(encoding: Encoding::ASCII)
template = Liquid::Template.parse("ascii text")
exc = assert_raises(Encoding::CompatibilityError) do
template.render!({}, output: output)
end
assert_equal("non-UTF8 encoded output (US-ASCII) not supported", exc.message)
end
def test_write_unicode_characters
output = String.new(encoding: Encoding::UTF_8)
template = Liquid::Template.parse("ü{{ unicode_char }}")
assert_equal("üñ", template.render!({ 'unicode_char' => 'ñ' }, output: output))
end
def test_op_write_raw_w
source = "a" * 2**8
template = Liquid::Template.parse(source)
assert_equal(source, template.render!)
end
# Test for bug: https://github.com/Shopify/liquid-c/pull/120
def test_bug_120_instrument
calls = []
Liquid::Usage.stub(:increment, ->(name) { calls << name }) do
Liquid::Template.parse("{{ -.1 }}")
end
assert_equal ["liquid_c_negative_float_without_integer"], calls
calls = []
Liquid::Usage.stub(:increment, ->(name) { calls << name }) do
Liquid::Template.parse("{{ .1 }}")
end
assert_equal [], calls
end
def test_disassemble_raw_w
source = "a" * 2**8
template = Liquid::Template.parse(source)
block_body = template.root.body
assert_equal(<<~ASM, block_body.disassemble)
0x0000: write_raw_w("#{source}")
0x0104: leave
ASM
end
def test_disassemble
source = <<~LIQUID
raw
{{- var | default: "none", allow_false: true -}}
{%- increment counter -%}
LIQUID
template = Liquid::Template.parse(source, line_numbers: true)
block_body = template.root.body
increment_node = block_body.nodelist[2]
assert_instance_of(Liquid::Increment, increment_node)
assert_equal(<<~ASM, block_body.disassemble)
0x0000: write_raw("raw")
0x0005: render_variable_rescue(line_number: 2)
0x0009: find_static_var("var")
0x000a: push_const("none")
0x000b: push_const("allow_false")
0x000c: push_true
0x000d: hash_new(1)
0x000f: filter(name: :default, num_args: 3)
0x0011: pop_write
0x0012: write_node(#{increment_node.inspect})
0x0013: leave
ASM
end
def test_exception_renderer_exception
original_error = Liquid::Error.new("original")
handler_error = RuntimeError.new("exception handler error")
context = Liquid::Context.new('raise_error' => ->(_ctx) { raise(original_error) })
context.exception_renderer = lambda do |exc|
if exc == original_error
raise(handler_error)
end
exc
end
template = Liquid::Template.parse("{% assign x = raise_error %}")
exc = assert_raises(RuntimeError) do
template.render(context)
end
assert_equal(handler_error, exc)
end
end