File: builder_spec.cr

package info (click to toggle)
crystal 1.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,384 kB
  • sloc: javascript: 6,400; sh: 695; makefile: 269; ansic: 121; python: 105; cpp: 77; xml: 32
file content (322 lines) | stat: -rw-r--r-- 6,923 bytes parent folder | download
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
313
314
315
316
317
318
319
320
321
322
require "spec"
require "json"
require "spec/helpers/string"

private def assert_built(expected, *, file = __FILE__, line = __LINE__, &)
  assert_prints JSON.build { |json| with json yield json }, expected, file: file, line: line
end

private class TestObject
  def to_json(builder)
    {"int" => 12}.to_json(builder)
  end
end

describe JSON::Builder do
  it "writes null" do
    assert_built("null") do
      null
    end
  end

  it "writes bool" do
    assert_built("true") do
      bool(true)
    end
  end

  it "writes integer" do
    assert_built("123") do
      number(123)
    end
  end

  it "writes float" do
    assert_built("123.45") do
      number(123.45)
    end
  end

  it "errors on nan" do
    json = JSON::Builder.new(IO::Memory.new)
    json.start_document
    expect_raises JSON::Error, "NaN not allowed in JSON" do
      json.number(0.0/0.0)
    end
  end

  it "errors on infinity" do
    json = JSON::Builder.new(IO::Memory.new)
    json.start_document
    expect_raises JSON::Error, "Infinity not allowed in JSON" do
      json.number(1.0/0.0)
    end
  end

  it "writes string" do
    assert_built(%<"hello">) do
      string("hello")
    end
  end

  it "writes string with controls and slashes " do
    assert_built("\" \\\" \\\\ \\b \\f \\n \\r \\t \\u0019 \"") do
      string(" \" \\ \b \f \n \r \t \u{19} ")
    end
  end

  it "errors if writing before document start" do
    json = JSON::Builder.new(IO::Memory.new)
    expect_raises JSON::Error, "Write before start_document" do
      json.number(1)
    end
  end

  it "errors if writing two scalars" do
    json = JSON::Builder.new(IO::Memory.new)
    json.start_document
    json.number(1)
    expect_raises JSON::Error, "Write past end_document and before start_document" do
      json.number(2)
    end
  end

  it "writes array" do
    assert_built(%<[1,"hello",true]>) do
      array do
        number(1)
        string("hello")
        bool(true)
      end
    end
  end

  it "writes nested array" do
    assert_built(%<[1,["hello",true],2]>) do
      array do
        number(1)
        array do
          string("hello")
          bool(true)
        end
        number(2)
      end
    end
  end

  it "writes object" do
    assert_built(%<{"foo":1,"bar":2}>) do
      object do
        string("foo")
        number(1)
        string("bar")
        number(2)
      end
    end
  end

  it "writes nested object" do
    assert_built(%<{"foo":{"bar":2,"baz":3},"another":{"baz":3}}>) do
      object do
        string("foo")
        object do
          string("bar")
          number(2)
          string("baz")
          number(3)
        end
        string("another")
        object do
          string("baz")
          number(3)
        end
      end
    end
  end

  it "writes array with indent level" do
    assert_built(%<[\n  1,\n  2,\n  3\n]>) do |json|
      json.indent = 2
      array do
        number(1)
        number(2)
        number(3)
      end
    end
  end

  it "writes array with indent string" do
    assert_built(%<[\n\t1,\n\t2,\n\t3\n]>) do |json|
      json.indent = "\t"
      array do
        number(1)
        number(2)
        number(3)
      end
    end
  end

  it "writes object with indent level" do
    assert_built(%<{\n  "foo": 1,\n  "bar": 2\n}>) do |json|
      json.indent = 2
      object do
        string "foo"
        number(1)
        string "bar"
        number(2)
      end
    end
  end

  it "writes empty array with indent level" do
    assert_built(%<[]>) do |json|
      json.indent = 2
      array do
      end
    end
  end

  it "writes empty object with indent level" do
    assert_built(%<{}>) do |json|
      json.indent = 2
      object do
      end
    end
  end

  it "writes nested array" do
    assert_built(%<[\n  []\n]>) do |json|
      json.indent = 2
      array do
        array do
        end
      end
    end
  end

  it "writes object with scalar and indent" do
    assert_built(%<{\n  "foo": 1\n}>) do |json|
      json.indent = 2
      object do
        string "foo"
        number 1
      end
    end
  end

  it "writes object with array and scalar and indent" do
    assert_built(%<{\n  "foo": [\n    1\n  ]\n}>) do |json|
      json.indent = 2
      object do
        string "foo"
        array do
          number 1
        end
      end
    end
  end

  it "writes raw" do
    assert_built(%<{\n  "foo": [1, 2, 3],\n  "bar": [\n    [4, 5, 6]\n  ]\n}>) do |json|
      json.indent = 2
      object do
        string "foo"
        raw "[1, 2, 3]"
        string "bar"
        array do
          raw "[4, 5, 6]"
        end
      end
    end
  end

  it "raises if nothing written" do
    json = JSON::Builder.new(IO::Memory.new)
    json.start_document
    expect_raises JSON::Error, "Empty JSON" do
      json.end_document
    end
  end

  it "raises if array is left open" do
    json = JSON::Builder.new(IO::Memory.new)
    json.start_document
    json.start_array
    expect_raises JSON::Error, "Unterminated JSON array" do
      json.end_document
    end
  end

  it "raises if object is left open" do
    json = JSON::Builder.new(IO::Memory.new)
    json.start_document
    json.start_object
    expect_raises JSON::Error, "Unterminated JSON object" do
      json.end_document
    end
  end

  it "writes field with scalar in object" do
    assert_built(%<{"int":42,"float":0.815,"null":null,"bool":true,"string":"string"}>) do
      object do
        field "int", 42
        field "float", 0.815
        field "null", nil
        field "bool", true
        field "string", "string"
      end
    end
  end

  it "writes field with arbitrary value in object" do
    assert_built(%<{"hash":{"hash":"value"},"object":{"int":12}}>) do
      object do
        field "hash", {"hash" => "value"}
        field "object", TestObject.new
      end
    end
  end

  it "errors on max nesting (array)" do
    io = IO::Memory.new
    builder = JSON::Builder.new(io)
    builder.max_nesting = 3
    builder.start_document
    3.times do
      builder.start_array
    end

    expect_raises(JSON::Error, "Nesting of 4 is too deep") do
      builder.start_array
    end
  end

  it "errors on max nesting (object)" do
    io = IO::Memory.new
    builder = JSON::Builder.new(io)
    builder.max_nesting = 3
    builder.start_document
    3.times do
      builder.start_object
      builder.string "key"
    end

    expect_raises(JSON::Error, "Nesting of 4 is too deep") do
      builder.start_object
    end
  end

  it "#next_is_object_key?" do
    io = IO::Memory.new
    builder = JSON::Builder.new(io)
    builder.next_is_object_key?.should be_false
    builder.start_document
    builder.next_is_object_key?.should be_false
    builder.start_object
    builder.next_is_object_key?.should be_true
    builder.scalar("foo")
    builder.next_is_object_key?.should be_false
    builder.scalar("bar")
    builder.next_is_object_key?.should be_true
  end
end