curlybars 1.8.0 → 1.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/lib/curlybars/error/lex.rb +1 -1
  3. data/lib/curlybars/error/parse.rb +1 -1
  4. data/lib/curlybars/node/path.rb +11 -13
  5. data/lib/curlybars/presenter.rb +2 -2
  6. data/lib/curlybars/template_handler.rb +3 -11
  7. data/lib/curlybars/version.rb +1 -1
  8. metadata +14 -90
  9. data/spec/acceptance/application_layout_spec.rb +0 -60
  10. data/spec/acceptance/collection_blocks_spec.rb +0 -28
  11. data/spec/acceptance/global_helper_spec.rb +0 -25
  12. data/spec/curlybars/configuration_spec.rb +0 -57
  13. data/spec/curlybars/error/base_spec.rb +0 -41
  14. data/spec/curlybars/error/compile_spec.rb +0 -19
  15. data/spec/curlybars/error/lex_spec.rb +0 -25
  16. data/spec/curlybars/error/parse_spec.rb +0 -74
  17. data/spec/curlybars/error/render_spec.rb +0 -19
  18. data/spec/curlybars/error/validate_spec.rb +0 -19
  19. data/spec/curlybars/lexer_spec.rb +0 -490
  20. data/spec/curlybars/method_whitelist_spec.rb +0 -299
  21. data/spec/curlybars/processor/tilde_spec.rb +0 -60
  22. data/spec/curlybars/rendering_support_spec.rb +0 -421
  23. data/spec/curlybars/safe_buffer_spec.rb +0 -46
  24. data/spec/curlybars/template_handler_spec.rb +0 -225
  25. data/spec/integration/cache_spec.rb +0 -126
  26. data/spec/integration/comment_spec.rb +0 -60
  27. data/spec/integration/exception_spec.rb +0 -31
  28. data/spec/integration/node/block_helper_else_spec.rb +0 -420
  29. data/spec/integration/node/each_else_spec.rb +0 -408
  30. data/spec/integration/node/each_spec.rb +0 -289
  31. data/spec/integration/node/escape_spec.rb +0 -27
  32. data/spec/integration/node/helper_spec.rb +0 -186
  33. data/spec/integration/node/if_else_spec.rb +0 -170
  34. data/spec/integration/node/if_spec.rb +0 -153
  35. data/spec/integration/node/output_spec.rb +0 -66
  36. data/spec/integration/node/partial_spec.rb +0 -64
  37. data/spec/integration/node/path_spec.rb +0 -296
  38. data/spec/integration/node/root_spec.rb +0 -13
  39. data/spec/integration/node/sub_expression_spec.rb +0 -426
  40. data/spec/integration/node/template_spec.rb +0 -84
  41. data/spec/integration/node/unless_else_spec.rb +0 -139
  42. data/spec/integration/node/unless_spec.rb +0 -128
  43. data/spec/integration/node/with_spec.rb +0 -178
  44. data/spec/integration/processor/tilde_spec.rb +0 -38
  45. data/spec/integration/processors_spec.rb +0 -29
  46. data/spec/integration/visitor_spec.rb +0 -154
@@ -1,299 +0,0 @@
1
- describe Curlybars::MethodWhitelist do
2
- let(:dummy_class) do
3
- Class.new do
4
- extend Curlybars::MethodWhitelist
5
-
6
- # A method available in the context
7
- def foo?
8
- true
9
- end
10
-
11
- def qux?
12
- false
13
- end
14
- end
15
- end
16
-
17
- let(:validation_context_class) do
18
- Class.new do
19
- attr_accessor :invocation_count
20
-
21
- def foo?
22
- true
23
- end
24
-
25
- def qux?
26
- false
27
- end
28
- end
29
- end
30
-
31
- describe "#allowed_methods" do
32
- it "returns an empty array as default" do
33
- expect(dummy_class.new.allowed_methods).to eq([])
34
- end
35
- end
36
-
37
- describe ".allow_methods" do
38
- before do
39
- link_presenter = Class.new { extend Curlybars::MethodWhitelist }
40
- article_presenter = Class.new { extend Curlybars::MethodWhitelist }
41
-
42
- dummy_class.class_eval do
43
- allow_methods :cook, link: link_presenter, article: article_presenter,
44
- translate_article: [:helper, article_presenter],
45
- reverse_articles: [:helper, [article_presenter]],
46
- translate: [:helper, Curlybars::Generic],
47
- slice: [:helper, [Curlybars::Generic]]
48
- end
49
- end
50
-
51
- it "sets the allowed methods" do
52
- expect(dummy_class.new.allowed_methods).to eq([:cook, :link, :article, :translate_article, :reverse_articles, :translate, :slice])
53
- end
54
-
55
- it "supports adding more methods for validation" do
56
- dummy_class.class_eval do
57
- allow_methods do |context, allow_method|
58
- if context.foo?
59
- allow_method.call(:bar)
60
- end
61
-
62
- if context.qux?
63
- allow_method.call(:quux)
64
- end
65
- end
66
- end
67
-
68
- aggregate_failures "test both allowed_methods and allows_method?" do
69
- expect(dummy_class.new.allowed_methods).to eq([:bar])
70
- expect(dummy_class.new.allows_method?(:bar)).to eq(true)
71
- end
72
- end
73
-
74
- it "raises when collection is not of presenters" do
75
- expect do
76
- dummy_class.class_eval { allow_methods :cook, links: ["foobar"] }
77
- end.to raise_error(RuntimeError)
78
- end
79
-
80
- it "raises when collection cardinality is greater than one" do
81
- stub_const("OnePresenter", Class.new { extend Curlybars::MethodWhitelist })
82
- stub_const("OtherPresenter", Class.new { extend Curlybars::MethodWhitelist })
83
-
84
- expect do
85
- dummy_class.class_eval { allow_methods :cook, links: [OnePresenter, OtherPresenter] }
86
- end.to raise_error(RuntimeError)
87
- end
88
- end
89
-
90
- describe "inheritance and composition" do
91
- let(:base_presenter) do
92
- stub_const("LinkPresenter", Class.new)
93
-
94
- Class.new do
95
- extend Curlybars::MethodWhitelist
96
- allow_methods :cook, link: LinkPresenter
97
- end
98
- end
99
-
100
- let(:helpers) do
101
- Module.new do
102
- extend Curlybars::MethodWhitelist
103
- allow_methods :form
104
- end
105
- end
106
-
107
- let(:post_presenter) do
108
- Class.new(base_presenter) do
109
- extend Curlybars::MethodWhitelist
110
- include Helpers
111
- allow_methods :wave
112
- end
113
- end
114
-
115
- before do
116
- stub_const("Helpers", helpers)
117
- end
118
-
119
- it "allows methods from inheritance and composition" do
120
- expect(post_presenter.new.allowed_methods).to eq([:cook, :link, :form, :wave])
121
- end
122
-
123
- it "returns a dependency_tree with inheritance and composition" do
124
- expect(post_presenter.dependency_tree).
125
- to eq(
126
- cook: nil,
127
- link: LinkPresenter,
128
- form: nil,
129
- wave: nil
130
- )
131
- end
132
-
133
- context "with context dependent methods" do
134
- let(:base_presenter) do
135
- stub_const("LinkPresenter", Class.new)
136
-
137
- Class.new do
138
- extend Curlybars::MethodWhitelist
139
- attr_accessor :invocation_count
140
-
141
- allow_methods :cook, link: LinkPresenter do |context, allow_method|
142
- if context.foo?
143
- allow_method.call(:bar)
144
- end
145
-
146
- context.invocation_count ||= 0
147
- context.invocation_count += 1
148
- end
149
-
150
- def foo?
151
- true
152
- end
153
- end
154
- end
155
-
156
- let(:helpers) do
157
- Module.new do
158
- extend Curlybars::MethodWhitelist
159
- allow_methods :form do |context, allow_method|
160
- if context.foo?
161
- allow_method.call(foo_bar: :helper)
162
- end
163
- end
164
-
165
- def foo?
166
- true
167
- end
168
- end
169
- end
170
-
171
- let(:post_presenter) do
172
- Class.new(base_presenter) do
173
- extend Curlybars::MethodWhitelist
174
- include Helpers
175
- allow_methods :wave
176
- end
177
- end
178
-
179
- before do
180
- stub_const("Helpers", helpers)
181
- end
182
-
183
- it "allows context methods from inheritance and composition" do
184
- expect(post_presenter.new.allowed_methods).to eq([:cook, :link, :bar, :form, :foo_bar, :wave])
185
- end
186
-
187
- it "only invokes the context block once" do
188
- presenter = post_presenter.new
189
-
190
- 10.times { presenter.allowed_methods }
191
-
192
- expect(presenter.invocation_count).to eq(1)
193
- end
194
-
195
- it "returns a dependency_tree with inheritance and composition with context" do
196
- expect(post_presenter.dependency_tree(validation_context_class.new)).
197
- to eq(
198
- cook: nil,
199
- link: LinkPresenter,
200
- form: nil,
201
- wave: nil,
202
- bar: nil,
203
- foo_bar: :helper
204
- )
205
- end
206
- end
207
- end
208
-
209
- describe ".methods_schema" do
210
- it "setups a schema propagating nil" do
211
- stub_const("LinkPresenter", Class.new { extend Curlybars::MethodWhitelist })
212
- dummy_class.class_eval { allow_methods :cook }
213
-
214
- expect(dummy_class.methods_schema).to eq(cook: nil)
215
- end
216
-
217
- it "setups a schema any random type" do
218
- stub_const("LinkPresenter", Class.new { extend Curlybars::MethodWhitelist })
219
- dummy_class.class_eval { allow_methods something: :foobar }
220
-
221
- expect(dummy_class.methods_schema).to eq(something: :foobar)
222
- end
223
-
224
- it "setups a schema propagating the return type of the method" do
225
- stub_const("ArticlePresenter", Class.new { extend Curlybars::MethodWhitelist })
226
- dummy_class.class_eval { allow_methods article: ArticlePresenter }
227
-
228
- expect(dummy_class.methods_schema).to eq(article: ArticlePresenter)
229
- end
230
-
231
- it "setups a schema propagating a collection" do
232
- stub_const("LinkPresenter", Class.new { extend Curlybars::MethodWhitelist })
233
- dummy_class.class_eval { allow_methods links: [LinkPresenter] }
234
-
235
- expect(dummy_class.methods_schema).to eq(links: [LinkPresenter])
236
- end
237
-
238
- it "supports procs with context in schema" do
239
- dummy_class.class_eval { allow_methods settings: ->(context) { context.foo? ? Hash[:background_color, nil] : nil } }
240
-
241
- expect(dummy_class.methods_schema(validation_context_class.new)).to eq(settings: { background_color: nil })
242
- end
243
-
244
- it "supports context methods" do
245
- dummy_class.class_eval do
246
- allow_methods do |context, allow_method|
247
- if context.foo?
248
- allow_method.call(:bar)
249
- end
250
- end
251
- end
252
-
253
- expect(dummy_class.methods_schema(validation_context_class.new)).to eq(bar: nil)
254
- end
255
- end
256
-
257
- describe ".dependency_tree" do
258
- it "returns a dependencies tree" do
259
- link_presenter = Class.new do
260
- extend Curlybars::MethodWhitelist
261
- allow_methods :url
262
- end
263
-
264
- article_presenter = Class.new do
265
- extend Curlybars::MethodWhitelist
266
- allow_methods :title, :body
267
- end
268
-
269
- stub_const("ArticlePresenter", article_presenter)
270
- stub_const("LinkPresenter", link_presenter)
271
-
272
- dummy_class.class_eval do
273
- allow_methods links: [LinkPresenter], article: ArticlePresenter
274
- end
275
-
276
- expect(dummy_class.dependency_tree).
277
- to eq(
278
- links: [{ url: nil }],
279
- article: {
280
- title: nil,
281
- body: nil
282
- }
283
- )
284
- end
285
-
286
- it "propagates arguments" do
287
- dummy_class.class_eval do
288
- allow_methods label: ->(label) { Hash[label, nil] }
289
- end
290
-
291
- expect(dummy_class.dependency_tree(:some_label)).
292
- to eq(
293
- label: {
294
- some_label: nil
295
- }
296
- )
297
- end
298
- end
299
- end
@@ -1,60 +0,0 @@
1
- describe Curlybars::Processor::Tilde do
2
- let(:tilde_start_token) { double(:tilde_start, type: :TILDE_START, value: nil, position: nil) }
3
- let(:tilde_end_token) { double(:tilde_end, type: :TILDE_END, value: nil, position: nil) }
4
- let(:start_token) { double(:start, type: :START, value: nil, position: nil) }
5
- let(:end_token) { double(:end, type: :END, value: nil, position: nil) }
6
-
7
- describe ":TILDE_START" do
8
- it "trims the previous text token" do
9
- tokens = [
10
- text_token("text \t\r\n"),
11
- tilde_start_token
12
- ]
13
- Curlybars::Processor::Tilde.process!(tokens, 'identifier')
14
-
15
- expect(tokens.first.value).to eq 'text'
16
- end
17
-
18
- it "doesn't trim the previous text token when not right before" do
19
- tokens = [
20
- text_token("text \t\r\n"),
21
- start_token,
22
- end_token,
23
- tilde_start_token
24
- ]
25
- Curlybars::Processor::Tilde.process!(tokens, 'identifier')
26
-
27
- expect(tokens.first.value).to eq "text \t\r\n"
28
- end
29
- end
30
-
31
- describe ":TILDE_END" do
32
- it "trims the following text token" do
33
- tokens = [
34
- tilde_end_token,
35
- text_token("\t\r\n text")
36
- ]
37
- Curlybars::Processor::Tilde.process!(tokens, 'identifier')
38
-
39
- expect(tokens.last.value).to eq 'text'
40
- end
41
-
42
- it "doesn't trim the following text token when not right after" do
43
- tokens = [
44
- tilde_end_token,
45
- start_token,
46
- end_token,
47
- text_token("\t\r\n text")
48
- ]
49
- Curlybars::Processor::Tilde.process!(tokens, 'identifier')
50
-
51
- expect(tokens.last.value).to eq "\t\r\n text"
52
- end
53
- end
54
-
55
- private
56
-
57
- def text_token(value)
58
- double(:text, type: :TEXT, value: value, position: nil)
59
- end
60
- end