File: test_attribute_methods.rb

package info (click to toggle)
ruby-nokogiri 1.11.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,576 kB
  • sloc: xml: 28,086; ruby: 18,456; java: 13,067; ansic: 5,138; yacc: 265; sh: 208; makefile: 27
file content (325 lines) | stat: -rw-r--r-- 10,290 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
323
324
325
require "helper"

class Nokogiri::XML::Node
  class TestAttributeMethods < Nokogiri::TestCase
    def setup
      super
      @xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE)
    end

    def test_each
      attributes = []
      @xml.xpath("//address")[1].each do |key, value|
        attributes << [key, value]
      end
      assert_equal [["domestic", "Yes"], ["street", "Yes"]], attributes
    end

    def test_remove_attribute
      address = @xml.xpath("/staff/employee/address").first
      assert_equal "Yes", address["domestic"]
      attr = address.attributes["domestic"]

      returned_attr = address.remove_attribute "domestic"
      assert_nil address["domestic"]
      assert_equal attr, returned_attr
    end

    def test_remove_attribute_when_not_found
      address = @xml.xpath("/staff/employee/address").first
      attr = address.remove_attribute "not-an-attribute"
      assert_nil attr
    end

    def test_attribute_setter_accepts_non_string
      address = @xml.xpath("/staff/employee/address").first
      assert_equal "Yes", address[:domestic]
      address[:domestic] = "Altered Yes"
      assert_equal "Altered Yes", address[:domestic]
    end

    def test_attribute_accessor_accepts_non_string
      address = @xml.xpath("/staff/employee/address").first
      assert_equal "Yes", address["domestic"]
      assert_equal "Yes", address[:domestic]
    end

    def test_empty_attribute_reading
      node = Nokogiri::XML '<foo empty="" whitespace="  "/>'

      assert_equal "", node.root["empty"]
      assert_equal "  ", node.root["whitespace"]
    end

    def test_delete
      address = @xml.xpath("/staff/employee/address").first
      assert_equal "Yes", address["domestic"]
      address.delete "domestic"
      assert_nil address["domestic"]
    end

    def test_attributes
      assert node = @xml.search("//address").first
      assert_nil(node["asdfasdfasdf"])
      assert_equal("Yes", node["domestic"])

      assert node = @xml.search("//address")[2]
      attr = node.attributes
      assert_equal 2, attr.size
      assert_equal "Yes", attr["domestic"].value
      assert_equal "Yes", attr["domestic"].to_s
      assert_equal "No", attr["street"].value
    end

    def test_values
      assert_equal %w{ Yes Yes }, @xml.xpath("//address")[1].values
    end

    def test_value?
      refute @xml.xpath("//address")[1].value?("no_such_value")
      assert @xml.xpath("//address")[1].value?("Yes")
    end

    def test_keys
      assert_equal %w{ domestic street }, @xml.xpath("//address")[1].keys
    end

    def test_attribute_with_symbol
      assert_equal "Yes", @xml.css("address").first[:domestic]
    end

    def test_non_existent_attribute_should_return_nil
      node = @xml.root.first_element_child
      assert_nil node.attribute("type")
    end

    #
    #  CSS classes, specifically
    #
    def test_classes
      xml = Nokogiri::XML(<<-eoxml)
        <div>
          <p class=" foo  bar foo ">test</p>
          <p class="">test</p>
        </div>
        eoxml
      div = xml.at_xpath("//div")
      p1, p2 = xml.xpath("//p")

      assert_equal [], div.classes
      assert_equal %w[foo bar foo], p1.classes
      assert_equal [], p2.classes
    end

    def test_add_class
      xml = Nokogiri::XML(<<-eoxml)
        <div>
          <p class=" foo  bar foo ">test</p>
          <p class="">test</p>
        </div>
        eoxml
      div = xml.at_xpath("//div")
      p1, p2 = xml.xpath("//p")

      assert_same div, div.add_class("main")
      assert_equal "main", div["class"]

      assert_same p1, p1.add_class("baz foo")
      assert_equal "foo bar foo baz", p1["class"]

      assert_same p2, p2.add_class("foo baz foo")
      assert_equal "foo baz foo", p2["class"]
    end

    def test_append_class
      xml = Nokogiri::XML(<<-eoxml)
        <div>
          <p class=" foo  bar foo ">test</p>
          <p class="">test</p>
        </div>
        eoxml
      div = xml.at_xpath("//div")
      p1, p2 = xml.xpath("//p")

      assert_same div, div.append_class("main")
      assert_equal "main", div["class"]

      assert_same p1, p1.append_class("baz foo")
      assert_equal "foo bar foo baz foo", p1["class"]

      assert_same p2, p2.append_class("foo baz foo")
      assert_equal "foo baz foo", p2["class"]
    end

    def test_remove_class
      xml = Nokogiri::XML(<<-eoxml)
        <div>
          <p class=" foo  bar baz foo ">test</p>
          <p class=" foo  bar baz foo ">test</p>
          <p class="foo foo">test</p>
          <p class="">test</p>
        </div>
        eoxml
      div = xml.at_xpath("//div")
      p1, p2, p3, p4 = xml.xpath("//p")

      assert_same div, div.remove_class("main")
      assert_nil div["class"]

      assert_same p1, p1.remove_class("bar baz")
      assert_equal "foo foo", p1["class"]

      assert_same p2, p2.remove_class()
      assert_nil p2["class"]

      assert_same p3, p3.remove_class("foo")
      assert_nil p3["class"]

      assert_same p4, p4.remove_class("foo")
      assert_nil p4["class"]
    end

    #
    #  keyword attributes, generally
    #
    describe "keyword attribute helpers" do
      let(:node) do
        Nokogiri::XML::DocumentFragment.parse(<<~EOM).at_css("div")
          <div blargh=" foo  bar baz bar foo  quux foo manx ">hello</div>
        EOM
      end

      describe "setup" do
        it { _(node.get_attribute("noob")).must_be_nil }
      end

      describe "#kwattr_values" do
        it "returns an array of space-delimited values" do
          _(node.kwattr_values("blargh")).must_equal(%w[foo bar baz bar foo quux foo manx])
        end

        describe "when no attribute exists" do
          it "returns an empty array" do
            _(node.kwattr_values("noob")).must_equal([])
          end
        end

        describe "when an empty attribute exists" do
          it "returns an empty array" do
            node.set_attribute("noob", "")
            _(node.kwattr_values("noob")).must_equal([])

            node.set_attribute("noob", "  ")
            _(node.kwattr_values("noob")).must_equal([])
          end
        end
      end

      describe "kwattr_add" do
        it "returns the node for chaining" do
          _(node.kwattr_add("noob", "asdf")).must_be_same_as(node)
        end

        it "creates a new attribute when necessary" do
          _(node.kwattr_add("noob", "asdf").get_attribute("noob")).wont_be_nil
        end

        it "adds a new bare keyword string" do
          _(node.kwattr_add("blargh", "jimmy").kwattr_values("blargh")).
            must_equal(%w[foo bar baz bar foo quux foo manx jimmy])
        end

        it "does not add a repeated bare keyword string" do
          _(node.kwattr_add("blargh", "foo").kwattr_values("blargh")).
            must_equal(%w[foo bar baz bar foo quux foo manx])
        end

        describe "given a string of keywords" do
          it "adds new keywords and ignores existing keywords" do
            _(node.kwattr_add("blargh", "foo jimmy\tjohnny").kwattr_values("blargh")).
              must_equal(%w[foo bar baz bar foo quux foo manx jimmy johnny])
          end
        end

        describe "given an array of keywords" do
          it "adds new keywords and ignores existing keywords" do
            _(node.kwattr_add("blargh", %w[foo jimmy]).kwattr_values("blargh")).
              must_equal(%w[foo bar baz bar foo quux foo manx jimmy])
          end
        end
      end

      describe "kwattr_append" do
        it "returns the node for chaining" do
          _(node.kwattr_append("noob", "asdf")).must_be_same_as(node)
        end

        it "creates a new attribute when necessary" do
          _(node.kwattr_append("noob", "asdf").get_attribute("noob")).wont_be_nil
        end

        it "adds a new bare keyword string" do
          _(node.kwattr_append("blargh", "jimmy").kwattr_values("blargh")).
            must_equal(%w[foo bar baz bar foo quux foo manx jimmy])
        end

        it "adds a repeated bare keyword string" do
          _(node.kwattr_append("blargh", "foo").kwattr_values("blargh")).
            must_equal(%w[foo bar baz bar foo quux foo manx foo])
        end

        describe "given a string of keywords" do
          it "adds new keywords and existing keywords" do
            _(node.kwattr_append("blargh", "foo jimmy\tjohnny").kwattr_values("blargh")).
              must_equal(%w[foo bar baz bar foo quux foo manx foo jimmy johnny])
          end
        end

        describe "given an array of keywords" do
          it "adds new keywords and existing keywords" do
            _(node.kwattr_append("blargh", %w[foo jimmy]).kwattr_values("blargh")).
              must_equal(%w[foo bar baz bar foo quux foo manx foo jimmy])
          end
        end
      end

      describe "kwattr_remove" do
        it "returns the node for chaining" do
          _(node.kwattr_remove("noob", "asdf")).must_be_same_as(node)
        end

        it "gracefully handles a non-existent attribute" do
          _(node.kwattr_remove("noob", "asdf").get_attribute("noob")).must_be_nil
        end

        it "removes an existing bare keyword string" do
          _(node.kwattr_remove("blargh", "foo").kwattr_values("blargh")).
            must_equal(%w[bar baz bar quux manx])
        end

        it "gracefully ignores a non-existent bare keyword string" do
          _(node.kwattr_remove("blargh", "jimmy").kwattr_values("blargh")).
            must_equal(%w[foo bar baz bar foo quux foo manx])
        end

        describe "given a string of keywords" do
          it "removes existing keywords and ignores other keywords" do
            _(node.kwattr_remove("blargh", "foo jimmy\tjohnny").kwattr_values("blargh")).
              must_equal(%w[bar baz bar quux manx])
          end
        end

        describe "given an array of keywords" do
          it "adds new keywords and existing keywords" do
            _(node.kwattr_remove("blargh", %w[foo jimmy]).kwattr_values("blargh")).
              must_equal(%w[bar baz bar quux manx])
          end
        end

        it "removes the attribute when no values are left" do
          _(node.kwattr_remove("blargh", %w[foo bar baz bar foo quux foo manx]).get_attribute("blargh")).must_be_nil
        end
      end
    end
  end
end