liquid 5.3.0 → 5.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (96) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +16 -1
  3. data/README.md +2 -2
  4. data/lib/liquid/block_body.rb +4 -4
  5. data/lib/liquid/context.rb +6 -2
  6. data/lib/liquid/forloop_drop.rb +44 -1
  7. data/lib/liquid/locales/en.yml +6 -5
  8. data/lib/liquid/partial_cache.rb +3 -3
  9. data/lib/liquid/{static_registers.rb → registers.rb} +13 -10
  10. data/lib/liquid/standardfilters.rb +406 -57
  11. data/lib/liquid/strainer_factory.rb +4 -0
  12. data/lib/liquid/strainer_template.rb +4 -0
  13. data/lib/liquid/tablerowloop_drop.rb +58 -1
  14. data/lib/liquid/tags/assign.rb +12 -8
  15. data/lib/liquid/tags/break.rb +8 -0
  16. data/lib/liquid/tags/capture.rb +13 -10
  17. data/lib/liquid/tags/case.rb +21 -0
  18. data/lib/liquid/tags/comment.rb +13 -0
  19. data/lib/liquid/tags/continue.rb +8 -9
  20. data/lib/liquid/tags/cycle.rb +12 -11
  21. data/lib/liquid/tags/decrement.rb +16 -17
  22. data/lib/liquid/tags/echo.rb +16 -9
  23. data/lib/liquid/tags/for.rb +22 -43
  24. data/lib/liquid/tags/if.rb +11 -9
  25. data/lib/liquid/tags/include.rb +15 -13
  26. data/lib/liquid/tags/increment.rb +16 -14
  27. data/lib/liquid/tags/inline_comment.rb +43 -0
  28. data/lib/liquid/tags/raw.rb +11 -0
  29. data/lib/liquid/tags/render.rb +29 -4
  30. data/lib/liquid/tags/table_row.rb +22 -0
  31. data/lib/liquid/tags/unless.rb +15 -4
  32. data/lib/liquid/template.rb +2 -3
  33. data/lib/liquid/variable.rb +4 -4
  34. data/lib/liquid/variable_lookup.rb +10 -7
  35. data/lib/liquid/version.rb +1 -1
  36. data/lib/liquid.rb +2 -2
  37. metadata +7 -123
  38. data/lib/liquid/register.rb +0 -6
  39. data/test/fixtures/en_locale.yml +0 -9
  40. data/test/integration/assign_test.rb +0 -117
  41. data/test/integration/blank_test.rb +0 -109
  42. data/test/integration/block_test.rb +0 -58
  43. data/test/integration/capture_test.rb +0 -58
  44. data/test/integration/context_test.rb +0 -634
  45. data/test/integration/document_test.rb +0 -21
  46. data/test/integration/drop_test.rb +0 -257
  47. data/test/integration/error_handling_test.rb +0 -272
  48. data/test/integration/expression_test.rb +0 -46
  49. data/test/integration/filter_kwarg_test.rb +0 -24
  50. data/test/integration/filter_test.rb +0 -189
  51. data/test/integration/hash_ordering_test.rb +0 -25
  52. data/test/integration/output_test.rb +0 -125
  53. data/test/integration/parsing_quirks_test.rb +0 -134
  54. data/test/integration/profiler_test.rb +0 -240
  55. data/test/integration/security_test.rb +0 -89
  56. data/test/integration/standard_filter_test.rb +0 -925
  57. data/test/integration/tag/disableable_test.rb +0 -59
  58. data/test/integration/tag_test.rb +0 -45
  59. data/test/integration/tags/break_tag_test.rb +0 -17
  60. data/test/integration/tags/continue_tag_test.rb +0 -17
  61. data/test/integration/tags/echo_test.rb +0 -13
  62. data/test/integration/tags/for_tag_test.rb +0 -466
  63. data/test/integration/tags/if_else_tag_test.rb +0 -190
  64. data/test/integration/tags/include_tag_test.rb +0 -269
  65. data/test/integration/tags/increment_tag_test.rb +0 -25
  66. data/test/integration/tags/liquid_tag_test.rb +0 -116
  67. data/test/integration/tags/raw_tag_test.rb +0 -34
  68. data/test/integration/tags/render_tag_test.rb +0 -213
  69. data/test/integration/tags/standard_tag_test.rb +0 -303
  70. data/test/integration/tags/statements_test.rb +0 -113
  71. data/test/integration/tags/table_row_test.rb +0 -66
  72. data/test/integration/tags/unless_else_tag_test.rb +0 -28
  73. data/test/integration/template_test.rb +0 -340
  74. data/test/integration/trim_mode_test.rb +0 -563
  75. data/test/integration/variable_test.rb +0 -138
  76. data/test/test_helper.rb +0 -207
  77. data/test/unit/block_unit_test.rb +0 -53
  78. data/test/unit/condition_unit_test.rb +0 -181
  79. data/test/unit/file_system_unit_test.rb +0 -37
  80. data/test/unit/i18n_unit_test.rb +0 -39
  81. data/test/unit/lexer_unit_test.rb +0 -53
  82. data/test/unit/parse_tree_visitor_test.rb +0 -261
  83. data/test/unit/parser_unit_test.rb +0 -84
  84. data/test/unit/partial_cache_unit_test.rb +0 -128
  85. data/test/unit/regexp_unit_test.rb +0 -46
  86. data/test/unit/static_registers_unit_test.rb +0 -156
  87. data/test/unit/strainer_factory_unit_test.rb +0 -101
  88. data/test/unit/strainer_template_unit_test.rb +0 -82
  89. data/test/unit/tag_unit_test.rb +0 -23
  90. data/test/unit/tags/case_tag_unit_test.rb +0 -12
  91. data/test/unit/tags/for_tag_unit_test.rb +0 -15
  92. data/test/unit/tags/if_tag_unit_test.rb +0 -10
  93. data/test/unit/template_factory_unit_test.rb +0 -12
  94. data/test/unit/template_unit_test.rb +0 -87
  95. data/test/unit/tokenizer_unit_test.rb +0 -62
  96. data/test/unit/variable_unit_test.rb +0 -164
@@ -1,261 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class ParseTreeVisitorTest < Minitest::Test
6
- include Liquid
7
-
8
- def test_variable
9
- assert_equal(
10
- ["test"],
11
- visit(%({{ test }}))
12
- )
13
- end
14
-
15
- def test_varible_with_filter
16
- assert_equal(
17
- ["test", "infilter"],
18
- visit(%({{ test | split: infilter }}))
19
- )
20
- end
21
-
22
- def test_dynamic_variable
23
- assert_equal(
24
- ["test", "inlookup"],
25
- visit(%({{ test[inlookup] }}))
26
- )
27
- end
28
-
29
- def test_echo
30
- assert_equal(
31
- ["test"],
32
- visit(%({% echo test %}))
33
- )
34
- end
35
-
36
- def test_if_condition
37
- assert_equal(
38
- ["test"],
39
- visit(%({% if test %}{% endif %}))
40
- )
41
- end
42
-
43
- def test_complex_if_condition
44
- assert_equal(
45
- ["test"],
46
- visit(%({% if 1 == 1 and 2 == test %}{% endif %}))
47
- )
48
- end
49
-
50
- def test_if_body
51
- assert_equal(
52
- ["test"],
53
- visit(%({% if 1 == 1 %}{{ test }}{% endif %}))
54
- )
55
- end
56
-
57
- def test_unless_condition
58
- assert_equal(
59
- ["test"],
60
- visit(%({% unless test %}{% endunless %}))
61
- )
62
- end
63
-
64
- def test_complex_unless_condition
65
- assert_equal(
66
- ["test"],
67
- visit(%({% unless 1 == 1 and 2 == test %}{% endunless %}))
68
- )
69
- end
70
-
71
- def test_unless_body
72
- assert_equal(
73
- ["test"],
74
- visit(%({% unless 1 == 1 %}{{ test }}{% endunless %}))
75
- )
76
- end
77
-
78
- def test_elsif_condition
79
- assert_equal(
80
- ["test"],
81
- visit(%({% if 1 == 1 %}{% elsif test %}{% endif %}))
82
- )
83
- end
84
-
85
- def test_complex_elsif_condition
86
- assert_equal(
87
- ["test"],
88
- visit(%({% if 1 == 1 %}{% elsif 1 == 1 and 2 == test %}{% endif %}))
89
- )
90
- end
91
-
92
- def test_elsif_body
93
- assert_equal(
94
- ["test"],
95
- visit(%({% if 1 == 1 %}{% elsif 2 == 2 %}{{ test }}{% endif %}))
96
- )
97
- end
98
-
99
- def test_else_body
100
- assert_equal(
101
- ["test"],
102
- visit(%({% if 1 == 1 %}{% else %}{{ test }}{% endif %}))
103
- )
104
- end
105
-
106
- def test_case_left
107
- assert_equal(
108
- ["test"],
109
- visit(%({% case test %}{% endcase %}))
110
- )
111
- end
112
-
113
- def test_case_condition
114
- assert_equal(
115
- ["test"],
116
- visit(%({% case 1 %}{% when test %}{% endcase %}))
117
- )
118
- end
119
-
120
- def test_case_when_body
121
- assert_equal(
122
- ["test"],
123
- visit(%({% case 1 %}{% when 2 %}{{ test }}{% endcase %}))
124
- )
125
- end
126
-
127
- def test_case_else_body
128
- assert_equal(
129
- ["test"],
130
- visit(%({% case 1 %}{% else %}{{ test }}{% endcase %}))
131
- )
132
- end
133
-
134
- def test_for_in
135
- assert_equal(
136
- ["test"],
137
- visit(%({% for x in test %}{% endfor %}))
138
- )
139
- end
140
-
141
- def test_for_limit
142
- assert_equal(
143
- ["test"],
144
- visit(%({% for x in (1..5) limit: test %}{% endfor %}))
145
- )
146
- end
147
-
148
- def test_for_offset
149
- assert_equal(
150
- ["test"],
151
- visit(%({% for x in (1..5) offset: test %}{% endfor %}))
152
- )
153
- end
154
-
155
- def test_for_body
156
- assert_equal(
157
- ["test"],
158
- visit(%({% for x in (1..5) %}{{ test }}{% endfor %}))
159
- )
160
- end
161
-
162
- def test_for_range
163
- assert_equal(
164
- ["test"],
165
- visit(%({% for x in (1..test) %}{% endfor %}))
166
- )
167
- end
168
-
169
- def test_tablerow_in
170
- assert_equal(
171
- ["test"],
172
- visit(%({% tablerow x in test %}{% endtablerow %}))
173
- )
174
- end
175
-
176
- def test_tablerow_limit
177
- assert_equal(
178
- ["test"],
179
- visit(%({% tablerow x in (1..5) limit: test %}{% endtablerow %}))
180
- )
181
- end
182
-
183
- def test_tablerow_offset
184
- assert_equal(
185
- ["test"],
186
- visit(%({% tablerow x in (1..5) offset: test %}{% endtablerow %}))
187
- )
188
- end
189
-
190
- def test_tablerow_body
191
- assert_equal(
192
- ["test"],
193
- visit(%({% tablerow x in (1..5) %}{{ test }}{% endtablerow %}))
194
- )
195
- end
196
-
197
- def test_cycle
198
- assert_equal(
199
- ["test"],
200
- visit(%({% cycle test %}))
201
- )
202
- end
203
-
204
- def test_assign
205
- assert_equal(
206
- ["test"],
207
- visit(%({% assign x = test %}))
208
- )
209
- end
210
-
211
- def test_capture
212
- assert_equal(
213
- ["test"],
214
- visit(%({% capture x %}{{ test }}{% endcapture %}))
215
- )
216
- end
217
-
218
- def test_include
219
- assert_equal(
220
- ["test"],
221
- visit(%({% include test %}))
222
- )
223
- end
224
-
225
- def test_include_with
226
- assert_equal(
227
- ["test"],
228
- visit(%({% include "hai" with test %}))
229
- )
230
- end
231
-
232
- def test_include_for
233
- assert_equal(
234
- ["test"],
235
- visit(%({% include "hai" for test %}))
236
- )
237
- end
238
-
239
- def test_preserve_tree_structure
240
- assert_equal(
241
- [[nil, [
242
- [nil, [[nil, [["other", []]]]]],
243
- ["test", []],
244
- ["xs", []],
245
- ]]],
246
- traversal(%({% for x in xs offset: test %}{{ other }}{% endfor %})).visit
247
- )
248
- end
249
-
250
- private
251
-
252
- def traversal(template)
253
- ParseTreeVisitor
254
- .for(Template.parse(template).root)
255
- .add_callback_for(VariableLookup) { |node| node.name } # rubocop:disable Style/SymbolProc
256
- end
257
-
258
- def visit(template)
259
- traversal(template).visit.flatten.compact
260
- end
261
- end
@@ -1,84 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class ParserUnitTest < Minitest::Test
6
- include Liquid
7
-
8
- def test_consume
9
- p = Parser.new("wat: 7")
10
- assert_equal('wat', p.consume(:id))
11
- assert_equal(':', p.consume(:colon))
12
- assert_equal('7', p.consume(:number))
13
- end
14
-
15
- def test_jump
16
- p = Parser.new("wat: 7")
17
- p.jump(2)
18
- assert_equal('7', p.consume(:number))
19
- end
20
-
21
- def test_consume?
22
- p = Parser.new("wat: 7")
23
- assert_equal('wat', p.consume?(:id))
24
- assert_equal(false, p.consume?(:dot))
25
- assert_equal(':', p.consume(:colon))
26
- assert_equal('7', p.consume?(:number))
27
- end
28
-
29
- def test_id?
30
- p = Parser.new("wat 6 Peter Hegemon")
31
- assert_equal('wat', p.id?('wat'))
32
- assert_equal(false, p.id?('endgame'))
33
- assert_equal('6', p.consume(:number))
34
- assert_equal('Peter', p.id?('Peter'))
35
- assert_equal(false, p.id?('Achilles'))
36
- end
37
-
38
- def test_look
39
- p = Parser.new("wat 6 Peter Hegemon")
40
- assert_equal(true, p.look(:id))
41
- assert_equal('wat', p.consume(:id))
42
- assert_equal(false, p.look(:comparison))
43
- assert_equal(true, p.look(:number))
44
- assert_equal(true, p.look(:id, 1))
45
- assert_equal(false, p.look(:number, 1))
46
- end
47
-
48
- def test_expressions
49
- p = Parser.new("hi.there hi?[5].there? hi.there.bob")
50
- assert_equal('hi.there', p.expression)
51
- assert_equal('hi?[5].there?', p.expression)
52
- assert_equal('hi.there.bob', p.expression)
53
-
54
- p = Parser.new("567 6.0 'lol' \"wut\"")
55
- assert_equal('567', p.expression)
56
- assert_equal('6.0', p.expression)
57
- assert_equal("'lol'", p.expression)
58
- assert_equal('"wut"', p.expression)
59
- end
60
-
61
- def test_ranges
62
- p = Parser.new("(5..7) (1.5..9.6) (young..old) (hi[5].wat..old)")
63
- assert_equal('(5..7)', p.expression)
64
- assert_equal('(1.5..9.6)', p.expression)
65
- assert_equal('(young..old)', p.expression)
66
- assert_equal('(hi[5].wat..old)', p.expression)
67
- end
68
-
69
- def test_arguments
70
- p = Parser.new("filter: hi.there[5], keyarg: 7")
71
- assert_equal('filter', p.consume(:id))
72
- assert_equal(':', p.consume(:colon))
73
- assert_equal('hi.there[5]', p.argument)
74
- assert_equal(',', p.consume(:comma))
75
- assert_equal('keyarg: 7', p.argument)
76
- end
77
-
78
- def test_invalid_expression
79
- assert_raises(SyntaxError) do
80
- p = Parser.new("==")
81
- p.expression
82
- end
83
- end
84
- end
@@ -1,128 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class PartialCacheUnitTest < Minitest::Test
6
- def test_uses_the_file_system_register_if_present
7
- context = Liquid::Context.build(
8
- registers: {
9
- file_system: StubFileSystem.new('my_partial' => 'my partial body'),
10
- }
11
- )
12
-
13
- partial = Liquid::PartialCache.load(
14
- 'my_partial',
15
- context: context,
16
- parse_context: Liquid::ParseContext.new
17
- )
18
-
19
- assert_equal('my partial body', partial.render)
20
- end
21
-
22
- def test_reads_from_the_file_system_only_once_per_file
23
- file_system = StubFileSystem.new('my_partial' => 'some partial body')
24
- context = Liquid::Context.build(
25
- registers: { file_system: file_system }
26
- )
27
-
28
- 2.times do
29
- Liquid::PartialCache.load(
30
- 'my_partial',
31
- context: context,
32
- parse_context: Liquid::ParseContext.new
33
- )
34
- end
35
-
36
- assert_equal(1, file_system.file_read_count)
37
- end
38
-
39
- def test_cache_state_is_stored_per_context
40
- parse_context = Liquid::ParseContext.new
41
- shared_file_system = StubFileSystem.new(
42
- 'my_partial' => 'my shared value'
43
- )
44
- context_one = Liquid::Context.build(
45
- registers: {
46
- file_system: shared_file_system,
47
- }
48
- )
49
- context_two = Liquid::Context.build(
50
- registers: {
51
- file_system: shared_file_system,
52
- }
53
- )
54
-
55
- 2.times do
56
- Liquid::PartialCache.load(
57
- 'my_partial',
58
- context: context_one,
59
- parse_context: parse_context
60
- )
61
- end
62
-
63
- Liquid::PartialCache.load(
64
- 'my_partial',
65
- context: context_two,
66
- parse_context: parse_context
67
- )
68
-
69
- assert_equal(2, shared_file_system.file_read_count)
70
- end
71
-
72
- def test_cache_is_not_broken_when_a_different_parse_context_is_used
73
- file_system = StubFileSystem.new('my_partial' => 'some partial body')
74
- context = Liquid::Context.build(
75
- registers: { file_system: file_system }
76
- )
77
-
78
- Liquid::PartialCache.load(
79
- 'my_partial',
80
- context: context,
81
- parse_context: Liquid::ParseContext.new(my_key: 'value one')
82
- )
83
- Liquid::PartialCache.load(
84
- 'my_partial',
85
- context: context,
86
- parse_context: Liquid::ParseContext.new(my_key: 'value two')
87
- )
88
-
89
- # Technically what we care about is that the file was parsed twice,
90
- # but measuring file reads is an OK proxy for this.
91
- assert_equal(1, file_system.file_read_count)
92
- end
93
-
94
- def test_uses_default_template_factory_when_no_template_factory_found_in_register
95
- context = Liquid::Context.build(
96
- registers: {
97
- file_system: StubFileSystem.new('my_partial' => 'my partial body'),
98
- }
99
- )
100
-
101
- partial = Liquid::PartialCache.load(
102
- 'my_partial',
103
- context: context,
104
- parse_context: Liquid::ParseContext.new
105
- )
106
-
107
- assert_equal('my partial body', partial.render)
108
- end
109
-
110
- def test_uses_template_factory_register_if_present
111
- template_factory = StubTemplateFactory.new
112
- context = Liquid::Context.build(
113
- registers: {
114
- file_system: StubFileSystem.new('my_partial' => 'my partial body'),
115
- template_factory: template_factory,
116
- }
117
- )
118
-
119
- partial = Liquid::PartialCache.load(
120
- 'my_partial',
121
- context: context,
122
- parse_context: Liquid::ParseContext.new
123
- )
124
-
125
- assert_equal('my partial body', partial.render)
126
- assert_equal(1, template_factory.count)
127
- end
128
- end
@@ -1,46 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class RegexpUnitTest < Minitest::Test
6
- include Liquid
7
-
8
- def test_empty
9
- assert_equal([], ''.scan(QuotedFragment))
10
- end
11
-
12
- def test_quote
13
- assert_equal(['"arg 1"'], '"arg 1"'.scan(QuotedFragment))
14
- end
15
-
16
- def test_words
17
- assert_equal(['arg1', 'arg2'], 'arg1 arg2'.scan(QuotedFragment))
18
- end
19
-
20
- def test_tags
21
- assert_equal(['<tr>', '</tr>'], '<tr> </tr>'.scan(QuotedFragment))
22
- assert_equal(['<tr></tr>'], '<tr></tr>'.scan(QuotedFragment))
23
- assert_equal(['<style', 'class="hello">', '</style>'], %(<style class="hello">' </style>).scan(QuotedFragment))
24
- end
25
-
26
- def test_double_quoted_words
27
- assert_equal(['arg1', 'arg2', '"arg 3"'], 'arg1 arg2 "arg 3"'.scan(QuotedFragment))
28
- end
29
-
30
- def test_single_quoted_words
31
- assert_equal(['arg1', 'arg2', "'arg 3'"], 'arg1 arg2 \'arg 3\''.scan(QuotedFragment))
32
- end
33
-
34
- def test_quoted_words_in_the_middle
35
- assert_equal(['arg1', 'arg2', '"arg 3"', 'arg4'], 'arg1 arg2 "arg 3" arg4 '.scan(QuotedFragment))
36
- end
37
-
38
- def test_variable_parser
39
- assert_equal(['var'], 'var'.scan(VariableParser))
40
- assert_equal(['var', 'method'], 'var.method'.scan(VariableParser))
41
- assert_equal(['var', '[method]'], 'var[method]'.scan(VariableParser))
42
- assert_equal(['var', '[method]', '[0]'], 'var[method][0]'.scan(VariableParser))
43
- assert_equal(['var', '["method"]', '[0]'], 'var["method"][0]'.scan(VariableParser))
44
- assert_equal(['var', '[method]', '[0]', 'method'], 'var[method][0].method'.scan(VariableParser))
45
- end
46
- end # RegexpTest
@@ -1,156 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class StaticRegistersUnitTest < Minitest::Test
6
- include Liquid
7
-
8
- def test_set
9
- static_register = StaticRegisters.new(a: 1, b: 2)
10
- static_register[:b] = 22
11
- static_register[:c] = 33
12
-
13
- assert_equal(1, static_register[:a])
14
- assert_equal(22, static_register[:b])
15
- assert_equal(33, static_register[:c])
16
- end
17
-
18
- def test_get_missing_key
19
- static_register = StaticRegisters.new
20
-
21
- assert_nil(static_register[:missing])
22
- end
23
-
24
- def test_delete
25
- static_register = StaticRegisters.new(a: 1, b: 2)
26
- static_register[:b] = 22
27
- static_register[:c] = 33
28
-
29
- assert_nil(static_register.delete(:a))
30
-
31
- assert_equal(22, static_register.delete(:b))
32
-
33
- assert_equal(33, static_register.delete(:c))
34
- assert_nil(static_register[:c])
35
-
36
- assert_nil(static_register.delete(:d))
37
- end
38
-
39
- def test_fetch
40
- static_register = StaticRegisters.new(a: 1, b: 2)
41
- static_register[:b] = 22
42
- static_register[:c] = 33
43
-
44
- assert_equal(1, static_register.fetch(:a))
45
- assert_equal(1, static_register.fetch(:a, "default"))
46
- assert_equal(22, static_register.fetch(:b))
47
- assert_equal(22, static_register.fetch(:b, "default"))
48
- assert_equal(33, static_register.fetch(:c))
49
- assert_equal(33, static_register.fetch(:c, "default"))
50
-
51
- assert_raises(KeyError) do
52
- static_register.fetch(:d)
53
- end
54
- assert_equal("default", static_register.fetch(:d, "default"))
55
-
56
- result = static_register.fetch(:d) { "default" }
57
- assert_equal("default", result)
58
-
59
- result = static_register.fetch(:d, "default 1") { "default 2" }
60
- assert_equal("default 2", result)
61
- end
62
-
63
- def test_key
64
- static_register = StaticRegisters.new(a: 1, b: 2)
65
- static_register[:b] = 22
66
- static_register[:c] = 33
67
-
68
- assert_equal(true, static_register.key?(:a))
69
- assert_equal(true, static_register.key?(:b))
70
- assert_equal(true, static_register.key?(:c))
71
- assert_equal(false, static_register.key?(:d))
72
- end
73
-
74
- def test_static_register_can_be_frozen
75
- static_register = StaticRegisters.new(a: 1)
76
-
77
- static_register.static.freeze
78
-
79
- assert_raises(RuntimeError) do
80
- static_register.static[:a] = "foo"
81
- end
82
-
83
- assert_raises(RuntimeError) do
84
- static_register.static[:b] = "foo"
85
- end
86
-
87
- assert_raises(RuntimeError) do
88
- static_register.static.delete(:a)
89
- end
90
-
91
- assert_raises(RuntimeError) do
92
- static_register.static.delete(:c)
93
- end
94
- end
95
-
96
- def test_new_static_retains_static
97
- static_register = StaticRegisters.new(a: 1, b: 2)
98
- static_register[:b] = 22
99
- static_register[:c] = 33
100
-
101
- new_static_register = StaticRegisters.new(static_register)
102
- new_static_register[:b] = 222
103
-
104
- newest_static_register = StaticRegisters.new(new_static_register)
105
- newest_static_register[:c] = 333
106
-
107
- assert_equal(1, static_register[:a])
108
- assert_equal(22, static_register[:b])
109
- assert_equal(33, static_register[:c])
110
-
111
- assert_equal(1, new_static_register[:a])
112
- assert_equal(222, new_static_register[:b])
113
- assert_nil(new_static_register[:c])
114
-
115
- assert_equal(1, newest_static_register[:a])
116
- assert_equal(2, newest_static_register[:b])
117
- assert_equal(333, newest_static_register[:c])
118
- end
119
-
120
- def test_multiple_instances_are_unique
121
- static_register_1 = StaticRegisters.new(a: 1, b: 2)
122
- static_register_1[:b] = 22
123
- static_register_1[:c] = 33
124
-
125
- static_register_2 = StaticRegisters.new(a: 10, b: 20)
126
- static_register_2[:b] = 220
127
- static_register_2[:c] = 330
128
-
129
- assert_equal({ a: 1, b: 2 }, static_register_1.static)
130
- assert_equal(1, static_register_1[:a])
131
- assert_equal(22, static_register_1[:b])
132
- assert_equal(33, static_register_1[:c])
133
-
134
- assert_equal({ a: 10, b: 20 }, static_register_2.static)
135
- assert_equal(10, static_register_2[:a])
136
- assert_equal(220, static_register_2[:b])
137
- assert_equal(330, static_register_2[:c])
138
- end
139
-
140
- def test_initialization_reused_static_same_memory_object
141
- static_register_1 = StaticRegisters.new(a: 1, b: 2)
142
- static_register_1[:b] = 22
143
- static_register_1[:c] = 33
144
-
145
- static_register_2 = StaticRegisters.new(static_register_1)
146
-
147
- assert_equal(1, static_register_2[:a])
148
- assert_equal(2, static_register_2[:b])
149
- assert_nil(static_register_2[:c])
150
-
151
- static_register_1.static[:b] = 222
152
- static_register_1.static[:c] = 333
153
-
154
- assert_same(static_register_1.static, static_register_2.static)
155
- end
156
- end