temple 0.8.2 → 0.9.1

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.
Files changed (52) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/test.yml +34 -0
  3. data/.gitignore +1 -0
  4. data/CHANGES +18 -1
  5. data/README.md +1 -1
  6. data/Rakefile +4 -11
  7. data/lib/temple/erb/parser.rb +1 -1
  8. data/lib/temple/filters/ambles.rb +21 -0
  9. data/lib/temple/filters/string_splitter.rb +11 -0
  10. data/lib/temple/generator.rb +11 -5
  11. data/lib/temple/generators/rails_output_buffer.rb +2 -6
  12. data/lib/temple/mixins/grammar_dsl.rb +2 -1
  13. data/lib/temple/templates/rails.rb +4 -0
  14. data/lib/temple/templates/tilt.rb +1 -9
  15. data/lib/temple/utils.rb +2 -14
  16. data/lib/temple/version.rb +1 -1
  17. data/lib/temple.rb +1 -0
  18. data/spec/engine_spec.rb +189 -0
  19. data/{test/test_erb.rb → spec/erb_spec.rb} +12 -12
  20. data/spec/filter_spec.rb +29 -0
  21. data/{test/filters/test_code_merger.rb → spec/filters/code_merger_spec.rb} +7 -7
  22. data/{test/filters/test_control_flow.rb → spec/filters/control_flow_spec.rb} +13 -13
  23. data/{test/filters/test_dynamic_inliner.rb → spec/filters/dynamic_inliner_spec.rb} +18 -18
  24. data/{test/filters/test_eraser.rb → spec/filters/eraser_spec.rb} +9 -9
  25. data/{test/filters/test_escapable.rb → spec/filters/escapable_spec.rb} +10 -10
  26. data/{test/filters/test_multi_flattener.rb → spec/filters/multi_flattener_spec.rb} +4 -4
  27. data/{test/filters/test_static_analyzer.rb → spec/filters/static_analyzer_spec.rb} +6 -8
  28. data/{test/filters/test_static_merger.rb → spec/filters/static_merger_spec.rb} +7 -7
  29. data/spec/filters/string_splitter_spec.rb +50 -0
  30. data/spec/generator_spec.rb +167 -0
  31. data/spec/grammar_spec.rb +47 -0
  32. data/{test/html/test_attribute_merger.rb → spec/html/attribute_merger_spec.rb} +11 -11
  33. data/{test/html/test_attribute_remover.rb → spec/html/attribute_remover_spec.rb} +7 -7
  34. data/{test/html/test_attribute_sorter.rb → spec/html/attribute_sorter_spec.rb} +7 -7
  35. data/{test/html/test_fast.rb → spec/html/fast_spec.rb} +23 -23
  36. data/{test/html/test_pretty.rb → spec/html/pretty_spec.rb} +7 -7
  37. data/spec/map_spec.rb +39 -0
  38. data/{test/mixins/test_dispatcher.rb → spec/mixins/dispatcher_spec.rb} +12 -12
  39. data/{test/mixins/test_grammar_dsl.rb → spec/mixins/grammar_dsl_spec.rb} +19 -19
  40. data/{test/helper.rb → spec/spec_helper.rb} +5 -6
  41. data/{test/test_static_analyzer.rb → spec/static_analyzer_spec.rb} +6 -6
  42. data/spec/utils_spec.rb +39 -0
  43. data/temple.gemspec +3 -3
  44. metadata +34 -59
  45. data/.travis.yml +0 -30
  46. data/test/filters/test_string_splitter.rb +0 -25
  47. data/test/test_engine.rb +0 -189
  48. data/test/test_filter.rb +0 -29
  49. data/test/test_generator.rb +0 -158
  50. data/test/test_grammar.rb +0 -47
  51. data/test/test_map.rb +0 -39
  52. data/test/test_utils.rb +0 -39
@@ -1,4 +1,4 @@
1
- require 'helper'
1
+ require 'spec_helper'
2
2
 
3
3
  describe Temple::Filters::ControlFlow do
4
4
  before do
@@ -6,18 +6,18 @@ describe Temple::Filters::ControlFlow do
6
6
  end
7
7
 
8
8
  it 'should process blocks' do
9
- @filter.call([:block, 'loop do',
9
+ expect(@filter.call([:block, 'loop do',
10
10
  [:static, 'Hello']
11
- ]).should.equal [:multi,
11
+ ])).to eq [:multi,
12
12
  [:code, 'loop do'],
13
13
  [:static, 'Hello'],
14
14
  [:code, 'end']]
15
15
  end
16
16
 
17
17
  it 'should process if' do
18
- @filter.call([:if, 'condition',
18
+ expect(@filter.call([:if, 'condition',
19
19
  [:static, 'Hello']
20
- ]).should.equal [:multi,
20
+ ])).to eq [:multi,
21
21
  [:code, 'if condition'],
22
22
  [:static, 'Hello'],
23
23
  [:code, 'end']
@@ -25,10 +25,10 @@ describe Temple::Filters::ControlFlow do
25
25
  end
26
26
 
27
27
  it 'should process if with else' do
28
- @filter.call([:if, 'condition',
28
+ expect(@filter.call([:if, 'condition',
29
29
  [:static, 'True'],
30
30
  [:static, 'False']
31
- ]).should.equal [:multi,
31
+ ])).to eq [:multi,
32
32
  [:code, 'if condition'],
33
33
  [:static, 'True'],
34
34
  [:code, 'else'],
@@ -38,12 +38,12 @@ describe Temple::Filters::ControlFlow do
38
38
  end
39
39
 
40
40
  it 'should create elsif' do
41
- @filter.call([:if, 'condition1',
41
+ expect(@filter.call([:if, 'condition1',
42
42
  [:static, '1'],
43
43
  [:if, 'condition2',
44
44
  [:static, '2'],
45
45
  [:static, '3']]
46
- ]).should.equal [:multi,
46
+ ])).to eq [:multi,
47
47
  [:code, 'if condition1'],
48
48
  [:static, '1'],
49
49
  [:code, 'elsif condition2'],
@@ -55,11 +55,11 @@ describe Temple::Filters::ControlFlow do
55
55
  end
56
56
 
57
57
  it 'should process cond' do
58
- @filter.call([:cond,
58
+ expect(@filter.call([:cond,
59
59
  ['cond1', [:exp1]],
60
60
  ['cond2', [:exp2]],
61
61
  [:else, [:exp3]],
62
- ]).should.equal [:multi,
62
+ ])).to eq [:multi,
63
63
  [:code, 'case'],
64
64
  [:code, 'when cond1'],
65
65
  [:exp1],
@@ -72,11 +72,11 @@ describe Temple::Filters::ControlFlow do
72
72
  end
73
73
 
74
74
  it 'should process case' do
75
- @filter.call([:case, 'var',
75
+ expect(@filter.call([:case, 'var',
76
76
  ['Array', [:exp1]],
77
77
  ['String', [:exp2]],
78
78
  [:else, [:exp3]],
79
- ]).should.equal [:multi,
79
+ ])).to eq [:multi,
80
80
  [:code, 'case (var)'],
81
81
  [:code, 'when Array'],
82
82
  [:exp1],
@@ -1,4 +1,4 @@
1
- require 'helper'
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
- ]).should.equal [:dynamic, '"Hello World\n Have a nice day"']
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
- ]).should.equal [:dynamic, '"#{@hello}#{@world}#{@yeah}"']
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
- ]).should.equal [:dynamic, '"Hello#{@world}#{@yeah}Nice"']
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
- exp = @filter.call([:multi,
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
- ]).should.equal [:multi,
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']]).should.equal [: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']]).should.equal [: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']]).should.equal [: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
- ]).should.equal [:multi,
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
- ]).should.equal [:dynamic, ['"Hello \n"', '"#{@world}"', '""'].join("\\\n")]
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
- ]).should.equal [:multi,
89
+ ])).to eq [:multi,
90
90
  [:static, "Hello \n"],
91
91
  [:newline],
92
92
  [:code, "world"]
@@ -1,13 +1,13 @@
1
- require 'helper'
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
- ]).should.equal [:multi,
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
- ]).should.equal [:multi,
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
- ]).should.equal [:multi,
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
- ]).should.equal [:multi,
49
+ ])).to eq [:multi,
50
50
  [:a, :a],
51
51
  [:multi],
52
52
  [:b]
@@ -1,4 +1,4 @@
1
- require 'helper'
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
- ]).should.equal [:multi,
13
+ ])).to eq [:multi,
14
14
  [:static, "a &lt; 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).should.equal 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).should.equal 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).should.equal 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
- ]).should.equal [:static, "a < b"]
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
- ]).should.equal [:static, "~~ Temple rocks ~~"]
47
+ ])).to eq [:static, "~~ Temple rocks ~~"]
48
48
  end
49
49
  end
@@ -1,4 +1,4 @@
1
- require 'helper'
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
- ]).should.equal [:multi,
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']]).should.equal [: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 'helper'
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).should.equal(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).should.equal([:multi, [:static, '[100, 200]'], [:newline], [:newline]])
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).should.equal(exp)
31
+ expect(@filter.call(exp)).to eq(exp)
34
32
  end
35
33
  end
36
34
  end
@@ -1,4 +1,4 @@
1
- require 'helper'
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
- ]).should.equal [:static, "Hello World, Good night"]
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
- ]).should.equal [:multi,
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
- ]).should.equal [:multi,
36
+ ])).to eq [:multi,
37
37
  [:static, "Hello World, Good night"],
38
38
  [:newline]
39
39
  ]
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+ begin
3
+ require 'ripper'
4
+ rescue LoadError
5
+ end
6
+
7
+ if defined?(Ripper) && RUBY_VERSION >= "2.0.0"
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
@@ -0,0 +1,167 @@
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 nested capture with the same capture_generator' do
66
+ gen = SimpleGenerator.new(buffer: "VAR", capture_generator: SimpleGenerator)
67
+ expect(gen.call([:capture, "foo", [:multi,
68
+ [:capture, "bar", [:multi,
69
+ [:static, "a"],
70
+ [:static, "b"]]]]
71
+ ])).to eq "VAR = BUFFER; foo = BUFFER; bar = BUFFER; bar << (S:a); bar << (S:b); bar; foo; VAR"
72
+ end
73
+
74
+ it 'should compile newlines' do
75
+ gen = SimpleGenerator.new(buffer: "VAR")
76
+ expect(gen.call([:multi,
77
+ [:static, "static"],
78
+ [:newline],
79
+ [:dynamic, "dynamic"],
80
+ [:newline],
81
+ [:code, "code"]
82
+ ])).to eq("VAR = BUFFER; VAR << (S:static); \n; " +
83
+ "VAR << (D:dynamic); \n; C:code; VAR")
84
+ end
85
+ end
86
+
87
+ describe Temple::Generators::Array do
88
+ it 'should compile simple expressions' do
89
+ gen = Temple::Generators::Array.new(freeze_static: false)
90
+ expect(gen.call([:static, 'test'])).to eq('_buf = []; _buf << ("test"); _buf')
91
+ expect(gen.call([:dynamic, 'test'])).to eq('_buf = []; _buf << (test); _buf')
92
+ expect(gen.call([:code, 'test'])).to eq('_buf = []; test; _buf')
93
+
94
+ expect(gen.call([:multi, [:static, 'a'], [:static, 'b']])).to eq('_buf = []; _buf << ("a"); _buf << ("b"); _buf')
95
+ expect(gen.call([:multi, [:static, 'a'], [:dynamic, 'b']])).to eq('_buf = []; _buf << ("a"); _buf << (b); _buf')
96
+ end
97
+
98
+ it 'should freeze static' do
99
+ gen = Temple::Generators::Array.new(freeze_static: true)
100
+ expect(gen.call([:static, 'test'])).to eq('_buf = []; _buf << ("test".freeze); _buf')
101
+ end
102
+ end
103
+
104
+ describe Temple::Generators::ArrayBuffer do
105
+ it 'should compile simple expressions' do
106
+ gen = Temple::Generators::ArrayBuffer.new(freeze_static: false)
107
+ expect(gen.call([:static, 'test'])).to eq('_buf = "test"')
108
+ expect(gen.call([:dynamic, 'test'])).to eq('_buf = (test).to_s')
109
+ expect(gen.call([:code, 'test'])).to eq('_buf = []; test; _buf = _buf.join("")')
110
+
111
+ expect(gen.call([:multi, [:static, 'a'], [:static, 'b']])).to eq('_buf = []; _buf << ("a"); _buf << ("b"); _buf = _buf.join("")')
112
+ expect(gen.call([:multi, [:static, 'a'], [:dynamic, 'b']])).to eq('_buf = []; _buf << ("a"); _buf << (b); _buf = _buf.join("")')
113
+ end
114
+
115
+ it 'should freeze static' do
116
+ gen = Temple::Generators::ArrayBuffer.new(freeze_static: true)
117
+ expect(gen.call([:static, 'test'])).to eq('_buf = "test"')
118
+ expect(gen.call([:multi, [:dynamic, '1'], [:static, 'test']])).to eq('_buf = []; _buf << (1); _buf << ("test".freeze); _buf = _buf.join("".freeze)')
119
+ end
120
+ end
121
+
122
+ describe Temple::Generators::StringBuffer do
123
+ it 'should compile simple expressions' do
124
+ gen = Temple::Generators::StringBuffer.new(freeze_static: false)
125
+ expect(gen.call([:static, 'test'])).to eq('_buf = "test"')
126
+ expect(gen.call([:dynamic, 'test'])).to eq('_buf = (test).to_s')
127
+ expect(gen.call([:code, 'test'])).to eq('_buf = \'\'; test; _buf')
128
+
129
+ expect(gen.call([:multi, [:static, 'a'], [:static, 'b']])).to eq('_buf = \'\'; _buf << ("a"); _buf << ("b"); _buf')
130
+ expect(gen.call([:multi, [:static, 'a'], [:dynamic, 'b']])).to eq('_buf = \'\'; _buf << ("a"); _buf << ((b).to_s); _buf')
131
+ end
132
+
133
+ it 'should freeze static' do
134
+ gen = Temple::Generators::StringBuffer.new(freeze_static: true)
135
+ expect(gen.call([:static, 'test'])).to eq('_buf = "test"')
136
+ expect(gen.call([:multi, [:dynamic, '1'], [:static, 'test']])).to eq('_buf = \'\'; _buf << ((1).to_s); _buf << ("test".freeze); _buf')
137
+ end
138
+ end
139
+
140
+ describe Temple::Generators::ERB do
141
+ it 'should compile simple expressions' do
142
+ gen = Temple::Generators::ERB.new
143
+ expect(gen.call([:static, 'test'])).to eq('test')
144
+ expect(gen.call([:dynamic, 'test'])).to eq('<%= test %>')
145
+ expect(gen.call([:code, 'test'])).to eq('<% test %>')
146
+
147
+ expect(gen.call([:multi, [:static, 'a'], [:static, 'b']])).to eq('ab')
148
+ expect(gen.call([:multi, [:static, 'a'], [:dynamic, 'b']])).to eq('a<%= b %>')
149
+ end
150
+ end
151
+
152
+ describe Temple::Generators::RailsOutputBuffer do
153
+ it 'should compile simple expressions' do
154
+ gen = Temple::Generators::RailsOutputBuffer.new(freeze_static: false)
155
+ expect(gen.call([:static, 'test'])).to eq('@output_buffer = output_buffer || ActionView::OutputBuffer.new; ' +
156
+ '@output_buffer.safe_concat(("test")); @output_buffer')
157
+ expect(gen.call([:dynamic, 'test'])).to eq('@output_buffer = output_buffer || ActionView::OutputBuffer.new; ' +
158
+ '@output_buffer.safe_concat(((test).to_s)); @output_buffer')
159
+ expect(gen.call([:code, 'test'])).to eq('@output_buffer = output_buffer || ActionView::OutputBuffer.new; ' +
160
+ 'test; @output_buffer')
161
+ end
162
+
163
+ it 'should freeze static' do
164
+ gen = Temple::Generators::RailsOutputBuffer.new(freeze_static: true)
165
+ expect(gen.call([:static, 'test'])).to eq('@output_buffer = output_buffer || ActionView::OutputBuffer.new; @output_buffer.safe_concat(("test".freeze)); @output_buffer')
166
+ end
167
+ end