forked from nesquena/rabl
-
Notifications
You must be signed in to change notification settings - Fork 11
/
builder_test.rb
157 lines (120 loc) · 5.57 KB
/
builder_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
require File.expand_path('../teststrap', __FILE__)
require File.expand_path('../models/user', __FILE__)
context "Rabl::Builder" do
helper(:builder) { |opt| Rabl::Builder.new(opt) }
helper(:build_hash) { |obj, opt| builder(opt).build(obj) }
setup do
@users = [User.new, User.new]
@user = User.new
builder({})
end
context "#initialize" do
asserts_topic.assigns :options
end
context "#build" do
setup { b = builder({}); b.build(User.new); b }
asserts_topic.assigns :_object
asserts_topic.assigns :_result
end
context "#to_hash" do
context "when given a simple object" do
setup { builder({ :attributes => { :name => :name } }) }
asserts "that the object is set properly" do
topic.build(User.new, :root_name => "user")
end.equivalent_to({ "user" => { :name => "rabl" } })
end
context "when given an object alias" do
setup { builder({ :attributes => { :name => :name } }) }
asserts "that the object is set properly" do
topic.build(User.new, :root_name => "person")
end.equivalent_to({ "person" => { :name => "rabl" } })
end
context "when specified with no root" do
setup { builder({ :attributes => { :name => :name } }) }
asserts "that the object is set properly" do
topic.build(User.new, :root => false)
end.equivalent_to({ :name => "rabl" })
end
end
context "#attribute" do
asserts "that the node" do
build_hash @user, :attributes => { :name => :name, :city => :city }
end.equivalent_to({:name => 'rabl', :city => 'irvine'})
asserts "that with a non-existent attribute the node" do
build_hash @user, :attributes => { :fake => :fake }
end.equals({})
end
context "#node" do
asserts "that it has node :foo" do
build_hash @user, :node => [{ :name => :foo, :options => {}, :block => lambda { |u| "bar" } }]
end.equivalent_to({:foo => 'bar'})
asserts "that using object it has node :boo" do
build_hash @user, :node => [
{ :name => :foo, :options => {}, :block => lambda { |u| "bar" } },
{ :name => :baz, :options => {}, :block => lambda { |u| u.city } }
]
end.equivalent_to({:foo => 'bar', :baz => 'irvine'})
end
context "#child" do
asserts "that it generates if no data present" do
builder(:child => []).build(@user)
end.equals({})
asserts "that it generates with a hash" do
b = builder(:child => [ { :data => { @user => :user }, :options => { }, :block => lambda { |u| attribute :name } } ])
b.build(@user)
end.equivalent_to({ :user => { :name => "rabl" } })
asserts "that it generates with a hash alias" do
b = builder :child => [{ :data => { @user => :person }, :options => {}, :block => lambda { |u| attribute :name } }]
b.build(@user)
end.equivalent_to({ :person => { :name => "rabl" } })
asserts "that it generates with an object" do
b = builder :child => [{ :data => @user, :options => {}, :block => lambda { |u| attribute :name } }]
mock(b).data_name(@user) { :user }
mock(b).object_to_hash(@user, { :root => false }).returns('xyz').subject
b.build(@user)
end.equivalent_to({ :user => 'xyz'})
asserts "that it generates with an collection and child_root" do
b = builder :child => [{ :data => @users, :options => {}, :block => lambda { |u| attribute :name } }], :child_root => true
mock(b).data_name(@users) { :users }
mock(b).object_to_hash(@users, { :root => true, :child_root => true }).returns('xyz').subject
b.build(@user)
end.equivalent_to({ :users => 'xyz'})
asserts "that it generates with an collection and no child root" do
b = builder :child => [{ :data => @users, :options => {}, :block => lambda { |u| attribute :name } }], :child_root => false
mock(b).data_name(@users) { :users }
mock(b).object_to_hash(@users, { :root => false, :child_root => false }).returns('xyz').subject
b.build(@user)
end.equivalent_to({ :users => 'xyz'})
end
context "#glue" do
asserts "that it generates if no data present" do
builder(:glue => []).build(@user)
end.equals({})
asserts "that it generates the glue attributes" do
b = builder :glue => [{ :data => @user, :block => lambda { |u| attribute :name }}]
mock(b).object_to_hash(@user, { :root => false }).returns({:user => 'xyz'}).subject
b.build(@user)
end.equivalent_to({ :user => 'xyz' })
asserts "that it appends the glue attributes to result" do
b = builder :glue => [{ :data => @user, :block => lambda { |u| attribute :name => :user_name }}]
b.build(@user)
end.equivalent_to({ :user_name => 'rabl' })
asserts "that it does not generate new attributes if no glue attributes are present" do
b = builder :glue => [{ :data => @user, :block => lambda { |u| attribute :name }}]
mock(b).object_to_hash(@user,{ :root => false }).returns({}).subject
b.build(@user)
end.equals({})
end
context "#extend" do
asserts "that it does not genereate if no data is present" do
b = builder :extends => [{ :file => 'users/show', :options => {}, :block => lambda { |u| attribute :name }}]
mock(b).partial('users/show',{ :object => @user}).returns({}).subject
b.build(@user)
end.equals({})
asserts "that it generates if data is present" do
b = builder :extends => [{ :file => 'users/show', :options => {}, :block => lambda { |u| attribute :name }}]
mock(b).partial('users/show', { :object => @user }).returns({:user => 'xyz'}).subject
b.build(@user)
end.equivalent_to({:user => 'xyz'})
end
end