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 (276 lines) | stat: -rw-r--r-- 7,351 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
require "spec"
require "xml"
require "spec/helpers/string"

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

describe XML::Builder do
  it "writes document" do
    assert_built(%[<?xml version="1.0"?>\n\n]) do
    end
  end

  it "writes element" do
    assert_built(%[<?xml version="1.0"?>\n<foo/>\n]) do
      element("foo") { }
    end
  end

  it "errors on invalid element names" do
    expect_raises(XML::Error, "Invalid element name: '1'") do
      XML.build do |xml|
        xml.element("1") do
        end
      end
    end

    expect_raises(XML::Error, "Invalid element name: 'a b=\"c\"'") do
      XML.build do |xml|
        xml.element("a b=\"c\"") do
        end
      end
    end
  end

  it "writes nested element" do
    assert_built(%[<?xml version="1.0"?>\n<foo><bar/></foo>\n]) do
      element("foo") do
        element("bar") { }
      end
    end
  end

  it "writes element with namespace" do
    assert_built(%[<?xml version="1.0"?>\n<x:foo id="1" xmlns:x="http://foo.com"/>\n]) do
      element("x", "foo", "http://foo.com", id: 1) { }
    end
  end

  it "writes element with namespace, without block" do
    assert_built(%[<?xml version="1.0"?>\n<x:foo id="1" xmlns:x="http://foo.com"/>\n]) do
      element("x", "foo", "http://foo.com", id: 1)
    end
  end

  it "writes attribute" do
    assert_built(%[<?xml version="1.0"?>\n<foo id="1"/>\n]) do
      element("foo") do
        attribute("id", 1)
      end
    end
  end

  it "writes attribute with namespace" do
    assert_built(%[<?xml version="1.0"?>\n<foo x:id="1" xmlns:x="http://ww.foo.com"/>\n]) do
      element("foo") do
        attribute("x", "id", "http://ww.foo.com", 1)
      end
    end
  end

  it "writes element with namespace" do
    assert_built(%[<?xml version="1.0"?>\n<foo xmlns="bar">baz</foo>\n]) do
      element(nil, "foo", "bar") do
        text "baz"
      end
    end
  end

  it "writes element with prefix" do
    assert_built(%[<?xml version="1.0"?>\n<foo:bar>baz</foo:bar>\n]) do
      element("foo", "bar", nil) do
        text "baz"
      end
    end
  end

  it "errors on invalid element name with prefix" do
    expect_raises(XML::Error, "Invalid prefix: 'foo='") do
      XML.build do |xml|
        xml.element("foo=", "bar", nil) do
          xml.text "baz"
        end
      end
    end
  end

  it "errors on invalid element name with prefix and namespace" do
    expect_raises(XML::Error, "Invalid prefix: 'foo '") do
      XML.build do |xml|
        xml.element("foo ", "bar", "ns") do
          xml.text "baz"
        end
      end
    end
  end

  it "writes text" do
    assert_built(%[<?xml version="1.0"?>\n<foo>1 &lt; 2</foo>\n]) do
      element("foo") do
        text "1 < 2"
      end
    end
  end

  it "sets indent with string" do
    assert_built("<?xml version=\"1.0\"?>\n<foo>\n\t<bar/>\n</foo>\n") do |xml|
      xml.indent = "\t"
      element("foo") do
        element("bar")
      end
    end
  end

  it "sets indent with count" do
    assert_built("<?xml version=\"1.0\"?>\n<foo>\n  <bar/>\n</foo>\n") do |xml|
      xml.indent = 2
      element("foo") do
        element("bar")
      end
    end
  end

  it "sets quote char" do
    assert_built("<?xml version='1.0'?>\n<foo id='1'/>\n", quote_char: '\'') do |xml|
      element("foo") do
        attribute("id", 1)
      end
    end
  end

  it "writes element with attributes as named tuple" do
    assert_built(%[<?xml version="1.0"?>\n<foo id="1" name="foo"/>\n]) do |xml|
      element("foo", id: 1, name: "foo")
    end
  end

  it "writes element with attributes as named tuple, nesting" do
    assert_built(%[<?xml version="1.0"?>\n<foo id="1" name="foo" baz="2"/>\n]) do |xml|
      element("foo", id: 1, name: "foo") do
        attribute "baz", 2
      end
    end
  end

  it "writes element with attributes as hash" do
    assert_built(%[<?xml version="1.0"?>\n<foo id="1" name="foo"/>\n]) do |xml|
      element("foo", {"id" => 1, "name" => "foo"})
    end
  end

  it "writes element with attributes as hash, nesting" do
    assert_built(%[<?xml version="1.0"?>\n<foo id="1" name="foo" baz="2"/>\n]) do |xml|
      element("foo", {"id" => 1, "name" => "foo"}) do
        attribute "baz", 2
      end
    end
  end

  describe "#cdata" do
    it "writes cdata" do
      assert_built(%{<?xml version="1.0"?>\n<foo><![CDATA[hello]]></foo>\n}) do |xml|
        element("foo") do
          cdata("hello")
        end
      end
    end

    it "escapes ]]> sequences" do
      assert_built(%{<?xml version="1.0"?>\n<foo><![CDATA[One]]]]><![CDATA[>Two]]]]><![CDATA[>Three]]></foo>\n}) do |xml|
        element("foo") do
          cdata("One]]>Two]]>Three")
        end
      end
    end

    it "writes cdata with block" do
      assert_built(%{<?xml version="1.0"?>\n<foo><![CDATA[hello]]></foo>\n}) do |xml|
        element("foo") do
          cdata do
            text "hello"
          end
        end
      end
    end
  end

  it "writes comment" do
    assert_built(%{<?xml version="1.0"?>\n<foo><!--hello--></foo>\n}) do |xml|
      element("foo") do
        comment("hello")
      end
    end
  end

  it "writes comment with block" do
    assert_built(%{<?xml version="1.0"?>\n<foo><!--hello--></foo>\n}) do |xml|
      element("foo") do
        comment do
          text "hello"
        end
      end
    end
  end

  it "writes DTD" do
    assert_built(%{<?xml version="1.0"?>\n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" [subset]>\n}) do |xml|
      dtd "html", "-//W3C//DTD XHTML 1.0 Transitional//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", "subset"
    end
  end

  it "writes DTD with block" do
    assert_built(%{<?xml version="1.0"?>\n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" [subset]>\n}) do |xml|
      dtd "html", "-//W3C//DTD XHTML 1.0 Transitional//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" do
        text "subset"
      end
    end
  end

  it "writes namespace" do
    assert_built(%{<?xml version="1.0"?>\n<foo xmlns:x="http://foo.com"/>\n}) do |xml|
      element("foo") do
        namespace "x", "http://foo.com"
      end
    end
  end

  it "writes to string" do
    str = XML.build do |xml|
      xml.element("foo", id: 1) do
        xml.text "hello"
      end
    end
    str.should eq("<?xml version=\"1.0\"?>\n<foo id=\"1\">hello</foo>\n")
  end

  it "writes to IO" do
    io = IO::Memory.new
    XML.build(io) do |xml|
      xml.element("foo", id: 1) do
        xml.text "hello"
      end
    end
    io.rewind
    io.to_s.should eq("<?xml version=\"1.0\"?>\n<foo id=\"1\">hello</foo>\n")
  end

  it "errors on null byte" do
    expect_raises(XML::Error, "String cannot contain null character") do
      XML.build do |xml|
        xml.element("example", number: "1") do
          xml.text "foo\0bar"
        end
      end
    end

    expect_raises(XML::Error, "String cannot contain null character") do
      XML.build do |xml|
        xml.element("exam\0ple", number: "1") do
          xml.text "foobar"
        end
      end
    end
  end
end