temple 0.8.2 → 0.10.4

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 (85) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/test.yml +37 -0
  3. data/.gitignore +1 -0
  4. data/CHANGES +43 -1
  5. data/Gemfile +0 -1
  6. data/README.md +1 -1
  7. data/Rakefile +4 -24
  8. data/lib/temple/engine.rb +1 -0
  9. data/lib/temple/erb/engine.rb +3 -0
  10. data/lib/temple/erb/parser.rb +2 -1
  11. data/lib/temple/erb/template.rb +1 -0
  12. data/lib/temple/erb/trimming.rb +1 -0
  13. data/lib/temple/exceptions.rb +1 -0
  14. data/lib/temple/filter.rb +1 -0
  15. data/lib/temple/filters/ambles.rb +22 -0
  16. data/lib/temple/filters/code_merger.rb +1 -0
  17. data/lib/temple/filters/control_flow.rb +1 -0
  18. data/lib/temple/filters/dynamic_inliner.rb +2 -1
  19. data/lib/temple/filters/dynamic_merger.rb +69 -0
  20. data/lib/temple/filters/encoding.rb +2 -1
  21. data/lib/temple/filters/eraser.rb +1 -0
  22. data/lib/temple/filters/escapable.rb +1 -0
  23. data/lib/temple/filters/multi_flattener.rb +1 -0
  24. data/lib/temple/filters/remove_bom.rb +1 -0
  25. data/lib/temple/filters/static_analyzer.rb +1 -0
  26. data/lib/temple/filters/static_merger.rb +1 -0
  27. data/lib/temple/filters/string_splitter.rb +13 -1
  28. data/lib/temple/filters/validator.rb +1 -0
  29. data/lib/temple/generator.rb +5 -2
  30. data/lib/temple/generators/array.rb +1 -0
  31. data/lib/temple/generators/array_buffer.rb +1 -0
  32. data/lib/temple/generators/erb.rb +1 -0
  33. data/lib/temple/generators/rails_output_buffer.rb +4 -4
  34. data/lib/temple/generators/string_buffer.rb +2 -1
  35. data/lib/temple/grammar.rb +1 -0
  36. data/lib/temple/html/attribute_merger.rb +1 -0
  37. data/lib/temple/html/attribute_remover.rb +1 -0
  38. data/lib/temple/html/attribute_sorter.rb +1 -0
  39. data/lib/temple/html/dispatcher.rb +1 -0
  40. data/lib/temple/html/fast.rb +1 -0
  41. data/lib/temple/html/filter.rb +1 -0
  42. data/lib/temple/html/pretty.rb +1 -0
  43. data/lib/temple/html/safe.rb +1 -0
  44. data/lib/temple/map.rb +1 -0
  45. data/lib/temple/mixins/dispatcher.rb +1 -0
  46. data/lib/temple/mixins/engine_dsl.rb +1 -0
  47. data/lib/temple/mixins/grammar_dsl.rb +4 -2
  48. data/lib/temple/mixins/options.rb +1 -0
  49. data/lib/temple/mixins/template.rb +1 -0
  50. data/lib/temple/parser.rb +1 -0
  51. data/lib/temple/static_analyzer.rb +1 -0
  52. data/lib/temple/templates/rails.rb +7 -2
  53. data/lib/temple/templates/tilt.rb +2 -9
  54. data/lib/temple/templates.rb +1 -0
  55. data/lib/temple/utils.rb +5 -15
  56. data/lib/temple/version.rb +2 -1
  57. data/lib/temple.rb +3 -0
  58. data/temple.gemspec +4 -6
  59. metadata +10 -63
  60. data/.travis.yml +0 -30
  61. data/test/filters/test_code_merger.rb +0 -38
  62. data/test/filters/test_control_flow.rb +0 -90
  63. data/test/filters/test_dynamic_inliner.rb +0 -95
  64. data/test/filters/test_eraser.rb +0 -55
  65. data/test/filters/test_escapable.rb +0 -49
  66. data/test/filters/test_multi_flattener.rb +0 -33
  67. data/test/filters/test_static_analyzer.rb +0 -37
  68. data/test/filters/test_static_merger.rb +0 -41
  69. data/test/filters/test_string_splitter.rb +0 -25
  70. data/test/helper.rb +0 -30
  71. data/test/html/test_attribute_merger.rb +0 -76
  72. data/test/html/test_attribute_remover.rb +0 -43
  73. data/test/html/test_attribute_sorter.rb +0 -48
  74. data/test/html/test_fast.rb +0 -97
  75. data/test/html/test_pretty.rb +0 -55
  76. data/test/mixins/test_dispatcher.rb +0 -70
  77. data/test/mixins/test_grammar_dsl.rb +0 -86
  78. data/test/test_engine.rb +0 -189
  79. data/test/test_erb.rb +0 -61
  80. data/test/test_filter.rb +0 -29
  81. data/test/test_generator.rb +0 -158
  82. data/test/test_grammar.rb +0 -47
  83. data/test/test_map.rb +0 -39
  84. data/test/test_static_analyzer.rb +0 -39
  85. data/test/test_utils.rb +0 -39
@@ -1,37 +0,0 @@
1
- require '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
- @filter.call([:dynamic, '"#{"hello"}#{100}"']
12
- ).should.equal [:static, 'hello100']
13
- end
14
-
15
- it 'should not convert :dynamic if code is dynamic' do
16
- exp = [:dynamic, '"#{hello}#{100}"']
17
- @filter.call(exp).should.equal(exp)
18
- end
19
-
20
- it 'should not change number of newlines in generated code' do
21
- exp = [:dynamic, "[100,\n200,\n]"]
22
- @filter.call(exp).should.equal([:multi, [:static, '[100, 200]'], [:newline], [:newline]])
23
-
24
- @generator.call(@filter.call(exp)).count("\n").
25
- should.equal(@generator.call(exp).count("\n"))
26
- end
27
- else
28
- it 'should do nothing' do
29
- [
30
- [:dynamic, '"#{"hello"}#{100}"'],
31
- [:dynamic, '"#{hello}#{100}"'],
32
- ].each do |exp|
33
- @filter.call(exp).should.equal(exp)
34
- end
35
- end
36
- end
37
- end
@@ -1,41 +0,0 @@
1
- require '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
- @filter.call([:multi,
10
- [:static, "Hello "],
11
- [:static, "World, "],
12
- [:static, "Good night"]
13
- ]).should.equal [:static, "Hello World, Good night"]
14
- end
15
-
16
- it 'should merge serveral statics around code' do
17
- @filter.call([:multi,
18
- [:static, "Hello "],
19
- [:static, "World!"],
20
- [:code, "123"],
21
- [:static, "Good night, "],
22
- [:static, "everybody"]
23
- ]).should.equal [: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
- @filter.call([:multi,
32
- [:static, "Hello "],
33
- [:static, "World, "],
34
- [:newline],
35
- [:static, "Good night"]
36
- ]).should.equal [:multi,
37
- [:static, "Hello World, Good night"],
38
- [:newline]
39
- ]
40
- end
41
- end
@@ -1,25 +0,0 @@
1
- require '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
- it 'should split :dynamic with string literal' do
14
- @filter.call([:dynamic, '"static#{dynamic}"']
15
- ).should.equal [:multi, [:static, 'static'], [:dynamic, 'dynamic']]
16
- end
17
-
18
- describe '.compile' do
19
- it 'should raise CompileError for non-string literals' do
20
- lambda { Temple::Filters::StringSplitter.compile('1') }.
21
- should.raise(Temple::FilterError)
22
- end
23
- end
24
- end
25
- end
data/test/helper.rb DELETED
@@ -1,30 +0,0 @@
1
- require 'bacon'
2
- require 'temple'
3
-
4
- module TestHelper
5
- def with_html_safe
6
- require 'temple/html/safe'
7
- String.send(:define_method, :html_safe?) { false }
8
- String.send(:define_method, :html_safe) { Temple::HTML::SafeString.new(self) }
9
- yield
10
- ensure
11
- String.send(:undef_method, :html_safe?) if String.method_defined?(:html_safe?)
12
- String.send(:undef_method, :html_safe) if String.method_defined?(:html_safe)
13
- end
14
-
15
- def grammar_validate(grammar, exp, message)
16
- lambda { grammar.validate!(exp) }.should.raise(Temple::InvalidExpression).message.should.equal message
17
- end
18
-
19
- def erb(src, options = {})
20
- Temple::ERB::Template.new(options) { src }.render
21
- end
22
-
23
- def erubis(src, options = {})
24
- Tilt::ErubisTemplate.new(options) { src }.render
25
- end
26
- end
27
-
28
- class Bacon::Context
29
- include TestHelper
30
- end
@@ -1,76 +0,0 @@
1
- require '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
- @merger.call([:html, :tag,
10
- 'div',
11
- [:html, :attrs, [:html, :attr, 'class', [:static, 'b']]],
12
- [:content]
13
- ]).should.equal [: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
- @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
- ]).should.equal [: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
- @merger.call([:html, :tag,
35
- 'meta',
36
- [:html, :attrs, [:html, :attr, 'http-equiv', [:static, 'Content-Type']],
37
- [:html, :attr, 'content', [:static, '']]]
38
- ]).should.equal [: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
- @merger.call([:html, :tag,
46
- 'div',
47
- [:html, :attrs, [:html, :attr, 'id', [:dynamic, 'a']], [:html, :attr, 'id', [:dynamic, 'b']]],
48
- [:content]
49
- ]).should.equal [: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
- @merger.call([:html, :tag,
62
- 'div',
63
- [:html, :attrs, [:html, :attr, 'class', [:static, 'a']], [:html, :attr, 'class', [:dynamic, 'b']]],
64
- [:content]
65
- ]).should.equal [: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
-
@@ -1,43 +0,0 @@
1
- require 'helper'
2
-
3
- describe Temple::HTML::AttributeRemover do
4
- before do
5
- @remover = Temple::HTML::AttributeRemover.new
6
- end
7
-
8
- it 'should pass static attributes through' do
9
- @remover.call([:html, :tag,
10
- 'div',
11
- [:html, :attrs, [:html, :attr, 'class', [:static, 'b']]],
12
- [:content]
13
- ]).should.equal [:html, :tag, "div",
14
- [:multi,
15
- [:html, :attr, "class", [:static, "b"]]],
16
- [:content]]
17
- end
18
-
19
- it 'should check for empty dynamic attribute if it is included in :remove_empty_attrs' do
20
- @remover.call([:html, :tag,
21
- 'div',
22
- [:html, :attrs, [:html, :attr, 'class', [:dynamic, 'b']]],
23
- [:content]
24
- ]).should.equal [:html, :tag, "div",
25
- [:multi,
26
- [:multi,
27
- [:capture, "_temple_html_attributeremover1", [:dynamic, "b"]],
28
- [:if, "!_temple_html_attributeremover1.empty?",
29
- [:html, :attr, "class", [:dynamic, "_temple_html_attributeremover1"]]]]],
30
- [:content]]
31
- end
32
-
33
- it 'should not check for empty dynamic attribute if it is not included in :remove_empty_attrs' do
34
- @remover.call([:html, :tag,
35
- 'div',
36
- [:html, :attrs, [:html, :attr, 'name', [:dynamic, 'b']]],
37
- [:content]
38
- ]).should.equal [:html, :tag, "div",
39
- [:multi,
40
- [:html, :attr, "name", [:dynamic, "b"]]],
41
- [:content]]
42
- end
43
- end
@@ -1,48 +0,0 @@
1
- require 'helper'
2
-
3
- describe Temple::HTML::AttributeSorter do
4
- before do
5
- @ordered = Temple::HTML::AttributeSorter.new
6
- @unordered = Temple::HTML::AttributeSorter.new sort_attrs: false
7
- end
8
-
9
- it 'should sort html attributes by name by default, when :sort_attrs is true' do
10
- @ordered.call([:html, :tag,
11
- 'meta',
12
- [:html, :attrs, [:html, :attr, 'c', [:static, '1']],
13
- [:html, :attr, 'd', [:static, '2']],
14
- [:html, :attr, 'a', [:static, '3']],
15
- [:html, :attr, 'b', [:static, '4']]]
16
- ]).should.equal [:html, :tag, 'meta',
17
- [:html, :attrs,
18
- [:html, :attr, 'a', [:static, '3']],
19
- [:html, :attr, 'b', [:static, '4']],
20
- [:html, :attr, 'c', [:static, '1']],
21
- [:html, :attr, 'd', [:static, '2']]]]
22
- end
23
-
24
- it 'should preserve the order of html attributes when :sort_attrs is false' do
25
- @unordered.call([:html, :tag,
26
- 'meta',
27
- [:html, :attrs, [:html, :attr, 'c', [:static, '1']],
28
- [:html, :attr, 'd', [:static, '2']],
29
- [:html, :attr, 'a', [:static, '3']],
30
- [:html, :attr, 'b', [:static, '4']]]
31
- ]).should.equal [:html, :tag, 'meta',
32
- [:html, :attrs,
33
- [:html, :attr, 'c', [:static, '1']],
34
- [:html, :attr, 'd', [:static, '2']],
35
- [:html, :attr, 'a', [:static, '3']],
36
- [:html, :attr, 'b', [:static, '4']]]]
37
-
38
- # Use case:
39
- @unordered.call([:html, :tag,
40
- 'meta',
41
- [:html, :attrs, [:html, :attr, 'http-equiv', [:static, 'Content-Type']],
42
- [:html, :attr, 'content', [:static, '']]]
43
- ]).should.equal [:html, :tag, 'meta',
44
- [:html, :attrs,
45
- [:html, :attr, 'http-equiv', [:static, 'Content-Type']],
46
- [:html, :attr, 'content', [:static, '']]]]
47
- end
48
- end
@@ -1,97 +0,0 @@
1
- require 'helper'
2
-
3
- describe Temple::HTML::Fast do
4
- before do
5
- @html = Temple::HTML::Fast.new
6
- end
7
-
8
- it 'should compile html doctype' do
9
- @html.call([:multi, [:html, :doctype, '5']]).should.equal [:multi, [:static, '<!DOCTYPE html>']]
10
- @html.call([:multi, [:html, :doctype, 'html']]).should.equal [:multi, [:static, '<!DOCTYPE html>']]
11
- @html.call([:multi, [:html, :doctype, '1.1']]).should.equal [:multi,
12
- [:static, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">']]
13
- end
14
-
15
- it 'should compile xml encoding' do
16
- @html.call([:html, :doctype, 'xml latin1']).should.equal [:static, "<?xml version=\"1.0\" encoding=\"latin1\" ?>"]
17
- end
18
-
19
- it 'should compile html comment' do
20
- @html.call([:html, :comment, [:static, 'test']]).should.equal [:multi, [:static, "<!--"], [:static, "test"], [:static, "-->"]]
21
- end
22
-
23
- it 'should compile js wrapped in comments' do
24
- Temple::HTML::Fast.new(js_wrapper: nil).call([:html, :js, [:static, 'test']]).should.equal [:static, "test"]
25
- Temple::HTML::Fast.new(js_wrapper: :comment).call([:html, :js, [:static, 'test']]).should.equal [:multi, [:static, "<!--\n"], [:static, "test"], [:static, "\n//-->"]]
26
- Temple::HTML::Fast.new(js_wrapper: :cdata).call([:html, :js, [:static, 'test']]).should.equal [:multi, [:static, "\n//<![CDATA[\n"], [:static, "test"], [:static, "\n//]]>\n"]]
27
- Temple::HTML::Fast.new(js_wrapper: :both).call([:html, :js, [:static, 'test']]).should.equal [:multi, [:static, "<!--\n//<![CDATA[\n"], [:static, "test"], [:static, "\n//]]>\n//-->"]]
28
- end
29
-
30
- it 'should guess default js comment' do
31
- Temple::HTML::Fast.new(js_wrapper: :guess, format: :xhtml).call([:html, :js, [:static, 'test']]).should.equal [:multi, [:static, "\n//<![CDATA[\n"], [:static, "test"], [:static, "\n//]]>\n"]]
32
- Temple::HTML::Fast.new(js_wrapper: :guess, format: :html).call([:html, :js, [:static, 'test']]).should.equal [:multi, [:static, "<!--\n"], [:static, "test"], [:static, "\n//-->"]]
33
- end
34
-
35
- it 'should compile autoclosed html tag' do
36
- @html.call([:html, :tag,
37
- 'img', [:attrs],
38
- [:multi, [:newline]]
39
- ]).should.equal [:multi,
40
- [:static, "<img"],
41
- [:attrs],
42
- [:static, " />"],
43
- [:multi, [:newline]]]
44
- end
45
-
46
- it 'should compile explicitly closed html tag' do
47
- @html.call([:html, :tag,
48
- 'closed', [:attrs]
49
- ]).should.equal [:multi,
50
- [:static, "<closed"],
51
- [:attrs],
52
- [:static, " />"]]
53
- end
54
-
55
- it 'should compile html with content' do
56
- @html.call([:html, :tag,
57
- 'div', [:attrs], [:content]
58
- ]).should.equal [:multi,
59
- [:static, "<div"],
60
- [:attrs],
61
- [:static, ">"],
62
- [:content],
63
- [:static, "</div>"]]
64
- end
65
-
66
- it 'should compile html with attrs' do
67
- @html.call([:html, :tag,
68
- 'div',
69
- [:html, :attrs,
70
- [:html, :attr, 'id', [:static, 'test']],
71
- [:html, :attr, 'class', [:dynamic, 'block']]],
72
- [:content]
73
- ]).should.equal [:multi,
74
- [:static, "<div"],
75
- [:multi,
76
- [:multi, [:static, " id=\""], [:static, "test"], [:static, '"']],
77
- [:multi, [:static, " class=\""], [:dynamic, "block"], [:static, '"']]],
78
- [:static, ">"],
79
- [:content],
80
- [:static, "</div>"]]
81
- end
82
-
83
- it 'should keep codes intact' do
84
- exp = [:multi, [:code, 'foo']]
85
- @html.call(exp).should.equal exp
86
- end
87
-
88
- it 'should keep statics intact' do
89
- exp = [:multi, [:static, '<']]
90
- @html.call(exp).should.equal exp
91
- end
92
-
93
- it 'should keep dynamic intact' do
94
- exp = [:multi, [:dynamic, 'foo']]
95
- @html.call(exp).should.equal exp
96
- end
97
- end
@@ -1,55 +0,0 @@
1
- require 'helper'
2
-
3
- describe Temple::HTML::Pretty do
4
- before do
5
- @html = Temple::HTML::Pretty.new
6
- end
7
-
8
- it 'should indent nested tags' do
9
- @html.call([:html, :tag, 'div', [:multi],
10
- [:html, :tag, 'p', [:multi], [:multi, [:static, 'text'], [:dynamic, 'code']]]
11
- ]).should.equal [:multi,
12
- [:code, "_temple_html_pretty1 = /<code|<pre|<textarea/"],
13
- [:multi,
14
- [:static, "<div"],
15
- [:multi],
16
- [:static, ">"],
17
- [:multi,
18
- [:static, "\n <p"],
19
- [:multi],
20
- [:static, ">"],
21
- [:multi,
22
- [:static, "\n text"],
23
- [:dynamic, "::Temple::Utils.indent_dynamic((code), false, \"\\n \", _temple_html_pretty1)"]],
24
- [:static, "\n </p>"]],
25
- [:static, "\n</div>"]]]
26
- end
27
-
28
- it 'should not indent preformatted tags' do
29
- @html.call([:html, :tag, 'pre', [:multi],
30
- [:html, :tag, 'p', [:multi], [:static, 'text']]
31
- ]).should.equal [:multi,
32
- [:code, "_temple_html_pretty1 = /<code|<pre|<textarea/"],
33
- [:multi,
34
- [:static, "<pre"],
35
- [:multi],
36
- [:static, ">"],
37
- [:multi,
38
- [:static, "<p"],
39
- [:multi],
40
- [:static, ">"],
41
- [:static, "text"],
42
- [:static, "</p>"]],
43
- [:static, "</pre>"]]]
44
- end
45
-
46
- it 'should not escape html_safe strings' do
47
- with_html_safe do
48
- @html.call(
49
- [:dynamic, '"text<".html_safe']
50
- ).should.equal [:multi,
51
- [:code, "_temple_html_pretty1 = /<code|<pre|<textarea/"],
52
- [:dynamic, "::Temple::Utils.indent_dynamic((\"text<\".html_safe), nil, \"\\n\", _temple_html_pretty1)"]]
53
- end
54
- end
55
- end
@@ -1,70 +0,0 @@
1
- require 'helper'
2
-
3
- class FilterWithDispatcherMixin
4
- include Temple::Mixins::Dispatcher
5
-
6
- def on_test(arg)
7
- [:on_test, arg]
8
- end
9
-
10
- def on_test_check(arg)
11
- [:on_check, arg]
12
- end
13
-
14
- def on_second_test(arg)
15
- [:on_second_test, arg]
16
- end
17
-
18
- def on_a_b(*arg)
19
- [:on_ab, *arg]
20
- end
21
-
22
- def on_a_b_test(arg)
23
- [:on_ab_test, arg]
24
- end
25
-
26
- def on_a_b_c_d_test(arg)
27
- [:on_abcd_test, arg]
28
- end
29
- end
30
-
31
- class FilterWithDispatcherMixinAndOn < FilterWithDispatcherMixin
32
- def on(*args)
33
- [:on_zero, *args]
34
- end
35
- end
36
-
37
- describe Temple::Mixins::Dispatcher do
38
- before do
39
- @filter = FilterWithDispatcherMixin.new
40
- end
41
-
42
- it 'should return unhandled expressions' do
43
- @filter.call([:unhandled]).should.equal [:unhandled]
44
- end
45
-
46
- it 'should dispatch first level' do
47
- @filter.call([:test, 42]).should.equal [:on_test, 42]
48
- end
49
-
50
- it 'should dispatch second level' do
51
- @filter.call([:second, :test, 42]).should.equal [:on_second_test, 42]
52
- end
53
-
54
- it 'should dispatch second level if prefixed' do
55
- @filter.call([:test, :check, 42]).should.equal [:on_check, 42]
56
- end
57
-
58
- it 'should dispatch parent level' do
59
- @filter.call([:a, 42]).should == [:a, 42]
60
- @filter.call([:a, :b, 42]).should == [:on_ab, 42]
61
- @filter.call([:a, :b, :test, 42]).should == [:on_ab_test, 42]
62
- @filter.call([:a, :b, :c, 42]).should == [:on_ab, :c, 42]
63
- @filter.call([:a, :b, :c, :d, 42]).should == [:on_ab, :c, :d, 42]
64
- @filter.call([:a, :b, :c, :d, :test, 42]).should == [:on_abcd_test, 42]
65
- end
66
-
67
- it 'should dispatch zero level' do
68
- FilterWithDispatcherMixinAndOn.new.call([:foo,42]).should == [:on_zero, :foo, 42]
69
- end
70
- end
@@ -1,86 +0,0 @@
1
- require 'helper'
2
-
3
- module BasicGrammar
4
- extend Temple::Mixins::GrammarDSL
5
-
6
- Expression <<
7
- Symbol |
8
- Answer |
9
- [:zero_or_more, 'Expression*'] |
10
- [:one_or_more, 'Expression+'] |
11
- [:zero_or_one, 'Expression?'] |
12
- [:bool, Bool] |
13
- nil
14
-
15
- Bool <<
16
- true | false
17
-
18
- Answer <<
19
- Value(42)
20
-
21
- end
22
-
23
- module ExtendedGrammar
24
- extend BasicGrammar
25
-
26
- Expression << [:extended, Expression]
27
- end
28
-
29
- describe Temple::Mixins::GrammarDSL do
30
- it 'should support class types' do
31
- BasicGrammar.should.match :symbol
32
- BasicGrammar.should.not.match [:symbol]
33
- BasicGrammar.should.not.match 'string'
34
- BasicGrammar.should.not.match ['string']
35
- end
36
-
37
- it 'should support value types' do
38
- BasicGrammar.should.match 42
39
- BasicGrammar.should.not.match 43
40
- end
41
-
42
- it 'should support nesting' do
43
- BasicGrammar.should.match [:zero_or_more, [:zero_or_more]]
44
- end
45
-
46
- it 'should support *' do
47
- BasicGrammar.should.match [:zero_or_more]
48
- BasicGrammar.should.match [:zero_or_more, nil, 42]
49
- end
50
-
51
- it 'should support +' do
52
- BasicGrammar.should.not.match [:one_or_more]
53
- BasicGrammar.should.match [:one_or_more, 42]
54
- BasicGrammar.should.match [:one_or_more, 42, nil]
55
- end
56
-
57
- it 'should support ?' do
58
- BasicGrammar.should.not.match [:zero_or_one, nil, 42]
59
- BasicGrammar.should.match [:zero_or_one]
60
- BasicGrammar.should.match [:zero_or_one, 42]
61
- end
62
-
63
- it 'should support extended grammars' do
64
- ExtendedGrammar.should.match [:extended, [:extended, 42]]
65
- BasicGrammar.should.not.match [:zero_or_more, [:extended, nil]]
66
- BasicGrammar.should.not.match [:extended, [:extended, 42]]
67
- end
68
-
69
- it 'should have validate!' do
70
- grammar_validate BasicGrammar,
71
- [:zero_or_more, [:zero_or_more, [:unknown]]],
72
- "BasicGrammar::Expression did not match\n[:unknown]\n"
73
-
74
- grammar_validate BasicGrammar,
75
- [:zero_or_more, [:one_or_more]],
76
- "BasicGrammar::Expression did not match\n[:one_or_more]\n"
77
-
78
- grammar_validate BasicGrammar,
79
- [:zero_or_more, 123, [:unknown]],
80
- "BasicGrammar::Expression did not match\n123\n"
81
-
82
- grammar_validate BasicGrammar,
83
- [:bool, 123],
84
- "BasicGrammar::Bool did not match\n123\n"
85
- end
86
- end