Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 232 lines (183 sloc) 5.514 kb
ec90c98 Sam Ruby update to the latest rails, minitest
authored
1 require 'minitest/autorun'
3a585b9 Sam Ruby Incorporate functionality inspired by jbuilder
authored
2 require 'wunderbar'
3
4 # these tests were "borrowed"/adapted from jbuilder so as to demonstrate
5 # functional parity:
6 # https://github.com/rails/jbuilder/blob/master/test/jbuilder_test.rb
7
ec90c98 Sam Ruby update to the latest rails, minitest
authored
8 class JbuilderTest < MiniTest::Test
3a585b9 Sam Ruby Incorporate functionality inspired by jbuilder
authored
9 def setup
1969977 Sam Ruby consistency in setting params
authored
10 @j = Wunderbar::JsonBuilder.new(Struct.new(:params).new({}))
3a585b9 Sam Ruby Incorporate functionality inspired by jbuilder
authored
11 end
12
13 def test_single_key
14 parsed = @j.encode do
15 _content "hello"
16 end
17
18 assert_equal "hello", parsed["content"]
19 end
20
21 def test_single_key_with_false_value
22 parsed = @j.encode do
23 _content false
24 end
25
26 assert_equal false, parsed["content"]
27 end
28
29 def test_single_key_with_nil_value
30 parsed = @j.encode do
31 _content nil
32 end
33
34 assert parsed.has_key?("content")
35 assert_equal nil, parsed["content"]
36 end
37
38 def test_multiple_keys
39 parsed = @j.encode do
40 _title "hello"
41 _content "world"
42 end
43
44 assert_equal "hello", parsed["title"]
45 assert_equal "world", parsed["content"]
46 end
47
48 def test_extracting_from_object
49 person = Struct.new(:name, :age).new("David", 32)
50
51 parsed = @j.encode do
52 _ person, :name, :age
53 end
54
55 assert_equal "David", parsed["name"]
56 assert_equal 32, parsed["age"]
57 end
58
59 def test_nesting_single_child_with_block
60 parsed = @j.encode do
61 _author do
62 _name "David"
63 _age 32
64 end
65 end
66
67 assert_equal "David", parsed["author"]["name"]
68 assert_equal 32, parsed["author"]["age"]
69 end
70
71 def test_nesting_multiple_children_with_block
72 parsed = @j.encode do
73 _comments do
5d1146b Sam Ruby Make it easier to build JSON arrays
authored
74 _ { _content "hello" }
75 _ { _content "world" }
3a585b9 Sam Ruby Incorporate functionality inspired by jbuilder
authored
76 end
77 end
78
5d1146b Sam Ruby Make it easier to build JSON arrays
authored
79 assert_equal "hello", parsed["comments"][0]["content"]
80 assert_equal "world", parsed["comments"][1]["content"]
3a585b9 Sam Ruby Incorporate functionality inspired by jbuilder
authored
81 end
82
83 def test_nesting_single_child_with_inline_extract
84 person = Class.new do
85 attr_reader :name, :age
86
87 def initialize(name, age)
88 @name, @age = name, age
89 end
90 end.new("David", 32)
91
92 parsed = @j.encode do
93 _author person, :name, :age
94 end
95
96 assert_equal "David", parsed["author"]["name"]
97 assert_equal 32, parsed["author"]["age"]
98 end
99
100 def test_nesting_multiple_children_from_array
5d1146b Sam Ruby Make it easier to build JSON arrays
authored
101 comments = [ Struct.new(:content, :id).new("hello", 1),
102 Struct.new(:content, :id).new("world", 2) ]
3a585b9 Sam Ruby Incorporate functionality inspired by jbuilder
authored
103
104 parsed = @j.encode do
105 _comments comments, :content
106 end
107
108 assert_equal ["content"], parsed["comments"].first.keys
5d1146b Sam Ruby Make it easier to build JSON arrays
authored
109 assert_equal "hello", parsed["comments"][0]["content"]
110 assert_equal "world", parsed["comments"][1]["content"]
3a585b9 Sam Ruby Incorporate functionality inspired by jbuilder
authored
111 end
112
113 def test_nesting_multiple_children_from_array_when_child_array_is_empty
114 comments = []
115
116 parsed = @j.encode do
117 _name "Parent"
118 _comments comments, :content
119 end
120
121 assert_equal "Parent", parsed["name"]
122 assert_equal [], parsed["comments"]
123 end
124
125 def test_nesting_multiple_children_from_array_with_inline_loop
5d1146b Sam Ruby Make it easier to build JSON arrays
authored
126 comments = [ Struct.new(:content, :id).new("hello", 1),
127 Struct.new(:content, :id).new("world", 2) ]
3a585b9 Sam Ruby Incorporate functionality inspired by jbuilder
authored
128
129 parsed = @j.encode do
130 _comments comments do |comment|
131 _content comment.content
132 end
133 end
134
135 assert_equal ["content"], parsed["comments"].first.keys
5d1146b Sam Ruby Make it easier to build JSON arrays
authored
136 assert_equal "hello", parsed["comments"][0]["content"]
137 assert_equal "world", parsed["comments"][1]["content"]
3a585b9 Sam Ruby Incorporate functionality inspired by jbuilder
authored
138 end
139
140 def test_nesting_multiple_children_from_array_with_inline_loop_on_root
141 comments = [ Struct.new(:content, :id).new("hello", 1), Struct.new(:content, :id).new("world", 2) ]
142
143 parsed = @j.encode do
144 _ comments do |comment|
145 _content comment.content
146 end
147 end
148
5d1146b Sam Ruby Make it easier to build JSON arrays
authored
149 assert_equal "hello", parsed[0]["content"]
150 assert_equal "world", parsed[1]["content"]
3a585b9 Sam Ruby Incorporate functionality inspired by jbuilder
authored
151 end
152
153 def test_array_nested_inside_nested_hash
154 parsed = @j.encode do
155 _author do
156 _name "David"
157 _age 32
158
159 _comments do
5d1146b Sam Ruby Make it easier to build JSON arrays
authored
160 _ { _content "hello" }
161 _ { _content "world" }
3a585b9 Sam Ruby Incorporate functionality inspired by jbuilder
authored
162 end
163 end
164 end
165
5d1146b Sam Ruby Make it easier to build JSON arrays
authored
166 assert_equal "hello", parsed["author"]["comments"][0]["content"]
167 assert_equal "world", parsed["author"]["comments"][1]["content"]
3a585b9 Sam Ruby Incorporate functionality inspired by jbuilder
authored
168 end
169
170 def test_array_nested_inside_array
171 parsed = @j.encode do
172 _comments do
5d1146b Sam Ruby Make it easier to build JSON arrays
authored
173 _ do
3a585b9 Sam Ruby Incorporate functionality inspired by jbuilder
authored
174 _authors do
5d1146b Sam Ruby Make it easier to build JSON arrays
authored
175 _ do
3a585b9 Sam Ruby Incorporate functionality inspired by jbuilder
authored
176 _name "david"
177 end
178 end
179 end
180 end
181 end
182
183 assert_equal "david", parsed["comments"].first["authors"].first["name"]
184 end
185
186 def test_top_level_array
5d1146b Sam Ruby Make it easier to build JSON arrays
authored
187 comments = [ Struct.new(:content, :id).new("hello", 1),
188 Struct.new(:content, :id).new("world", 2) ]
3a585b9 Sam Ruby Incorporate functionality inspired by jbuilder
authored
189
190 parsed = @j.encode do
191 _ comments do |comment|
192 _content comment.content
193 end
194 end
195
5d1146b Sam Ruby Make it easier to build JSON arrays
authored
196 assert_equal "hello", parsed[0]["content"]
197 assert_equal "world", parsed[1]["content"]
3a585b9 Sam Ruby Incorporate functionality inspired by jbuilder
authored
198 end
199
200 def test_empty_top_level_array
201 comments = []
202
203 parsed = @j.encode do
204 _ comments do |comment|
205 _content comment.content
206 end
207 end
208
209 assert_equal [], parsed
210 end
211
212 def test_dynamically_set_a_key_value
213 parsed = @j.encode do
5d1146b Sam Ruby Make it easier to build JSON arrays
authored
214 _["each"] = "stuff"
3a585b9 Sam Ruby Incorporate functionality inspired by jbuilder
authored
215 end
216
217 assert_equal "stuff", parsed["each"]
218 end
219
220 def test_dynamically_set_a_key_nested_child_with_block
221 parsed = @j.encode do
222 _ :author do
223 _name "David"
224 _age 32
225 end
226 end
227
228 assert_equal "David", parsed["author"]["name"]
229 assert_equal 32, parsed["author"]["age"]
230 end
231 end
Something went wrong with that request. Please try again.