tilt 2.0.0 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (96) hide show
  1. checksums.yaml +5 -5
  2. data/COPYING +2 -1
  3. data/bin/tilt +2 -110
  4. data/lib/tilt/_emacs_org.rb +2 -0
  5. data/lib/tilt/_handlebars.rb +2 -0
  6. data/lib/tilt/_jbuilder.rb +2 -0
  7. data/lib/tilt/_org.rb +2 -0
  8. data/lib/tilt/asciidoc.rb +11 -23
  9. data/lib/tilt/babel.rb +8 -0
  10. data/lib/tilt/builder.rb +23 -14
  11. data/lib/tilt/cli.rb +134 -0
  12. data/lib/tilt/coffee.rb +17 -25
  13. data/lib/tilt/commonmarker.rb +95 -0
  14. data/lib/tilt/creole.rb +9 -20
  15. data/lib/tilt/csv.rb +9 -21
  16. data/lib/tilt/erb.rb +27 -19
  17. data/lib/tilt/erubi.rb +55 -0
  18. data/lib/tilt/erubis.rb +20 -12
  19. data/lib/tilt/etanni.rb +5 -4
  20. data/lib/tilt/haml.rb +79 -42
  21. data/lib/tilt/kramdown.rb +8 -28
  22. data/lib/tilt/liquid.rb +11 -9
  23. data/lib/tilt/livescript.rb +11 -0
  24. data/lib/tilt/mapping.rb +242 -95
  25. data/lib/tilt/markaby.rb +5 -7
  26. data/lib/tilt/maruku.rb +6 -18
  27. data/lib/tilt/nokogiri.rb +11 -10
  28. data/lib/tilt/pandoc.rb +39 -0
  29. data/lib/tilt/pipeline.rb +19 -0
  30. data/lib/tilt/plain.rb +4 -19
  31. data/lib/tilt/prawn.rb +28 -0
  32. data/lib/tilt/radius.rb +15 -22
  33. data/lib/tilt/rdiscount.rb +17 -33
  34. data/lib/tilt/rdoc.rb +14 -35
  35. data/lib/tilt/redcarpet.rb +20 -93
  36. data/lib/tilt/redcloth.rb +9 -19
  37. data/lib/tilt/rst-pandoc.rb +10 -0
  38. data/lib/tilt/sass.rb +59 -22
  39. data/lib/tilt/slim.rb +5 -0
  40. data/lib/tilt/string.rb +9 -3
  41. data/lib/tilt/template.rb +246 -78
  42. data/lib/tilt/typescript.rb +19 -0
  43. data/lib/tilt/wikicloth.rb +8 -18
  44. data/lib/tilt/yajl.rb +5 -11
  45. data/lib/tilt.rb +91 -41
  46. metadata +39 -102
  47. data/CHANGELOG.md +0 -61
  48. data/Gemfile +0 -39
  49. data/HACKING +0 -16
  50. data/README.md +0 -206
  51. data/Rakefile +0 -95
  52. data/docs/TEMPLATES.md +0 -523
  53. data/docs/common.css +0 -14
  54. data/lib/tilt/bluecloth.rb +0 -24
  55. data/lib/tilt/less.rb +0 -38
  56. data/test/markaby/locals.mab +0 -1
  57. data/test/markaby/markaby.mab +0 -1
  58. data/test/markaby/markaby_other_static.mab +0 -1
  59. data/test/markaby/render_twice.mab +0 -1
  60. data/test/markaby/scope.mab +0 -1
  61. data/test/markaby/yielding.mab +0 -2
  62. data/test/test_helper.rb +0 -64
  63. data/test/tilt_asciidoctor_test.rb +0 -44
  64. data/test/tilt_blueclothtemplate_test.rb +0 -33
  65. data/test/tilt_buildertemplate_test.rb +0 -59
  66. data/test/tilt_cache_test.rb +0 -32
  67. data/test/tilt_coffeescripttemplate_test.rb +0 -104
  68. data/test/tilt_compilesite_test.rb +0 -51
  69. data/test/tilt_creoletemplate_test.rb +0 -24
  70. data/test/tilt_csv_test.rb +0 -65
  71. data/test/tilt_erbtemplate_test.rb +0 -239
  72. data/test/tilt_erubistemplate_test.rb +0 -151
  73. data/test/tilt_etannitemplate_test.rb +0 -174
  74. data/test/tilt_hamltemplate_test.rb +0 -144
  75. data/test/tilt_kramdown_test.rb +0 -20
  76. data/test/tilt_lesstemplate_test.less +0 -1
  77. data/test/tilt_lesstemplate_test.rb +0 -42
  78. data/test/tilt_liquidtemplate_test.rb +0 -78
  79. data/test/tilt_mapping_test.rb +0 -229
  80. data/test/tilt_markaby_test.rb +0 -88
  81. data/test/tilt_markdown_test.rb +0 -174
  82. data/test/tilt_marukutemplate_test.rb +0 -36
  83. data/test/tilt_metadata_test.rb +0 -42
  84. data/test/tilt_nokogiritemplate_test.rb +0 -87
  85. data/test/tilt_radiustemplate_test.rb +0 -75
  86. data/test/tilt_rdiscounttemplate_test.rb +0 -43
  87. data/test/tilt_rdoctemplate_test.rb +0 -29
  88. data/test/tilt_redcarpettemplate_test.rb +0 -59
  89. data/test/tilt_redclothtemplate_test.rb +0 -36
  90. data/test/tilt_sasstemplate_test.rb +0 -41
  91. data/test/tilt_stringtemplate_test.rb +0 -171
  92. data/test/tilt_template_test.rb +0 -316
  93. data/test/tilt_test.rb +0 -60
  94. data/test/tilt_wikiclothtemplate_test.rb +0 -32
  95. data/test/tilt_yajltemplate_test.rb +0 -101
  96. data/tilt.gemspec +0 -107
@@ -1,44 +0,0 @@
1
- require 'test_helper'
2
- require 'tilt'
3
-
4
- begin
5
- require 'tilt/asciidoc'
6
-
7
- class AsciidoctorTemplateTest < Minitest::Test
8
- HTML5_OUTPUT = "<div class=\"sect1\"><h2 id=\"_hello_world\">Hello World!</h2><div class=\"sectionbody\"></div></div>"
9
- DOCBOOK_OUTPUT = "<section id=\"_hello_world\"><title>Hello World!</title></section>"
10
-
11
- def strip_space(str)
12
- str.gsub(/>\s+</, '><').strip
13
- end
14
-
15
- test "registered for '.ad' files" do
16
- assert_equal Tilt::AsciidoctorTemplate, Tilt['ad']
17
- end
18
-
19
- test "registered for '.adoc' files" do
20
- assert_equal Tilt::AsciidoctorTemplate, Tilt['adoc']
21
- end
22
-
23
- test "registered for '.asciidoc' files" do
24
- assert_equal Tilt::AsciidoctorTemplate, Tilt['asciidoc']
25
- end
26
-
27
- test "preparing and evaluating html5 templates on #render" do
28
- template = Tilt::AsciidoctorTemplate.new(:attributes => {"backend" => 'html5'}) { |t| "== Hello World!" }
29
- assert_equal HTML5_OUTPUT, strip_space(template.render)
30
- end
31
-
32
- test "preparing and evaluating docbook templates on #render" do
33
- template = Tilt::AsciidoctorTemplate.new(:attributes => {"backend" => 'docbook'}) { |t| "== Hello World!" }
34
- assert_equal DOCBOOK_OUTPUT, strip_space(template.render)
35
- end
36
-
37
- test "can be rendered more than once" do
38
- template = Tilt::AsciidoctorTemplate.new(:attributes => {"backend" => 'html5'}) { |t| "== Hello World!" }
39
- 3.times { assert_equal HTML5_OUTPUT, strip_space(template.render) }
40
- end
41
- end
42
- rescue LoadError => boom
43
- warn "Tilt::AsciidoctorTemplate (disabled)"
44
- end
@@ -1,33 +0,0 @@
1
- require 'test_helper'
2
- require 'tilt'
3
-
4
- begin
5
- require 'tilt/bluecloth'
6
-
7
- class BlueClothTemplateTest < Minitest::Test
8
- test "preparing and evaluating templates on #render" do
9
- template = Tilt::BlueClothTemplate.new { |t| "# Hello World!" }
10
- assert_equal "<h1>Hello World!</h1>", template.render
11
- end
12
-
13
- test "can be rendered more than once" do
14
- template = Tilt::BlueClothTemplate.new { |t| "# Hello World!" }
15
- 3.times { assert_equal "<h1>Hello World!</h1>", template.render }
16
- end
17
-
18
- test "smartypants when :smart is set" do
19
- template = Tilt::BlueClothTemplate.new(:smartypants => true) { |t|
20
- "OKAY -- 'Smarty Pants'" }
21
- assert_equal "<p>OKAY &mdash; &lsquo;Smarty Pants&rsquo;</p>",
22
- template.render
23
- end
24
-
25
- test "stripping HTML when :filter_html is set" do
26
- template = Tilt::BlueClothTemplate.new(:escape_html => true) { |t|
27
- "HELLO <blink>WORLD</blink>" }
28
- assert_equal "<p>HELLO &lt;blink>WORLD&lt;/blink></p>", template.render
29
- end
30
- end
31
- rescue LoadError => boom
32
- warn "Tilt::BlueClothTemplate (disabled)"
33
- end
@@ -1,59 +0,0 @@
1
- require 'test_helper'
2
- require 'tilt'
3
-
4
- begin
5
- require 'tilt/builder'
6
- class BuilderTemplateTest < Minitest::Test
7
- test "registered for '.builder' files" do
8
- assert_equal Tilt::BuilderTemplate, Tilt['test.builder']
9
- assert_equal Tilt::BuilderTemplate, Tilt['test.xml.builder']
10
- end
11
-
12
- test "preparing and evaluating the template on #render" do
13
- template = Tilt::BuilderTemplate.new { |t| "xml.em 'Hello World!'" }
14
- assert_equal "<em>Hello World!</em>\n", template.render
15
- end
16
-
17
- test "can be rendered more than once" do
18
- template = Tilt::BuilderTemplate.new { |t| "xml.em 'Hello World!'" }
19
- 3.times { assert_equal "<em>Hello World!</em>\n", template.render }
20
- end
21
-
22
- test "passing locals" do
23
- template = Tilt::BuilderTemplate.new { "xml.em('Hey ' + name + '!')" }
24
- assert_equal "<em>Hey Joe!</em>\n", template.render(Object.new, :name => 'Joe')
25
- end
26
-
27
- test "evaluating in an object scope" do
28
- template = Tilt::BuilderTemplate.new { "xml.em('Hey ' + @name + '!')" }
29
- scope = Object.new
30
- scope.instance_variable_set :@name, 'Joe'
31
- assert_equal "<em>Hey Joe!</em>\n", template.render(scope)
32
- end
33
-
34
- test "passing a block for yield" do
35
- template = Tilt::BuilderTemplate.new { "xml.em('Hey ' + yield + '!')" }
36
- 3.times { assert_equal "<em>Hey Joe!</em>\n", template.render { 'Joe' }}
37
- end
38
-
39
- test "block style templates" do
40
- template =
41
- Tilt::BuilderTemplate.new do |t|
42
- lambda { |xml| xml.em('Hey Joe!') }
43
- end
44
- assert_equal "<em>Hey Joe!</em>\n", template.render
45
- end
46
-
47
- test "allows nesting raw XML" do
48
- subtemplate = Tilt::BuilderTemplate.new { "xml.em 'Hello World!'" }
49
- template = Tilt::BuilderTemplate.new { "xml.strong { xml << yield }" }
50
- 3.times do
51
- options = { :xml => Builder::XmlMarkup.new }
52
- assert_equal "<strong>\n<em>Hello World!</em>\n</strong>\n",
53
- template.render(options) { subtemplate.render(options) }
54
- end
55
- end
56
- end
57
- rescue LoadError
58
- warn "Tilt::BuilderTemplate (disabled)"
59
- end
@@ -1,32 +0,0 @@
1
- require 'test_helper'
2
- require 'tilt'
3
-
4
- class TiltCacheTest < Minitest::Test
5
- setup { @cache = Tilt::Cache.new }
6
-
7
- test "caching with single simple argument to #fetch" do
8
- template = nil
9
- result = @cache.fetch('hello') { template = Tilt::StringTemplate.new {''} }
10
- assert_same template, result
11
- result = @cache.fetch('hello') { fail 'should be cached' }
12
- assert_same template, result
13
- end
14
-
15
- test "caching with multiple complex arguments to #fetch" do
16
- template = nil
17
- result = @cache.fetch('hello', {:foo => 'bar', :baz => 'bizzle'}) { template = Tilt::StringTemplate.new {''} }
18
- assert_same template, result
19
- result = @cache.fetch('hello', {:foo => 'bar', :baz => 'bizzle'}) { fail 'should be cached' }
20
- assert_same template, result
21
- end
22
-
23
- test "clearing the cache with #clear" do
24
- template, other = nil
25
- result = @cache.fetch('hello') { template = Tilt::StringTemplate.new {''} }
26
- assert_same template, result
27
-
28
- @cache.clear
29
- result = @cache.fetch('hello') { other = Tilt::StringTemplate.new {''} }
30
- assert_same other, result
31
- end
32
- end
@@ -1,104 +0,0 @@
1
- require 'test_helper'
2
- require 'tilt'
3
-
4
- begin
5
- require 'tilt/coffee'
6
-
7
- class CoffeeScriptTemplateTest < Minitest::Test
8
-
9
- test "is registered for '.coffee' files" do
10
- assert_equal Tilt::CoffeeScriptTemplate, Tilt['test.coffee']
11
- end
12
-
13
- test "bare is disabled by default" do
14
- assert_equal false, Tilt::CoffeeScriptTemplate.default_bare
15
- end
16
-
17
- test "compiles and evaluates the template on #render" do
18
- template = Tilt::CoffeeScriptTemplate.new { |t| "puts 'Hello, World!'\n" }
19
- assert_match "puts('Hello, World!');", template.render
20
- end
21
-
22
- test "can be rendered more than once" do
23
- template = Tilt::CoffeeScriptTemplate.new { |t| "puts 'Hello, World!'\n" }
24
- 3.times { assert_match "puts('Hello, World!');", template.render }
25
- end
26
-
27
- test "disabling coffee-script wrapper" do
28
- str = 'name = "Josh"; puts "Hello #{name}"'
29
-
30
- template = Tilt::CoffeeScriptTemplate.new { str }
31
- assert_match "(function() {", template.render
32
- assert_match "puts(\"Hello \" + name);\n", template.render
33
-
34
- template = Tilt::CoffeeScriptTemplate.new(:bare => true) { str }
35
- refute_match "(function() {", template.render
36
- assert_equal "var name;\n\nname = \"Josh\";\n\nputs(\"Hello \" + name);\n", template.render
37
-
38
- template2 = Tilt::CoffeeScriptTemplate.new(:no_wrap => true) { str}
39
- refute_match "(function() {", template.render
40
- assert_equal "var name;\n\nname = \"Josh\";\n\nputs(\"Hello \" + name);\n", template.render
41
- end
42
-
43
- context "wrapper globally enabled" do
44
- setup do
45
- @bare = Tilt::CoffeeScriptTemplate.default_bare
46
- Tilt::CoffeeScriptTemplate.default_bare = false
47
- end
48
-
49
- teardown do
50
- Tilt::CoffeeScriptTemplate.default_bare = @bare
51
- end
52
-
53
- test "no options" do
54
- template = Tilt::CoffeeScriptTemplate.new { |t| 'name = "Josh"; puts "Hello, #{name}"' }
55
- assert_match "puts(\"Hello, \" + name);", template.render
56
- assert_match "(function() {", template.render
57
- end
58
-
59
- test "overridden by :bare" do
60
- template = Tilt::CoffeeScriptTemplate.new(:bare => true) { |t| 'name = "Josh"; puts "Hello, #{name}"' }
61
- assert_match "puts(\"Hello, \" + name);", template.render
62
- refute_match "(function() {", template.render
63
- end
64
-
65
- test "overridden by :no_wrap" do
66
- template = Tilt::CoffeeScriptTemplate.new(:no_wrap => true) { |t| 'name = "Josh"; puts "Hello, #{name}"' }
67
- assert_match "puts(\"Hello, \" + name);", template.render
68
- refute_match "(function() {", template.render
69
- end
70
- end
71
-
72
- context "wrapper globally disabled" do
73
- setup do
74
- @bare = Tilt::CoffeeScriptTemplate.default_bare
75
- Tilt::CoffeeScriptTemplate.default_bare = true
76
- end
77
-
78
- teardown do
79
- Tilt::CoffeeScriptTemplate.default_bare = @bare
80
- end
81
-
82
- test "no options" do
83
- template = Tilt::CoffeeScriptTemplate.new { |t| 'name = "Josh"; puts "Hello, #{name}"' }
84
- assert_match "puts(\"Hello, \" + name);", template.render
85
- refute_match "(function() {", template.render
86
- end
87
-
88
- test "overridden by :bare" do
89
- template = Tilt::CoffeeScriptTemplate.new(:bare => false) { |t| 'name = "Josh"; puts "Hello, #{name}"' }
90
- assert_match "puts(\"Hello, \" + name);", template.render
91
- assert_match "(function() {", template.render
92
- end
93
-
94
- test "overridden by :no_wrap" do
95
- template = Tilt::CoffeeScriptTemplate.new(:no_wrap => false) { |t| 'name = "Josh"; puts "Hello, #{name}"' }
96
- assert_match "puts(\"Hello, \" + name);", template.render
97
- assert_match "(function() {", template.render
98
- end
99
- end
100
- end
101
-
102
- rescue LoadError => boom
103
- warn "Tilt::CoffeeScriptTemplate (disabled)"
104
- end
@@ -1,51 +0,0 @@
1
- require 'test_helper'
2
- require 'tilt'
3
- require 'thread'
4
-
5
- class CompileSiteTest < Minitest::Test
6
- def setup
7
- GC.start
8
- end
9
-
10
- class CompilingTemplate < Tilt::Template
11
- def prepare
12
- end
13
-
14
- def precompiled_template(locals)
15
- @data.inspect
16
- end
17
- end
18
-
19
- class Scope
20
- end
21
-
22
- test "compiling template source to a method" do
23
- template = CompilingTemplate.new { |t| "Hello World!" }
24
- template.render(Scope.new)
25
- method = template.send(:compiled_method, [])
26
- assert_kind_of UnboundMethod, method
27
- end
28
-
29
- # This test attempts to surface issues with compiling templates from
30
- # multiple threads.
31
- test "using compiled templates from multiple threads" do
32
- template = CompilingTemplate.new { 'template' }
33
- main_thread = Thread.current
34
- 10.times do |i|
35
- threads =
36
- (1..50).map do |j|
37
- Thread.new {
38
- begin
39
- locals = { "local#{i}" => 'value' }
40
- res = template.render(self, locals)
41
- thread_id = Thread.current.object_id
42
- res = template.render(self, "local#{thread_id.abs.to_s}" => 'value')
43
- rescue => boom
44
- main_thread.raise(boom)
45
- end
46
- }
47
- end
48
- threads.each { |t| t.join }
49
- end
50
- end
51
- end
@@ -1,24 +0,0 @@
1
- require 'test_helper'
2
- require 'tilt'
3
-
4
- begin
5
- require 'tilt/creole'
6
-
7
- class CreoleTemplateTest < Minitest::Test
8
- test "is registered for '.creole' files" do
9
- assert_equal Tilt::CreoleTemplate, Tilt['test.creole']
10
- end
11
-
12
- test "compiles and evaluates the template on #render" do
13
- template = Tilt::CreoleTemplate.new { |t| "= Hello World!" }
14
- assert_equal "<h1>Hello World!</h1>", template.render
15
- end
16
-
17
- test "can be rendered more than once" do
18
- template = Tilt::CreoleTemplate.new { |t| "= Hello World!" }
19
- 3.times { assert_equal "<h1>Hello World!</h1>", template.render }
20
- end
21
- end
22
- rescue LoadError => boom
23
- warn "Tilt::CreoleTemplate (disabled)"
24
- end
@@ -1,65 +0,0 @@
1
- require 'test_helper'
2
- require 'tilt'
3
-
4
- begin
5
- require 'tilt/csv'
6
-
7
- class CSVTemplateTest < Minitest::Test
8
-
9
- test "registered for '.rcsv' files" do
10
- assert_equal Tilt::CSVTemplate, Tilt['rcsv']
11
- end
12
-
13
- test "compiles and evaluates the template on #render" do
14
- template = Tilt::CSVTemplate.new { "csv << ['hello', 'world']" }
15
- assert_equal "hello,world\n", template.render
16
- end
17
-
18
- test "can be rendered more than once" do
19
- template = Tilt::CSVTemplate.new { "csv << [1,2,3]" }
20
- 3.times { assert_equal "1,2,3\n", template.render }
21
- end
22
-
23
- test "can pass locals" do
24
- template = Tilt::CSVTemplate.new { 'csv << [1, name]' }
25
- assert_equal "1,Joe\n", template.render(Object.new, :name => 'Joe')
26
- end
27
-
28
- test "evaluating in an object scope" do
29
- template = Tilt::CSVTemplate.new { 'csv << [1, @name]' }
30
- scope = Object.new
31
- scope.instance_variable_set :@name, 'Joe'
32
- assert_equal "1,Joe\n", template.render(scope)
33
- end
34
-
35
- test "backtrace file and line reporting" do
36
- data = File.read(__FILE__).split("\n__END__\n").last
37
- template = Tilt::CSVTemplate.new('test.csv') { data }
38
- begin
39
- template.render
40
- fail 'should have raised an exception'
41
- rescue => boom
42
- assert_kind_of NameError, boom
43
- line = boom.backtrace.grep(/^test\.csv:/).first
44
- assert line, "Backtrace didn't contain test.csv"
45
- file, line, meth = line.split(":")
46
- assert_equal '4', line
47
- end
48
- end
49
-
50
- end
51
-
52
- rescue LoadError => boom
53
- warn "Tilt::CSVTemplate (disabled) please install 'fastercsv' if using ruby 1.8.x"
54
- end
55
-
56
-
57
- __END__
58
- # header
59
- csv << ['Type', 'Age']
60
-
61
- raise NameError
62
-
63
- # rows
64
- csv << ['Frog', 2]
65
- csv << ['Cat', 5]
@@ -1,239 +0,0 @@
1
- # coding: utf-8
2
- require 'test_helper'
3
- require 'tilt'
4
- require 'tilt/erb'
5
- require 'tempfile'
6
-
7
- class ERBTemplateTest < Minitest::Test
8
- test "registered for '.erb' files" do
9
- assert_includes Tilt.lazy_map['erb'], ['Tilt::ERBTemplate', 'tilt/erb']
10
- end
11
-
12
- test "registered for '.rhtml' files" do
13
- assert_includes Tilt.lazy_map['rhtml'], ['Tilt::ERBTemplate', 'tilt/erb']
14
- end
15
-
16
- test "loading and evaluating templates on #render" do
17
- template = Tilt::ERBTemplate.new { |t| "Hello World!" }
18
- assert_equal "Hello World!", template.render
19
- end
20
-
21
- test "can be rendered more than once" do
22
- template = Tilt::ERBTemplate.new { |t| "Hello World!" }
23
- 3.times { assert_equal "Hello World!", template.render }
24
- end
25
-
26
- test "passing locals" do
27
- template = Tilt::ERBTemplate.new { 'Hey <%= name %>!' }
28
- assert_equal "Hey Joe!", template.render(Object.new, :name => 'Joe')
29
- end
30
-
31
- test "evaluating in an object scope" do
32
- template = Tilt::ERBTemplate.new { 'Hey <%= @name %>!' }
33
- scope = Object.new
34
- scope.instance_variable_set :@name, 'Joe'
35
- assert_equal "Hey Joe!", template.render(scope)
36
- end
37
-
38
- class MockOutputVariableScope
39
- attr_accessor :exposed_buffer
40
- end
41
-
42
- test "exposing the buffer to the template by default" do
43
- begin
44
- Tilt::ERBTemplate.default_output_variable = '@_out_buf'
45
- template = Tilt::ERBTemplate.new { '<% self.exposed_buffer = @_out_buf %>hey' }
46
- scope = MockOutputVariableScope.new
47
- template.render(scope)
48
- refute_nil scope.exposed_buffer
49
- assert_equal scope.exposed_buffer, 'hey'
50
- ensure
51
- Tilt::ERBTemplate.default_output_variable = '_erbout'
52
- end
53
- end
54
-
55
- test "passing a block for yield" do
56
- template = Tilt::ERBTemplate.new { 'Hey <%= yield %>!' }
57
- assert_equal "Hey Joe!", template.render { 'Joe' }
58
- end
59
-
60
- test "backtrace file and line reporting without locals" do
61
- data = File.read(__FILE__).split("\n__END__\n").last
62
- fail unless data[0] == ?<
63
- template = Tilt::ERBTemplate.new('test.erb', 11) { data }
64
- begin
65
- template.render
66
- fail 'should have raised an exception'
67
- rescue => boom
68
- assert_kind_of NameError, boom
69
- line = boom.backtrace.grep(/^test\.erb:/).first
70
- assert line, "Backtrace didn't contain test.erb"
71
- file, line, meth = line.split(":")
72
- assert_equal '13', line
73
- end
74
- end
75
-
76
- test "backtrace file and line reporting with locals" do
77
- data = File.read(__FILE__).split("\n__END__\n").last
78
- fail unless data[0] == ?<
79
- template = Tilt::ERBTemplate.new('test.erb', 1) { data }
80
- begin
81
- template.render(nil, :name => 'Joe', :foo => 'bar')
82
- fail 'should have raised an exception'
83
- rescue => boom
84
- assert_kind_of RuntimeError, boom
85
- line = boom.backtrace.first
86
- file, line, meth = line.split(":")
87
- assert_equal 'test.erb', file
88
- assert_equal '6', line
89
- end
90
- end
91
-
92
- test "explicit disabling of trim mode" do
93
- template = Tilt::ERBTemplate.new('test.erb', 1, :trim => false) { "\n<%= 1 + 1 %>\n" }
94
- assert_equal "\n2\n", template.render
95
- end
96
-
97
- test "default stripping trim mode" do
98
- template = Tilt::ERBTemplate.new('test.erb', 1) { "\n<%= 1 + 1 %>\n" }
99
- assert_equal "\n2", template.render
100
- end
101
-
102
- test "stripping trim mode" do
103
- template = Tilt::ERBTemplate.new('test.erb', 1, :trim => '-') { "\n<%= 1 + 1 -%>\n" }
104
- assert_equal "\n2", template.render
105
- end
106
-
107
- test "shorthand whole line syntax trim mode" do
108
- template = Tilt::ERBTemplate.new('test.erb', :trim => '%') { "\n% if true\nhello\n%end\n" }
109
- assert_equal "\nhello\n", template.render
110
- end
111
-
112
- test "using an instance variable as the outvar" do
113
- template = Tilt::ERBTemplate.new(nil, :outvar => '@buf') { "<%= 1 + 1 %>" }
114
- scope = Object.new
115
- scope.instance_variable_set(:@buf, 'original value')
116
- assert_equal '2', template.render(scope)
117
- assert_equal 'original value', scope.instance_variable_get(:@buf)
118
- end
119
- end
120
-
121
- class CompiledERBTemplateTest < Minitest::Test
122
- def teardown
123
- GC.start
124
- end
125
-
126
- class Scope
127
- end
128
-
129
- test "compiling template source to a method" do
130
- template = Tilt::ERBTemplate.new { |t| "Hello World!" }
131
- template.render(Scope.new)
132
- method = template.send(:compiled_method, [])
133
- assert_kind_of UnboundMethod, method
134
- end
135
-
136
- test "loading and evaluating templates on #render" do
137
- template = Tilt::ERBTemplate.new { |t| "Hello World!" }
138
- assert_equal "Hello World!", template.render(Scope.new)
139
- assert_equal "Hello World!", template.render(Scope.new)
140
- end
141
-
142
- test "passing locals" do
143
- template = Tilt::ERBTemplate.new { 'Hey <%= name %>!' }
144
- assert_equal "Hey Joe!", template.render(Scope.new, :name => 'Joe')
145
- end
146
-
147
- test "evaluating in an object scope" do
148
- template = Tilt::ERBTemplate.new { 'Hey <%= @name %>!' }
149
- scope = Scope.new
150
- scope.instance_variable_set :@name, 'Joe'
151
- assert_equal "Hey Joe!", template.render(scope)
152
- scope.instance_variable_set :@name, 'Jane'
153
- assert_equal "Hey Jane!", template.render(scope)
154
- end
155
-
156
- test "passing a block for yield" do
157
- template = Tilt::ERBTemplate.new { 'Hey <%= yield %>!' }
158
- assert_equal "Hey Joe!", template.render(Scope.new) { 'Joe' }
159
- assert_equal "Hey Jane!", template.render(Scope.new) { 'Jane' }
160
- end
161
-
162
- test "backtrace file and line reporting without locals" do
163
- data = File.read(__FILE__).split("\n__END__\n").last
164
- fail unless data[0] == ?<
165
- template = Tilt::ERBTemplate.new('test.erb', 11) { data }
166
- begin
167
- template.render(Scope.new)
168
- fail 'should have raised an exception'
169
- rescue => boom
170
- assert_kind_of NameError, boom
171
- line = boom.backtrace.grep(/^test\.erb:/).first
172
- assert line, "Backtrace didn't contain test.erb"
173
- file, line, meth = line.split(":")
174
- assert_equal '13', line
175
- end
176
- end
177
-
178
- test "backtrace file and line reporting with locals" do
179
- data = File.read(__FILE__).split("\n__END__\n").last
180
- fail unless data[0] == ?<
181
- template = Tilt::ERBTemplate.new('test.erb') { data }
182
- begin
183
- template.render(Scope.new, :name => 'Joe', :foo => 'bar')
184
- fail 'should have raised an exception'
185
- rescue => boom
186
- assert_kind_of RuntimeError, boom
187
- line = boom.backtrace.first
188
- file, line, meth = line.split(":")
189
- assert_equal 'test.erb', file
190
- assert_equal '6', line
191
- end
192
- end
193
-
194
- test "default stripping trim mode" do
195
- template = Tilt::ERBTemplate.new('test.erb') { "\n<%= 1 + 1 %>\n" }
196
- assert_equal "\n2", template.render(Scope.new)
197
- end
198
-
199
- test "stripping trim mode" do
200
- template = Tilt::ERBTemplate.new('test.erb', :trim => '-') { "\n<%= 1 + 1 -%>\n" }
201
- assert_equal "\n2", template.render(Scope.new)
202
- end
203
-
204
- test "shorthand whole line syntax trim mode" do
205
- template = Tilt::ERBTemplate.new('test.erb', :trim => '%') { "\n% if true\nhello\n%end\n" }
206
- assert_equal "\nhello\n", template.render(Scope.new)
207
- end
208
-
209
- test "encoding with magic comment" do
210
- f = Tempfile.open("template")
211
- f.puts('<%# coding: UTF-8 %>')
212
- f.puts('ふが <%= @hoge %>')
213
- f.close()
214
- @hoge = "ほげ"
215
- erb = Tilt::ERBTemplate.new(f.path)
216
- 3.times { erb.render(self) }
217
- f.delete
218
- end
219
-
220
- test "encoding with :default_encoding" do
221
- f = Tempfile.open("template")
222
- f.puts('ふが <%= @hoge %>')
223
- f.close()
224
- @hoge = "ほげ"
225
- erb = Tilt::ERBTemplate.new(f.path, :default_encoding => 'UTF-8')
226
- 3.times { erb.render(self) }
227
- f.delete
228
- end
229
- end
230
-
231
- __END__
232
- <html>
233
- <body>
234
- <h1>Hey <%= name %>!</h1>
235
-
236
-
237
- <p><% fail %></p>
238
- </body>
239
- </html>