temple 0.10.2 → 0.10.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,95 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Temple::Filters::DynamicInliner do
4
- before do
5
- @filter = Temple::Filters::DynamicInliner.new
6
- end
7
-
8
- it 'should compile several statics into dynamic' do
9
- expect(@filter.call([:multi,
10
- [:static, "Hello "],
11
- [:static, "World\n "],
12
- [:static, "Have a nice day"]
13
- ])).to eq [:dynamic, '"Hello World\n Have a nice day"']
14
- end
15
-
16
- it 'should compile several dynamics into dynamic' do
17
- expect(@filter.call([:multi,
18
- [:dynamic, "@hello"],
19
- [:dynamic, "@world"],
20
- [:dynamic, "@yeah"]
21
- ])).to eq [:dynamic, '"#{@hello}#{@world}#{@yeah}"']
22
- end
23
-
24
- it 'should compile static and dynamic into dynamic' do
25
- expect(@filter.call([:multi,
26
- [:static, "Hello"],
27
- [:dynamic, "@world"],
28
- [:dynamic, "@yeah"],
29
- [:static, "Nice"]
30
- ])).to eq [:dynamic, '"Hello#{@world}#{@yeah}Nice"']
31
- end
32
-
33
- it 'should merge statics and dynamics around a code' do
34
- expect(@filter.call([:multi,
35
- [:static, "Hello "],
36
- [:dynamic, "@world"],
37
- [:code, "Oh yeah"],
38
- [:dynamic, "@yeah"],
39
- [:static, "Once more"]
40
- ])).to eq [:multi,
41
- [:dynamic, '"Hello #{@world}"'],
42
- [:code, "Oh yeah"],
43
- [:dynamic, '"#{@yeah}Once more"']
44
- ]
45
- end
46
-
47
- it 'should keep codes intact' do
48
- expect(@filter.call([:multi, [:code, 'foo']])).to eq([:code, 'foo'])
49
- end
50
-
51
- it 'should keep single statics intact' do
52
- expect(@filter.call([:multi, [:static, 'foo']])).to eq([:static, 'foo'])
53
- end
54
-
55
- it 'should keep single dynamic intact' do
56
- expect(@filter.call([:multi, [:dynamic, 'foo']])).to eq([:dynamic, 'foo'])
57
- end
58
-
59
- it 'should inline inside multi' do
60
- expect(@filter.call([:multi,
61
- [:static, "Hello "],
62
- [:dynamic, "@world"],
63
- [:multi,
64
- [:static, "Hello "],
65
- [:dynamic, "@world"]],
66
- [:static, "Hello "],
67
- [:dynamic, "@world"]
68
- ])).to eq [:multi,
69
- [:dynamic, '"Hello #{@world}"'],
70
- [:dynamic, '"Hello #{@world}"'],
71
- [:dynamic, '"Hello #{@world}"']
72
- ]
73
- end
74
-
75
- it 'should merge across newlines' do
76
- exp = expect(@filter.call([:multi,
77
- [:static, "Hello \n"],
78
- [:newline],
79
- [:dynamic, "@world"],
80
- [:newline]
81
- ])).to eq [:dynamic, ['"Hello \n"', '"#{@world}"', '""'].join("\\\n")]
82
- end
83
-
84
- it 'should compile static followed by newline' do
85
- expect(@filter.call([:multi,
86
- [:static, "Hello \n"],
87
- [:newline],
88
- [:code, "world"]
89
- ])).to eq [:multi,
90
- [:static, "Hello \n"],
91
- [:newline],
92
- [:code, "world"]
93
- ]
94
- end
95
- end
@@ -1,55 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Temple::Filters::Eraser do
4
- it 'should respect keep' do
5
- eraser = Temple::Filters::Eraser.new(keep: [:a])
6
- expect(eraser.call([:multi,
7
- [:a],
8
- [:b],
9
- [:c]
10
- ])).to eq [:multi,
11
- [:a],
12
- [:multi],
13
- [:multi]
14
- ]
15
- end
16
-
17
- it 'should respect erase' do
18
- eraser = Temple::Filters::Eraser.new(erase: [:a])
19
- expect(eraser.call([:multi,
20
- [:a],
21
- [:b],
22
- [:c]
23
- ])).to eq [:multi,
24
- [:multi],
25
- [:b],
26
- [:c]
27
- ]
28
- end
29
-
30
- it 'should choose erase over keep' do
31
- eraser = Temple::Filters::Eraser.new(keep: [:a, :b], erase: [:a])
32
- expect(eraser.call([:multi,
33
- [:a],
34
- [:b],
35
- [:c]
36
- ])).to eq [:multi,
37
- [:multi],
38
- [:b],
39
- [:multi]
40
- ]
41
- end
42
-
43
- it 'should erase nested types' do
44
- eraser = Temple::Filters::Eraser.new(erase: [[:a, :b]])
45
- expect(eraser.call([:multi,
46
- [:a, :a],
47
- [:a, :b],
48
- [:b]
49
- ])).to eq [:multi,
50
- [:a, :a],
51
- [:multi],
52
- [:b]
53
- ]
54
- end
55
- end
@@ -1,49 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Temple::Filters::Escapable do
4
- before do
5
- @filter = Temple::Filters::Escapable.new
6
- end
7
-
8
- it 'should handle escape expressions' do
9
- expect(@filter.call([:escape, true,
10
- [:multi,
11
- [:static, "a < b"],
12
- [:dynamic, "ruby_method"]]
13
- ])).to eq [:multi,
14
- [:static, "a &lt; b"],
15
- [:dynamic, "::Temple::Utils.escape_html((ruby_method))"],
16
- ]
17
- end
18
-
19
- it 'should keep codes intact' do
20
- exp = [:multi, [:code, 'foo']]
21
- expect(@filter.call(exp)).to eq(exp)
22
- end
23
-
24
- it 'should keep statics intact' do
25
- exp = [:multi, [:static, '<']]
26
- expect(@filter.call(exp)).to eq(exp)
27
- end
28
-
29
- it 'should keep dynamic intact' do
30
- exp = [:multi, [:dynamic, 'foo']]
31
- expect(@filter.call(exp)).to eq(exp)
32
- end
33
-
34
- it 'should have use_html_safe option' do
35
- with_html_safe do
36
- filter = Temple::Filters::Escapable.new(use_html_safe: true)
37
- expect(filter.call([:escape, true,
38
- [:static, Temple::HTML::SafeString.new("a < b")]
39
- ])).to eq [:static, "a < b"]
40
- end
41
- end
42
-
43
- it 'should support censoring' do
44
- filter = Temple::Filters::Escapable.new(escape_code: '(%s).gsub("Temple sucks", "Temple rocks")')
45
- expect(filter.call([:escape, true,
46
- [:static, "~~ Temple sucks ~~"]
47
- ])).to eq [:static, "~~ Temple rocks ~~"]
48
- end
49
- end
@@ -1,33 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Temple::Filters::MultiFlattener do
4
- before do
5
- @filter = Temple::Filters::MultiFlattener.new
6
- end
7
-
8
- it 'should flatten nested multi expressions' do
9
- expect(@filter.call([:multi,
10
- [:static, "a"],
11
- [:multi,
12
- [:dynamic, "aa"],
13
- [:multi,
14
- [:static, "aaa"],
15
- [:static, "aab"],
16
- ],
17
- [:dynamic, "ab"],
18
- ],
19
- [:static, "b"],
20
- ])).to eq [:multi,
21
- [:static, "a"],
22
- [:dynamic, "aa"],
23
- [:static, "aaa"],
24
- [:static, "aab"],
25
- [:dynamic, "ab"],
26
- [:static, "b"],
27
- ]
28
- end
29
-
30
- it 'should return first element' do
31
- expect(@filter.call([:multi, [:code, 'foo']])).to eq([:code, 'foo'])
32
- end
33
- end
@@ -1,35 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Temple::Filters::StaticAnalyzer do
4
- before do
5
- @filter = Temple::Filters::StaticAnalyzer.new
6
- @generator = Temple::Generator.new
7
- end
8
-
9
- if Temple::StaticAnalyzer.available?
10
- it 'should convert :dynamic to :static if code is static' do
11
- expect(@filter.call([:dynamic, '"#{"hello"}#{100}"'])).to eq([:static, 'hello100'])
12
- end
13
-
14
- it 'should not convert :dynamic if code is dynamic' do
15
- exp = [:dynamic, '"#{hello}#{100}"']
16
- expect(@filter.call(exp)).to eq(exp)
17
- end
18
-
19
- it 'should not change number of newlines in generated code' do
20
- exp = [:dynamic, "[100,\n200,\n]"]
21
- expect(@filter.call(exp)).to eq([:multi, [:static, '[100, 200]'], [:newline], [:newline]])
22
-
23
- expect(@generator.call(@filter.call(exp)).count("\n")).to eq(@generator.call(exp).count("\n"))
24
- end
25
- else
26
- it 'should do nothing' do
27
- [
28
- [:dynamic, '"#{"hello"}#{100}"'],
29
- [:dynamic, '"#{hello}#{100}"'],
30
- ].each do |exp|
31
- expect(@filter.call(exp)).to eq(exp)
32
- end
33
- end
34
- end
35
- end
@@ -1,41 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Temple::Filters::StaticMerger do
4
- before do
5
- @filter = Temple::Filters::StaticMerger.new
6
- end
7
-
8
- it 'should merge serveral statics' do
9
- expect(@filter.call([:multi,
10
- [:static, "Hello "],
11
- [:static, "World, "],
12
- [:static, "Good night"]
13
- ])).to eq [:static, "Hello World, Good night"]
14
- end
15
-
16
- it 'should merge serveral statics around code' do
17
- expect(@filter.call([:multi,
18
- [:static, "Hello "],
19
- [:static, "World!"],
20
- [:code, "123"],
21
- [:static, "Good night, "],
22
- [:static, "everybody"]
23
- ])).to eq [:multi,
24
- [:static, "Hello World!"],
25
- [:code, "123"],
26
- [:static, "Good night, everybody"]
27
- ]
28
- end
29
-
30
- it 'should merge serveral statics across newlines' do
31
- expect(@filter.call([:multi,
32
- [:static, "Hello "],
33
- [:static, "World, "],
34
- [:newline],
35
- [:static, "Good night"]
36
- ])).to eq [:multi,
37
- [:static, "Hello World, Good night"],
38
- [:newline]
39
- ]
40
- end
41
- end
@@ -1,50 +0,0 @@
1
- require 'spec_helper'
2
- begin
3
- require 'ripper'
4
- rescue LoadError
5
- end
6
-
7
- if defined?(Ripper)
8
- describe Temple::Filters::StringSplitter do
9
- before do
10
- @filter = Temple::Filters::StringSplitter.new
11
- end
12
-
13
- {
14
- %q|''| => [:multi],
15
- %q|""| => [:multi],
16
- %q|"hello"| => [:multi, [:static, 'hello']],
17
- %q|"hello #{}world"| => [:multi, [:static, 'hello '], [:static, 'world']],
18
- %q|"#{hello}"| => [:multi, [:dynamic, 'hello']],
19
- %q|"nya#{123}"| => [:multi, [:static, 'nya'], [:dynamic, '123']],
20
- %q|"#{()}()"| => [:multi, [:dynamic, '()'], [:static, '()']],
21
- %q|" #{ " #{ '#{}' } " }"| => [:multi, [:static, ' '], [:multi, [:static, ' '], [:multi, [:static, '#{}']], [:static, ' ']]],
22
- %q|%Q[a#{b}c#{d}e]| => [:multi, [:static, 'a'], [:dynamic, 'b'], [:static, 'c'], [:dynamic, 'd'], [:static, 'e']],
23
- %q|%q[a#{b}c#{d}e]| => [:multi, [:static, 'a#{b}c#{d}e']],
24
- %q|"\#{}#{123}"| => [:multi, [:static, '#{}'], [:dynamic, '123']],
25
- %q|"#{ '}' }"| => [:multi, [:multi, [:static, '}']]],
26
- %q| "a" # hello | => [:multi, [:static, 'a']],
27
- %q|"\""| => [:multi, [:static, '"']],
28
- %q|"\\\\\\""| => [:multi, [:static, '\\"']],
29
- %q|'\"'| => [:multi, [:static, '\"']],
30
- %q|'\\\"'| => [:multi, [:static, '\\"']],
31
- %q|"static#{dynamic}"| => [:multi, [:static, 'static'], [:dynamic, 'dynamic']],
32
- }.each do |code, expected|
33
- it "should split #{code}" do
34
- actual = @filter.call([:dynamic, code])
35
- expect(actual).to eq(expected)
36
- end
37
- end
38
-
39
- describe '.compile' do
40
- it 'should raise CompileError for non-string literals' do
41
- expect { Temple::Filters::StringSplitter.compile('1') }.to raise_error(Temple::FilterError)
42
- end
43
-
44
- it 'should compile strings quoted with parenthesis' do
45
- tokens = Temple::Filters::StringSplitter.compile('%Q(href("#{1 + 1}");)')
46
- expect(tokens).to eq([[:static, "href(\""], [:dynamic, "1 + 1"], [:static, "\");"]])
47
- end
48
- end
49
- end
50
- end
@@ -1,179 +0,0 @@
1
- require 'spec_helper'
2
-
3
- class SimpleGenerator < Temple::Generator
4
- def preamble
5
- "#{buffer} = BUFFER"
6
- end
7
-
8
- def postamble
9
- buffer
10
- end
11
-
12
- def on_static(s)
13
- concat "S:#{s}"
14
- end
15
-
16
- def on_dynamic(s)
17
- concat "D:#{s}"
18
- end
19
-
20
- def on_code(s)
21
- "C:#{s}"
22
- end
23
- end
24
-
25
- class SimpleCaptureGenerator < SimpleGenerator
26
- def preamble
27
- "#{buffer} = CAPTURE_BUFFER"
28
- end
29
- end
30
-
31
- describe Temple::Generator do
32
- it 'should compile simple expressions' do
33
- gen = SimpleGenerator.new
34
-
35
- expect(gen.call([:static, 'test'])).to eq('_buf = BUFFER; _buf << (S:test); _buf')
36
- expect(gen.call([:dynamic, 'test'])).to eq('_buf = BUFFER; _buf << (D:test); _buf')
37
- expect(gen.call([:code, 'test'])).to eq('_buf = BUFFER; C:test; _buf')
38
- end
39
-
40
- it 'should compile multi expression' do
41
- gen = SimpleGenerator.new(buffer: "VAR")
42
- expect(gen.call([:multi,
43
- [:static, "static"],
44
- [:dynamic, "dynamic"],
45
- [:code, "code"]
46
- ])).to eq('VAR = BUFFER; VAR << (S:static); VAR << (D:dynamic); C:code; VAR')
47
- end
48
-
49
- it 'should compile capture' do
50
- gen = SimpleGenerator.new(buffer: "VAR", capture_generator: SimpleGenerator)
51
- expect(gen.call([:capture, "foo",
52
- [:static, "test"]
53
- ])).to eq('VAR = BUFFER; foo = BUFFER; foo << (S:test); foo; VAR')
54
- end
55
-
56
- it 'should compile capture with multi' do
57
- gen = SimpleGenerator.new(buffer: "VAR", capture_generator: SimpleGenerator)
58
- expect(gen.call([:multi,
59
- [:static, "before"],
60
-
61
- [:capture, "foo", [:multi,
62
- [:static, "static"],
63
- [:dynamic, "dynamic"],
64
- [:code, "code"]]],
65
-
66
- [:static, "after"]
67
- ])).to eq('VAR = BUFFER; VAR << (S:before); foo = BUFFER; foo << (S:static); ' +
68
- 'foo << (D:dynamic); C:code; foo; VAR << (S:after); VAR')
69
- end
70
-
71
- it 'should compile nested captures with the same capture_generator' do
72
- gen = SimpleGenerator.new(buffer: "VAR", capture_generator: SimpleCaptureGenerator)
73
- expect(gen.call(
74
- [:capture, "foo", [:multi,
75
- [:static, "a"],
76
- [:capture, "bar", [:multi,
77
- [:static, "b"],
78
- [:capture, "baz", [:multi,
79
- [:static, "c"],
80
- ]]
81
- ]]
82
- ]]
83
- )).to eq "VAR = BUFFER; foo = CAPTURE_BUFFER; foo << (S:a); bar = CAPTURE_BUFFER; bar << (S:b); baz = CAPTURE_BUFFER; baz << (S:c); baz; bar; foo; VAR"
84
- end
85
-
86
- it 'should compile newlines' do
87
- gen = SimpleGenerator.new(buffer: "VAR")
88
- expect(gen.call([:multi,
89
- [:static, "static"],
90
- [:newline],
91
- [:dynamic, "dynamic"],
92
- [:newline],
93
- [:code, "code"]
94
- ])).to eq("VAR = BUFFER; VAR << (S:static); \n; " +
95
- "VAR << (D:dynamic); \n; C:code; VAR")
96
- end
97
- end
98
-
99
- describe Temple::Generators::Array do
100
- it 'should compile simple expressions' do
101
- gen = Temple::Generators::Array.new(freeze_static: false)
102
- expect(gen.call([:static, 'test'])).to eq('_buf = []; _buf << ("test"); _buf')
103
- expect(gen.call([:dynamic, 'test'])).to eq('_buf = []; _buf << (test); _buf')
104
- expect(gen.call([:code, 'test'])).to eq('_buf = []; test; _buf')
105
-
106
- expect(gen.call([:multi, [:static, 'a'], [:static, 'b']])).to eq('_buf = []; _buf << ("a"); _buf << ("b"); _buf')
107
- expect(gen.call([:multi, [:static, 'a'], [:dynamic, 'b']])).to eq('_buf = []; _buf << ("a"); _buf << (b); _buf')
108
- end
109
-
110
- it 'should freeze static' do
111
- gen = Temple::Generators::Array.new(freeze_static: true)
112
- expect(gen.call([:static, 'test'])).to eq('_buf = []; _buf << ("test".freeze); _buf')
113
- end
114
- end
115
-
116
- describe Temple::Generators::ArrayBuffer do
117
- it 'should compile simple expressions' do
118
- gen = Temple::Generators::ArrayBuffer.new(freeze_static: false)
119
- expect(gen.call([:static, 'test'])).to eq('_buf = "test"')
120
- expect(gen.call([:dynamic, 'test'])).to eq('_buf = (test).to_s')
121
- expect(gen.call([:code, 'test'])).to eq('_buf = []; test; _buf = _buf.join("")')
122
-
123
- expect(gen.call([:multi, [:static, 'a'], [:static, 'b']])).to eq('_buf = []; _buf << ("a"); _buf << ("b"); _buf = _buf.join("")')
124
- expect(gen.call([:multi, [:static, 'a'], [:dynamic, 'b']])).to eq('_buf = []; _buf << ("a"); _buf << (b); _buf = _buf.join("")')
125
- end
126
-
127
- it 'should freeze static' do
128
- gen = Temple::Generators::ArrayBuffer.new(freeze_static: true)
129
- expect(gen.call([:static, 'test'])).to eq('_buf = "test"')
130
- expect(gen.call([:multi, [:dynamic, '1'], [:static, 'test']])).to eq('_buf = []; _buf << (1); _buf << ("test".freeze); _buf = _buf.join("".freeze)')
131
- end
132
- end
133
-
134
- describe Temple::Generators::StringBuffer do
135
- it 'should compile simple expressions' do
136
- gen = Temple::Generators::StringBuffer.new(freeze_static: false)
137
- expect(gen.call([:static, 'test'])).to eq('_buf = "test"')
138
- expect(gen.call([:dynamic, 'test'])).to eq('_buf = (test).to_s')
139
- expect(gen.call([:code, 'test'])).to eq('_buf = \'\'.dup; test; _buf')
140
-
141
- expect(gen.call([:multi, [:static, 'a'], [:static, 'b']])).to eq('_buf = \'\'.dup; _buf << ("a"); _buf << ("b"); _buf')
142
- expect(gen.call([:multi, [:static, 'a'], [:dynamic, 'b']])).to eq('_buf = \'\'.dup; _buf << ("a"); _buf << ((b).to_s); _buf')
143
- end
144
-
145
- it 'should freeze static' do
146
- gen = Temple::Generators::StringBuffer.new(freeze_static: true)
147
- expect(gen.call([:static, 'test'])).to eq('_buf = "test"')
148
- expect(gen.call([:multi, [:dynamic, '1'], [:static, 'test']])).to eq('_buf = \'\'.dup; _buf << ((1).to_s); _buf << ("test".freeze); _buf')
149
- end
150
- end
151
-
152
- describe Temple::Generators::ERB do
153
- it 'should compile simple expressions' do
154
- gen = Temple::Generators::ERB.new
155
- expect(gen.call([:static, 'test'])).to eq('test')
156
- expect(gen.call([:dynamic, 'test'])).to eq('<%= test %>')
157
- expect(gen.call([:code, 'test'])).to eq('<% test %>')
158
-
159
- expect(gen.call([:multi, [:static, 'a'], [:static, 'b']])).to eq('ab')
160
- expect(gen.call([:multi, [:static, 'a'], [:dynamic, 'b']])).to eq('a<%= b %>')
161
- end
162
- end
163
-
164
- describe Temple::Generators::RailsOutputBuffer do
165
- it 'should compile simple expressions' do
166
- gen = Temple::Generators::RailsOutputBuffer.new(freeze_static: false)
167
- expect(gen.call([:static, 'test'])).to eq('@output_buffer = output_buffer || ActionView::OutputBuffer.new; ' +
168
- '@output_buffer.safe_concat(("test")); @output_buffer')
169
- expect(gen.call([:dynamic, 'test'])).to eq('@output_buffer = output_buffer || ActionView::OutputBuffer.new; ' +
170
- '@output_buffer.safe_concat(((test).to_s)); @output_buffer')
171
- expect(gen.call([:code, 'test'])).to eq('@output_buffer = output_buffer || ActionView::OutputBuffer.new; ' +
172
- 'test; @output_buffer')
173
- end
174
-
175
- it 'should freeze static' do
176
- gen = Temple::Generators::RailsOutputBuffer.new(freeze_static: true)
177
- expect(gen.call([:static, 'test'])).to eq('@output_buffer = output_buffer || ActionView::OutputBuffer.new; @output_buffer.safe_concat(("test".freeze)); @output_buffer')
178
- end
179
- end
data/spec/grammar_spec.rb DELETED
@@ -1,47 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Temple::Grammar do
4
- it 'should match core expressions' do
5
- expect(Temple::Grammar).to be_match([:multi])
6
- expect(Temple::Grammar).to be_match([:multi, [:multi]])
7
- expect(Temple::Grammar).to be_match([:static, 'Text'])
8
- expect(Temple::Grammar).to be_match([:dynamic, 'Text'])
9
- expect(Temple::Grammar).to be_match([:code, 'Text'])
10
- expect(Temple::Grammar).to be_match([:capture, 'Text', [:multi]])
11
- expect(Temple::Grammar).to be_match([:newline])
12
- end
13
-
14
- it 'should not match invalid core expressions' do
15
- expect(Temple::Grammar).not_to be_match([:multi, 'String'])
16
- expect(Temple::Grammar).not_to be_match([:static])
17
- expect(Temple::Grammar).not_to be_match([:dynamic, 1])
18
- expect(Temple::Grammar).not_to be_match([:code, :sym])
19
- expect(Temple::Grammar).not_to be_match([:capture, [:multi]])
20
- expect(Temple::Grammar).not_to be_match([:newline, [:multi]])
21
- end
22
-
23
- it 'should match control flow expressions' do
24
- expect(Temple::Grammar).to be_match([:if, 'Condition', [:multi]])
25
- expect(Temple::Grammar).to be_match([:if, 'Condition', [:multi], [:multi]])
26
- expect(Temple::Grammar).to be_match([:block, 'Loop', [:multi]])
27
- expect(Temple::Grammar).to be_match([:case, 'Arg', ['Cond1', [:multi]], ['Cond1', [:multi]], [:else, [:multi]]])
28
- expect(Temple::Grammar).not_to be_match([:case, 'Arg', [:sym, [:multi]]])
29
- expect(Temple::Grammar).to be_match([:cond, ['Cond1', [:multi]], ['Cond2', [:multi]], [:else, [:multi]]])
30
- expect(Temple::Grammar).not_to be_match([:cond, [:sym, [:multi]]])
31
- end
32
-
33
- it 'should match escape expression' do
34
- expect(Temple::Grammar).to be_match([:escape, true, [:multi]])
35
- expect(Temple::Grammar).to be_match([:escape, false, [:multi]])
36
- end
37
-
38
- it 'should match html expressions' do
39
- expect(Temple::Grammar).to be_match([:html, :doctype, 'Doctype'])
40
- expect(Temple::Grammar).to be_match([:html, :comment, [:multi]])
41
- expect(Temple::Grammar).to be_match([:html, :tag, 'Tag', [:multi]])
42
- expect(Temple::Grammar).to be_match([:html, :tag, 'Tag', [:multi], [:multi]])
43
- expect(Temple::Grammar).to be_match([:html, :tag, 'Tag', [:multi], [:static, 'Text']])
44
- expect(Temple::Grammar).to be_match([:html, :tag, 'Tag', [:html, :attrs, [:html, :attr, 'id',
45
- [:static, 'val']]], [:static, 'Text']])
46
- end
47
- end
@@ -1,76 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Temple::HTML::AttributeMerger do
4
- before do
5
- @merger = Temple::HTML::AttributeMerger.new
6
- end
7
-
8
- it 'should pass static attributes through' do
9
- expect(@merger.call([:html, :tag,
10
- 'div',
11
- [:html, :attrs, [:html, :attr, 'class', [:static, 'b']]],
12
- [:content]
13
- ])).to eq [:html, :tag, "div",
14
- [:html, :attrs,
15
- [:html, :attr, "class", [:static, "b"]]],
16
- [:content]]
17
- end
18
-
19
- it 'should preserve the order of html attributes' do
20
- expect(@merger.call([:html, :tag,
21
- 'meta',
22
- [:html, :attrs, [:html, :attr, 'c', [:static, '1']],
23
- [:html, :attr, 'd', [:static, '2']],
24
- [:html, :attr, 'a', [:static, '3']],
25
- [:html, :attr, 'b', [:static, '4']]]
26
- ])).to eq [:html, :tag, 'meta',
27
- [:html, :attrs,
28
- [:html, :attr, 'c', [:static, '1']],
29
- [:html, :attr, 'd', [:static, '2']],
30
- [:html, :attr, 'a', [:static, '3']],
31
- [:html, :attr, 'b', [:static, '4']]]]
32
-
33
- # Use case:
34
- expect(@merger.call([:html, :tag,
35
- 'meta',
36
- [:html, :attrs, [:html, :attr, 'http-equiv', [:static, 'Content-Type']],
37
- [:html, :attr, 'content', [:static, '']]]
38
- ])).to eq [:html, :tag, 'meta',
39
- [:html, :attrs,
40
- [:html, :attr, 'http-equiv', [:static, 'Content-Type']],
41
- [:html, :attr, 'content', [:static, '']]]]
42
- end
43
-
44
- it 'should merge ids' do
45
- expect(@merger.call([:html, :tag,
46
- 'div',
47
- [:html, :attrs, [:html, :attr, 'id', [:dynamic, 'a']], [:html, :attr, 'id', [:dynamic, 'b']]],
48
- [:content]
49
- ])).to eq [:html, :tag, "div",
50
- [:html, :attrs,
51
- [:html, :attr, "id",
52
- [:multi,
53
- [:code, "_temple_html_attributemerger1 = []"],
54
- [:capture, "_temple_html_attributemerger1[0]", [:dynamic, "a"]],
55
- [:capture, "_temple_html_attributemerger1[1]", [:dynamic, "b"]],
56
- [:dynamic, "_temple_html_attributemerger1.reject(&:empty?).join(\"_\")"]]]],
57
- [:content]]
58
- end
59
-
60
- it 'should merge classes' do
61
- expect(@merger.call([:html, :tag,
62
- 'div',
63
- [:html, :attrs, [:html, :attr, 'class', [:static, 'a']], [:html, :attr, 'class', [:dynamic, 'b']]],
64
- [:content]
65
- ])).to eq [:html, :tag, "div",
66
- [:html, :attrs,
67
- [:html, :attr, "class",
68
- [:multi,
69
- [:code, "_temple_html_attributemerger1 = []"],
70
- [:capture, "_temple_html_attributemerger1[0]", [:static, "a"]],
71
- [:capture, "_temple_html_attributemerger1[1]", [:dynamic, "b"]],
72
- [:dynamic, "_temple_html_attributemerger1.reject(&:empty?).join(\" \")"]]]],
73
- [:content]]
74
- end
75
- end
76
-