temple 0.9.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/test.yml +2 -8
- data/.gitignore +1 -0
- data/CHANGES +12 -0
- data/Gemfile +0 -1
- data/README.md +1 -1
- data/Rakefile +3 -11
- data/lib/temple/erb/engine.rb +2 -0
- data/lib/temple/erb/parser.rb +1 -1
- data/lib/temple/filters/string_splitter.rb +1 -1
- data/lib/temple/generator.rb +2 -2
- data/lib/temple/generators/rails_output_buffer.rb +6 -3
- data/lib/temple/templates/rails.rb +2 -2
- data/lib/temple/utils.rb +2 -1
- data/lib/temple/version.rb +1 -1
- data/spec/engine_spec.rb +189 -0
- data/{test/test_erb.rb → spec/erb_spec.rb} +11 -11
- data/spec/filter_spec.rb +29 -0
- data/{test/filters/test_code_merger.rb → spec/filters/code_merger_spec.rb} +7 -7
- data/{test/filters/test_control_flow.rb → spec/filters/control_flow_spec.rb} +13 -13
- data/{test/filters/test_dynamic_inliner.rb → spec/filters/dynamic_inliner_spec.rb} +18 -18
- data/{test/filters/test_eraser.rb → spec/filters/eraser_spec.rb} +9 -9
- data/{test/filters/test_escapable.rb → spec/filters/escapable_spec.rb} +10 -10
- data/{test/filters/test_multi_flattener.rb → spec/filters/multi_flattener_spec.rb} +4 -4
- data/{test/filters/test_static_analyzer.rb → spec/filters/static_analyzer_spec.rb} +6 -8
- data/{test/filters/test_static_merger.rb → spec/filters/static_merger_spec.rb} +7 -7
- data/{test/filters/test_string_splitter.rb → spec/filters/string_splitter_spec.rb} +5 -6
- data/spec/generator_spec.rb +158 -0
- data/spec/grammar_spec.rb +47 -0
- data/{test/html/test_attribute_merger.rb → spec/html/attribute_merger_spec.rb} +11 -11
- data/{test/html/test_attribute_remover.rb → spec/html/attribute_remover_spec.rb} +7 -7
- data/{test/html/test_attribute_sorter.rb → spec/html/attribute_sorter_spec.rb} +7 -7
- data/{test/html/test_fast.rb → spec/html/fast_spec.rb} +23 -23
- data/{test/html/test_pretty.rb → spec/html/pretty_spec.rb} +7 -7
- data/spec/map_spec.rb +39 -0
- data/{test/mixins/test_dispatcher.rb → spec/mixins/dispatcher_spec.rb} +12 -12
- data/{test/mixins/test_grammar_dsl.rb → spec/mixins/grammar_dsl_spec.rb} +19 -19
- data/{test/helper.rb → spec/spec_helper.rb} +3 -4
- data/{test/test_static_analyzer.rb → spec/static_analyzer_spec.rb} +6 -6
- data/spec/utils_spec.rb +39 -0
- data/temple.gemspec +1 -2
- metadata +29 -29
- data/test/test_engine.rb +0 -189
- data/test/test_filter.rb +0 -29
- data/test/test_generator.rb +0 -158
- data/test/test_grammar.rb +0 -47
- data/test/test_map.rb +0 -39
- data/test/test_utils.rb +0 -39
@@ -1,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Temple::Filters::DynamicInliner do
|
4
4
|
before do
|
@@ -6,38 +6,38 @@ describe Temple::Filters::DynamicInliner do
|
|
6
6
|
end
|
7
7
|
|
8
8
|
it 'should compile several statics into dynamic' do
|
9
|
-
@filter.call([:multi,
|
9
|
+
expect(@filter.call([:multi,
|
10
10
|
[:static, "Hello "],
|
11
11
|
[:static, "World\n "],
|
12
12
|
[:static, "Have a nice day"]
|
13
|
-
]).
|
13
|
+
])).to eq [:dynamic, '"Hello World\n Have a nice day"']
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'should compile several dynamics into dynamic' do
|
17
|
-
@filter.call([:multi,
|
17
|
+
expect(@filter.call([:multi,
|
18
18
|
[:dynamic, "@hello"],
|
19
19
|
[:dynamic, "@world"],
|
20
20
|
[:dynamic, "@yeah"]
|
21
|
-
]).
|
21
|
+
])).to eq [:dynamic, '"#{@hello}#{@world}#{@yeah}"']
|
22
22
|
end
|
23
23
|
|
24
24
|
it 'should compile static and dynamic into dynamic' do
|
25
|
-
@filter.call([:multi,
|
25
|
+
expect(@filter.call([:multi,
|
26
26
|
[:static, "Hello"],
|
27
27
|
[:dynamic, "@world"],
|
28
28
|
[:dynamic, "@yeah"],
|
29
29
|
[:static, "Nice"]
|
30
|
-
]).
|
30
|
+
])).to eq [:dynamic, '"Hello#{@world}#{@yeah}Nice"']
|
31
31
|
end
|
32
32
|
|
33
33
|
it 'should merge statics and dynamics around a code' do
|
34
|
-
|
34
|
+
expect(@filter.call([:multi,
|
35
35
|
[:static, "Hello "],
|
36
36
|
[:dynamic, "@world"],
|
37
37
|
[:code, "Oh yeah"],
|
38
38
|
[:dynamic, "@yeah"],
|
39
39
|
[:static, "Once more"]
|
40
|
-
]).
|
40
|
+
])).to eq [:multi,
|
41
41
|
[:dynamic, '"Hello #{@world}"'],
|
42
42
|
[:code, "Oh yeah"],
|
43
43
|
[:dynamic, '"#{@yeah}Once more"']
|
@@ -45,19 +45,19 @@ describe Temple::Filters::DynamicInliner do
|
|
45
45
|
end
|
46
46
|
|
47
47
|
it 'should keep codes intact' do
|
48
|
-
@filter.call([:multi, [:code, 'foo']]).
|
48
|
+
expect(@filter.call([:multi, [:code, 'foo']])).to eq([:code, 'foo'])
|
49
49
|
end
|
50
50
|
|
51
51
|
it 'should keep single statics intact' do
|
52
|
-
@filter.call([:multi, [:static, 'foo']]).
|
52
|
+
expect(@filter.call([:multi, [:static, 'foo']])).to eq([:static, 'foo'])
|
53
53
|
end
|
54
54
|
|
55
55
|
it 'should keep single dynamic intact' do
|
56
|
-
@filter.call([:multi, [:dynamic, 'foo']]).
|
56
|
+
expect(@filter.call([:multi, [:dynamic, 'foo']])).to eq([:dynamic, 'foo'])
|
57
57
|
end
|
58
58
|
|
59
59
|
it 'should inline inside multi' do
|
60
|
-
@filter.call([:multi,
|
60
|
+
expect(@filter.call([:multi,
|
61
61
|
[:static, "Hello "],
|
62
62
|
[:dynamic, "@world"],
|
63
63
|
[:multi,
|
@@ -65,7 +65,7 @@ describe Temple::Filters::DynamicInliner do
|
|
65
65
|
[:dynamic, "@world"]],
|
66
66
|
[:static, "Hello "],
|
67
67
|
[:dynamic, "@world"]
|
68
|
-
]).
|
68
|
+
])).to eq [:multi,
|
69
69
|
[:dynamic, '"Hello #{@world}"'],
|
70
70
|
[:dynamic, '"Hello #{@world}"'],
|
71
71
|
[:dynamic, '"Hello #{@world}"']
|
@@ -73,20 +73,20 @@ describe Temple::Filters::DynamicInliner do
|
|
73
73
|
end
|
74
74
|
|
75
75
|
it 'should merge across newlines' do
|
76
|
-
exp = @filter.call([:multi,
|
76
|
+
exp = expect(@filter.call([:multi,
|
77
77
|
[:static, "Hello \n"],
|
78
78
|
[:newline],
|
79
79
|
[:dynamic, "@world"],
|
80
80
|
[:newline]
|
81
|
-
]).
|
81
|
+
])).to eq [:dynamic, ['"Hello \n"', '"#{@world}"', '""'].join("\\\n")]
|
82
82
|
end
|
83
83
|
|
84
84
|
it 'should compile static followed by newline' do
|
85
|
-
@filter.call([:multi,
|
85
|
+
expect(@filter.call([:multi,
|
86
86
|
[:static, "Hello \n"],
|
87
87
|
[:newline],
|
88
88
|
[:code, "world"]
|
89
|
-
]).
|
89
|
+
])).to eq [:multi,
|
90
90
|
[:static, "Hello \n"],
|
91
91
|
[:newline],
|
92
92
|
[:code, "world"]
|
@@ -1,13 +1,13 @@
|
|
1
|
-
require '
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Temple::Filters::Eraser do
|
4
4
|
it 'should respect keep' do
|
5
5
|
eraser = Temple::Filters::Eraser.new(keep: [:a])
|
6
|
-
eraser.call([:multi,
|
6
|
+
expect(eraser.call([:multi,
|
7
7
|
[:a],
|
8
8
|
[:b],
|
9
9
|
[:c]
|
10
|
-
]).
|
10
|
+
])).to eq [:multi,
|
11
11
|
[:a],
|
12
12
|
[:multi],
|
13
13
|
[:multi]
|
@@ -16,11 +16,11 @@ describe Temple::Filters::Eraser do
|
|
16
16
|
|
17
17
|
it 'should respect erase' do
|
18
18
|
eraser = Temple::Filters::Eraser.new(erase: [:a])
|
19
|
-
eraser.call([:multi,
|
19
|
+
expect(eraser.call([:multi,
|
20
20
|
[:a],
|
21
21
|
[:b],
|
22
22
|
[:c]
|
23
|
-
]).
|
23
|
+
])).to eq [:multi,
|
24
24
|
[:multi],
|
25
25
|
[:b],
|
26
26
|
[:c]
|
@@ -29,11 +29,11 @@ describe Temple::Filters::Eraser do
|
|
29
29
|
|
30
30
|
it 'should choose erase over keep' do
|
31
31
|
eraser = Temple::Filters::Eraser.new(keep: [:a, :b], erase: [:a])
|
32
|
-
eraser.call([:multi,
|
32
|
+
expect(eraser.call([:multi,
|
33
33
|
[:a],
|
34
34
|
[:b],
|
35
35
|
[:c]
|
36
|
-
]).
|
36
|
+
])).to eq [:multi,
|
37
37
|
[:multi],
|
38
38
|
[:b],
|
39
39
|
[:multi]
|
@@ -42,11 +42,11 @@ describe Temple::Filters::Eraser do
|
|
42
42
|
|
43
43
|
it 'should erase nested types' do
|
44
44
|
eraser = Temple::Filters::Eraser.new(erase: [[:a, :b]])
|
45
|
-
eraser.call([:multi,
|
45
|
+
expect(eraser.call([:multi,
|
46
46
|
[:a, :a],
|
47
47
|
[:a, :b],
|
48
48
|
[:b]
|
49
|
-
]).
|
49
|
+
])).to eq [:multi,
|
50
50
|
[:a, :a],
|
51
51
|
[:multi],
|
52
52
|
[:b]
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Temple::Filters::Escapable do
|
4
4
|
before do
|
@@ -6,11 +6,11 @@ describe Temple::Filters::Escapable do
|
|
6
6
|
end
|
7
7
|
|
8
8
|
it 'should handle escape expressions' do
|
9
|
-
@filter.call([:escape, true,
|
9
|
+
expect(@filter.call([:escape, true,
|
10
10
|
[:multi,
|
11
11
|
[:static, "a < b"],
|
12
12
|
[:dynamic, "ruby_method"]]
|
13
|
-
]).
|
13
|
+
])).to eq [:multi,
|
14
14
|
[:static, "a < b"],
|
15
15
|
[:dynamic, "::Temple::Utils.escape_html((ruby_method))"],
|
16
16
|
]
|
@@ -18,32 +18,32 @@ describe Temple::Filters::Escapable do
|
|
18
18
|
|
19
19
|
it 'should keep codes intact' do
|
20
20
|
exp = [:multi, [:code, 'foo']]
|
21
|
-
@filter.call(exp).
|
21
|
+
expect(@filter.call(exp)).to eq(exp)
|
22
22
|
end
|
23
23
|
|
24
24
|
it 'should keep statics intact' do
|
25
25
|
exp = [:multi, [:static, '<']]
|
26
|
-
@filter.call(exp).
|
26
|
+
expect(@filter.call(exp)).to eq(exp)
|
27
27
|
end
|
28
28
|
|
29
29
|
it 'should keep dynamic intact' do
|
30
30
|
exp = [:multi, [:dynamic, 'foo']]
|
31
|
-
@filter.call(exp).
|
31
|
+
expect(@filter.call(exp)).to eq(exp)
|
32
32
|
end
|
33
33
|
|
34
34
|
it 'should have use_html_safe option' do
|
35
35
|
with_html_safe do
|
36
36
|
filter = Temple::Filters::Escapable.new(use_html_safe: true)
|
37
|
-
filter.call([:escape, true,
|
37
|
+
expect(filter.call([:escape, true,
|
38
38
|
[:static, Temple::HTML::SafeString.new("a < b")]
|
39
|
-
]).
|
39
|
+
])).to eq [:static, "a < b"]
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
43
|
it 'should support censoring' do
|
44
44
|
filter = Temple::Filters::Escapable.new(escape_code: '(%s).gsub("Temple sucks", "Temple rocks")')
|
45
|
-
filter.call([:escape, true,
|
45
|
+
expect(filter.call([:escape, true,
|
46
46
|
[:static, "~~ Temple sucks ~~"]
|
47
|
-
]).
|
47
|
+
])).to eq [:static, "~~ Temple rocks ~~"]
|
48
48
|
end
|
49
49
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Temple::Filters::MultiFlattener do
|
4
4
|
before do
|
@@ -6,7 +6,7 @@ describe Temple::Filters::MultiFlattener do
|
|
6
6
|
end
|
7
7
|
|
8
8
|
it 'should flatten nested multi expressions' do
|
9
|
-
@filter.call([:multi,
|
9
|
+
expect(@filter.call([:multi,
|
10
10
|
[:static, "a"],
|
11
11
|
[:multi,
|
12
12
|
[:dynamic, "aa"],
|
@@ -17,7 +17,7 @@ describe Temple::Filters::MultiFlattener do
|
|
17
17
|
[:dynamic, "ab"],
|
18
18
|
],
|
19
19
|
[:static, "b"],
|
20
|
-
]).
|
20
|
+
])).to eq [:multi,
|
21
21
|
[:static, "a"],
|
22
22
|
[:dynamic, "aa"],
|
23
23
|
[:static, "aaa"],
|
@@ -28,6 +28,6 @@ describe Temple::Filters::MultiFlattener do
|
|
28
28
|
end
|
29
29
|
|
30
30
|
it 'should return first element' do
|
31
|
-
@filter.call([:multi, [:code, 'foo']]).
|
31
|
+
expect(@filter.call([:multi, [:code, 'foo']])).to eq([:code, 'foo'])
|
32
32
|
end
|
33
33
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Temple::Filters::StaticAnalyzer do
|
4
4
|
before do
|
@@ -8,21 +8,19 @@ describe Temple::Filters::StaticAnalyzer do
|
|
8
8
|
|
9
9
|
if Temple::StaticAnalyzer.available?
|
10
10
|
it 'should convert :dynamic to :static if code is static' do
|
11
|
-
@filter.call([:dynamic, '"#{"hello"}#{100}"']
|
12
|
-
).should.equal [:static, 'hello100']
|
11
|
+
expect(@filter.call([:dynamic, '"#{"hello"}#{100}"'])).to eq([:static, 'hello100'])
|
13
12
|
end
|
14
13
|
|
15
14
|
it 'should not convert :dynamic if code is dynamic' do
|
16
15
|
exp = [:dynamic, '"#{hello}#{100}"']
|
17
|
-
@filter.call(exp).
|
16
|
+
expect(@filter.call(exp)).to eq(exp)
|
18
17
|
end
|
19
18
|
|
20
19
|
it 'should not change number of newlines in generated code' do
|
21
20
|
exp = [:dynamic, "[100,\n200,\n]"]
|
22
|
-
@filter.call(exp).
|
21
|
+
expect(@filter.call(exp)).to eq([:multi, [:static, '[100, 200]'], [:newline], [:newline]])
|
23
22
|
|
24
|
-
@generator.call(@filter.call(exp)).count("\n").
|
25
|
-
should.equal(@generator.call(exp).count("\n"))
|
23
|
+
expect(@generator.call(@filter.call(exp)).count("\n")).to eq(@generator.call(exp).count("\n"))
|
26
24
|
end
|
27
25
|
else
|
28
26
|
it 'should do nothing' do
|
@@ -30,7 +28,7 @@ describe Temple::Filters::StaticAnalyzer do
|
|
30
28
|
[:dynamic, '"#{"hello"}#{100}"'],
|
31
29
|
[:dynamic, '"#{hello}#{100}"'],
|
32
30
|
].each do |exp|
|
33
|
-
@filter.call(exp).
|
31
|
+
expect(@filter.call(exp)).to eq(exp)
|
34
32
|
end
|
35
33
|
end
|
36
34
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Temple::Filters::StaticMerger do
|
4
4
|
before do
|
@@ -6,21 +6,21 @@ describe Temple::Filters::StaticMerger do
|
|
6
6
|
end
|
7
7
|
|
8
8
|
it 'should merge serveral statics' do
|
9
|
-
@filter.call([:multi,
|
9
|
+
expect(@filter.call([:multi,
|
10
10
|
[:static, "Hello "],
|
11
11
|
[:static, "World, "],
|
12
12
|
[:static, "Good night"]
|
13
|
-
]).
|
13
|
+
])).to eq [:static, "Hello World, Good night"]
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'should merge serveral statics around code' do
|
17
|
-
@filter.call([:multi,
|
17
|
+
expect(@filter.call([:multi,
|
18
18
|
[:static, "Hello "],
|
19
19
|
[:static, "World!"],
|
20
20
|
[:code, "123"],
|
21
21
|
[:static, "Good night, "],
|
22
22
|
[:static, "everybody"]
|
23
|
-
]).
|
23
|
+
])).to eq [:multi,
|
24
24
|
[:static, "Hello World!"],
|
25
25
|
[:code, "123"],
|
26
26
|
[:static, "Good night, everybody"]
|
@@ -28,12 +28,12 @@ describe Temple::Filters::StaticMerger do
|
|
28
28
|
end
|
29
29
|
|
30
30
|
it 'should merge serveral statics across newlines' do
|
31
|
-
@filter.call([:multi,
|
31
|
+
expect(@filter.call([:multi,
|
32
32
|
[:static, "Hello "],
|
33
33
|
[:static, "World, "],
|
34
34
|
[:newline],
|
35
35
|
[:static, "Good night"]
|
36
|
-
]).
|
36
|
+
])).to eq [:multi,
|
37
37
|
[:static, "Hello World, Good night"],
|
38
38
|
[:newline]
|
39
39
|
]
|
@@ -1,10 +1,10 @@
|
|
1
|
-
require '
|
1
|
+
require 'spec_helper'
|
2
2
|
begin
|
3
3
|
require 'ripper'
|
4
4
|
rescue LoadError
|
5
5
|
end
|
6
6
|
|
7
|
-
if defined?(Ripper)
|
7
|
+
if defined?(Ripper)
|
8
8
|
describe Temple::Filters::StringSplitter do
|
9
9
|
before do
|
10
10
|
@filter = Temple::Filters::StringSplitter.new
|
@@ -32,19 +32,18 @@ if defined?(Ripper) && RUBY_VERSION >= "2.0.0"
|
|
32
32
|
}.each do |code, expected|
|
33
33
|
it "should split #{code}" do
|
34
34
|
actual = @filter.call([:dynamic, code])
|
35
|
-
actual.
|
35
|
+
expect(actual).to eq(expected)
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
39
|
describe '.compile' do
|
40
40
|
it 'should raise CompileError for non-string literals' do
|
41
|
-
|
42
|
-
should.raise(Temple::FilterError)
|
41
|
+
expect { Temple::Filters::StringSplitter.compile('1') }.to raise_error(Temple::FilterError)
|
43
42
|
end
|
44
43
|
|
45
44
|
it 'should compile strings quoted with parenthesis' do
|
46
45
|
tokens = Temple::Filters::StringSplitter.compile('%Q(href("#{1 + 1}");)')
|
47
|
-
tokens.
|
46
|
+
expect(tokens).to eq([[:static, "href(\""], [:dynamic, "1 + 1"], [:static, "\");"]])
|
48
47
|
end
|
49
48
|
end
|
50
49
|
end
|
@@ -0,0 +1,158 @@
|
|
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
|
+
describe Temple::Generator do
|
26
|
+
it 'should compile simple expressions' do
|
27
|
+
gen = SimpleGenerator.new
|
28
|
+
|
29
|
+
expect(gen.call([:static, 'test'])).to eq('_buf = BUFFER; _buf << (S:test); _buf')
|
30
|
+
expect(gen.call([:dynamic, 'test'])).to eq('_buf = BUFFER; _buf << (D:test); _buf')
|
31
|
+
expect(gen.call([:code, 'test'])).to eq('_buf = BUFFER; C:test; _buf')
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should compile multi expression' do
|
35
|
+
gen = SimpleGenerator.new(buffer: "VAR")
|
36
|
+
expect(gen.call([:multi,
|
37
|
+
[:static, "static"],
|
38
|
+
[:dynamic, "dynamic"],
|
39
|
+
[:code, "code"]
|
40
|
+
])).to eq('VAR = BUFFER; VAR << (S:static); VAR << (D:dynamic); C:code; VAR')
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'should compile capture' do
|
44
|
+
gen = SimpleGenerator.new(buffer: "VAR", capture_generator: SimpleGenerator)
|
45
|
+
expect(gen.call([:capture, "foo",
|
46
|
+
[:static, "test"]
|
47
|
+
])).to eq('VAR = BUFFER; foo = BUFFER; foo << (S:test); foo; VAR')
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should compile capture with multi' do
|
51
|
+
gen = SimpleGenerator.new(buffer: "VAR", capture_generator: SimpleGenerator)
|
52
|
+
expect(gen.call([:multi,
|
53
|
+
[:static, "before"],
|
54
|
+
|
55
|
+
[:capture, "foo", [:multi,
|
56
|
+
[:static, "static"],
|
57
|
+
[:dynamic, "dynamic"],
|
58
|
+
[:code, "code"]]],
|
59
|
+
|
60
|
+
[:static, "after"]
|
61
|
+
])).to eq('VAR = BUFFER; VAR << (S:before); foo = BUFFER; foo << (S:static); ' +
|
62
|
+
'foo << (D:dynamic); C:code; foo; VAR << (S:after); VAR')
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'should compile newlines' do
|
66
|
+
gen = SimpleGenerator.new(buffer: "VAR")
|
67
|
+
expect(gen.call([:multi,
|
68
|
+
[:static, "static"],
|
69
|
+
[:newline],
|
70
|
+
[:dynamic, "dynamic"],
|
71
|
+
[:newline],
|
72
|
+
[:code, "code"]
|
73
|
+
])).to eq("VAR = BUFFER; VAR << (S:static); \n; " +
|
74
|
+
"VAR << (D:dynamic); \n; C:code; VAR")
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe Temple::Generators::Array do
|
79
|
+
it 'should compile simple expressions' do
|
80
|
+
gen = Temple::Generators::Array.new(freeze_static: false)
|
81
|
+
expect(gen.call([:static, 'test'])).to eq('_buf = []; _buf << ("test"); _buf')
|
82
|
+
expect(gen.call([:dynamic, 'test'])).to eq('_buf = []; _buf << (test); _buf')
|
83
|
+
expect(gen.call([:code, 'test'])).to eq('_buf = []; test; _buf')
|
84
|
+
|
85
|
+
expect(gen.call([:multi, [:static, 'a'], [:static, 'b']])).to eq('_buf = []; _buf << ("a"); _buf << ("b"); _buf')
|
86
|
+
expect(gen.call([:multi, [:static, 'a'], [:dynamic, 'b']])).to eq('_buf = []; _buf << ("a"); _buf << (b); _buf')
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'should freeze static' do
|
90
|
+
gen = Temple::Generators::Array.new(freeze_static: true)
|
91
|
+
expect(gen.call([:static, 'test'])).to eq('_buf = []; _buf << ("test".freeze); _buf')
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe Temple::Generators::ArrayBuffer do
|
96
|
+
it 'should compile simple expressions' do
|
97
|
+
gen = Temple::Generators::ArrayBuffer.new(freeze_static: false)
|
98
|
+
expect(gen.call([:static, 'test'])).to eq('_buf = "test"')
|
99
|
+
expect(gen.call([:dynamic, 'test'])).to eq('_buf = (test).to_s')
|
100
|
+
expect(gen.call([:code, 'test'])).to eq('_buf = []; test; _buf = _buf.join("")')
|
101
|
+
|
102
|
+
expect(gen.call([:multi, [:static, 'a'], [:static, 'b']])).to eq('_buf = []; _buf << ("a"); _buf << ("b"); _buf = _buf.join("")')
|
103
|
+
expect(gen.call([:multi, [:static, 'a'], [:dynamic, 'b']])).to eq('_buf = []; _buf << ("a"); _buf << (b); _buf = _buf.join("")')
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'should freeze static' do
|
107
|
+
gen = Temple::Generators::ArrayBuffer.new(freeze_static: true)
|
108
|
+
expect(gen.call([:static, 'test'])).to eq('_buf = "test"')
|
109
|
+
expect(gen.call([:multi, [:dynamic, '1'], [:static, 'test']])).to eq('_buf = []; _buf << (1); _buf << ("test".freeze); _buf = _buf.join("".freeze)')
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
describe Temple::Generators::StringBuffer do
|
114
|
+
it 'should compile simple expressions' do
|
115
|
+
gen = Temple::Generators::StringBuffer.new(freeze_static: false)
|
116
|
+
expect(gen.call([:static, 'test'])).to eq('_buf = "test"')
|
117
|
+
expect(gen.call([:dynamic, 'test'])).to eq('_buf = (test).to_s')
|
118
|
+
expect(gen.call([:code, 'test'])).to eq('_buf = \'\'; test; _buf')
|
119
|
+
|
120
|
+
expect(gen.call([:multi, [:static, 'a'], [:static, 'b']])).to eq('_buf = \'\'; _buf << ("a"); _buf << ("b"); _buf')
|
121
|
+
expect(gen.call([:multi, [:static, 'a'], [:dynamic, 'b']])).to eq('_buf = \'\'; _buf << ("a"); _buf << ((b).to_s); _buf')
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'should freeze static' do
|
125
|
+
gen = Temple::Generators::StringBuffer.new(freeze_static: true)
|
126
|
+
expect(gen.call([:static, 'test'])).to eq('_buf = "test"')
|
127
|
+
expect(gen.call([:multi, [:dynamic, '1'], [:static, 'test']])).to eq('_buf = \'\'; _buf << ((1).to_s); _buf << ("test".freeze); _buf')
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
describe Temple::Generators::ERB do
|
132
|
+
it 'should compile simple expressions' do
|
133
|
+
gen = Temple::Generators::ERB.new
|
134
|
+
expect(gen.call([:static, 'test'])).to eq('test')
|
135
|
+
expect(gen.call([:dynamic, 'test'])).to eq('<%= test %>')
|
136
|
+
expect(gen.call([:code, 'test'])).to eq('<% test %>')
|
137
|
+
|
138
|
+
expect(gen.call([:multi, [:static, 'a'], [:static, 'b']])).to eq('ab')
|
139
|
+
expect(gen.call([:multi, [:static, 'a'], [:dynamic, 'b']])).to eq('a<%= b %>')
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
describe Temple::Generators::RailsOutputBuffer do
|
144
|
+
it 'should compile simple expressions' do
|
145
|
+
gen = Temple::Generators::RailsOutputBuffer.new(freeze_static: false)
|
146
|
+
expect(gen.call([:static, 'test'])).to eq('@output_buffer = output_buffer || ActionView::OutputBuffer.new; ' +
|
147
|
+
'@output_buffer.safe_concat(("test")); @output_buffer')
|
148
|
+
expect(gen.call([:dynamic, 'test'])).to eq('@output_buffer = output_buffer || ActionView::OutputBuffer.new; ' +
|
149
|
+
'@output_buffer.safe_concat(((test).to_s)); @output_buffer')
|
150
|
+
expect(gen.call([:code, 'test'])).to eq('@output_buffer = output_buffer || ActionView::OutputBuffer.new; ' +
|
151
|
+
'test; @output_buffer')
|
152
|
+
end
|
153
|
+
|
154
|
+
it 'should freeze static' do
|
155
|
+
gen = Temple::Generators::RailsOutputBuffer.new(freeze_static: true)
|
156
|
+
expect(gen.call([:static, 'test'])).to eq('@output_buffer = output_buffer || ActionView::OutputBuffer.new; @output_buffer.safe_concat(("test".freeze)); @output_buffer')
|
157
|
+
end
|
158
|
+
end
|
@@ -0,0 +1,47 @@
|
|
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,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Temple::HTML::AttributeMerger do
|
4
4
|
before do
|
@@ -6,24 +6,24 @@ describe Temple::HTML::AttributeMerger do
|
|
6
6
|
end
|
7
7
|
|
8
8
|
it 'should pass static attributes through' do
|
9
|
-
@merger.call([:html, :tag,
|
9
|
+
expect(@merger.call([:html, :tag,
|
10
10
|
'div',
|
11
11
|
[:html, :attrs, [:html, :attr, 'class', [:static, 'b']]],
|
12
12
|
[:content]
|
13
|
-
]).
|
13
|
+
])).to eq [:html, :tag, "div",
|
14
14
|
[:html, :attrs,
|
15
15
|
[:html, :attr, "class", [:static, "b"]]],
|
16
16
|
[:content]]
|
17
17
|
end
|
18
18
|
|
19
19
|
it 'should preserve the order of html attributes' do
|
20
|
-
@merger.call([:html, :tag,
|
20
|
+
expect(@merger.call([:html, :tag,
|
21
21
|
'meta',
|
22
22
|
[:html, :attrs, [:html, :attr, 'c', [:static, '1']],
|
23
23
|
[:html, :attr, 'd', [:static, '2']],
|
24
24
|
[:html, :attr, 'a', [:static, '3']],
|
25
25
|
[:html, :attr, 'b', [:static, '4']]]
|
26
|
-
]).
|
26
|
+
])).to eq [:html, :tag, 'meta',
|
27
27
|
[:html, :attrs,
|
28
28
|
[:html, :attr, 'c', [:static, '1']],
|
29
29
|
[:html, :attr, 'd', [:static, '2']],
|
@@ -31,22 +31,22 @@ describe Temple::HTML::AttributeMerger do
|
|
31
31
|
[:html, :attr, 'b', [:static, '4']]]]
|
32
32
|
|
33
33
|
# Use case:
|
34
|
-
@merger.call([:html, :tag,
|
34
|
+
expect(@merger.call([:html, :tag,
|
35
35
|
'meta',
|
36
36
|
[:html, :attrs, [:html, :attr, 'http-equiv', [:static, 'Content-Type']],
|
37
37
|
[:html, :attr, 'content', [:static, '']]]
|
38
|
-
]).
|
38
|
+
])).to eq [:html, :tag, 'meta',
|
39
39
|
[:html, :attrs,
|
40
40
|
[:html, :attr, 'http-equiv', [:static, 'Content-Type']],
|
41
41
|
[:html, :attr, 'content', [:static, '']]]]
|
42
42
|
end
|
43
43
|
|
44
44
|
it 'should merge ids' do
|
45
|
-
@merger.call([:html, :tag,
|
45
|
+
expect(@merger.call([:html, :tag,
|
46
46
|
'div',
|
47
47
|
[:html, :attrs, [:html, :attr, 'id', [:dynamic, 'a']], [:html, :attr, 'id', [:dynamic, 'b']]],
|
48
48
|
[:content]
|
49
|
-
]).
|
49
|
+
])).to eq [:html, :tag, "div",
|
50
50
|
[:html, :attrs,
|
51
51
|
[:html, :attr, "id",
|
52
52
|
[:multi,
|
@@ -58,11 +58,11 @@ describe Temple::HTML::AttributeMerger do
|
|
58
58
|
end
|
59
59
|
|
60
60
|
it 'should merge classes' do
|
61
|
-
@merger.call([:html, :tag,
|
61
|
+
expect(@merger.call([:html, :tag,
|
62
62
|
'div',
|
63
63
|
[:html, :attrs, [:html, :attr, 'class', [:static, 'a']], [:html, :attr, 'class', [:dynamic, 'b']]],
|
64
64
|
[:content]
|
65
|
-
]).
|
65
|
+
])).to eq [:html, :tag, "div",
|
66
66
|
[:html, :attrs,
|
67
67
|
[:html, :attr, "class",
|
68
68
|
[:multi,
|