File: test_builder.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 (164 lines) | stat: -rw-r--r-- 4,632 bytes parent folder | download | duplicates (7)
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
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 {
          div.slide(:class => 'another_class') {
            node = Nokogiri::XML::Node.new("id", doc)
            node.content = "hello"
            insert(node)
          }
        }.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 { ||
          div.slide(:class => 'another_class') {
            span 'Slide 1'
          }
        }.to_html
        assert_match 'class="slide another_class"', html
      end

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

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

      def test_tag_nesting
        builder = Nokogiri::HTML::Builder.new do
          body {
            span.left ''
            span.middle {
              div.icon ''
            }
            span.right ''
          }
        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! {
            text "<awe&some>"
            b "hello & world"
          }
        end
        assert_equal(
          '<div class="rad" id="thing">&lt;awe&amp;some&gt;<b>hello &amp; world</b></div>',
                     builder.doc.root.to_html.gsub(/\n/, ''))
      end

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

      def test_attributes_plus_block
        builder = Nokogiri::HTML::Builder.new do
          div.rad.thing! {
            text "<awesome>"
          }
        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 {
            body {
              b "bold tag"
            }
          }
        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 builder.to_html.include?("foo!")
      end

      def test_builder_with_param
        doc = Nokogiri::HTML::Builder.new { |html|
          html.body {
            html.p "hello world"
          }
        }.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 { |html|
          html.body {
            html.id_ text
          }
        }.doc

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