File: test_builder.rb

package info (click to toggle)
ruby-nokogiri 1.13.10%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 7,416 kB
  • sloc: ansic: 38,198; xml: 28,086; ruby: 22,271; java: 15,517; cpp: 7,037; yacc: 244; sh: 148; makefile: 136
file content (168 lines) | stat: -rw-r--r-- 4,679 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
# frozen_string_literal: true

require "helper"

module Nokogiri
  module HTML
    class TestBuilder < Nokogiri::TestCase
      def test_top_level_function_builds
        foo = nil
        Nokogiri() { |xml| foo = xml }
        assert_instance_of(Nokogiri::HTML::Builder, foo)
      end

      def test_builder_with_explicit_tags
        html_doc = Nokogiri::HTML::Builder.new do
          div.slide(class: "another_class") do
            node = Nokogiri::XML::Node.new("id", doc)
            node.content = "hello"
            insert(node)
          end
        end.doc
        assert_equal(1, html_doc.css("div.slide > id").length)
        assert_equal("hello", html_doc.at("div.slide > id").content)
      end

      def test_hash_as_attributes_for_attribute_method
        html = Nokogiri::HTML::Builder.new do ||
          div.slide(class: "another_class") do
            span("Slide 1")
          end
        end.to_html
        assert_match('class="slide another_class"', html)
      end

      def test_hash_as_attributes
        builder = Nokogiri::HTML::Builder.new do
          div(id: "awesome") do
            h1("america")
          end
        end
        assert_equal('<div id="awesome"><h1>america</h1></div>',
          builder.doc.root.to_html.delete("\n").gsub(/>\s*</, "><"))
      end

      def test_href_with_attributes
        uri = "http://tenderlovemaking.com/"
        built = Nokogiri::XML::Builder.new do
          div do
            a("King Khan & The Shrines", href: uri)
          end
        end
        assert_equal("http://tenderlovemaking.com/",
          built.doc.at("a")[:href])
      end

      def test_tag_nesting
        builder = Nokogiri::HTML::Builder.new do
          body do
            span.left("")
            span.middle do
              div.icon("")
            end
            span.right("")
          end
        end
        assert(node = builder.doc.css("span.right").first)
        assert_equal("middle", node.previous_sibling["class"])
      end

      def test_has_ampersand
        builder = Nokogiri::HTML::Builder.new do
          div.rad.thing! do
            text("<awe&some>")
            b("hello & world")
          end
        end
        assert_equal(
          '<div class="rad" id="thing">&lt;awe&amp;some&gt;<b>hello &amp; world</b></div>',
          builder.doc.root.to_html.delete("\n")
        )
      end

      def test_multi_tags
        builder = Nokogiri::HTML::Builder.new do
          div.rad.thing! do
            text("<awesome>")
            b("hello")
          end
        end
        assert_equal(
          '<div class="rad" id="thing">&lt;awesome&gt;<b>hello</b></div>',
          builder.doc.root.to_html.delete("\n")
        )
      end

      def test_attributes_plus_block
        builder = Nokogiri::HTML::Builder.new do
          div.rad.thing! do
            text("<awesome>")
          end
        end
        assert_equal('<div class="rad" id="thing">&lt;awesome&gt;</div>',
          builder.doc.root.to_html.chomp)
      end

      def test_builder_adds_attributes
        builder = Nokogiri::HTML::Builder.new do
          div.rad.thing!("tender div")
        end
        assert_equal('<div class="rad" id="thing">tender div</div>',
          builder.doc.root.to_html.chomp)
      end

      def test_bold_tag
        builder = Nokogiri::HTML::Builder.new do
          b("bold tag")
        end
        assert_equal("<b>bold tag</b>", builder.doc.root.to_html.chomp)
      end

      def test_html_then_body_tag
        builder = Nokogiri::HTML::Builder.new do
          html do
            body do
              b("bold tag")
            end
          end
        end
        assert_equal("<html><body><b>bold tag</b></body></html>",
          builder.doc.root.to_html.chomp.gsub(/>\s*</, "><"))
      end

      def test_instance_eval_with_delegation_to_block_context
        class << self
          def foo
            "foo!"
          end
        end

        builder = Nokogiri::HTML::Builder.new { text(foo) }
        assert_includes(builder.to_html, "foo!")
      end

      def test_builder_with_param
        doc = Nokogiri::HTML::Builder.new do |html|
          html.body do
            html.p("hello world")
          end
        end.doc

        assert(node = doc.xpath("//body/p").first)
        assert_equal("hello world", node.content)
      end

      def test_builder_with_id
        text = "hello world"
        doc = Nokogiri::HTML::Builder.new do |html|
          html.body do
            html.id_(text)
          end
        end.doc

        assert(node = doc.xpath("//body/id").first)
        assert_equal(text, node.content)
      end
    end
  end
end