-
Notifications
You must be signed in to change notification settings - Fork 334
/
msgpack_engine_test.rb
312 lines (278 loc) · 9.72 KB
/
msgpack_engine_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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# encoding: iso-8859-1
require File.expand_path('../teststrap', __FILE__)
require 'rabl/template'
context "Rabl::Engine" do
helper(:rabl) { |t| RablTemplate.new("code", :format => 'msgpack') { t } }
context "with msgpack defaults" do
setup do
Rabl.configure do |config|
# Comment this line out because include_msgpack_root is default.
#config.include_msgpack_root = true
end
end
context "#object" do
asserts "that it sets data source" do
template = rabl %q{
object @user
}
scope = Object.new
scope.instance_variable_set :@user, User.new
char_split template.render(scope)
end.equals char_split("\x81\xA4user\x80")
asserts "that it can set root node" do
template = rabl %q{
object @user => :person
}
scope = Object.new
scope.instance_variable_set :@user, User.new
char_split template.render(scope)
end.equals char_split("\x81\xA6person\x80")
end
context "#collection" do
asserts "that it sets object to be casted as a simple array" do
template = rabl %{
collection @users
}
scope = Object.new
scope.instance_variable_set :@users, [User.new, User.new]
char_split template.render(scope)
end.equals char_split("\x92\x81\xA4user\x80\x81\xA4user\x80")
asserts "that it sets root node for objects" do
template = rabl %{
collection @users => :person
}
scope = Object.new
scope.instance_variable_set :@users, [User.new, User.new]
char_split template.render(scope)
end.equals char_split("\x81\xA6person\x92\x81\xA6person\x80\x81\xA6person\x80")
end
context "#attribute" do
asserts "that it adds an attribute or method to be included in output" do
template = rabl %{
object @user
attribute :name
}
scope = Object.new
scope.instance_variable_set :@user, User.new(:name => 'irvine')
char_split template.render(scope)
end.equals char_split("\x81\xA4user\x81\xA4name\xA6irvine")
asserts "that it can add attribute under a different key name through :as" do
template = rabl %{
object @user
attribute :name, :as => 'city'
}
scope = Object.new
scope.instance_variable_set :@user, User.new(:name => 'irvine')
char_split template.render(scope)
end.equals char_split("\x81\xA4user\x81\xA4city\xA6irvine")
asserts "that it can add attribute under a different key name through hash" do
template = rabl %{
object @user
attribute :name => :city
}
scope = Object.new
scope.instance_variable_set :@user, User.new(:name => 'irvine')
char_split template.render(scope)
end.equals char_split("\x81\xA4user\x81\xA4city\xA6irvine")
end
context "#code" do
asserts "that it can create an arbitrary code node" do
template = rabl %{
code(:foo) { 'bar' }
}
char_split template.render(Object.new)
end.equals char_split("\x81\xA3foo\xA3bar")
asserts "that it can be passed conditionals" do
template = rabl %{
code(:foo, :if => lambda { |i| false }) { 'bar' }
}
char_split template.render(Object.new)
end.equals char_split("\x80")
end
context "#child" do
asserts "that it can create a child node" do
template = rabl %{
object @user
attribute :name
child(@user) { attribute :city }
}
scope = Object.new
scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA')
char_split template.render(scope)
end.equals char_split("\x81\xA4user\x82\xA4name\xA3leo\xA4user\x81\xA4city\xA2LA")
asserts "that it can create a child node with different key" do
template = rabl %{
object @user
attribute :name
child(@user => :person) { attribute :city }
}
scope = Object.new
scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA')
char_split template.render(scope)
end.equals char_split("\x81\xA4user\x82\xA4name\xA3leo\xA6person\x81\xA4city\xA2LA")
end
context "#glue" do
asserts "that it glues data from a child node" do
template = rabl %{
object @user
attribute :name
glue(@user) { attribute :city }
glue(@user) { attribute :age }
}
scope = Object.new
scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA', :age => 12)
char_split template.render(scope)
end.equals char_split("\x81\xA4user\x83\xA4name\xA3leo\xA4city\xA2LA\xA3age\f")
end
teardown do
Rabl.reset_configuration!
end
end
context "with msgpack_engine" do
setup do
class CustomEncodeEngine
def self.pack string
42
end
end
Rabl.configure do |config|
config.msgpack_engine = CustomEncodeEngine
end
end
asserts 'that it returns process by custom to_json' do
template = rabl %q{
object @user
}
scope = Object.new
scope.instance_variable_set :@user, User.new
template.render(scope)
end.equals 42
teardown do
Rabl.reset_configuration!
end
end
context "without msgpack root" do
setup do
Rabl.configure do |config|
config.include_msgpack_root = false
end
end
context "#object" do
asserts "that it sets data source" do
template = rabl %q{
object @user
}
scope = Object.new
scope.instance_variable_set :@user, User.new
char_split template.render(scope)
end.equals char_split("\x80")
asserts "that it can set root node" do
template = rabl %q{
object @user => :person
}
scope = Object.new
scope.instance_variable_set :@user, User.new
char_split template.render(scope)
end.equals char_split("\x80")
end
context "#collection" do
asserts "that it sets object to be casted as a simple array" do
template = rabl %{
collection @users
}
scope = Object.new
scope.instance_variable_set :@users, [User.new, User.new]
char_split template.render(scope)
end.equals char_split("\x92\x80\x80")
asserts "that it sets root node for objects" do
template = rabl %{
collection @users => :person
}
scope = Object.new
scope.instance_variable_set :@users, [User.new, User.new]
char_split template.render(scope)
end.equals char_split("\x81\xA6person\x92\x80\x80")
end
context "#attribute" do
asserts "that it adds an attribute or method to be included in output" do
template = rabl %{
object @user
attribute :name
}
scope = Object.new
scope.instance_variable_set :@user, User.new(:name => 'irvine')
char_split template.render(scope)
end.equals char_split("\x81\xA4name\xA6irvine")
asserts "that it can add attribute under a different key name through :as" do
template = rabl %{
object @user
attribute :name, :as => 'city'
}
scope = Object.new
scope.instance_variable_set :@user, User.new(:name => 'irvine')
char_split template.render(scope)
end.equals char_split("\x81\xA4city\xA6irvine")
asserts "that it can add attribute under a different key name through hash" do
template = rabl %{
object @user
attribute :name => :city
}
scope = Object.new
scope.instance_variable_set :@user, User.new(:name => 'irvine')
char_split template.render(scope)
end.equals char_split("\x81\xA4city\xA6irvine")
end
context "#code" do
asserts "that it can create an arbitrary code node" do
template = rabl %{
code(:foo) { 'bar' }
}
char_split template.render(Object.new)
end.equals char_split("\x81\xA3foo\xA3bar")
asserts "that it can be passed conditionals" do
template = rabl %{
code(:foo, :if => lambda { |i| false }) { 'bar' }
}
char_split template.render(Object.new)
end.equals char_split("\x80")
end
context "#child" do
asserts "that it can create a child node" do
template = rabl %{
object @user
attribute :name
child(@user) { attribute :city }
}
scope = Object.new
scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA')
char_split template.render(scope)
end.equals char_split("\x82\xA4name\xA3leo\xA4user\x81\xA4city\xA2LA")
asserts "that it can create a child node with different key" do
template = rabl %{
object @user
attribute :name
child(@user => :person) { attribute :city }
}
scope = Object.new
scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA')
char_split template.render(scope)
end.equals char_split("\x82\xA4name\xA3leo\xA6person\x81\xA4city\xA2LA")
end
context "#glue" do
asserts "that it glues data from a child node" do
template = rabl %{
object @user
attribute :name
glue(@user) { attribute :city }
glue(@user) { attribute :age }
}
scope = Object.new
scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA', :age => 12)
char_split template.render(scope)
end.equals char_split("\x83\xA4name\xA3leo\xA4city\xA2LA\xA3age\f")
end
teardown do
Rabl.reset_configuration!
end
end
end