File: attribute_merger_spec.rb

package info (click to toggle)
ruby-temple 0.10.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 476 kB
  • sloc: ruby: 3,347; makefile: 6
file content (76 lines) | stat: -rw-r--r-- 3,058 bytes parent folder | download | duplicates (2)
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
require 'spec_helper'

describe Temple::HTML::AttributeMerger do
  before do
    @merger = Temple::HTML::AttributeMerger.new
  end

  it 'should pass static attributes through' do
    expect(@merger.call([:html, :tag,
      'div',
      [:html, :attrs, [:html, :attr, 'class', [:static, 'b']]],
      [:content]
    ])).to eq [:html, :tag, "div",
                     [:html, :attrs,
                      [:html, :attr, "class", [:static, "b"]]],
                     [:content]]
  end

  it 'should preserve the order of html attributes' do
    expect(@merger.call([:html, :tag,
      'meta',
      [:html, :attrs, [:html, :attr, 'c', [:static, '1']],
                      [:html, :attr, 'd', [:static, '2']],
                      [:html, :attr, 'a', [:static, '3']],
                      [:html, :attr, 'b', [:static, '4']]]
    ])).to eq [:html, :tag, 'meta',
                     [:html, :attrs,
                      [:html, :attr, 'c', [:static, '1']],
                      [:html, :attr, 'd', [:static, '2']],
                      [:html, :attr, 'a', [:static, '3']],
                      [:html, :attr, 'b', [:static, '4']]]]

    # Use case:
    expect(@merger.call([:html, :tag,
      'meta',
      [:html, :attrs, [:html, :attr, 'http-equiv', [:static, 'Content-Type']],
                      [:html, :attr, 'content', [:static, '']]]
    ])).to eq [:html, :tag, 'meta',
                     [:html, :attrs,
                      [:html, :attr, 'http-equiv', [:static, 'Content-Type']],
                      [:html, :attr, 'content', [:static, '']]]]
  end

  it 'should merge ids' do
    expect(@merger.call([:html, :tag,
      'div',
      [:html, :attrs, [:html, :attr, 'id', [:dynamic, 'a']], [:html, :attr, 'id', [:dynamic, 'b']]],
      [:content]
    ])).to eq [:html, :tag, "div",
                     [:html, :attrs,
                      [:html, :attr, "id",
                       [:multi,
                        [:code, "_temple_html_attributemerger1 = []"],
                        [:capture, "_temple_html_attributemerger1[0]", [:dynamic, "a"]],
                        [:capture, "_temple_html_attributemerger1[1]", [:dynamic, "b"]],
                        [:dynamic, "_temple_html_attributemerger1.reject(&:empty?).join(\"_\")"]]]],
                     [:content]]
  end

  it 'should merge classes' do
    expect(@merger.call([:html, :tag,
      'div',
      [:html, :attrs, [:html, :attr, 'class', [:static, 'a']], [:html, :attr, 'class', [:dynamic, 'b']]],
      [:content]
    ])).to eq [:html, :tag, "div",
                     [:html, :attrs,
                      [:html, :attr, "class",
                       [:multi,
                        [:code, "_temple_html_attributemerger1 = []"],
                        [:capture, "_temple_html_attributemerger1[0]", [:static, "a"]],
                        [:capture, "_temple_html_attributemerger1[1]", [:dynamic, "b"]],
                        [:dynamic, "_temple_html_attributemerger1.reject(&:empty?).join(\" \")"]]]],
                     [:content]]
  end
end