Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 231 lines (183 sloc) 5.514 kb
ec90c98e »
2014-04-29 update to the latest rails, minitest
1 require 'minitest/autorun'
3a585b96 »
2012-03-31 Incorporate functionality inspired by jbuilder
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
ec90c98e »
2014-04-29 update to the latest rails, minitest
8 class JbuilderTest < MiniTest::Test
3a585b96 »
2012-03-31 Incorporate functionality inspired by jbuilder
9 def setup
19699776 »
2012-04-10 consistency in setting params
10 @j = Wunderbar::JsonBuilder.new(Struct.new(:params).new({}))
3a585b96 »
2012-03-31 Incorporate functionality inspired by jbuilder
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
5d1146b1 »
2012-03-31 Make it easier to build JSON arrays
74 _ { _content "hello" }
75 _ { _content "world" }
3a585b96 »
2012-03-31 Incorporate functionality inspired by jbuilder
76 end
77 end
78
5d1146b1 »
2012-03-31 Make it easier to build JSON arrays
79 assert_equal "hello", parsed["comments"][0]["content"]
80 assert_equal "world", parsed["comments"][1]["content"]
3a585b96 »
2012-03-31 Incorporate functionality inspired by jbuilder
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
5d1146b1 »
2012-03-31 Make it easier to build JSON arrays
101 comments = [ Struct.new(:content, :id).new("hello", 1),
102 Struct.new(:content, :id).new("world", 2) ]
3a585b96 »
2012-03-31 Incorporate functionality inspired by jbuilder
103
104 parsed = @j.encode do
105 _comments comments, :content
106 end
107
108 assert_equal ["content"], parsed["comments"].first.keys
5d1146b1 »
2012-03-31 Make it easier to build JSON arrays
109 assert_equal "hello", parsed["comments"][0]["content"]
110 assert_equal "world", parsed["comments"][1]["content"]
3a585b96 »
2012-03-31 Incorporate functionality inspired by jbuilder
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
5d1146b1 »
2012-03-31 Make it easier to build JSON arrays
126 comments = [ Struct.new(:content, :id).new("hello", 1),
127 Struct.new(:content, :id).new("world", 2) ]
3a585b96 »
2012-03-31 Incorporate functionality inspired by jbuilder
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
5d1146b1 »
2012-03-31 Make it easier to build JSON arrays
136 assert_equal "hello", parsed["comments"][0]["content"]
137 assert_equal "world", parsed["comments"][1]["content"]
3a585b96 »
2012-03-31 Incorporate functionality inspired by jbuilder
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
5d1146b1 »
2012-03-31 Make it easier to build JSON arrays
149 assert_equal "hello", parsed[0]["content"]
150 assert_equal "world", parsed[1]["content"]
3a585b96 »
2012-03-31 Incorporate functionality inspired by jbuilder
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
5d1146b1 »
2012-03-31 Make it easier to build JSON arrays
160 _ { _content "hello" }
161 _ { _content "world" }
3a585b96 »
2012-03-31 Incorporate functionality inspired by jbuilder
162 end
163 end
164 end
165
5d1146b1 »
2012-03-31 Make it easier to build JSON arrays
166 assert_equal "hello", parsed["author"]["comments"][0]["content"]
167 assert_equal "world", parsed["author"]["comments"][1]["content"]
3a585b96 »
2012-03-31 Incorporate functionality inspired by jbuilder
168 end
169
170 def test_array_nested_inside_array
171 parsed = @j.encode do
172 _comments do
5d1146b1 »
2012-03-31 Make it easier to build JSON arrays
173 _ do
3a585b96 »
2012-03-31 Incorporate functionality inspired by jbuilder
174 _authors do
5d1146b1 »
2012-03-31 Make it easier to build JSON arrays
175 _ do
3a585b96 »
2012-03-31 Incorporate functionality inspired by jbuilder
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
5d1146b1 »
2012-03-31 Make it easier to build JSON arrays
187 comments = [ Struct.new(:content, :id).new("hello", 1),
188 Struct.new(:content, :id).new("world", 2) ]
3a585b96 »
2012-03-31 Incorporate functionality inspired by jbuilder
189
190 parsed = @j.encode do
191 _ comments do |comment|
192 _content comment.content
193 end
194 end
195
5d1146b1 »
2012-03-31 Make it easier to build JSON arrays
196 assert_equal "hello", parsed[0]["content"]
197 assert_equal "world", parsed[1]["content"]
3a585b96 »
2012-03-31 Incorporate functionality inspired by jbuilder
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
5d1146b1 »
2012-03-31 Make it easier to build JSON arrays
214 _["each"] = "stuff"
3a585b96 »
2012-03-31 Incorporate functionality inspired by jbuilder
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.