From e3e5279357a6765447398b734b3de22fd9be835c Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Fri, 5 Aug 2022 15:32:04 -0700 Subject: [PATCH] Switch to minitest/spec Instead of building a poor clone of minitest/spec inside test_helper, just use minitest/spec since it is already loaded (by minitest/autorun). This eliminates top-level constants being defined by tilt_template_test.rb. The helper classes used in the file are now anonymous classes assigned to local variables. In addition to switch to mintest/spec, the following fixes are made: 1. Avoid displaying warning when testing deprecated Tilt::ERBTemplate.default_output_variable in the Erubis tests (similar change made previously in the ERB tests). 2. The pandoc tests in tilt_markdown_test.rb are not run if nokogiri is not installed (the tests depend on nokogiri). 3. Avoid loading tilt/sigil if sigil is not installed. 4. Paths adjusted in the markaby tests in order to get them passing in my environment. In my environment, there are test failures if both bluecoth and rdiscount are installed, since they both define the same non-static function (markdown_version). If either is installed, but not both, the related tests pass (this was true both before and after the change to minitest/spec). The diff is best viewed with --color-words -b. --- test/test_helper.rb | 57 +---- test/tilt_asciidoctor_test.rb | 26 +- test/tilt_babeltemplate.rb | 12 +- test/tilt_blueclothtemplate_test.rb | 10 +- test/tilt_buildertemplate_test.rb | 23 +- test/tilt_cache_test.rb | 12 +- test/tilt_coffeescripttemplate_test.rb | 188 +++++++------- test/tilt_commonmarkertemplate_test.rb | 10 +- test/tilt_compilesite_test.rb | 21 +- test/tilt_creoletemplate_test.rb | 8 +- test/tilt_csv_test.rb | 19 +- test/tilt_erbtemplate_test.rb | 91 ++++--- test/tilt_erubistemplate_test.rb | 40 +-- test/tilt_erubitemplate_test.rb | 37 +-- test/tilt_etannitemplate_test.rb | 68 +++-- test/tilt_hamltemplate_test.rb | 67 +++-- test/tilt_kramdown_test.rb | 6 +- test/tilt_lesstemplate_test.less | 2 +- test/tilt_lesstemplate_test.rb | 10 +- test/tilt_liquidtemplate_test.rb | 20 +- test/tilt_livescripttemplate_test.rb | 12 +- test/tilt_mapping_test.rb | 331 ++++++++++++------------- test/tilt_markaby_test.rb | 42 ++-- test/tilt_markdown_test.rb | 113 +++++---- test/tilt_marukutemplate_test.rb | 10 +- test/tilt_metadata_test.rb | 57 ++--- test/tilt_nokogiritemplate_test.rb | 21 +- test/tilt_pandoctemplate_test.rb | 26 +- test/tilt_pipeline_test.rb | 28 +-- test/tilt_prawntemplate.prawn | 2 +- test/tilt_prawntemplate_test.rb | 34 +-- test/tilt_radiustemplate_test.rb | 24 +- test/tilt_rdiscounttemplate_test.rb | 12 +- test/tilt_rdoctemplate_test.rb | 9 +- test/tilt_redcarpettemplate_test.rb | 16 +- test/tilt_redclothtemplate_test.rb | 12 +- test/tilt_rstpandoctemplate_test.rb | 10 +- test/tilt_sasstemplate_test.rb | 16 +- test/tilt_sigil_test.rb | 15 +- test/tilt_stringtemplate_test.rb | 66 +++-- test/tilt_template_test.rb | 215 ++++++++-------- test/tilt_test.rb | 46 ++-- test/tilt_typescript_test.rb | 14 +- test/tilt_wikiclothtemplate_test.rb | 12 +- test/tilt_yajltemplate_test.rb | 28 +-- 45 files changed, 917 insertions(+), 981 deletions(-) diff --git a/test/test_helper.rb b/test/test_helper.rb index 9b5667e5..3bd59fce 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -6,42 +6,7 @@ require 'minitest/autorun' require 'minitest/mock' -# Contest adds +teardown+, +test+ and +context+ as class methods, and the -# instance methods +setup+ and +teardown+ now iterate on the corresponding -# blocks. Note that all setup and teardown blocks must be defined with the -# block syntax. Adding setup or teardown instance methods defeats the purpose -# of this library. -class Minitest::Test - def self.setup(&block) - define_method :setup do - super(&block) - instance_eval(&block) - end - end - - def self.teardown(&block) - define_method :teardown do - instance_eval(&block) - super(&block) - end - end - - def self.context(name, &block) - subclass = Class.new(self) - remove_tests(subclass) - subclass.class_eval(&block) if block_given? - const_set(context_name(name), subclass) - end - - def self.test(name, &block) - define_method(test_name(name), &block) - end - - class << self - alias_method :should, :test - alias_method :describe, :context - end - +class Minitest::Spec # Returns true if line numbers are reported incorrectly in heredocs. def heredoc_line_number_bug? # https://github.com/jruby/jruby/issues/7272 @@ -63,24 +28,4 @@ def with_default_encoding(encoding) def with_utf8_default_encoding(&block) with_default_encoding('UTF-8', &block) end - -private - - def self.context_name(name) - "Test#{sanitize_name(name).gsub(/(^| )(\w)/) { $2.upcase }}".to_sym - end - - def self.test_name(name) - "test_#{sanitize_name(name).gsub(/\s+/,'_')}".to_sym - end - - def self.sanitize_name(name) - name.gsub(/\W+/, ' ').strip - end - - def self.remove_tests(subclass) - subclass.public_instance_methods.grep(/^test_/).each do |meth| - subclass.send(:undef_method, meth.to_sym) - end - end end diff --git a/test/tilt_asciidoctor_test.rb b/test/tilt_asciidoctor_test.rb index d9e80d95..98c7a006 100644 --- a/test/tilt_asciidoctor_test.rb +++ b/test/tilt_asciidoctor_test.rb @@ -3,46 +3,46 @@ begin require 'tilt/asciidoc' - class AsciidoctorTemplateTest < Minitest::Test - HTML5_OUTPUT = "

Hello World!

" - DOCBOOK5_OUTPUT = "
Hello World!
" + describe 'tilt/asciidoctor' do + html5_output = "

Hello World!

" + docbook5_output = "
Hello World!
" def strip_space(str) str.gsub(/>\s+<').strip end - test "registered for '.ad' files" do + it "registered for '.ad' files" do assert_equal Tilt::AsciidoctorTemplate, Tilt['ad'] end - test "registered for '.adoc' files" do + it "registered for '.adoc' files" do assert_equal Tilt::AsciidoctorTemplate, Tilt['adoc'] end - test "registered for '.asciidoc' files" do + it "registered for '.asciidoc' files" do assert_equal Tilt::AsciidoctorTemplate, Tilt['asciidoc'] end - test "#extensions_for returns a unique list of extensions" do + it "#extensions_for returns a unique list of extensions" do Tilt.default_mapping.extensions_for(Tilt::AsciidoctorTemplate).each do |ext| Tilt[ext] end assert_equal ['ad', 'adoc', 'asciidoc'], Tilt.default_mapping.extensions_for(Tilt::AsciidoctorTemplate).sort end - test "preparing and evaluating html5 templates on #render" do + it "preparing and evaluating html5 templates on #render" do template = Tilt::AsciidoctorTemplate.new(:attributes => {"backend" => 'html5'}) { |t| "== Hello World!" } - assert_equal HTML5_OUTPUT, strip_space(template.render) + assert_equal html5_output, strip_space(template.render) end - test "preparing and evaluating docbook 5 templates on #render" do + it "preparing and evaluating docbook 5 templates on #render" do template = Tilt::AsciidoctorTemplate.new(:attributes => {"backend" => 'docbook5'}) { |t| "== Hello World!" } - assert_equal DOCBOOK5_OUTPUT, strip_space(template.render) + assert_equal docbook5_output, strip_space(template.render) end - test "can be rendered more than once" do + it "can be rendered more than once" do template = Tilt::AsciidoctorTemplate.new(:attributes => {"backend" => 'html5'}) { |t| "== Hello World!" } - 3.times { assert_equal HTML5_OUTPUT, strip_space(template.render) } + 3.times { assert_equal html5_output, strip_space(template.render) } end end rescue LoadError diff --git a/test/tilt_babeltemplate.rb b/test/tilt_babeltemplate.rb index 30456ef6..c6af4759 100644 --- a/test/tilt_babeltemplate.rb +++ b/test/tilt_babeltemplate.rb @@ -3,27 +3,27 @@ begin require 'tilt/babel' - class BabelTemplateTest < Minitest::Test - test "registered for '.es6' files" do + describe 'tilt/babel' do + it "registered for '.es6' files" do assert_equal Tilt::BabelTemplate, Tilt['es6'] end - test "registered for '.babel' files" do + it "registered for '.babel' files" do assert_equal Tilt::BabelTemplate, Tilt['babel'] end - test "registered for '.jsx' files" do + it "registered for '.jsx' files" do assert_equal Tilt::BabelTemplate, Tilt['jsx'] end - test "basic ES6 features" do + it "basic ES6 features" do with_utf8_default_encoding do template = Tilt::BabelTemplate.new { "square = (x) => x * x" } assert_match "function", template.render end end - test "JSX support" do + it "JSX support" do with_utf8_default_encoding do template = Tilt::BabelTemplate.new { "" } assert_match "React.createElement", template.render diff --git a/test/tilt_blueclothtemplate_test.rb b/test/tilt_blueclothtemplate_test.rb index 227944dd..34cda08e 100644 --- a/test/tilt_blueclothtemplate_test.rb +++ b/test/tilt_blueclothtemplate_test.rb @@ -3,25 +3,25 @@ begin require 'tilt/bluecloth' - class BlueClothTemplateTest < Minitest::Test - test "preparing and evaluating templates on #render" do + describe 'tilt/bluecloth' do + it "preparing and evaluating templates on #render" do template = Tilt::BlueClothTemplate.new { |t| "# Hello World!" } assert_equal "

Hello World!

", template.render end - test "can be rendered more than once" do + it "can be rendered more than once" do template = Tilt::BlueClothTemplate.new { |t| "# Hello World!" } 3.times { assert_equal "

Hello World!

", template.render } end - test "smartypants when :smart is set" do + it "smartypants when :smart is set" do template = Tilt::BlueClothTemplate.new(:smartypants => true) { |t| "OKAY -- 'Smarty Pants'" } assert_equal "

OKAY — ‘Smarty Pants’

", template.render end - test "stripping HTML when :filter_html is set" do + it "stripping HTML when :filter_html is set" do template = Tilt::BlueClothTemplate.new(:escape_html => true) { |t| "HELLO WORLD" } assert_equal "

HELLO <blink>WORLD</blink>

", template.render diff --git a/test/tilt_buildertemplate_test.rb b/test/tilt_buildertemplate_test.rb index e9a200f1..cef61843 100644 --- a/test/tilt_buildertemplate_test.rb +++ b/test/tilt_buildertemplate_test.rb @@ -2,40 +2,41 @@ begin require 'tilt/builder' - class BuilderTemplateTest < Minitest::Test - test "registered for '.builder' files" do + + describe 'tilt/builder' do + it "registered for '.builder' files" do assert_equal Tilt::BuilderTemplate, Tilt['test.builder'] assert_equal Tilt::BuilderTemplate, Tilt['test.xml.builder'] end - test "preparing and evaluating the template on #render" do + it "preparing and evaluating the template on #render" do template = Tilt::BuilderTemplate.new { |t| "xml.em 'Hello World!'" } assert_equal "Hello World!\n", template.render end - test "can be rendered more than once" do + it "can be rendered more than once" do template = Tilt::BuilderTemplate.new { |t| "xml.em 'Hello World!'" } 3.times { assert_equal "Hello World!\n", template.render } end - test "passing locals" do + it "passing locals" do template = Tilt::BuilderTemplate.new { "xml.em('Hey ' + name + '!')" } assert_equal "Hey Joe!\n", template.render(Object.new, :name => 'Joe') end - test "evaluating in an object scope" do + it "evaluating in an object scope" do template = Tilt::BuilderTemplate.new { "xml.em('Hey ' + @name + '!')" } scope = Object.new scope.instance_variable_set :@name, 'Joe' assert_equal "Hey Joe!\n", template.render(scope) end - test "passing a block for yield" do + it "passing a block for yield" do template = Tilt::BuilderTemplate.new { "xml.em('Hey ' + yield + '!')" } 3.times { assert_equal "Hey Joe!\n", template.render { 'Joe' }} end - test "block style templates" do + it "block style templates" do template = Tilt::BuilderTemplate.new do |t| lambda { |xml| xml.em('Hey Joe!') } @@ -43,12 +44,12 @@ class BuilderTemplateTest < Minitest::Test assert_equal "Hey Joe!\n", template.render end - test "options can be overridden" do + it "options can be overridden" do template = Tilt::BuilderTemplate.new(:indent => 0) { "xml.div { xml.em('Hey') }" } assert_equal "
Hey
", template.render end - test "can re-use locals for multiple calls" do + it "can re-use locals for multiple calls" do locals = { :name => "world" } template = Tilt::BuilderTemplate.new(:indent => 0) { "xml.em name" } 3.times do @@ -56,7 +57,7 @@ class BuilderTemplateTest < Minitest::Test end end - test "allows nesting raw XML" do + it "allows nesting raw XML" do subtemplate = Tilt::BuilderTemplate.new { "xml.em 'Hello World!'" } template = Tilt::BuilderTemplate.new { "xml.strong { xml << yield }" } 3.times do diff --git a/test/tilt_cache_test.rb b/test/tilt_cache_test.rb index 3ee0ae63..da017453 100644 --- a/test/tilt_cache_test.rb +++ b/test/tilt_cache_test.rb @@ -1,9 +1,9 @@ require_relative 'test_helper' -class TiltCacheTest < Minitest::Test - setup { @cache = Tilt::Cache.new } +describe 'Tilt::Cache' do + before { @cache = Tilt::Cache.new } - test "caching with single simple argument to #fetch" do + it "caching with single simple argument to #fetch" do template = nil result = @cache.fetch('hello') { template = Tilt::StringTemplate.new {''} } assert_same template, result @@ -11,7 +11,7 @@ class TiltCacheTest < Minitest::Test assert_same template, result end - test "caching with multiple complex arguments to #fetch" do + it "caching with multiple complex arguments to #fetch" do template = nil result = @cache.fetch('hello', {:foo => 'bar', :baz => 'bizzle'}) { template = Tilt::StringTemplate.new {''} } assert_same template, result @@ -19,7 +19,7 @@ class TiltCacheTest < Minitest::Test assert_same template, result end - test "caching nil" do + it "caching nil" do called = false result = @cache.fetch("blah") {called = true; nil} assert_equal true, called @@ -30,7 +30,7 @@ class TiltCacheTest < Minitest::Test assert_nil result end - test "clearing the cache with #clear" do + it "clearing the cache with #clear" do template, other = nil result = @cache.fetch('hello') { template = Tilt::StringTemplate.new {''} } assert_same template, result diff --git a/test/tilt_coffeescripttemplate_test.rb b/test/tilt_coffeescripttemplate_test.rb index 9229c3dd..7b7c0f39 100644 --- a/test/tilt_coffeescripttemplate_test.rb +++ b/test/tilt_coffeescripttemplate_test.rb @@ -3,114 +3,110 @@ begin require 'tilt/coffee' - module CoffeeScriptTests - def self.included(mod) - mod.class_eval do - test "bare is disabled by default" do - assert_equal false, @renderer.default_bare - end - - test "compiles and evaluates the template on #render" do - template = @renderer.new { |t| @code_without_variables } - assert_match "puts('Hello, World!');", template.render - end - - test "can be rendered more than once" do - template = @renderer.new { |t| @code_without_variables } - 3.times { assert_match "puts('Hello, World!');", template.render } - end - - test "disabling coffee-script wrapper" do - template = @renderer.new { @code_with_variables } - assert_match "(function() {", template.render - assert_match "puts(\"Hello, \" + name);\n", template.render - - template = @renderer.new(:bare => true) { @code_with_variables } - refute_match "(function() {", template.render - assert_equal "var name;\n\nname = \"Josh\";\n\nputs(\"Hello, \" + name);\n", template.render - - template = @renderer.new(:no_wrap => true) { @code_with_variables} - refute_match "(function() {", template.render - assert_equal "var name;\n\nname = \"Josh\";\n\nputs(\"Hello, \" + name);\n", template.render - end - - context "wrapper globally enabled" do - setup do - @bare = @renderer.default_bare - @renderer.default_bare = false - end - - teardown do - @renderer.default_bare = @bare - end - - test "no options" do - template = @renderer.new { |t| @code_with_variables } - assert_match "puts(\"Hello, \" + name);", template.render - assert_match "(function() {", template.render - end - - test "overridden by :bare" do - template = @renderer.new(:bare => true) { |t| @code_with_variables } - assert_match "puts(\"Hello, \" + name);", template.render - refute_match "(function() {", template.render - end - - test "overridden by :no_wrap" do - template = @renderer.new(:no_wrap => true) { |t| @code_with_variables } - assert_match "puts(\"Hello, \" + name);", template.render - refute_match "(function() {", template.render - end - end - - context "wrapper globally disabled" do - setup do - @bare = @renderer.default_bare - @renderer.default_bare = true - end - - teardown do - @renderer.default_bare = @bare - end - - test "no options" do - template = @renderer.new { |t| @code_with_variables } - assert_match "puts(\"Hello, \" + name);", template.render - refute_match "(function() {", template.render - end - - test "overridden by :bare" do - template = @renderer.new(:bare => false) { |t| @code_with_variables } - assert_match "puts(\"Hello, \" + name);", template.render - assert_match "(function() {", template.render - end - - test "overridden by :no_wrap" do - template = @renderer.new(:no_wrap => false) { |t| @code_with_variables } - assert_match "puts(\"Hello, \" + name);", template.render - assert_match "(function() {", template.render - end - end + _CoffeeScriptTests = proc do + it "bare is disabled by default" do + assert_equal false, @renderer.default_bare + end + + it "compiles and evaluates the template on #render" do + template = @renderer.new { |t| @code_without_variables } + assert_match "puts('Hello, World!');", template.render + end + + it "can be rendered more than once" do + template = @renderer.new { |t| @code_without_variables } + 3.times { assert_match "puts('Hello, World!');", template.render } + end + + it "disabling coffee-script wrapper" do + template = @renderer.new { @code_with_variables } + assert_match "(function() {", template.render + assert_match "puts(\"Hello, \" + name);\n", template.render + + template = @renderer.new(:bare => true) { @code_with_variables } + refute_match "(function() {", template.render + assert_equal "var name;\n\nname = \"Josh\";\n\nputs(\"Hello, \" + name);\n", template.render + + template = @renderer.new(:no_wrap => true) { @code_with_variables} + refute_match "(function() {", template.render + assert_equal "var name;\n\nname = \"Josh\";\n\nputs(\"Hello, \" + name);\n", template.render + end + + describe "wrapper globally enabled" do + before do + @bare = @renderer.default_bare + @renderer.default_bare = false + end + + after do + @renderer.default_bare = @bare + end + + it "no options" do + template = @renderer.new { |t| @code_with_variables } + assert_match "puts(\"Hello, \" + name);", template.render + assert_match "(function() {", template.render + end + + it "overridden by :bare" do + template = @renderer.new(:bare => true) { |t| @code_with_variables } + assert_match "puts(\"Hello, \" + name);", template.render + refute_match "(function() {", template.render + end + + it "overridden by :no_wrap" do + template = @renderer.new(:no_wrap => true) { |t| @code_with_variables } + assert_match "puts(\"Hello, \" + name);", template.render + refute_match "(function() {", template.render + end + end + + describe "wrapper globally disabled" do + before do + @bare = @renderer.default_bare + @renderer.default_bare = true + end + + after do + @renderer.default_bare = @bare + end + + it "no options" do + template = @renderer.new { |t| @code_with_variables } + assert_match "puts(\"Hello, \" + name);", template.render + refute_match "(function() {", template.render + end + + it "overridden by :bare" do + template = @renderer.new(:bare => false) { |t| @code_with_variables } + assert_match "puts(\"Hello, \" + name);", template.render + assert_match "(function() {", template.render + end + + it "overridden by :no_wrap" do + template = @renderer.new(:no_wrap => false) { |t| @code_with_variables } + assert_match "puts(\"Hello, \" + name);", template.render + assert_match "(function() {", template.render end end end - class CoffeeScriptTemplateTest < Minitest::Test - setup do + describe 'tilt coffeescript' do + before do @code_without_variables = "puts 'Hello, World!'\n" @code_with_variables = 'name = "Josh"; puts "Hello, #{name}"' @renderer = Tilt::CoffeeScriptTemplate end - include CoffeeScriptTests + class_eval(&_CoffeeScriptTests) - test "is registered for '.coffee' files" do + it "is registered for '.coffee' files" do assert_equal @renderer, Tilt['test.coffee'] end end - class LiterateCoffeeScriptTemplateTest < Minitest::Test - setup do + describe 'tilt literate coffeescript' do + before do @code_without_variables = <Hello World!\n", template.render end - test "can be rendered more than once" do + it "can be rendered more than once" do template = Tilt::CommonMarkerTemplate.new { |t| "# Hello World!" } 3.times { assert_equal "

Hello World!

\n", template.render } end - test "smartypants when :smartypants is set" do + it "smartypants when :smartypants is set" do template = Tilt::CommonMarkerTemplate.new(:smartypants => true) do |t| "OKAY -- 'Smarty Pants'" end assert_match('

OKAY – ‘Smarty Pants’

', template.render) end - test 'Renders unsafe HTML when :UNSAFE is set' do + it 'Renders unsafe HTML when :UNSAFE is set' do template = Tilt::CommonMarkerTemplate.new(UNSAFE: true) do |_t| <<~MARKDOWN
diff --git a/test/tilt_compilesite_test.rb b/test/tilt_compilesite_test.rb index e2d44f33..15f2f281 100644 --- a/test/tilt_compilesite_test.rb +++ b/test/tilt_compilesite_test.rb @@ -1,11 +1,11 @@ require_relative 'test_helper' -class CompileSiteTest < Minitest::Test - def setup +describe 'tilt compile site' do + before do GC.start end - class CompilingTemplate < Tilt::Template + _CompilingTemplate = Class.new(Tilt::Template) do def prepare end @@ -14,20 +14,19 @@ def precompiled_template(locals) end end - class Scope - end + _Scope = Class.new - test "compiling template source to a method" do - template = CompilingTemplate.new { |t| "Hello World!" } - template.render(Scope.new) + it "compiling template source to a method" do + template = _CompilingTemplate.new { |t| "Hello World!" } + template.render(_Scope.new) method = template.send(:compiled_method, []) assert_kind_of UnboundMethod, method end - # This test attempts to surface issues with compiling templates from + # This it attempts to surface issues with compiling templates from # multiple threads. - test "using compiled templates from multiple threads" do - template = CompilingTemplate.new { 'template' } + it "using compiled templates from multiple threads" do + template = _CompilingTemplate.new { 'template' } main_thread = Thread.current 10.times do |i| threads = diff --git a/test/tilt_creoletemplate_test.rb b/test/tilt_creoletemplate_test.rb index 8ec522a5..1b8104a8 100644 --- a/test/tilt_creoletemplate_test.rb +++ b/test/tilt_creoletemplate_test.rb @@ -3,17 +3,17 @@ begin require 'tilt/creole' - class CreoleTemplateTest < Minitest::Test - test "is registered for '.creole' files" do + describe 'tilt/creole' do + it "is registered for '.creole' files" do assert_equal Tilt::CreoleTemplate, Tilt['test.creole'] end - test "compiles and evaluates the template on #render" do + it "compiles and evaluates the template on #render" do template = Tilt::CreoleTemplate.new { |t| "= Hello World!" } assert_equal "

Hello World!

", template.render end - test "can be rendered more than once" do + it "can be rendered more than once" do template = Tilt::CreoleTemplate.new { |t| "= Hello World!" } 3.times { assert_equal "

Hello World!

", template.render } end diff --git a/test/tilt_csv_test.rb b/test/tilt_csv_test.rb index 8ac5c054..5c0ec763 100644 --- a/test/tilt_csv_test.rb +++ b/test/tilt_csv_test.rb @@ -3,35 +3,34 @@ begin require 'tilt/csv' - class CSVTemplateTest < Minitest::Test - - test "registered for '.rcsv' files" do + describe 'tilt/csv' do + it "registered for '.rcsv' files" do assert_equal Tilt::CSVTemplate, Tilt['rcsv'] end - test "compiles and evaluates the template on #render" do + it "compiles and evaluates the template on #render" do template = Tilt::CSVTemplate.new { "csv << ['hello', 'world']" } assert_equal "hello,world\n", template.render end - test "can be rendered more than once" do + it "can be rendered more than once" do template = Tilt::CSVTemplate.new { "csv << [1,2,3]" } 3.times { assert_equal "1,2,3\n", template.render } end - test "can pass locals" do + it "can pass locals" do template = Tilt::CSVTemplate.new { 'csv << [1, name]' } assert_equal "1,Joe\n", template.render(Object.new, :name => 'Joe') end - test "evaluating in an object scope" do + it "evaluating in an object scope" do template = Tilt::CSVTemplate.new { 'csv << [1, @name]' } scope = Object.new scope.instance_variable_set :@name, 'Joe' assert_equal "1,Joe\n", template.render(scope) end - test "backtrace file and line reporting" do + it "backtrace file and line reporting" do data = File.read(__FILE__).split("\n__END__\n").last template = Tilt::CSVTemplate.new('test.csv') { data } begin @@ -46,12 +45,12 @@ class CSVTemplateTest < Minitest::Test end end - test "passing options to engine" do + it "passing options to engine" do template = Tilt::CSVTemplate.new(:col_sep => '|') { 'csv << [1,2,3]' } assert_equal "1|2|3\n", template.render end - test "outvar option" do + it "outvar option" do outvar = '@_output' scope = Object.new template = Tilt::CSVTemplate.new(:outvar => outvar) { 'csv << [1,2,3]' } diff --git a/test/tilt_erbtemplate_test.rb b/test/tilt_erbtemplate_test.rb index f0e14f0f..043135b2 100644 --- a/test/tilt_erbtemplate_test.rb +++ b/test/tilt_erbtemplate_test.rb @@ -2,48 +2,48 @@ require 'tilt/erb' require 'tempfile' -class ERBTemplateTest < Minitest::Test - test "registered for '.erb' files" do +describe 'tilt/erb' do + it "registered for '.erb' files" do assert_includes Tilt.lazy_map['erb'], ['Tilt::ERBTemplate', 'tilt/erb'] end - test "registered for '.rhtml' files" do + it "registered for '.rhtml' files" do assert_includes Tilt.lazy_map['rhtml'], ['Tilt::ERBTemplate', 'tilt/erb'] end - test "loading and evaluating templates on #render" do + it "loading and evaluating templates on #render" do template = Tilt::ERBTemplate.new { |t| "Hello World!" } assert_equal "Hello World!", template.render end - test "can be rendered more than once" do + it "can be rendered more than once" do template = Tilt::ERBTemplate.new { |t| "Hello World!" } 3.times { assert_equal "Hello World!", template.render } end - test "passing locals" do + it "passing locals" do template = Tilt::ERBTemplate.new { 'Hey <%= name %>!' } assert_equal "Hey Joe!", template.render(Object.new, :name => 'Joe') end - test "evaluating in an object scope" do + it "evaluating in an object scope" do template = Tilt::ERBTemplate.new { 'Hey <%= @name %>!' } scope = Object.new scope.instance_variable_set :@name, 'Joe' assert_equal "Hey Joe!", template.render(scope) end - class MockOutputVariableScope + _MockOutputVariableScope = Class.new do attr_accessor :exposed_buffer end - test "exposing the buffer to the template by default" do + it "exposing the buffer to the template by default" do verbose = $VERBOSE begin $VERBOSE = nil Tilt::ERBTemplate.default_output_variable = '@_out_buf' template = Tilt::ERBTemplate.new { '<% self.exposed_buffer = @_out_buf %>hey' } - scope = MockOutputVariableScope.new + scope = _MockOutputVariableScope.new template.render(scope) refute_nil scope.exposed_buffer assert_equal scope.exposed_buffer, 'hey' @@ -53,12 +53,12 @@ class MockOutputVariableScope end end - test "passing a block for yield" do + it "passing a block for yield" do template = Tilt::ERBTemplate.new { 'Hey <%= yield %>!' } assert_equal "Hey Joe!", template.render { 'Joe' } end - test "backtrace file and line reporting without locals" do + it "backtrace file and line reporting without locals" do data = File.read(__FILE__, :encoding=>'UTF-8').split("\n__END__\n").last fail unless data[0] == ?< template = Tilt::ERBTemplate.new('test.erb', 11) { data } @@ -74,7 +74,7 @@ class MockOutputVariableScope end end - test "backtrace file and line reporting with locals" do + it "backtrace file and line reporting with locals" do data = File.read(__FILE__, :encoding=>'UTF-8').split("\n__END__\n").last fail unless data[0] == ?< template = Tilt::ERBTemplate.new('test.erb', 1) { data } @@ -90,27 +90,27 @@ class MockOutputVariableScope end end - test "explicit disabling of trim mode" do + it "explicit disabling of trim mode" do template = Tilt::ERBTemplate.new('test.erb', 1, :trim => false) { "\n<%= 1 + 1 %>\n" } assert_equal "\n2\n", template.render end - test "default stripping trim mode" do + it "default stripping trim mode" do template = Tilt::ERBTemplate.new('test.erb', 1) { "\n<%= 1 + 1 %>\n" } assert_equal "\n2", template.render end - test "stripping trim mode" do + it "stripping trim mode" do template = Tilt::ERBTemplate.new('test.erb', 1, :trim => '-') { "\n<%= 1 + 1 -%>\n" } assert_equal "\n2", template.render end - test "shorthand whole line syntax trim mode" do + it "shorthand whole line syntax trim mode" do template = Tilt::ERBTemplate.new('test.erb', :trim => '%') { "\n% if true\nhello\n%end\n" } assert_equal "\nhello\n", template.render end - test "using an instance variable as the outvar" do + it "using an instance variable as the outvar" do template = Tilt::ERBTemplate.new(nil, :outvar => '@buf') { "<%= 1 + 1 %>" } scope = Object.new scope.instance_variable_set(:@buf, 'original value') @@ -119,53 +119,52 @@ class MockOutputVariableScope end end -class CompiledERBTemplateTest < Minitest::Test - def teardown +describe 'tilt/erb (compiled)' do + after do GC.start end - class Scope - end + _Scope = Class.new - test "compiling template source to a method" do + it "compiling template source to a method" do template = Tilt::ERBTemplate.new { |t| "Hello World!" } - template.render(Scope.new) + template.render(_Scope.new) method = template.send(:compiled_method, []) assert_kind_of UnboundMethod, method end - test "loading and evaluating templates on #render" do + it "loading and evaluating templates on #render" do template = Tilt::ERBTemplate.new { |t| "Hello World!" } - assert_equal "Hello World!", template.render(Scope.new) - assert_equal "Hello World!", template.render(Scope.new) + assert_equal "Hello World!", template.render(_Scope.new) + assert_equal "Hello World!", template.render(_Scope.new) end - test "passing locals" do + it "passing locals" do template = Tilt::ERBTemplate.new { 'Hey <%= name %>!' } - assert_equal "Hey Joe!", template.render(Scope.new, :name => 'Joe') + assert_equal "Hey Joe!", template.render(_Scope.new, :name => 'Joe') end - test "evaluating in an object scope" do + it "evaluating in an object scope" do template = Tilt::ERBTemplate.new { 'Hey <%= @name %>!' } - scope = Scope.new + scope = _Scope.new scope.instance_variable_set :@name, 'Joe' assert_equal "Hey Joe!", template.render(scope) scope.instance_variable_set :@name, 'Jane' assert_equal "Hey Jane!", template.render(scope) end - test "passing a block for yield" do + it "passing a block for yield" do template = Tilt::ERBTemplate.new { 'Hey <%= yield %>!' } - assert_equal "Hey Joe!", template.render(Scope.new) { 'Joe' } - assert_equal "Hey Jane!", template.render(Scope.new) { 'Jane' } + assert_equal "Hey Joe!", template.render(_Scope.new) { 'Joe' } + assert_equal "Hey Jane!", template.render(_Scope.new) { 'Jane' } end - test "backtrace file and line reporting without locals" do + it "backtrace file and line reporting without locals" do data = File.read(__FILE__, encoding: 'UTF-8').split("\n__END__\n").last fail unless data[0] == ?< template = Tilt::ERBTemplate.new('test.erb', 11) { data } begin - template.render(Scope.new) + template.render(_Scope.new) fail 'should have raised an exception' rescue => boom assert_kind_of NameError, boom @@ -176,12 +175,12 @@ class Scope end end - test "backtrace file and line reporting with locals" do + it "backtrace file and line reporting with locals" do data = File.read(__FILE__, encoding: 'UTF-8').split("\n__END__\n").last fail unless data[0] == ?< template = Tilt::ERBTemplate.new('test.erb') { data } begin - template.render(Scope.new, :name => 'Joe', :foo => 'bar') + template.render(_Scope.new, :name => 'Joe', :foo => 'bar') fail 'should have raised an exception' rescue => boom assert_kind_of RuntimeError, boom @@ -192,22 +191,22 @@ class Scope end end - test "default stripping trim mode" do + it "default stripping trim mode" do template = Tilt::ERBTemplate.new('test.erb') { "\n<%= 1 + 1 %>\n" } - assert_equal "\n2", template.render(Scope.new) + assert_equal "\n2", template.render(_Scope.new) end - test "stripping trim mode" do + it "stripping trim mode" do template = Tilt::ERBTemplate.new('test.erb', :trim => '-') { "\n<%= 1 + 1 -%>\n" } - assert_equal "\n2", template.render(Scope.new) + assert_equal "\n2", template.render(_Scope.new) end - test "shorthand whole line syntax trim mode" do + it "shorthand whole line syntax trim mode" do template = Tilt::ERBTemplate.new('test.erb', :trim => '%') { "\n% if true\nhello\n%end\n" } - assert_equal "\nhello\n", template.render(Scope.new) + assert_equal "\nhello\n", template.render(_Scope.new) end - test "encoding with source_encoding" do + it "encoding with source_encoding" do f = Tempfile.open("template") f.puts('ふが <%= @hoge %>') f.close() @@ -217,7 +216,7 @@ class Scope f.delete end - test "encoding with :default_encoding" do + it "encoding with :default_encoding" do f = Tempfile.open("template") f.puts('ふが <%= @hoge %>') f.close() diff --git a/test/tilt_erubistemplate_test.rb b/test/tilt_erubistemplate_test.rb index c867ddc9..99a763c8 100644 --- a/test/tilt_erubistemplate_test.rb +++ b/test/tilt_erubistemplate_test.rb @@ -2,13 +2,14 @@ begin require 'tilt/erubis' - class ErubisTemplateTest < Minitest::Test - test "registered for '.erubis' files" do + + describe 'tilt/erubis' do + it "registered for '.erubis' files" do assert_equal Tilt::ErubisTemplate, Tilt['test.erubis'] assert_equal Tilt::ErubisTemplate, Tilt['test.html.erubis'] end - test "registered above ERB" do + it "registered above ERB" do %w[erb rhtml].each do |ext| lazy = Tilt.lazy_map[ext] erubis_idx = lazy.index { |klass, file| klass == 'Tilt::ErubisTemplate' } @@ -18,22 +19,22 @@ class ErubisTemplateTest < Minitest::Test end end - test "preparing and evaluating templates on #render" do + it "preparing and evaluating templates on #render" do template = Tilt::ErubisTemplate.new { |t| "Hello World!" } assert_equal "Hello World!", template.render end - test "can be rendered more than once" do + it "can be rendered more than once" do template = Tilt::ErubisTemplate.new { |t| "Hello World!" } 3.times { assert_equal "Hello World!", template.render } end - test "passing locals" do + it "passing locals" do template = Tilt::ErubisTemplate.new { 'Hey <%= name %>!' } assert_equal "Hey Joe!", template.render(Object.new, :name => 'Joe') end - test "evaluating in an object scope" do + it "evaluating in an object scope" do template = Tilt::ErubisTemplate.new { 'Hey <%= @name %>!' } scope = Object.new scope.instance_variable_set :@name, 'Joe' @@ -44,8 +45,10 @@ class MockOutputVariableScope attr_accessor :exposed_buffer end - test "exposing the buffer to the template by default" do + it "exposing the buffer to the template by default" do + verbose = $VERBOSE begin + $VERBOSE = nil Tilt::ErubisTemplate.default_output_variable = '@_out_buf' template = Tilt::ErubisTemplate.new { '<% self.exposed_buffer = @_out_buf %>hey' } scope = MockOutputVariableScope.new @@ -54,15 +57,16 @@ class MockOutputVariableScope assert_equal scope.exposed_buffer, 'hey' ensure Tilt::ErubisTemplate.default_output_variable = '_erbout' + $VERBOSE = verbose end end - test "passing a block for yield" do + it "passing a block for yield" do template = Tilt::ErubisTemplate.new { 'Hey <%= yield %>!' } assert_equal "Hey Joe!", template.render { 'Joe' } end - test "backtrace file and line reporting without locals" do + it "backtrace file and line reporting without locals" do data = File.read(__FILE__).split("\n__END__\n").last fail unless data[0] == ?< template = Tilt::ErubisTemplate.new('test.erubis', 11) { data } @@ -78,7 +82,7 @@ class MockOutputVariableScope end end - test "backtrace file and line reporting with locals" do + it "backtrace file and line reporting with locals" do data = File.read(__FILE__).split("\n__END__\n").last fail unless data[0] == ?< template = Tilt::ErubisTemplate.new('test.erubis', 1) { data } @@ -94,14 +98,14 @@ class MockOutputVariableScope end end - test "erubis template options" do + it "erubis template options" do template = Tilt::ErubisTemplate.new(nil, :pattern => '\{% %\}') { 'Hey {%= @name %}!' } scope = Object.new scope.instance_variable_set :@name, 'Joe' assert_equal "Hey Joe!", template.render(scope) end - test "using an instance variable as the outvar" do + it "using an instance variable as the outvar" do template = Tilt::ErubisTemplate.new(nil, :outvar => '@buf') { "<%= 1 + 1 %>" } scope = Object.new scope.instance_variable_set(:@buf, 'original value') @@ -109,27 +113,27 @@ class MockOutputVariableScope assert_equal 'original value', scope.instance_variable_get(:@buf) end - test "using Erubis::EscapedEruby subclass via :engine_class option" do + it "using Erubis::EscapedEruby subclass via :engine_class option" do template = Tilt::ErubisTemplate.new(nil, :engine_class => ::Erubis::EscapedEruby) { |t| %(<%= "

Hello World!

" %>) } assert_equal "<p>Hello World!</p>", template.render end - test "using :escape_html => true option" do + it "using :escape_html => true option" do template = Tilt::ErubisTemplate.new(nil, :escape_html => true) { |t| %(<%= "

Hello World!

" %>) } assert_equal "<p>Hello World!</p>", template.render end - test "using :escape_html => false option" do + it "using :escape_html => false option" do template = Tilt::ErubisTemplate.new(nil, :escape_html => false) { |t| %(<%= "

Hello World!

" %>) } assert_equal "

Hello World!

", template.render end - test "erubis default does not escape html" do + it "erubis default does not escape html" do template = Tilt::ErubisTemplate.new { |t| %(<%= "

Hello World!

" %>) } assert_equal "

Hello World!

", template.render end - test "does not modify options argument" do + it "does not modify options argument" do options_hash = {:escape_html => true} Tilt::ErubisTemplate.new(nil, options_hash) { |t| "Hello World!" } assert_equal({:escape_html => true}, options_hash) diff --git a/test/tilt_erubitemplate_test.rb b/test/tilt_erubitemplate_test.rb index 0c183d80..331ff9d2 100644 --- a/test/tilt_erubitemplate_test.rb +++ b/test/tilt_erubitemplate_test.rb @@ -2,13 +2,14 @@ begin require 'tilt/erubi' - class ErubiTemplateTest < Minitest::Test - test "registered for '.erubi' files" do + + describe 'tilt/erubi' do + it "registered for '.erubi' files" do assert_equal Tilt::ErubiTemplate, Tilt['test.erubi'] assert_equal Tilt::ErubiTemplate, Tilt['test.html.erubi'] end - test "registered above ERB and Erubis" do + it "registered above ERB and Erubis" do %w[erb rhtml].each do |ext| lazy = Tilt.lazy_map[ext] erubi_idx = lazy.index { |klass, file| klass == 'Tilt::ErubiTemplate' } @@ -21,22 +22,22 @@ class ErubiTemplateTest < Minitest::Test end end - test "preparing and evaluating templates on #render" do + it "preparing and evaluating templates on #render" do template = Tilt::ErubiTemplate.new { |t| "Hello World!" } assert_equal "Hello World!", template.render end - test "can be rendered more than once" do + it "can be rendered more than once" do template = Tilt::ErubiTemplate.new { |t| "Hello World!" } 3.times { assert_equal "Hello World!", template.render } end - test "passing locals" do + it "passing locals" do template = Tilt::ErubiTemplate.new { 'Hey <%= name %>!' } assert_equal "Hey Joe!", template.render(Object.new, :name => 'Joe') end - test "evaluating in an object scope" do + it "evaluating in an object scope" do template = Tilt::ErubiTemplate.new { 'Hey <%= @name %>!' } scope = Object.new scope.instance_variable_set :@name, 'Joe' @@ -47,7 +48,7 @@ class MockOutputVariableScope attr_accessor :exposed_buffer end - test "exposing the buffer to the template by default" do + it "exposing the buffer to the template by default" do template = Tilt::ErubiTemplate.new(nil, :bufvar=>'@_out_buf') { '<% self.exposed_buffer = @_out_buf %>hey' } scope = MockOutputVariableScope.new template.render(scope) @@ -55,12 +56,12 @@ class MockOutputVariableScope assert_equal scope.exposed_buffer, 'hey' end - test "passing a block for yield" do + it "passing a block for yield" do template = Tilt::ErubiTemplate.new { 'Hey <%= yield %>!' } assert_equal "Hey Joe!", template.render { 'Joe' } end - test "backtrace file and line reporting without locals" do + it "backtrace file and line reporting without locals" do data = File.read(__FILE__).split("\n__END__\n").last fail unless data[0] == ?< template = Tilt::ErubiTemplate.new('test.erubis', 11) { data } @@ -76,7 +77,7 @@ class MockOutputVariableScope end end - test "backtrace file and line reporting with locals" do + it "backtrace file and line reporting with locals" do data = File.read(__FILE__).split("\n__END__\n").last fail unless data[0] == ?< template = Tilt::ErubiTemplate.new('test.erubis', 1) { data } @@ -92,7 +93,7 @@ class MockOutputVariableScope end end - test "erubis template options" do + it "erubis template options" do template = Tilt::ErubiTemplate.new(nil, :escapefunc=> 'h') { 'Hey <%== @name %>!' } scope = Object.new def scope.h(s) s * 2 end @@ -100,7 +101,7 @@ def scope.h(s) s * 2 end assert_equal "Hey JoeJoe!", template.render(scope) end - test "using an instance variable as the outvar" do + it "using an instance variable as the outvar" do template = Tilt::ErubiTemplate.new(nil, :outvar => '@buf') { "<%= 1 + 1 %>" } scope = Object.new scope.instance_variable_set(:@buf, 'original value') @@ -108,7 +109,7 @@ def scope.h(s) s * 2 end assert_equal 'original value', scope.instance_variable_get(:@buf) end - test "using Erubi::CaptureEndEngine subclass via :engine_class option" do + it "using Erubi::CaptureEndEngine subclass via :engine_class option" do require 'erubi/capture_end' def self.bar @a << "a" @@ -120,22 +121,22 @@ def self.bar assert_equal "cADBe", template.render(self) end - test "using :escape_html => true option" do + it "using :escape_html => true option" do template = Tilt::ErubiTemplate.new(nil, :escape_html => true) { |t| %(<%= "

Hello World!

" %>) } assert_equal "<p>Hello World!</p>", template.render end - test "using :escape_html => false option" do + it "using :escape_html => false option" do template = Tilt::ErubiTemplate.new(nil, :escape_html => false) { |t| %(<%= "

Hello World!

" %>) } assert_equal "

Hello World!

", template.render end - test "erubi default does not escape html" do + it "erubi default does not escape html" do template = Tilt::ErubiTemplate.new { |t| %(<%= "

Hello World!

" %>) } assert_equal "

Hello World!

", template.render end - test "does not modify options argument" do + it "does not modify options argument" do options_hash = {:escape_html => true} Tilt::ErubiTemplate.new(nil, options_hash) { |t| "Hello World!" } assert_equal({:escape_html => true}, options_hash) diff --git a/test/tilt_etannitemplate_test.rb b/test/tilt_etannitemplate_test.rb index 9353f047..5bfc9ebc 100644 --- a/test/tilt_etannitemplate_test.rb +++ b/test/tilt_etannitemplate_test.rb @@ -1,49 +1,49 @@ require_relative 'test_helper' require 'tilt/etanni' -class EtanniTemplateTest < Minitest::Test - test "registered for '.etn' files" do +describe 'tilt/etanni' do + it "registered for '.etn' files" do assert_equal Tilt::EtanniTemplate, Tilt['test.etn'] end - test "registered for '.etanni' files" do + it "registered for '.etanni' files" do assert_equal Tilt::EtanniTemplate, Tilt['test.etanni'] end - test "loading and evaluating templates on #render" do + it "loading and evaluating templates on #render" do template = Tilt::EtanniTemplate.new { |t| "Hello World!" } assert_equal "Hello World!", template.render end - test "can be rendered more than once" do + it "can be rendered more than once" do template = Tilt::EtanniTemplate.new { |t| "Hello World!" } 3.times { assert_equal "Hello World!", template.render } end - test "passing locals" do + it "passing locals" do template = Tilt::EtanniTemplate.new { 'Hey #{name}!' } assert_equal "Hey Joe!", template.render(Object.new, :name => 'Joe') end - test "evaluating in an object scope" do + it "evaluating in an object scope" do template = Tilt::EtanniTemplate.new { 'Hey #{@name}!' } scope = Object.new scope.instance_variable_set :@name, 'Joe' assert_equal "Hey Joe!", template.render(scope) end - test "passing a block for yield" do + it "passing a block for yield" do template = Tilt::EtanniTemplate.new { 'Hey #{yield}!' } assert_equal "Hey Joe!", template.render { 'Joe' } assert_equal "Hey Moe!", template.render { 'Moe' } end - test "multiline templates" do + it "multiline templates" do template = Tilt::EtanniTemplate.new { "Hello\nWorld!\n" } assert_equal "Hello\nWorld!", template.render end - test "backtrace file and line reporting without locals" do + it "backtrace file and line reporting without locals" do data = File.read(__FILE__).split("\n__END__\n").last fail unless data[0] == ?< template = Tilt::EtanniTemplate.new('test.etn', 11) { data } @@ -60,7 +60,7 @@ class EtanniTemplateTest < Minitest::Test end end - test "backtrace file and line reporting with locals" do + it "backtrace file and line reporting with locals" do data = File.read(__FILE__).split("\n__END__\n").last fail unless data[0] == ?< template = Tilt::EtanniTemplate.new('test.etn', 1) { data } @@ -78,64 +78,62 @@ class EtanniTemplateTest < Minitest::Test end end - -class CompiledEtanniTemplateTest < Minitest::Test - def teardown +describe 'tilt/etanni (compiled)' do + after do GC.start end - class Scope - end + _Scope = Class.new - test "compiling template source to a method" do + it "compiling template source to a method" do template = Tilt::EtanniTemplate.new { |t| "Hello World!" } - template.render(Scope.new) + template.render(_Scope.new) method = template.send(:compiled_method, []) assert_kind_of UnboundMethod, method end - test "loading and evaluating templates on #render" do + it "loading and evaluating templates on #render" do template = Tilt::EtanniTemplate.new { |t| "Hello World!" } - assert_equal "Hello World!", template.render(Scope.new) + assert_equal "Hello World!", template.render(_Scope.new) end - test "passing locals" do + it "passing locals" do template = Tilt::EtanniTemplate.new { 'Hey #{name}!' } - assert_equal "Hey Joe!", template.render(Scope.new, :name => 'Joe') - assert_equal "Hey Moe!", template.render(Scope.new, :name => 'Moe') + assert_equal "Hey Joe!", template.render(_Scope.new, :name => 'Joe') + assert_equal "Hey Moe!", template.render(_Scope.new, :name => 'Moe') end - test "evaluating in an object scope" do + it "evaluating in an object scope" do template = Tilt::EtanniTemplate.new { 'Hey #{@name}!' } - scope = Scope.new + scope = _Scope.new scope.instance_variable_set :@name, 'Joe' assert_equal "Hey Joe!", template.render(scope) scope.instance_variable_set :@name, 'Moe' assert_equal "Hey Moe!", template.render(scope) end - test "passing a block for yield" do + it "passing a block for yield" do template = Tilt::EtanniTemplate.new { 'Hey #{yield}!' } - assert_equal "Hey Joe!", template.render(Scope.new) { 'Joe' } - assert_equal "Hey Moe!", template.render(Scope.new) { 'Moe' } + assert_equal "Hey Joe!", template.render(_Scope.new) { 'Joe' } + assert_equal "Hey Moe!", template.render(_Scope.new) { 'Moe' } end - test "multiline templates" do + it "multiline templates" do template = Tilt::EtanniTemplate.new { "Hello\nWorld!\n" } - assert_equal "Hello\nWorld!", template.render(Scope.new) + assert_equal "Hello\nWorld!", template.render(_Scope.new) end - test "template with '}'" do + it "template with '}'" do template = Tilt::EtanniTemplate.new { "Hello }" } assert_equal "Hello }", template.render end - test "backtrace file and line reporting without locals" do + it "backtrace file and line reporting without locals" do data = File.read(__FILE__).split("\n__END__\n").last fail unless data[0] == ?< template = Tilt::EtanniTemplate.new('test.etn', 11) { data } begin - template.render(Scope.new) + template.render(_Scope.new) fail 'should have raised an exception' rescue => boom assert_kind_of NameError, boom @@ -148,12 +146,12 @@ class Scope end end - test "backtrace file and line reporting with locals" do + it "backtrace file and line reporting with locals" do data = File.read(__FILE__).split("\n__END__\n").last fail unless data[0] == ?< template = Tilt::EtanniTemplate.new('test.etn') { data } begin - template.render(Scope.new, :name => 'Joe', :foo => 'bar') + template.render(_Scope.new, :name => 'Joe', :foo => 'bar') fail 'should have raised an exception' rescue => boom assert_kind_of RuntimeError, boom diff --git a/test/tilt_hamltemplate_test.rb b/test/tilt_hamltemplate_test.rb index ac3ab308..781392a7 100644 --- a/test/tilt_hamltemplate_test.rb +++ b/test/tilt_hamltemplate_test.rb @@ -1,55 +1,54 @@ require_relative 'test_helper' begin - class ::MockError < NameError - end - require 'tilt/haml' - class HamlTemplateTest < Minitest::Test - test "registered for '.haml' files" do + describe 'tilt/haml' do + self::MockError = Class.new(NameError) + + it "registered for '.haml' files" do assert_equal Tilt::HamlTemplate, Tilt['test.haml'] end - test "preparing and evaluating templates on #render" do + it "preparing and evaluating templates on #render" do template = Tilt::HamlTemplate.new { |t| "%p Hello World!" } assert_equal "

Hello World!

\n", template.render end - test "can be rendered more than once" do + it "can be rendered more than once" do template = Tilt::HamlTemplate.new { |t| "%p Hello World!" } 3.times { assert_equal "

Hello World!

\n", template.render } end - test "passing locals" do + it "passing locals" do template = Tilt::HamlTemplate.new { "%p= 'Hey ' + name + '!'" } assert_equal "

Hey Joe!

\n", template.render(Object.new, :name => 'Joe') end - test 'evaluating in default/nil scope' do + it 'evaluating in default/nil scope' do template = Tilt::HamlTemplate.new { |t| '%p Hey unknown!' } assert_equal "

Hey unknown!

\n", template.render assert_equal "

Hey unknown!

\n", template.render(nil) end - test 'evaluating in invalid, frozen scope' do + it 'evaluating in invalid, frozen scope' do template = Tilt::HamlTemplate.new { |t| '%p Hey unknown!' } assert_raises(ArgumentError) { template.render(Object.new.freeze) } end - test "evaluating in an object scope" do + it "evaluating in an object scope" do template = Tilt::HamlTemplate.new { "%p= 'Hey ' + @name + '!'" } scope = Object.new scope.instance_variable_set :@name, 'Joe' assert_equal "

Hey Joe!

\n", template.render(scope) end - test "passing a block for yield" do + it "passing a block for yield" do template = Tilt::HamlTemplate.new { "%p= 'Hey ' + yield + '!'" } assert_equal "

Hey Joe!

\n", template.render { 'Joe' } end - test "backtrace file and line reporting without locals" do + it "backtrace file and line reporting without locals" do data = File.read(__FILE__).split("\n__END__\n").last fail unless data[0] == ?% template = Tilt::HamlTemplate.new('test.haml', 10) { data } @@ -65,14 +64,14 @@ class HamlTemplateTest < Minitest::Test end end - test "backtrace file and line reporting with locals" do + it "backtrace file and line reporting with locals" do data = File.read(__FILE__).split("\n__END__\n").last fail unless data[0] == ?% template = Tilt::HamlTemplate.new('test.haml') { data } begin - template.render(Object.new, :name => 'Joe', :foo => 'bar') + template.render(self, :name => 'Joe', :foo => 'bar') rescue => boom - assert_kind_of MockError, boom + assert_kind_of self.class::MockError, boom line = boom.backtrace.first file, line, _meth = line.split(":") assert_equal 'test.haml', file @@ -81,51 +80,51 @@ class HamlTemplateTest < Minitest::Test end end - class CompiledHamlTemplateTest < Minitest::Test - class Scope - end + describe 'tilt/haml (compiled)' do + _Scope = Class.new + _Scope::MockError = Class.new(NameError) - test "compiling template source to a method" do + it "compiling template source to a method" do template = Tilt::HamlTemplate.new { |t| "Hello World!" } - template.render(Scope.new) + template.render(_Scope.new) method = template.send(:compiled_method, []) assert_kind_of UnboundMethod, method end - test "passing locals" do + it "passing locals" do template = Tilt::HamlTemplate.new { "%p= 'Hey ' + name + '!'" } - assert_equal "

Hey Joe!

\n", template.render(Scope.new, :name => 'Joe') + assert_equal "

Hey Joe!

\n", template.render(_Scope.new, :name => 'Joe') end - test 'evaluating in default/nil scope' do + it 'evaluating in default/nil scope' do template = Tilt::HamlTemplate.new { |t| '%p Hey unknown!' } assert_equal "

Hey unknown!

\n", template.render assert_equal "

Hey unknown!

\n", template.render(nil) end - test 'evaluating in invalid, frozen scope' do + it 'evaluating in invalid, frozen scope' do template = Tilt::HamlTemplate.new { |t| '%p Hey unknown!' } assert_raises(ArgumentError) { template.render(Object.new.freeze) } end - test "evaluating in an object scope" do + it "evaluating in an object scope" do template = Tilt::HamlTemplate.new { "%p= 'Hey ' + @name + '!'" } - scope = Scope.new + scope = _Scope.new scope.instance_variable_set :@name, 'Joe' assert_equal "

Hey Joe!

\n", template.render(scope) end - test "passing a block for yield" do + it "passing a block for yield" do template = Tilt::HamlTemplate.new { "%p= 'Hey ' + yield + '!'" } - assert_equal "

Hey Joe!

\n", template.render(Scope.new) { 'Joe' } + assert_equal "

Hey Joe!

\n", template.render(_Scope.new) { 'Joe' } end - test "backtrace file and line reporting without locals" do + it "backtrace file and line reporting without locals" do data = File.read(__FILE__).split("\n__END__\n").last fail unless data[0] == ?% template = Tilt::HamlTemplate.new('test.haml', 10) { data } begin - template.render(Scope.new) + template.render(_Scope.new) fail 'should have raised an exception' rescue => boom assert_kind_of NameError, boom @@ -136,14 +135,14 @@ class Scope end end - test "backtrace file and line reporting with locals" do + it "backtrace file and line reporting with locals" do data = File.read(__FILE__).split("\n__END__\n").last fail unless data[0] == ?% template = Tilt::HamlTemplate.new('test.haml') { data } begin - template.render(Scope.new, :name => 'Joe', :foo => 'bar') + template.render(_Scope.new, :name => 'Joe', :foo => 'bar') rescue => boom - assert_kind_of MockError, boom + assert_kind_of _Scope::MockError, boom line = boom.backtrace.first file, line, _meth = line.split(":") assert_equal 'test.haml', file diff --git a/test/tilt_kramdown_test.rb b/test/tilt_kramdown_test.rb index b63b9aec..b3f569b2 100644 --- a/test/tilt_kramdown_test.rb +++ b/test/tilt_kramdown_test.rb @@ -3,13 +3,13 @@ begin require 'tilt/kramdown' - class KramdownTemplateTest < Minitest::Test - test "preparing and evaluating templates on #render" do + describe 'tilt/kramdown' do + it "preparing and evaluating templates on #render" do template = Tilt::KramdownTemplate.new { |t| "# Hello World!" } assert_equal '

Hello World!

', template.render.strip end - test "can be rendered more than once" do + it "can be rendered more than once" do template = Tilt::KramdownTemplate.new { |t| "# Hello World!" } 3.times { assert_equal '

Hello World!

', template.render.strip } end diff --git a/test/tilt_lesstemplate_test.less b/test/tilt_lesstemplate_test.less index 3e1dafa0..c41b3fdf 100644 --- a/test/tilt_lesstemplate_test.less +++ b/test/tilt_lesstemplate_test.less @@ -1 +1 @@ -@text-color: #ffc0cb; \ No newline at end of file +@text-color: #ffc0cb; diff --git a/test/tilt_lesstemplate_test.rb b/test/tilt_lesstemplate_test.rb index c34d1af9..1a7505de 100644 --- a/test/tilt_lesstemplate_test.rb +++ b/test/tilt_lesstemplate_test.rb @@ -4,26 +4,26 @@ require 'pathname' require 'tilt/less' - class LessTemplateTest < Minitest::Test + describe 'tilt/less' do def assert_similar(a, b) assert_equal a.gsub(/\s+/m, ' '), b.gsub(/\s+/m, ' ') end - test "is registered for '.less' files" do + it "is registered for '.less' files" do assert_equal Tilt::LessTemplate, Tilt['test.less'] end - test "compiles and evaluates the template on #render" do + it "compiles and evaluates the template on #render" do template = Tilt::LessTemplate.new { |t| ".bg { background-color: #0000ff; } \n#main\n { .bg; }\n" } assert_similar ".bg {\n background-color: #0000ff;\n}\n#main {\n background-color: #0000ff;\n}\n", template.render end - test "can be rendered more than once" do + it "can be rendered more than once" do template = Tilt::LessTemplate.new { |t| ".bg { background-color: #0000ff; } \n#main\n { .bg; }\n" } 3.times { assert_similar ".bg {\n background-color: #0000ff;\n}\n#main {\n background-color: #0000ff;\n}\n", template.render } end - test "can be passed a load path" do + it "can be passed a load path" do template = Tilt::LessTemplate.new({ :paths => [Pathname(__FILE__).dirname] }) { diff --git a/test/tilt_liquidtemplate_test.rb b/test/tilt_liquidtemplate_test.rb index e0e4380b..3d6cb138 100644 --- a/test/tilt_liquidtemplate_test.rb +++ b/test/tilt_liquidtemplate_test.rb @@ -3,27 +3,27 @@ begin require 'tilt/liquid' - class LiquidTemplateTest < Minitest::Test - test "registered for '.liquid' files" do + describe 'tilt/liquid' do + it "registered for '.liquid' files" do assert_equal Tilt::LiquidTemplate, Tilt['test.liquid'] end - test "preparing and evaluating templates on #render" do + it "preparing and evaluating templates on #render" do template = Tilt::LiquidTemplate.new { |t| "Hello World!" } assert_equal "Hello World!", template.render end - test "can be rendered more than once" do + it "can be rendered more than once" do template = Tilt::LiquidTemplate.new { |t| "Hello World!" } 3.times { assert_equal "Hello World!", template.render } end - test "passing locals" do + it "passing locals" do template = Tilt::LiquidTemplate.new { "Hey {{ name }}!" } assert_equal "Hey Joe!", template.render(nil, :name => 'Joe') end - test "options can be set" do + it "options can be set" do err = assert_raises(Liquid::SyntaxError) do options = { line_numbers: false, error_mode: :strict } Tilt::LiquidTemplate.new(options) { "{{%%%}}" }.render @@ -41,7 +41,7 @@ def to_h end end - test "combining scope and locals when scope responds to #to_h" do + it "combining scope and locals when scope responds to #to_h" do template = Tilt::LiquidTemplate.new { 'Beer is {{ beer }} but Whisky is {{ whisky }}.' @@ -50,7 +50,7 @@ def to_h assert_equal "Beer is wet but Whisky is wetter.", template.render(scope) end - test "precedence when locals and scope define same variables" do + it "precedence when locals and scope define same variables" do template = Tilt::LiquidTemplate.new { 'Beer is {{ beer }} but Whisky is {{ whisky }}.' @@ -65,13 +65,13 @@ def to_h class ExampleIgnoredLiquidScope end - test "handling scopes that do not respond to #to_h" do + it "handling scopes that do not respond to #to_h" do template = Tilt::LiquidTemplate.new { 'Whisky' } scope = ExampleIgnoredLiquidScope.new assert_equal "Whisky", template.render(scope) end - test "passing a block for yield" do + it "passing a block for yield" do template = Tilt::LiquidTemplate.new { 'Beer is {{ yield }} but Whisky is {{ content }}ter.' diff --git a/test/tilt_livescripttemplate_test.rb b/test/tilt_livescripttemplate_test.rb index 3e8b7d59..9fc40243 100644 --- a/test/tilt_livescripttemplate_test.rb +++ b/test/tilt_livescripttemplate_test.rb @@ -3,23 +3,23 @@ begin require 'tilt/livescript' - class LiveScriptTemplateTest < Minitest::Test - setup do + describe 'tilt/livescript' do + before do @code_without_variables = "puts 'Hello, World!'\n" @renderer = Tilt::LiveScriptTemplate end - test "compiles and evaluates the template on #render" do + it "compiles and evaluates the template on #render" do template = @renderer.new { |t| @code_without_variables } assert_match "puts('Hello, World!');", template.render end - test "can be rendered more than once" do + it "can be rendered more than once" do template = @renderer.new { |t| @code_without_variables } 3.times { assert_match "puts('Hello, World!');", template.render } end - test "supports bare-option" do + it "supports bare-option" do template = @renderer.new(:bare => false) { |t| @code_without_variables } assert_match "function", template.render @@ -27,7 +27,7 @@ class LiveScriptTemplateTest < Minitest::Test refute_match "function", template.render end - test "is registered for '.ls' files" do + it "is registered for '.ls' files" do assert_equal @renderer, Tilt['test.ls'] end end diff --git a/test/tilt_mapping_test.rb b/test/tilt_mapping_test.rb index 63d810ac..cfe3ef1e 100644 --- a/test/tilt_mapping_test.rb +++ b/test/tilt_mapping_test.rb @@ -1,235 +1,228 @@ require_relative 'test_helper' -module Tilt +describe 'tilt/mapping' do + _Stub = Class.new + _Stub2 = Class.new - class MappingTest < Minitest::Test - class Stub - end + before do + @mapping = Tilt::Mapping.new + end - class Stub2 - end + it "registered?" do + @mapping.register(_Stub, 'foo', 'bar') + assert @mapping.registered?('foo') + assert @mapping.registered?('bar') + refute @mapping.registered?('baz') + end - setup do - @mapping = Mapping.new - end + it "lookups on registered" do + @mapping.register(_Stub, 'foo', 'bar') + assert_equal _Stub, @mapping['foo'] + assert_equal _Stub, @mapping['bar'] + assert_equal _Stub, @mapping['hello.foo'] + assert_nil @mapping['foo.baz'] + end - test "registered?" do - @mapping.register(Stub, 'foo', 'bar') - assert @mapping.registered?('foo') - assert @mapping.registered?('bar') - refute @mapping.registered?('baz') - end + it "can be dup'd" do + @mapping.register(_Stub, 'foo') + other = @mapping.dup + assert other.registered?('foo') - test "lookups on registered" do - @mapping.register(Stub, 'foo', 'bar') - assert_equal Stub, @mapping['foo'] - assert_equal Stub, @mapping['bar'] - assert_equal Stub, @mapping['hello.foo'] - assert_nil @mapping['foo.baz'] - end + # @mapping doesn't leak to other + @mapping.register(_Stub, 'bar') + refute other.registered?('bar') - test "can be dup'd" do - @mapping.register(Stub, 'foo') - other = @mapping.dup - assert other.registered?('foo') + # other doesn't leak to @mapping + other.register(_Stub, 'baz') + refute @mapping.registered?('baz') + end - # @mapping doesn't leak to other - @mapping.register(Stub, 'bar') - refute other.registered?('bar') + it "#extensions_for" do + @mapping.register(_Stub, 'foo', 'bar') + assert_equal ['foo', 'bar'].sort, @mapping.extensions_for(_Stub).sort + end - # other doesn't leak to @mapping - other.register(Stub, 'baz') - refute @mapping.registered?('baz') - end + it "supports old-style #register" do + @mapping.register('foo', _Stub) + assert_equal _Stub, @mapping['foo'] + end - test "#extensions_for" do - @mapping.register(Stub, 'foo', 'bar') - assert_equal ['foo', 'bar'].sort, @mapping.extensions_for(Stub).sort + describe "lazy with one template class" do + before do + @mapping.register_lazy('MyTemplate', 'my_template', 'mt') + @loaded_before = $LOADED_FEATURES.dup end - test "supports old-style #register" do - @mapping.register('foo', Stub) - assert_equal Stub, @mapping['foo'] + after do + Object.send :remove_const, :MyTemplate if defined? ::MyTemplate + $LOADED_FEATURES.replace(@loaded_before) end - context "lazy with one template class" do - setup do - @mapping.register_lazy('MyTemplate', 'my_template', 'mt') - @loaded_before = $LOADED_FEATURES.dup - end - - teardown do - Object.send :remove_const, :MyTemplate if defined? ::MyTemplate - $LOADED_FEATURES.replace(@loaded_before) - end - - test "registered?" do - assert @mapping.registered?('mt') - end - - test "#extensions_for" do - assert_equal ['mt'], @mapping.extensions_for('MyTemplate') - end - - test "basic lookup" do - req = proc do |file| - assert_equal 'my_template', file - class ::MyTemplate; end - true - end + it "registered?" do + assert @mapping.registered?('mt') + end - @mapping.stub :require, req do - klass = @mapping['hello.mt'] - assert_equal ::MyTemplate, klass - end - end + it "#extensions_for" do + assert_equal ['mt'], @mapping.extensions_for('MyTemplate') + end - test "doesn't require when template class is present" do + it "basic lookup" do + req = proc do |file| + assert_equal 'my_template', file class ::MyTemplate; end - - req = proc do |file| - flunk "#require shouldn't be called" - end - - @mapping.stub :require, req do - klass = @mapping['hello.mt'] - assert_equal ::MyTemplate, klass - end + true end - test "doesn't require when the template class is autoloaded, and then defined" do - $LOAD_PATH << __dir__ - begin - Object.autoload :MyTemplate, 'mytemplate' - did_load = require 'mytemplate' - ensure - $LOAD_PATH.delete(__dir__) - end - assert did_load, "mytemplate wasn't freshly required" + @mapping.stub :require, req do + klass = @mapping['hello.mt'] + assert_equal ::MyTemplate, klass + end + end - req = proc do |file| - flunk "#require shouldn't be called" - end + it "doesn't require when template class is present" do + class ::MyTemplate; end - @mapping.stub :require, req do - klass = @mapping['hello.mt'] - assert_equal ::MyTemplate, klass - end + req = proc do |file| + flunk "#require shouldn't be called" end - test "raises NameError when the class name is defined" do - req = proc do |file| - # do nothing - end - - @mapping.stub :require, req do - assert_raises(NameError) do - @mapping['hello.mt'] - end - end + @mapping.stub :require, req do + klass = @mapping['hello.mt'] + assert_equal ::MyTemplate, klass end end - context "lazy with two template classes" do - setup do - @mapping.register_lazy('MyTemplate1', 'my_template1', 'mt') - @mapping.register_lazy('MyTemplate2', 'my_template2', 'mt') + it "doesn't require when the template class is autoloaded, and then defined" do + $LOAD_PATH << __dir__ + begin + Object.autoload :MyTemplate, 'mytemplate' + did_load = require 'mytemplate' + ensure + $LOAD_PATH.delete(__dir__) end + assert did_load, "mytemplate wasn't freshly required" - teardown do - Object.send :remove_const, :MyTemplate1 if defined? ::MyTemplate1 - Object.send :remove_const, :MyTemplate2 if defined? ::MyTemplate2 + req = proc do |file| + flunk "#require shouldn't be called" end - test "registered?" do - assert @mapping.registered?('mt') + @mapping.stub :require, req do + klass = @mapping['hello.mt'] + assert_equal ::MyTemplate, klass end + end - test "only attempt to load the last template" do - req = proc do |file| - assert_equal 'my_template2', file - class ::MyTemplate2; end - true - end + it "raises NameError when the class name is defined" do + req = proc do |file| + # do nothing + end - @mapping.stub :require, req do - klass = @mapping['hello.mt'] - assert_equal ::MyTemplate2, klass + @mapping.stub :require, req do + assert_raises(NameError) do + @mapping['hello.mt'] end end + end + end - test "uses the first template if it's present" do - class ::MyTemplate1; end + describe "lazy with two template classes" do + before do + @mapping.register_lazy('MyTemplate1', 'my_template1', 'mt') + @mapping.register_lazy('MyTemplate2', 'my_template2', 'mt') + end - req = proc do |file| - flunk - end + after do + Object.send :remove_const, :MyTemplate1 if defined? ::MyTemplate1 + Object.send :remove_const, :MyTemplate2 if defined? ::MyTemplate2 + end - @mapping.stub :require, req do - klass = @mapping['hello.mt'] - assert_equal ::MyTemplate1, klass - end - end + it "registered?" do + assert @mapping.registered?('mt') + end - test "falls back when LoadError is thrown" do - req = proc do |file| - raise LoadError unless file == 'my_template1' - class ::MyTemplate1; end - true - end + it "only attempt to load the last template" do + req = proc do |file| + assert_equal 'my_template2', file + class ::MyTemplate2; end + true + end - @mapping.stub :require, req do - klass = @mapping['hello.mt'] - assert_equal ::MyTemplate1, klass - end + @mapping.stub :require, req do + klass = @mapping['hello.mt'] + assert_equal ::MyTemplate2, klass end + end - test "raises the first LoadError when everything fails" do - req = proc do |file| - raise LoadError, file - end + it "uses the first template if it's present" do + class ::MyTemplate1; end - @mapping.stub :require, req do - err = assert_raises(LoadError) do - @mapping['hello.mt'] - end + req = proc do |file| + flunk + end - assert_equal 'my_template2', err.message - end + @mapping.stub :require, req do + klass = @mapping['hello.mt'] + assert_equal ::MyTemplate1, klass end + end - test "handles autoloaded constants" do - Object.autoload :MyTemplate2, 'my_template2' + it "falls back when LoadError is thrown" do + req = proc do |file| + raise LoadError unless file == 'my_template1' class ::MyTemplate1; end + true + end - assert_equal MyTemplate1, @mapping['hello.mt'] + @mapping.stub :require, req do + klass = @mapping['hello.mt'] + assert_equal ::MyTemplate1, klass end end - test "raises NameError on invalid class name" do - @mapping.register_lazy '#foo', 'my_template', 'mt' - + it "raises the first LoadError when everything fails" do req = proc do |file| - # do nothing + raise LoadError, file end @mapping.stub :require, req do - assert_raises(NameError) do + err = assert_raises(LoadError) do @mapping['hello.mt'] end + + assert_equal 'my_template2', err.message end end - context "#templates_for" do - setup do - @mapping.register Stub, 'a' - @mapping.register Stub2, 'b' - end + it "handles autoloaded constants" do + Object.autoload :MyTemplate2, 'my_template2' + class ::MyTemplate1; end - test "handles multiple engines" do - assert_equal [Stub2, Stub], @mapping.templates_for('hello/world.a.b') + assert_equal MyTemplate1, @mapping['hello.mt'] + end + end + + it "raises NameError on invalid class name" do + @mapping.register_lazy '#foo', 'my_template', 'mt' + + req = proc do |file| + # do nothing + end + + @mapping.stub :require, req do + assert_raises(NameError) do + @mapping['hello.mt'] end end end -end + describe "#templates_for" do + before do + @mapping.register _Stub, 'a' + @mapping.register _Stub2, 'b' + end + + it "handles multiple engines" do + assert_equal [_Stub2, _Stub], @mapping.templates_for('hello/world.a.b') + end + end +end diff --git a/test/tilt_markaby_test.rb b/test/tilt_markaby_test.rb index 5d5aafad..d8ba6831 100644 --- a/test/tilt_markaby_test.rb +++ b/test/tilt_markaby_test.rb @@ -3,50 +3,50 @@ begin require 'tilt/markaby' - class MarkabyTiltTest < Minitest::Test - def setup + describe 'tilt/markaby' do + def before @block = lambda do |t| File.read(File.dirname(__FILE__) + "/#{t.file}") end end - test "should be able to render a markaby template with static html" do - tilt = Tilt::MarkabyTemplate.new("markaby/markaby.mab", &@block) + it "should be able to render a markaby template with static html" do + tilt = Tilt::MarkabyTemplate.new("test/markaby/markaby.mab", &@block) assert_equal "hello from markaby!", tilt.render end - test "should use the contents of the template" do - tilt = ::Tilt::MarkabyTemplate.new("markaby/markaby_other_static.mab", &@block) + it "should use the contents of the template" do + tilt = ::Tilt::MarkabyTemplate.new("test/markaby/markaby_other_static.mab", &@block) assert_equal "_why?", tilt.render end - test "should render from a string (given as data)" do + it "should render from a string (given as data)" do tilt = ::Tilt::MarkabyTemplate.new { "html do; end" } assert_equal "", tilt.render end - test "can be rendered more than once" do + it "can be rendered more than once" do tilt = ::Tilt::MarkabyTemplate.new { "html do; end" } 3.times { assert_equal "", tilt.render } end - test "should evaluate a template file in the scope given" do + it "should evaluate a template file in the scope given" do scope = Object.new def scope.foo "bar" end - tilt = ::Tilt::MarkabyTemplate.new("markaby/scope.mab", &@block) + tilt = ::Tilt::MarkabyTemplate.new("test/markaby/scope.mab", &@block) assert_equal "
  • bar
  • ", tilt.render(scope) end - test "should pass locals to the template" do - tilt = ::Tilt::MarkabyTemplate.new("markaby/locals.mab", &@block) + it "should pass locals to the template" do + tilt = ::Tilt::MarkabyTemplate.new("test/markaby/locals.mab", &@block) assert_equal "
  • bar
  • ", tilt.render(Object.new, { :foo => "bar" }) end - test "should yield to the block given" do - tilt = ::Tilt::MarkabyTemplate.new("markaby/yielding.mab", &@block) + it "should yield to the block given" do + tilt = ::Tilt::MarkabyTemplate.new("test/markaby/yielding.mab", &@block) eval_scope = Markaby::Builder.new output = tilt.render(Object.new, {}) do @@ -56,27 +56,27 @@ def scope.foo assert_equal "Hey Joe", output end - test "should be able to render two templates in a row" do - tilt = ::Tilt::MarkabyTemplate.new("markaby/render_twice.mab", &@block) + it "should be able to render two templates in a row" do + tilt = ::Tilt::MarkabyTemplate.new("test/markaby/render_twice.mab", &@block) assert_equal "foo", tilt.render assert_equal "foo", tilt.render end - test "should retrieve a Tilt::MarkabyTemplate when calling Tilt['hello.mab']" do - assert_equal Tilt::MarkabyTemplate, ::Tilt['./markaby/markaby.mab'] + it "should retrieve a Tilt::MarkabyTemplate when calling Tilt['hello.mab']" do + assert_equal Tilt::MarkabyTemplate, ::Tilt['test/markaby/markaby.mab'] end - test "should return a new instance of the implementation class (when calling Tilt.new)" do + it "should return a new instance of the implementation class (when calling Tilt.new)" do assert ::Tilt.new(File.dirname(__FILE__) + "/markaby/markaby.mab").kind_of?(Tilt::MarkabyTemplate) end - test "should be able to evaluate block style templates" do + it "should be able to evaluate block style templates" do tilt = Tilt::MarkabyTemplate.new { |t| lambda { h1 "Hello World!" }} assert_equal "

    Hello World!

    ", tilt.render end - test "should pass locals to block style templates" do + it "should pass locals to block style templates" do tilt = Tilt::MarkabyTemplate.new { |t| lambda { h1 "Hello #{name}!" }} assert_equal "

    Hello _why!

    ", tilt.render(nil, :name => "_why") end diff --git a/test/tilt_markdown_test.rb b/test/tilt_markdown_test.rb index 9390aa1c..3b39ccd3 100644 --- a/test/tilt_markdown_test.rb +++ b/test/tilt_markdown_test.rb @@ -3,7 +3,9 @@ begin require 'nokogiri' -module MarkdownTests +_MarkdownTests = Module.new do + extend Minitest::Spec::DSL + def self.included(mod) class << mod def template(t = nil) @@ -26,57 +28,57 @@ def nrender(text, options = {}) normalize(html) end - def test_escape_html + it "should not escape html by default" do html = nrender "Hello World" assert_equal "

    Hello World

    ", html end - def test_escape_html_false + it "should not escape html for :escape_html => false" do html = nrender "Hello World", :escape_html => false assert_equal "

    Hello World

    ", html end - def test_escape_html_true + it "should escape html for :escape_html => true" do html = nrender "Hello World", :escape_html => true assert_equal "

    Hello <b>World</b>

    ", html end - def test_smart_quotes + it "should not use smart quotes by default" do html = nrender 'Hello "World"' assert_equal '

    Hello "World"

    ', html end - def test_smart_quotes_false + it "should not use smart quotes if :smartypants => false" do html = nrender 'Hello "World"', :smartypants => false assert_equal '

    Hello "World"

    ', html end - def test_smart_quotes_true + it "should use smart quotes if :smartypants => true" do html = nrender 'Hello "World"', :smartypants => true assert_equal '

    Hello “World”

    ', html end - def test_smarty_pants - html = nrender "Hello ``World'' -- This is --- a test ..." - assert_equal "

    Hello ``World'' -- This is --- a test ...

    ", html + it "should not use smartypants by default" do + html = nrender "Hello ``World'' -- This is --- a it ..." + assert_equal "

    Hello ``World'' -- This is --- a it ...

    ", html end - def test_smarty_pants_false - html = nrender "Hello ``World'' -- This is --- a test ...", :smartypants => false - assert_equal "

    Hello ``World'' -- This is --- a test ...

    ", html + it "should not use smartypants if :smartypants => false" do + html = nrender "Hello ``World'' -- This is --- a it ...", :smartypants => false + assert_equal "

    Hello ``World'' -- This is --- a it ...

    ", html end end begin require 'tilt/rdiscount' - class MarkdownRDiscountTest < Minitest::Test - include MarkdownTests + describe 'tilt/rdiscount (markdown)' do + include _MarkdownTests template Tilt::RDiscountTemplate - def test_smarty_pants_true - html = nrender "Hello ``World'' -- This is --- a test ...", :smartypants => true - assert_equal "

    Hello “World” – This is — a test …

    ", html + it "should use smartypants if :smartypants => true" do + html = nrender "Hello ``World'' -- This is --- a it ...", :smartypants => true + assert_equal "

    Hello “World” – This is — a it …

    ", html end end rescue LoadError => boom @@ -86,22 +88,22 @@ def test_smarty_pants_true begin require 'tilt/redcarpet' - class MarkdownRedcarpetTest < Minitest::Test - include MarkdownTests + describe 'tilt/redcarpet (markdown)' do + include _MarkdownTests template Tilt::RedcarpetTemplate - def test_smarty_pants_true + it "should use smartypants if :smartypants => true" do # Various versions of Redcarpet support various versions of Smart pants. - html = nrender "Hello ``World'' -- This is --- a test ...", :smartypants => true - assert_match %r!

    Hello “World(''|”) – This is — a test …<\/p>!, html + html = nrender "Hello ``World'' -- This is --- a it ...", :smartypants => true + assert_match %r!

    Hello “World(''|”) – This is — a it …<\/p>!, html end - def test_renderer_options + it "should support :no_links option" do html = nrender "Hello [World](http://example.com)", :smartypants => true, :no_links => true assert_equal "

    Hello [World](http://example.com)

    ", html end - def test_fenced_code_blocks_with_lang + it "should support fenced code blocks with lang" do code = <<-COD.gsub(/^\s+/,"") ```ruby puts "hello world" @@ -119,13 +121,13 @@ def test_fenced_code_blocks_with_lang begin require 'tilt/bluecloth' - class MarkdownBlueClothTest < Minitest::Test - include MarkdownTests + describe 'tilt/bluecloth (markdown)' do + include _MarkdownTests template Tilt::BlueClothTemplate - def test_smarty_pants_true - html = nrender "Hello ``World'' -- This is --- a test ...", :smartypants => true - assert_equal "

    Hello “World” — This is —– a test …

    ", html + it "should use smartypants if :smartypants => true" do + html = nrender "Hello ``World'' -- This is --- a it ...", :smartypants => true + assert_equal "

    Hello “World” — This is —– a it …

    ", html end end rescue LoadError => boom @@ -135,14 +137,17 @@ def test_smarty_pants_true begin require 'tilt/kramdown' - class MarkdownKramdownTest < Minitest::Test - include MarkdownTests + describe 'tilt/kramdown (markdown)' do + include _MarkdownTests template Tilt::KramdownTemplate - # Doesn't support escaping - undef test_escape_html_true - # Smarty Pants is *always* on, but doesn't support it fully - undef test_smarty_pants - undef test_smarty_pants_false + skip_tests = [ + ':escape_html => true', + 'smartypants by default', + 'smartypants if :smartypants => false', + ] + instance_methods.grep(/#{Regexp.union(skip_tests)}\z/).each do |method| + undef_method method + end end rescue LoadError => boom # It should already be warned in the main tests @@ -152,33 +157,35 @@ class MarkdownKramdownTest < Minitest::Test begin require 'tilt/maruku' - class MarkdownMarukuTest < Minitest::Test - include MarkdownTests + describe 'tilt/maruku (markdown)' do + include _MarkdownTests template Tilt::MarukuTemplate - # Doesn't support escaping - undef test_escape_html_true - # Doesn't support Smarty Pants, and even fails on ``Foobar'' - undef test_smarty_pants - undef test_smarty_pants_false - # Smart Quotes is always on - undef test_smart_quotes - undef test_smart_quotes_false + skip_tests = [ + ':escape_html => true', + 'smartypants by default', + 'smartypants if :smartypants => false', + 'smart quotes by default', + 'smart quotes if :smartypants => false', + ] + instance_methods.grep(/#{Regexp.union(skip_tests)}\z/).each do |method| + undef_method method + end end rescue LoadError => boom # It should already be warned in the main tests end -rescue LoadError - warn "Markdown tests need Nokogiri" -end - begin require 'tilt/pandoc' - class MarkdownPandocTest < Minitest::Test - include MarkdownTests + describe 'tilt/pandoc (markdown)' do + include _MarkdownTests template Tilt::PandocTemplate end rescue LoadError => boom # It should already be warned in the main tests end + +rescue LoadError + warn "Markdown tests need Nokogiri" +end diff --git a/test/tilt_marukutemplate_test.rb b/test/tilt_marukutemplate_test.rb index a657625e..2c1de28e 100644 --- a/test/tilt_marukutemplate_test.rb +++ b/test/tilt_marukutemplate_test.rb @@ -3,8 +3,8 @@ begin require 'tilt/maruku' - class MarukuTemplateTest < Minitest::Test - test "registered below Kramdown" do + describe 'tilt/maruku' do + it "registered below Kramdown" do %w[md mkd markdown].each do |ext| lazy = Tilt.lazy_map[ext] kram_idx = lazy.index { |klass, file| klass == 'Tilt::KramdownTemplate' } @@ -14,17 +14,17 @@ class MarukuTemplateTest < Minitest::Test end end - test "preparing and evaluating templates on #render" do + it "preparing and evaluating templates on #render" do template = Tilt::MarukuTemplate.new { |t| "# Hello World!" } assert_equal "

    Hello World!

    ", template.render.strip end - test "can be rendered more than once" do + it "can be rendered more than once" do template = Tilt::MarukuTemplate.new { |t| "# Hello World!" } 3.times { assert_equal "

    Hello World!

    ", template.render.strip } end - test "removes HTML when :filter_html is set" do + it "removes HTML when :filter_html is set" do template = Tilt::MarukuTemplate.new(:filter_html => true) { |t| "HELLO WORLD" } assert_equal "

    HELLO

    ", template.render.strip diff --git a/test/tilt_metadata_test.rb b/test/tilt_metadata_test.rb index d31c01f7..bf38eb56 100644 --- a/test/tilt_metadata_test.rb +++ b/test/tilt_metadata_test.rb @@ -1,41 +1,38 @@ require_relative 'test_helper' -module Tilt - class TemplateMetadataTest < Minitest::Test - class MyTemplate < Template - metadata[:global] = 1 - self.default_mime_type = 'text/html' - - def prepare - end - - def allows_script? - true - end - end +describe 'tilt metadata' do + _MyTemplate = Class.new(Tilt::Template) do + metadata[:global] = 1 + self.default_mime_type = 'text/html' - test "global metadata" do - assert MyTemplate.metadata - assert_equal 1, MyTemplate.metadata[:global] + def prepare end - test "instance metadata" do - tmpl = MyTemplate.new { '' } - assert_equal 1, tmpl.metadata[:global] + def allows_script? + true end + end - test "gracefully handles default_mime_type" do - assert_equal 'text/html', MyTemplate.metadata[:mime_type] - end + it "global metadata" do + assert _MyTemplate.metadata + assert_equal 1, _MyTemplate.metadata[:global] + end - test "still allows .default_mime_type" do - assert_equal 'text/html', MyTemplate.default_mime_type - end + it "instance metadata" do + tmpl = _MyTemplate.new { '' } + assert_equal 1, tmpl.metadata[:global] + end - test "gracefully handles allows_script?" do - tmpl = MyTemplate.new { '' } - assert_equal true, tmpl.metadata[:allows_script] - end + it "gracefully handles default_mime_type" do + assert_equal 'text/html', _MyTemplate.metadata[:mime_type] + end + + it "still allows .default_mime_type" do + assert_equal 'text/html', _MyTemplate.default_mime_type end -end + it "gracefully handles allows_script?" do + tmpl = _MyTemplate.new { '' } + assert_equal true, tmpl.metadata[:allows_script] + end +end diff --git a/test/tilt_nokogiritemplate_test.rb b/test/tilt_nokogiritemplate_test.rb index 2d0a563c..af13046a 100644 --- a/test/tilt_nokogiritemplate_test.rb +++ b/test/tilt_nokogiritemplate_test.rb @@ -2,20 +2,21 @@ begin require 'tilt/nokogiri' - class NokogiriTemplateTest < Minitest::Test - test "registered for '.nokogiri' files" do + + describe 'tilt/nokogiri' do + it "registered for '.nokogiri' files" do assert_equal Tilt::NokogiriTemplate, Tilt['test.nokogiri'] assert_equal Tilt::NokogiriTemplate, Tilt['test.xml.nokogiri'] end - test "preparing and evaluating the template on #render" do + it "preparing and evaluating the template on #render" do template = Tilt::NokogiriTemplate.new { |t| "xml.em 'Hello World!'" } doc = Nokogiri.XML template.render assert_equal 'Hello World!', doc.root.text assert_equal 'em', doc.root.name end - test "can be rendered more than once" do + it "can be rendered more than once" do template = Tilt::NokogiriTemplate.new { |t| "xml.em 'Hello World!'" } 3.times do doc = Nokogiri.XML template.render @@ -24,14 +25,14 @@ class NokogiriTemplateTest < Minitest::Test end end - test "passing locals" do + it "passing locals" do template = Tilt::NokogiriTemplate.new { "xml.em('Hey ' + name + '!')" } doc = Nokogiri.XML template.render(Object.new, :name => 'Joe') assert_equal 'Hey Joe!', doc.root.text assert_equal 'em', doc.root.name end - test "evaluating in an object scope" do + it "evaluating in an object scope" do template = Tilt::NokogiriTemplate.new { "xml.em('Hey ' + @name + '!')" } scope = Object.new scope.instance_variable_set :@name, 'Joe' @@ -40,7 +41,7 @@ class NokogiriTemplateTest < Minitest::Test assert_equal 'em', doc.root.name end - test "passing a block for yield" do + it "passing a block for yield" do template = Tilt::NokogiriTemplate.new { "xml.em('Hey ' + yield + '!')" } 3.times do doc = Nokogiri.XML template.render { 'Joe' } @@ -49,7 +50,7 @@ class NokogiriTemplateTest < Minitest::Test end end - test "block style templates" do + it "block style templates" do template = Tilt::NokogiriTemplate.new do |t| lambda { |xml| xml.em('Hey Joe!') } @@ -59,7 +60,7 @@ class NokogiriTemplateTest < Minitest::Test assert_equal 'em', doc.root.name end - test "allows nesting raw XML, API-compatible to Builder" do + it "allows nesting raw XML, API-compatible to Builder" do subtemplate = Tilt::NokogiriTemplate.new { "xml.em 'Hello World!'" } template = Tilt::NokogiriTemplate.new { "xml.strong { xml << yield }" } 3.times do @@ -70,7 +71,7 @@ class NokogiriTemplateTest < Minitest::Test end end - test "doesn't modify self when template is a string" do + it "doesn't modify self when template is a string" do template = Tilt::NokogiriTemplate.new { "xml.root { xml.child @hello }" } scope = Object.new scope.instance_variable_set(:@hello, "Hello World!") diff --git a/test/tilt_pandoctemplate_test.rb b/test/tilt_pandoctemplate_test.rb index 5f89960c..1ea07045 100644 --- a/test/tilt_pandoctemplate_test.rb +++ b/test/tilt_pandoctemplate_test.rb @@ -3,59 +3,59 @@ begin require 'tilt/pandoc' - class PandocTemplateTest < Minitest::Test - test "preparing and evaluating templates on #render" do + describe 'tilt/pandoc' do + it "preparing and evaluating templates on #render" do template = Tilt::PandocTemplate.new { |t| "# Hello World!" } assert_equal "

    Hello World!

    ", template.render end - test "can be rendered more than once" do + it "can be rendered more than once" do template = Tilt::PandocTemplate.new { |t| "# Hello World!" } 3.times { assert_equal "

    Hello World!

    ", template.render } end - test "smartypants when :smartypants is set" do + it "smartypants when :smartypants is set" do template = Tilt::PandocTemplate.new(:smartypants => true) { |t| "OKAY -- 'Smarty Pants'" } assert_equal "

    OKAY – ‘Smarty Pants’

    ", template.render end - test "stripping HTML when :escape_html is set" do + it "stripping HTML when :escape_html is set" do template = Tilt::PandocTemplate.new(:escape_html => true) { |t| "HELLO WORLD" } assert_equal "

    HELLO <blink>WORLD</blink>

    ", template.render end # Pandoc has tons of additional markdown features (see http://pandoc.org/README.html#pandocs-markdown). - # The test for footnotes should be seen as a general representation for all of them. + # The it for footnotes should be seen as a general representation for all of them. # use markdown_strict => true to disable additional markdown features describe "passing in Pandoc options" do - test "generates footnotes" do + it "generates footnotes" do template = Tilt::PandocTemplate.new { |t| "Here is an inline note.^[Inlines notes are cool!]" } result = template.render assert_match "Here is an inline note", result assert_match "Inlines notes are cool!", result end - test "doesn't generate footnotes with markdown_strict option" do + it "doesn't generate footnotes with markdown_strict option" do template = Tilt::PandocTemplate.new(:markdown_strict => true) { |t| "Here is an inline note.^[Inlines notes are cool!]" } assert_equal "

    Here is an inline note.^[Inlines notes are cool!]

    ", template.render end - test "doesn't generate footnotes with commonmark option" do + it "doesn't generate footnotes with commonmark option" do template = Tilt::PandocTemplate.new(:commonmark => true) { |t| "Here is an inline note.^[Inlines notes are cool!]" } assert_equal "

    Here is an inline note.^[Inlines notes are cool!]

    ", template.render end - test "accepts arguments with values (e.g. :id_prefix => 'xyz')" do + it "accepts arguments with values (e.g. :id_prefix => 'xyz')" do # Table of contents isn't on by default template = Tilt::PandocTemplate.new { |t| "# This is a heading" } assert_equal "

    This is a heading

    ", template.render # But it can be activated - template = Tilt::PandocTemplate.new(:id_prefix => 'test-') { |t| "# This is a heading" } - assert_equal "

    This is a heading

    ", template.render + template = Tilt::PandocTemplate.new(:id_prefix => 'it-') { |t| "# This is a heading" } + assert_equal "

    This is a heading

    ", template.render end - test "requires arguments without value (e.g. --standalone) to be passed as hash keys (:standalone => true)" do + it "requires arguments without value (e.g. --standalone) to be passed as hash keys (:standalone => true)" do template = Tilt::PandocTemplate.new(:standalone => true) { |t| "# This is a heading" } assert_match(/^This is a heading<\/h1>.*<\/html>$/m, template.render) end diff --git a/test/tilt_pipeline_test.rb b/test/tilt_pipeline_test.rb index e202831f..98682c89 100644 --- a/test/tilt_pipeline_test.rb +++ b/test/tilt_pipeline_test.rb @@ -1,60 +1,60 @@ require_relative 'test_helper' -class PipelineTest < Minitest::Test - setup do +describe 'tilt/pipeline' do + before do @mapping = Tilt.default_mapping.dup @pipeline_class = @mapping.register_pipeline('str.erb') end - test "returns a template class" do + it "returns a template class" do assert_equal Tilt::Pipeline, @pipeline_class.superclass end - test "registers itself for the given extension" do + it "registers itself for the given extension" do assert_equal @pipeline_class, @mapping['test.str.erb'] end - test "renders templates starting with final extension to inner extensions" do + it "renders templates starting with final extension to inner extensions" do template = @pipeline_class.new { |t| '#<%= \'{a = 1}\' %><%= \'#{a}\' %>' } assert_equal "11", template.render end - test "can be rendered more than once" do + it "can be rendered more than once" do template = @pipeline_class.new { |t| '<%= \'#{1}\' %>' } 3.times { assert_equal "1", template.render } end - test "passing locals" do + it "passing locals" do template = @pipeline_class.new { |t| '<%= \'#{a}\' * a %>' } assert_equal "333", template.render(Object.new, :a => 3) end - test "evaluating in an object scope" do + it "evaluating in an object scope" do template = @pipeline_class.new { |t| '<%= \'#{a}\' * a %>' } o = Object.new def o.a; 3 end assert_equal "333", template.render(o) end - test "passing a block for yield" do + it "passing a block for yield" do template = @pipeline_class.new { |t| '<%= \'#{yield}\' * yield %>' } assert_equal "333", template.render { 3 } assert_equal "22", template.render { 2 } end end -class PipelineOptionsTest < Minitest::Test - setup do +describe 'tilt/pipeline (options)' do + before do @mapping = Tilt.default_mapping.dup end - test "supports :templates option for specifying templates to use in order" do + it "supports :templates option for specifying templates to use in order" do pipeline = @mapping.register_pipeline('setrrb', :templates=>['erb', 'str']) template = pipeline.new { |t| '#<%= \'{a = 1}\' %><%= \'#{a}\' %>' } assert_equal "11", template.render end - test "supports :extra_exts option for specifying additional extensions to register" do + it "supports :extra_exts option for specifying additional extensions to register" do @mapping.register_pipeline('str.erb', :extra_exts=>['setrrb', 'asdfoa']) ['str.erb', 'setrrb', 'asdfoa'].each do |ext| template = @mapping[ext].new { |t| '#<%= \'{a = 1}\' %><%= \'#{a}\' %>' } @@ -62,7 +62,7 @@ class PipelineOptionsTest < Minitest::Test end end - test "supports per template class options" do + it "supports per template class options" do pipeline = @mapping.register_pipeline('str.erb', 'erb'=>{:outvar=>'@foo'}) template = pipeline.new { |t| '#<% @foo << \'{a = 1}\' %><%= \'#{a}\' %>' } assert_equal "11", template.render diff --git a/test/tilt_prawntemplate.prawn b/test/tilt_prawntemplate.prawn index 4f2aa66b..91603c3c 100644 --- a/test/tilt_prawntemplate.prawn +++ b/test/tilt_prawntemplate.prawn @@ -1 +1 @@ -pdf.text "Hello Template!" \ No newline at end of file +pdf.text "Hello Template!" diff --git a/test/tilt_prawntemplate_test.rb b/test/tilt_prawntemplate_test.rb index 277d2e46..45f413b1 100644 --- a/test/tilt_prawntemplate_test.rb +++ b/test/tilt_prawntemplate_test.rb @@ -4,7 +4,7 @@ require 'tilt/prawn' require 'pdf-reader' - class PdfOutput + _PdfOutput = Class.new do def initialize(pdf_raw) @reader = PDF::Reader.new(StringIO.new(pdf_raw)) end @@ -18,51 +18,51 @@ def page_attributes(page_num=1) end end - class PrawnTemplateTest < Minitest::Test - test "is registered for '.prawn' files" do + describe 'tilt/prawn' do + it "is registered for '.prawn' files" do assert_equal Tilt::PrawnTemplate, Tilt['test.prawn'] end - test "compiles and evaluates the template on #render" do + it "compiles and evaluates the template on #render" do template = Tilt::PrawnTemplate.new { |t| "pdf.text \"Hello PDF!\"" } - output = PdfOutput.new(template.render) + output = _PdfOutput.new(template.render) assert_includes output.text, "Hello PDF!" end - test "can be rendered more than once" do + it "can be rendered more than once" do template = Tilt::PrawnTemplate.new { |t| "pdf.text \"Hello PDF!\"" } 3.times do - output = PdfOutput.new(template.render) + output = _PdfOutput.new(template.render) assert_includes output.text, "Hello PDF!" end end - test "loads the template from a file and renders it correctly" do - template = Tilt::PrawnTemplate.new("#{Dir.pwd}/test/tilt_prawntemplate.prawn") - output = PdfOutput.new(template.render) + it "loads the template from a file and renders it correctly" do + template = Tilt::PrawnTemplate.new("test/tilt_prawntemplate.prawn") + output = _PdfOutput.new(template.render) assert_includes output.text, "Hello Template!" end - test "loads the template from a file and can be rendered more than once" do - template = Tilt::PrawnTemplate.new("#{Dir.pwd}/test/tilt_prawntemplate.prawn") + it "loads the template from a file and can be rendered more than once" do + template = Tilt::PrawnTemplate.new("test/tilt_prawntemplate.prawn") 3.times do - output = PdfOutput.new(template.render) + output = _PdfOutput.new(template.render) assert_includes output.text, "Hello Template!" end end - test "have the correct default page size & layout settings - (default: A4 portrait)" do + it "have the correct default page size & layout settings - (default: A4 portrait)" do # NOTE! Dear North Americans, # Please follow the ISO 216 international standard format (A4) that dominates everywhere else in the world template = Tilt::PrawnTemplate.new { |t| "pdf.text \"Hello A4 portrait!\"" } - output = PdfOutput.new(template.render) + output = _PdfOutput.new(template.render) assert_includes output.text, "Hello A4 portrait!" assert_equal [0, 0, 595.28, 841.89], output.page_attributes(1)[:MediaBox] end - test "allows page size & layout settings - A3 landscape" do + it "allows page size & layout settings - A3 landscape" do template = Tilt::PrawnTemplate.new( :page_size => 'A3', :page_layout => :landscape) { |t| "pdf.text \"Hello A3 landscape!\"" } - output = PdfOutput.new(template.render) + output = _PdfOutput.new(template.render) assert_includes output.text, "Hello A3 landscape!" assert_equal [0, 0, 1190.55, 841.89], output.page_attributes(1)[:MediaBox] end diff --git a/test/tilt_radiustemplate_test.rb b/test/tilt_radiustemplate_test.rb index d4059b7e..79a92dba 100644 --- a/test/tilt_radiustemplate_test.rb +++ b/test/tilt_radiustemplate_test.rb @@ -7,49 +7,49 @@ # Remove when fixed upstream. raise LoadError if Radius.version < "0.7" - class RadiusTemplateTest < Minitest::Test - test "registered for '.radius' files" do + describe 'tilt/radius' do + it "registered for '.radius' files" do assert_equal Tilt::RadiusTemplate, Tilt['test.radius'] end - test "preparing and evaluating templates on #render" do + it "preparing and evaluating templates on #render" do template = Tilt::RadiusTemplate.new { |t| "Hello World!" } assert_equal "Hello World!", template.render end - test "can be rendered more than once" do + it "can be rendered more than once" do template = Tilt::RadiusTemplate.new { |t| "Hello World!" } 3.times { assert_equal "Hello World!", template.render } end - test "passing locals" do + it "passing locals" do template = Tilt::RadiusTemplate.new { "Hey !" } assert_equal "Hey Joe!", template.render(nil, :name => 'Joe') end - class ExampleRadiusScope + _ExampleRadiusScope = Class.new do def beer; 'wet'; end def whisky; 'wetter'; end end - test "combining scope and locals when scope responds" do + it "combining scope and locals when scope responds" do template = Tilt::RadiusTemplate.new { 'Beer is but Whisky is .' } - scope = ExampleRadiusScope.new + scope = _ExampleRadiusScope.new assert_equal "Beer is wet but Whisky is wetter.", template.render(scope) end - test "precedence when locals and scope define same variables" do + it "precedence when locals and scope define same variables" do template = Tilt::RadiusTemplate.new { 'Beer is but Whisky is .' } - scope = ExampleRadiusScope.new + scope = _ExampleRadiusScope.new assert_equal "Beer is great but Whisky is greater.", template.render(scope, :beer => 'great', :whisky => 'greater') end - #test "handles local scope" do + #it "handles local scope" do # beer = 'wet' # whisky = 'wetter' # @@ -59,7 +59,7 @@ def whisky; 'wetter'; end # assert_equal "Beer is wet but Whisky is wetter.", template.render(self) #end - test "passing a block for yield" do + it "passing a block for yield" do template = Tilt::RadiusTemplate.new { 'Beer is but Whisky is ter.' } diff --git a/test/tilt_rdiscounttemplate_test.rb b/test/tilt_rdiscounttemplate_test.rb index e8a55607..afef523c 100644 --- a/test/tilt_rdiscounttemplate_test.rb +++ b/test/tilt_rdiscounttemplate_test.rb @@ -3,8 +3,8 @@ begin require 'tilt/rdiscount' - class RDiscountTemplateTest < Minitest::Test - test "registered above BlueCloth" do + describe 'tilt/rdiscount' do + it "registered above BlueCloth" do %w[md mkd markdown].each do |ext| lazy = Tilt.lazy_map[ext] rdis_idx = lazy.index { |klass, file| klass == 'Tilt::RDiscountTemplate' } @@ -14,24 +14,24 @@ class RDiscountTemplateTest < Minitest::Test end end - test "preparing and evaluating templates on #render" do + it "preparing and evaluating templates on #render" do template = Tilt::RDiscountTemplate.new { |t| "# Hello World!" } assert_equal "

    Hello World!

    \n", template.render end - test "can be rendered more than once" do + it "can be rendered more than once" do template = Tilt::RDiscountTemplate.new { |t| "# Hello World!" } 3.times { assert_equal "

    Hello World!

    \n", template.render } end - test "smartypants when :smart is set" do + it "smartypants when :smart is set" do template = Tilt::RDiscountTemplate.new(:smart => true) { |t| "OKAY -- 'Smarty Pants'" } assert_equal "

    OKAY – ‘Smarty Pants’

    \n", template.render end - test "stripping HTML when :filter_html is set" do + it "stripping HTML when :filter_html is set" do template = Tilt::RDiscountTemplate.new(:filter_html => true) { |t| "HELLO WORLD" } assert_equal "

    HELLO <blink>WORLD</blink>

    \n", template.render diff --git a/test/tilt_rdoctemplate_test.rb b/test/tilt_rdoctemplate_test.rb index 6031b542..545e5c3a 100644 --- a/test/tilt_rdoctemplate_test.rb +++ b/test/tilt_rdoctemplate_test.rb @@ -2,19 +2,20 @@ begin require 'tilt/rdoc' - class RDocTemplateTest < Minitest::Test - test "is registered for '.rdoc' files" do + + describe 'tilt/rdoc' do + it "is registered for '.rdoc' files" do assert_equal Tilt::RDocTemplate, Tilt['test.rdoc'] end - test "preparing and evaluating the template with #render" do + it "preparing and evaluating the template with #render" do template = Tilt::RDocTemplate.new { |t| "= Hello World!" } result = template.render.strip assert_match %r(Hello World!<), result end - test "can be rendered more than once" do + it "can be rendered more than once" do template = Tilt::RDocTemplate.new { |t| "= Hello World!" } 3.times do result = template.render.strip diff --git a/test/tilt_redcarpettemplate_test.rb b/test/tilt_redcarpettemplate_test.rb index d28b616e..091e11ee 100644 --- a/test/tilt_redcarpettemplate_test.rb +++ b/test/tilt_redcarpettemplate_test.rb @@ -3,13 +3,13 @@ begin require 'tilt/redcarpet' - class RedcarpetTemplateTest < Minitest::Test - test "works correctly with #extensions_for" do + describe 'tilt/redcarpet' do + it "works correctly with #extensions_for" do extensions = Tilt.default_mapping.extensions_for(Tilt::RedcarpetTemplate) assert_equal ['markdown', 'mkd', 'md'], extensions end - test "registered above BlueCloth" do + it "registered above BlueCloth" do %w[md mkd markdown].each do |ext| lazy = Tilt.lazy_map[ext] blue_idx = lazy.index { |klass, file| klass == 'Tilt::BlueClothTemplate' } @@ -19,7 +19,7 @@ class RedcarpetTemplateTest < Minitest::Test end end - test "registered above RDiscount" do + it "registered above RDiscount" do %w[md mkd markdown].each do |ext| lazy = Tilt.lazy_map[ext] rdis_idx = lazy.index { |klass, file| klass == 'Tilt::RDiscountTemplate' } @@ -29,24 +29,24 @@ class RedcarpetTemplateTest < Minitest::Test end end - test "preparing and evaluating templates on #render" do + it "preparing and evaluating templates on #render" do template = Tilt::RedcarpetTemplate.new { |t| "# Hello World!" } assert_equal "

    Hello World!

    \n", template.render end - test "can be rendered more than once" do + it "can be rendered more than once" do template = Tilt::RedcarpetTemplate.new { |t| "# Hello World!" } 3.times { assert_equal "

    Hello World!

    \n", template.render } end - test "smartypants when :smart is set" do + it "smartypants when :smart is set" do template = Tilt::RedcarpetTemplate.new(:smartypants => true) { |t| "OKAY -- 'Smarty Pants'" } assert_match %r!

    OKAY – ('|‘)Smarty Pants('|’)<\/p>!, template.render end - test "smartypants with a rendererer instance" do + it "smartypants with a rendererer instance" do template = Tilt::RedcarpetTemplate.new(:renderer => Redcarpet::Render::HTML.new(:hard_wrap => true), :smartypants => true) { |t| "OKAY -- 'Smarty Pants'" } assert_match %r!

    OKAY – ('|‘)Smarty Pants('|’)<\/p>!, diff --git a/test/tilt_redclothtemplate_test.rb b/test/tilt_redclothtemplate_test.rb index e9c0c9d1..a47cb044 100644 --- a/test/tilt_redclothtemplate_test.rb +++ b/test/tilt_redclothtemplate_test.rb @@ -3,27 +3,27 @@ begin require 'tilt/redcloth' - class RedClothTemplateTest < Minitest::Test - test "is registered for '.textile' files" do + describe 'tilt/redcloth' do + it "is registered for '.textile' files" do assert_equal Tilt::RedClothTemplate, Tilt['test.textile'] end - test "compiles and evaluates the template on #render" do + it "compiles and evaluates the template on #render" do template = Tilt::RedClothTemplate.new { |t| "h1. Hello World!" } assert_equal "

    Hello World!

    ", template.render end - test "can be rendered more than once" do + it "can be rendered more than once" do template = Tilt::RedClothTemplate.new { |t| "h1. Hello World!" } 3.times { assert_equal "

    Hello World!

    ", template.render } end - test "ignores unknown options" do + it "ignores unknown options" do template = Tilt::RedClothTemplate.new(:foo => "bar") { |t| "h1. Hello World!" } 3.times { assert_equal "

    Hello World!

    ", template.render } end - test "passes in RedCloth options" do + it "passes in RedCloth options" do template = Tilt::RedClothTemplate.new { |t| "Hard breaks are\ninserted by default." } assert_equal "

    Hard breaks are
    \ninserted by default.

    ", template.render template = Tilt::RedClothTemplate.new(:hard_breaks => false) { |t| "But they can be\nturned off." } diff --git a/test/tilt_rstpandoctemplate_test.rb b/test/tilt_rstpandoctemplate_test.rb index 10ae2cd6..03ea253f 100644 --- a/test/tilt_rstpandoctemplate_test.rb +++ b/test/tilt_rstpandoctemplate_test.rb @@ -3,24 +3,24 @@ begin require 'tilt/rst-pandoc' - class RstPandocTemplateTest < Minitest::Test - test "is registered for '.rst' files" do + describe 'tilt/rst-pandoc' do + it "is registered for '.rst' files" do assert_equal Tilt::RstPandocTemplate, Tilt['test.rst'] end - test "compiles and evaluates the template on #render" do + it "compiles and evaluates the template on #render" do template = Tilt::RstPandocTemplate.new { |t| "Hello World!\n============" } assert_equal "

    Hello World!

    ", template.render end - test "can be rendered more than once" do + it "can be rendered more than once" do template = Tilt::RstPandocTemplate.new { |t| "Hello World!\n============" } 3.times do assert_equal "

    Hello World!

    ", template.render end end - test "doens't use markdown options" do + it "doens't use markdown options" do template = Tilt::RstPandocTemplate.new(:escape_html => true) { |t| "HELLO WORLD" } err = assert_raises(RuntimeError) { template.render } assert_match %r(pandoc: unrecognized option `--escape-html), err.message diff --git a/test/tilt_sasstemplate_test.rb b/test/tilt_sasstemplate_test.rb index 17ab3c30..4aee33e1 100644 --- a/test/tilt_sasstemplate_test.rb +++ b/test/tilt_sasstemplate_test.rb @@ -3,33 +3,33 @@ begin require 'tilt/sass' - class SassTemplateTest < Minitest::Test - test "is registered for '.sass' files" do + describe 'tilt/sass' do + it "is registered for '.sass' files" do assert_equal Tilt::SassTemplate, Tilt['test.sass'] end - test "compiles and evaluates the template on #render" do + it "compiles and evaluates the template on #render" do template = Tilt::SassTemplate.new({ style: :compressed }) { |t| "#main\n background-color: #0000f1" } assert_equal "#main{background-color:#0000f1}", template.render.chomp end - test "can be rendered more than once" do + it "can be rendered more than once" do template = Tilt::SassTemplate.new({ style: :compressed }) { |t| "#main\n background-color: #0000f1" } 3.times { assert_equal "#main{background-color:#0000f1}", template.render.chomp } end end - class ScssTemplateTest < Minitest::Test - test "is registered for '.scss' files" do + describe 'tilt/sass (scss)' do + it "is registered for '.scss' files" do assert_equal Tilt::ScssTemplate, Tilt['test.scss'] end - test "compiles and evaluates the template on #render" do + it "compiles and evaluates the template on #render" do template = Tilt::ScssTemplate.new({ style: :compressed }) { |t| "#main {\n background-color: #0000f1;\n}" } assert_equal "#main{background-color:#0000f1}", template.render.chomp end - test "can be rendered more than once" do + it "can be rendered more than once" do template = Tilt::ScssTemplate.new({ style: :compressed }) { |t| "#main {\n background-color: #0000f1;\n}" } 3.times { assert_equal "#main{background-color:#0000f1}", template.render.chomp } end diff --git a/test/tilt_sigil_test.rb b/test/tilt_sigil_test.rb index eca4fc7c..ae290a4b 100644 --- a/test/tilt_sigil_test.rb +++ b/test/tilt_sigil_test.rb @@ -1,30 +1,31 @@ require_relative 'test_helper' -require 'tilt/sigil' system('sigil -v') if $?.success? - class SigilTemplateTest < Minitest::Test - test "registered for '.sigil' files" do + require 'tilt/sigil' + + describe 'tilt/sigil' do + it "registered for '.sigil' files" do assert_equal Tilt::SigilTemplate, Tilt['test.sigil'] end - test "loading and evaluating templates on #render" do + it "loading and evaluating templates on #render" do template = Tilt::SigilTemplate.new { |t| "Hello World!" } assert_equal "Hello World!", template.render end - test "can be rendered more than once" do + it "can be rendered more than once" do template = Tilt::SigilTemplate.new { |t| "Hello World!" } 3.times { assert_equal "Hello World!", template.render } end - test "passing locals" do + it "passing locals" do template = Tilt::SigilTemplate.new { 'Hey $name!' } assert_equal "Hey Joe!", template.render(Object.new, :name => 'Joe') end - test "error message" do + it "error message" do template = Tilt::SigilTemplate.new('test.sigil') { '{{undef_func}}' } begin template.render diff --git a/test/tilt_stringtemplate_test.rb b/test/tilt_stringtemplate_test.rb index e49dbffd..f2ef520b 100644 --- a/test/tilt_stringtemplate_test.rb +++ b/test/tilt_stringtemplate_test.rb @@ -1,45 +1,45 @@ require_relative 'test_helper' require 'tilt/string' -class StringTemplateTest < Minitest::Test - test "registered for '.str' files" do +describe 'tilt/string' do + it "registered for '.str' files" do assert_equal Tilt::StringTemplate, Tilt['test.str'] end - test "loading and evaluating templates on #render" do + it "loading and evaluating templates on #render" do template = Tilt::StringTemplate.new { |t| "Hello World!" } assert_equal "Hello World!", template.render end - test "can be rendered more than once" do + it "can be rendered more than once" do template = Tilt::StringTemplate.new { |t| "Hello World!" } 3.times { assert_equal "Hello World!", template.render } end - test "passing locals" do + it "passing locals" do template = Tilt::StringTemplate.new { 'Hey #{name}!' } assert_equal "Hey Joe!", template.render(Object.new, :name => 'Joe') end - test "evaluating in an object scope" do + it "evaluating in an object scope" do template = Tilt::StringTemplate.new { 'Hey #{@name}!' } scope = Object.new scope.instance_variable_set :@name, 'Joe' assert_equal "Hey Joe!", template.render(scope) end - test "passing a block for yield" do + it "passing a block for yield" do template = Tilt::StringTemplate.new { 'Hey #{yield}!' } assert_equal "Hey Joe!", template.render { 'Joe' } assert_equal "Hey Moe!", template.render { 'Moe' } end - test "multiline templates" do + it "multiline templates" do template = Tilt::StringTemplate.new { "Hello\nWorld!\n" } assert_equal "Hello\nWorld!\n", template.render end - test "backtrace file and line reporting without locals" do + it "backtrace file and line reporting without locals" do data = File.read(__FILE__).split("\n__END__\n").last fail unless data[0] == ?< template = Tilt::StringTemplate.new('test.str', 11) { data } @@ -56,7 +56,7 @@ class StringTemplateTest < Minitest::Test end end - test "backtrace file and line reporting with locals" do + it "backtrace file and line reporting with locals" do data = File.read(__FILE__).split("\n__END__\n").last fail unless data[0] == ?< template = Tilt::StringTemplate.new('test.str', 1) { data } @@ -74,65 +74,63 @@ class StringTemplateTest < Minitest::Test end end - -class CompiledStringTemplateTest < Minitest::Test - def teardown +describe 'tilt/string (compiled)' do + after do GC.start end - class Scope - end + _Scope = Class.new - test "compiling template source to a method" do + it "compiling template source to a method" do template = Tilt::StringTemplate.new { |t| "Hello World!" } - template.render(Scope.new) + template.render(_Scope.new) method = template.send(:compiled_method, []) assert_kind_of UnboundMethod, method end - test "loading and evaluating templates on #render" do + it "loading and evaluating templates on #render" do template = Tilt::StringTemplate.new { |t| "Hello World!" } - assert_equal "Hello World!", template.render(Scope.new) + assert_equal "Hello World!", template.render(_Scope.new) end - test "passing locals" do + it "passing locals" do template = Tilt::StringTemplate.new { 'Hey #{name}!' } - assert_equal "Hey Joe!", template.render(Scope.new, :name => 'Joe') - assert_equal "Hey Moe!", template.render(Scope.new, :name => 'Moe') + assert_equal "Hey Joe!", template.render(_Scope.new, :name => 'Joe') + assert_equal "Hey Moe!", template.render(_Scope.new, :name => 'Moe') end - test "evaluating in an object scope" do + it "evaluating in an object scope" do template = Tilt::StringTemplate.new { 'Hey #{@name}!' } - scope = Scope.new + scope = _Scope.new scope.instance_variable_set :@name, 'Joe' assert_equal "Hey Joe!", template.render(scope) scope.instance_variable_set :@name, 'Moe' assert_equal "Hey Moe!", template.render(scope) end - test "passing a block for yield" do + it "passing a block for yield" do template = Tilt::StringTemplate.new { 'Hey #{yield}!' } - assert_equal "Hey Joe!", template.render(Scope.new) { 'Joe' } - assert_equal "Hey Moe!", template.render(Scope.new) { 'Moe' } + assert_equal "Hey Joe!", template.render(_Scope.new) { 'Joe' } + assert_equal "Hey Moe!", template.render(_Scope.new) { 'Moe' } end - test "multiline templates" do + it "multiline templates" do template = Tilt::StringTemplate.new { "Hello\nWorld!\n" } - assert_equal "Hello\nWorld!\n", template.render(Scope.new) + assert_equal "Hello\nWorld!\n", template.render(_Scope.new) end - test "template with '}'" do + it "template with '}'" do template = Tilt::StringTemplate.new { "Hello }" } assert_equal "Hello }", template.render end - test "backtrace file and line reporting without locals" do + it "backtrace file and line reporting without locals" do data = File.read(__FILE__).split("\n__END__\n").last fail unless data[0] == ?< template = Tilt::StringTemplate.new('test.str', 11) { data } begin - template.render(Scope.new) + template.render(_Scope.new) fail 'should have raised an exception' rescue => boom assert_kind_of NameError, boom @@ -145,12 +143,12 @@ class Scope end end - test "backtrace file and line reporting with locals" do + it "backtrace file and line reporting with locals" do data = File.read(__FILE__).split("\n__END__\n").last fail unless data[0] == ?< template = Tilt::StringTemplate.new('test.str') { data } begin - template.render(Scope.new, :name => 'Joe', :foo => 'bar') + template.render(_Scope.new, :name => 'Joe', :foo => 'bar') fail 'should have raised an exception' rescue => boom assert_kind_of RuntimeError, boom diff --git a/test/tilt_template_test.rb b/test/tilt_template_test.rb index e2bd088b..1e40d7be 100644 --- a/test/tilt_template_test.rb +++ b/test/tilt_template_test.rb @@ -2,78 +2,77 @@ require 'tempfile' require 'pathname' -class TiltTemplateTest < Minitest::Test - - class MockTemplate < Tilt::Template - def prepare - end +_MockTemplate = Class.new(Tilt::Template) do + def prepare end +end - test "needs a file or block" do +describe "tilt/template" do + it "needs a file or block" do assert_raises(ArgumentError) { Tilt::Template.new } end - test "initializing with a file" do - inst = MockTemplate.new('foo.erb') {} + it "initializing with a file" do + inst = _MockTemplate.new('foo.erb') {} assert_equal 'foo.erb', inst.file end - test "initializing with a file and line" do - inst = MockTemplate.new('foo.erb', 55) {} + it "initializing with a file and line" do + inst = _MockTemplate.new('foo.erb', 55) {} assert_equal 'foo.erb', inst.file assert_equal 55, inst.line end - test "initializing with a tempfile" do + it "initializing with a tempfile" do tempfile = Tempfile.new('tilt_template_test') - inst = MockTemplate.new(tempfile) + inst = _MockTemplate.new(tempfile) assert_equal File.basename(tempfile.path), inst.basename end - test "initializing with a pathname" do + it "initializing with a pathname" do tempfile = Tempfile.new('tilt_template_test') pathname = Pathname.new(tempfile.path) - inst = MockTemplate.new(pathname) + inst = _MockTemplate.new(pathname) assert_equal File.basename(tempfile.path), inst.basename end - class SillyHash < Hash - def path(arg) + it "initialize with hash that implements #path" do + _SillyHash = Class.new(Hash) do + def path(arg) + end end - end - test "initialize with hash that implements #path" do - options = SillyHash[:key => :value] - inst = MockTemplate.new(options) {} + options = _SillyHash[:key => :value] + inst = _MockTemplate.new(options) {} assert_equal :value, inst.options[:key] end - test "uses correct eval_file" do - inst = MockTemplate.new('foo.erb', 55) {} + it "uses correct eval_file" do + inst = _MockTemplate.new('foo.erb', 55) {} assert_equal 'foo.erb', inst.eval_file end - test "uses a default filename for #eval_file when no file provided" do - inst = MockTemplate.new { 'Hi' } + it "uses a default filename for #eval_file when no file provided" do + inst = _MockTemplate.new { 'Hi' } refute_nil inst.eval_file assert !inst.eval_file.include?("\n") end - test "calculating template's #basename" do - inst = MockTemplate.new('/tmp/templates/foo.html.erb') {} + it "calculating template's #basename" do + inst = _MockTemplate.new('/tmp/templates/foo.html.erb') {} assert_equal 'foo.html.erb', inst.basename end - test "calculating the template's #name" do - inst = MockTemplate.new('/tmp/templates/foo.html.erb') {} + it "calculating the template's #name" do + inst = _MockTemplate.new('/tmp/templates/foo.html.erb') {} assert_equal 'foo', inst.name end - test "initializing with a data loading block" do - MockTemplate.new { |template| "Hello World!" } + it "initializing with a data loading block" do + _MockTemplate.new { |template| "Hello World!" } end - class PreparingMockTemplate < Tilt::Template + _PreparingMockTemplate = Class.new(Tilt::Template) do def prepare raise "data must be set" if data.nil? @prepared = true @@ -81,17 +80,17 @@ def prepare def prepared? ; @prepared ; end end - test "raises NotImplementedError when #prepare not defined" do + it "raises NotImplementedError when #prepare not defined" do assert_raises(NotImplementedError) { Tilt::Template.new { |template| "Hello World!" } } end - test "raises NotImplementedError when #evaluate or #template_source not defined" do - inst = PreparingMockTemplate.new { |t| "Hello World!" } + it "raises NotImplementedError when #evaluate or #template_source not defined" do + inst = _PreparingMockTemplate.new { |t| "Hello World!" } assert_raises(NotImplementedError) { inst.render } assert inst.prepared? end - class SimpleMockTemplate < PreparingMockTemplate + _SimpleMockTemplate = Class.new(_PreparingMockTemplate) do def evaluate(scope, locals, &block) raise "should be prepared" unless prepared? raise "scope should be present" if scope.nil? @@ -100,56 +99,56 @@ def evaluate(scope, locals, &block) end end - test "prepares and evaluates the template on #render" do - inst = SimpleMockTemplate.new { |t| "Hello World!" } + it "prepares and evaluates the template on #render" do + inst = _SimpleMockTemplate.new { |t| "Hello World!" } assert_equal "Hello World!", inst.render assert inst.prepared? end - test 'prepares and evaluates the template on #render with nil arg' do - inst = SimpleMockTemplate.new { |t| "Hello World!" } + it 'prepares and evaluates the template on #render with nil arg' do + inst = _SimpleMockTemplate.new { |t| "Hello World!" } assert_equal 'Hello World!', inst.render(nil) assert inst.prepared? end - class SourceGeneratingMockTemplate < PreparingMockTemplate + _SourceGeneratingMockTemplate = Class.new(_PreparingMockTemplate) do def precompiled_template(locals) "foo = [] ; foo << %Q{#{data}} ; foo.join" end end - test "template_source with locals" do - inst = SourceGeneratingMockTemplate.new { |t| 'Hey #{name}!' } + it "template_source with locals" do + inst = _SourceGeneratingMockTemplate.new { |t| 'Hey #{name}!' } assert_equal "Hey Joe!", inst.render(Object.new, :name => 'Joe') assert inst.prepared? end - test "template_source with locals of strings" do - inst = SourceGeneratingMockTemplate.new { |t| 'Hey #{name}!' } + it "template_source with locals of strings" do + inst = _SourceGeneratingMockTemplate.new { |t| 'Hey #{name}!' } assert_equal "Hey Joe!", inst.render(Object.new, 'name' => 'Joe') assert inst.prepared? end - test "template_source with locals of strings" do - inst = SourceGeneratingMockTemplate.new { |t| 'Hey #{name}!' } + it "template_source with locals of strings" do + inst = _SourceGeneratingMockTemplate.new { |t| 'Hey #{name}!' } assert_equal "Hey Joe!", inst.render(Object.new, 'name' => 'Joe', :name=>'Joe') assert inst.prepared? end - test "template_source with locals having non-variable keys raises error" do - inst = SourceGeneratingMockTemplate.new { |t| '1 + 2 = #{_answer}' } + it "template_source with locals having non-variable keys raises error" do + inst = _SourceGeneratingMockTemplate.new { |t| '1 + 2 = #{_answer}' } err = assert_raises(RuntimeError) { inst.render(Object.new, 'ANSWER' => 3) } assert_equal "invalid locals key: \"ANSWER\" (keys must be variable names)", err.message assert_equal "1 + 2 = 3", inst.render(Object.new, '_answer' => 3) end - test "template_source with nil locals" do - inst = SourceGeneratingMockTemplate.new { |t| 'Hey' } + it "template_source with nil locals" do + inst = _SourceGeneratingMockTemplate.new { |t| 'Hey' } assert_equal 'Hey', inst.render(Object.new, nil) assert inst.prepared? end - class CustomGeneratingMockTemplate < PreparingMockTemplate + _CustomGeneratingMockTemplate = Class.new(_PreparingMockTemplate) do def precompiled_template(locals) data end @@ -163,8 +162,8 @@ def precompiled_postamble(locals) end end - test "supports pre/postamble" do - inst = CustomGeneratingMockTemplate.new( + it "supports pre/postamble" do + inst = _CustomGeneratingMockTemplate.new( :preamble => 'buf = []', :postamble => 'buf.join' ) { 'buf << 1' } @@ -172,8 +171,8 @@ def precompiled_postamble(locals) assert_equal "1", inst.render end - class Person - CONSTANT = "Bob" + _Person = Class.new do + self::CONSTANT = "Bob" attr_accessor :name def initialize(name) @@ -181,29 +180,29 @@ def initialize(name) end end - test "template_source with an object scope" do - inst = SourceGeneratingMockTemplate.new { |t| 'Hey #{@name}!' } - scope = Person.new('Joe') + it "template_source with an object scope" do + inst = _SourceGeneratingMockTemplate.new { |t| 'Hey #{@name}!' } + scope = _Person.new('Joe') assert_equal "Hey Joe!", inst.render(scope) end - test "template_source with a block for yield" do - inst = SourceGeneratingMockTemplate.new { |t| 'Hey #{yield}!' } + it "template_source with a block for yield" do + inst = _SourceGeneratingMockTemplate.new { |t| 'Hey #{yield}!' } assert_equal "Hey Joe!", inst.render(Object.new){ 'Joe' } end - test "template which accesses a constant" do - inst = SourceGeneratingMockTemplate.new { |t| 'Hey #{CONSTANT}!' } - assert_equal "Hey Bob!", inst.render(Person.new("Joe")) + it "template which accesses a constant" do + inst = _SourceGeneratingMockTemplate.new { |t| 'Hey #{CONSTANT}!' } + assert_equal "Hey Bob!", inst.render(_Person.new("Joe")) end - test "template which accesses a constant using scope class" do - inst = SourceGeneratingMockTemplate.new { |t| 'Hey #{CONSTANT}!' } - assert_equal "Hey Bob!", inst.render(Person) + it "template which accesses a constant using scope class" do + inst = _SourceGeneratingMockTemplate.new { |t| 'Hey #{CONSTANT}!' } + assert_equal "Hey Bob!", inst.render(_Person) end - class BasicPerson < BasicObject - CONSTANT = "Bob" + _BasicPerson = Class.new(BasicObject) do + self::CONSTANT = "Bob" attr_accessor :name def initialize(name) @@ -211,37 +210,37 @@ def initialize(name) end end - test "template_source with an BasicObject scope" do - inst = SourceGeneratingMockTemplate.new { |t| 'Hey #{@name}!' } - scope = BasicPerson.new('Joe') + it "template_source with an BasicObject scope" do + inst = _SourceGeneratingMockTemplate.new { |t| 'Hey #{@name}!' } + scope = _BasicPerson.new('Joe') assert_equal "Hey Joe!", inst.render(scope) end - test "template_source with a block for yield using BasicObject instance" do - inst = SourceGeneratingMockTemplate.new { |t| 'Hey #{yield}!' } + it "template_source with a block for yield using BasicObject instance" do + inst = _SourceGeneratingMockTemplate.new { |t| 'Hey #{yield}!' } assert_equal "Hey Joe!", inst.render(BasicObject.new){ 'Joe' } end - test "template which accesses a BasicObject constant" do - inst = SourceGeneratingMockTemplate.new { |t| 'Hey #{CONSTANT}!' } - assert_equal "Hey Bob!", inst.render(BasicPerson.new("Joe")) + it "template which accesses a BasicObject constant" do + inst = _SourceGeneratingMockTemplate.new { |t| 'Hey #{CONSTANT}!' } + assert_equal "Hey Bob!", inst.render(_BasicPerson.new("Joe")) end - test "template which accesses a constant using BasicObject scope class" do - inst = SourceGeneratingMockTemplate.new { |t| 'Hey #{CONSTANT}!' } - assert_equal "Hey Bob!", inst.render(BasicPerson) + it "template which accesses a constant using BasicObject scope class" do + inst = _SourceGeneratingMockTemplate.new { |t| 'Hey #{CONSTANT}!' } + assert_equal "Hey Bob!", inst.render(_BasicPerson) end - test "populates Tilt.current_template during rendering" do - inst = SourceGeneratingMockTemplate.new { '#{$inst = Tilt.current_template}' } + it "populates Tilt.current_template during rendering" do + inst = _SourceGeneratingMockTemplate.new { '#{$inst = Tilt.current_template}' } inst.render assert_equal inst, $inst assert_nil Tilt.current_template end - test "populates Tilt.current_template in nested rendering" do - inst1 = SourceGeneratingMockTemplate.new { '#{$inst.render; $inst1 = Tilt.current_template}' } - inst2 = SourceGeneratingMockTemplate.new { '#{$inst2 = Tilt.current_template}' } + it "populates Tilt.current_template in nested rendering" do + inst1 = _SourceGeneratingMockTemplate.new { '#{$inst.render; $inst1 = Tilt.current_template}' } + inst2 = _SourceGeneratingMockTemplate.new { '#{$inst2 = Tilt.current_template}' } $inst = inst2 inst1.render assert_equal inst1, $inst1 @@ -250,51 +249,49 @@ def initialize(name) end end -class TiltTemplateEncodingTest < Minitest::Test - MockTemplate = TiltTemplateTest::MockTemplate - - class DynamicMockTemplate < MockTemplate +describe "tilt/template (encoding)" do + _DynamicMockTemplate = Class.new(_MockTemplate) do def precompiled_template(locals) options[:code] end end - class UTF8Template < MockTemplate + _UTF8Template = Class.new(_MockTemplate) do def default_encoding Encoding::UTF_8 end end - setup do + before do @file = Tempfile.open('template') @file.puts "stuff" @file.close @template = @file.path end - teardown do + after do @file.delete end - test "reading from file assumes default external encoding" do + it "reading from file assumes default external encoding" do with_default_encoding('Big5') do - inst = MockTemplate.new(@template) + inst = _MockTemplate.new(@template) assert_equal 'Big5', inst.data.encoding.to_s end end - test "reading from file with a :default_encoding overrides default external" do + it "reading from file with a :default_encoding overrides default external" do with_default_encoding('Big5') do - inst = MockTemplate.new(@template, :default_encoding => 'GBK') + inst = _MockTemplate.new(@template, :default_encoding => 'GBK') assert_equal 'GBK', inst.data.encoding.to_s end end - test "reading from file with default_internal set does no transcoding" do + it "reading from file with default_internal set does no transcoding" do begin Encoding.default_internal = 'utf-8' with_default_encoding('Big5') do - inst = MockTemplate.new(@template) + inst = _MockTemplate.new(@template) assert_equal 'Big5', inst.data.encoding.to_s end ensure @@ -302,54 +299,54 @@ def default_encoding end end - test "using provided template data verbatim when given as string" do + it "using provided template data verbatim when given as string" do with_default_encoding('Big5') do - inst = MockTemplate.new(@template) { "blah".force_encoding('GBK') } + inst = _MockTemplate.new(@template) { "blah".force_encoding('GBK') } assert_equal 'GBK', inst.data.encoding.to_s end end - test "uses the template from the generated source code" do + it "uses the template from the generated source code" do with_utf8_default_encoding do tmpl = "ふが" code = tmpl.inspect.encode('Shift_JIS') - inst = DynamicMockTemplate.new(:code => code) { '' } + inst = _DynamicMockTemplate.new(:code => code) { '' } res = inst.render assert_equal 'Shift_JIS', res.encoding.to_s assert_equal tmpl, res.encode(tmpl.encoding) end end - test "uses the magic comment from the generated source code" do + it "uses the magic comment from the generated source code" do with_utf8_default_encoding do tmpl = "ふが" code = ("# coding: Shift_JIS\n" + tmpl.inspect).encode('Shift_JIS') # Set it to an incorrect encoding code.force_encoding('UTF-8') - inst = DynamicMockTemplate.new(:code => code) { '' } + inst = _DynamicMockTemplate.new(:code => code) { '' } res = inst.render assert_equal 'Shift_JIS', res.encoding.to_s assert_equal tmpl, res.encode(tmpl.encoding) end end - test "uses #default_encoding instead of default_external" do + it "uses #default_encoding instead of default_external" do with_default_encoding('Big5') do - inst = UTF8Template.new(@template) + inst = _UTF8Template.new(@template) assert_equal 'UTF-8', inst.data.encoding.to_s end end - test "uses #default_encoding instead of current encoding" do + it "uses #default_encoding instead of current encoding" do tmpl = "".force_encoding('Big5') - inst = UTF8Template.new(@template) { tmpl } + inst = _UTF8Template.new(@template) { tmpl } assert_equal 'UTF-8', inst.data.encoding.to_s end - test "raises error if the encoding is not valid" do + it "raises error if the encoding is not valid" do assert_raises(Encoding::InvalidByteSequenceError) do - UTF8Template.new(@template) { "\xe4" } + _UTF8Template.new(@template) { "\xe4" } end end end diff --git a/test/tilt_test.rb b/test/tilt_test.rb index 6051cb7f..55d901c2 100644 --- a/test/tilt_test.rb +++ b/test/tilt_test.rb @@ -1,7 +1,7 @@ require_relative 'test_helper' -class TiltTest < Minitest::Test - class MockTemplate +describe 'tilt' do + _MockTemplate = Class.new do attr_reader :args, :block def initialize(*args, &block) @args = args @@ -9,49 +9,49 @@ def initialize(*args, &block) end end - test "registering template implementation classes by file extension" do - Tilt.register(MockTemplate, 'mock') + it "registering template implementation classes by file extension" do + Tilt.register(_MockTemplate, 'mock') end - test "an extension is registered if explicit handle is found" do - Tilt.register(MockTemplate, 'mock') + it "an extension is registered if explicit handle is found" do + Tilt.register(_MockTemplate, 'mock') assert Tilt.registered?('mock') end - test "registering template classes by symbol file extension" do - Tilt.register(MockTemplate, :mock) + it "registering template classes by symbol file extension" do + Tilt.register(_MockTemplate, :mock) end - test "looking up template classes by exact file extension" do - Tilt.register(MockTemplate, 'mock') + it "looking up template classes by exact file extension" do + Tilt.register(_MockTemplate, 'mock') impl = Tilt['mock'] - assert_equal MockTemplate, impl + assert_equal _MockTemplate, impl end - test "looking up template classes by implicit file extension" do - Tilt.register(MockTemplate, 'mock') + it "looking up template classes by implicit file extension" do + Tilt.register(_MockTemplate, 'mock') impl = Tilt['.mock'] - assert_equal MockTemplate, impl + assert_equal _MockTemplate, impl end - test "looking up template classes with multiple file extensions" do - Tilt.register(MockTemplate, 'mock') + it "looking up template classes with multiple file extensions" do + Tilt.register(_MockTemplate, 'mock') impl = Tilt['index.html.mock'] - assert_equal MockTemplate, impl + assert_equal _MockTemplate, impl end - test "looking up template classes by file name" do - Tilt.register(MockTemplate, 'mock') + it "looking up template classes by file name" do + Tilt.register(_MockTemplate, 'mock') impl = Tilt['templates/test.mock'] - assert_equal MockTemplate, impl + assert_equal _MockTemplate, impl end - test "looking up non-existant template class" do + it "looking up non-existant template class" do assert_nil Tilt['none'] end - test "creating new template instance with a filename" do - Tilt.register(MockTemplate, 'mock') + it "creating new template instance with a filename" do + Tilt.register(_MockTemplate, 'mock') template = Tilt.new('foo.mock', 1, :key => 'val') { 'Hello World!' } assert_equal ['foo.mock', 1, {:key => 'val'}], template.args assert_equal 'Hello World!', template.block.call diff --git a/test/tilt_typescript_test.rb b/test/tilt_typescript_test.rb index e4c6ba5a..1be9dfb9 100644 --- a/test/tilt_typescript_test.rb +++ b/test/tilt_typescript_test.rb @@ -3,31 +3,31 @@ begin require 'tilt/typescript' - class TypeScriptTemplateTest < Minitest::Test - def setup + describe 'tilt/typescript' do + before do @ts = "var x:number = 5" @js = /var x = 5;\s*/ end - test "is registered for '.ts' files" do + it "is registered for '.ts' files" do assert_equal Tilt::TypeScriptTemplate, Tilt['test.ts'] end - test "is registered for '.tsx' files" do + it "is registered for '.tsx' files" do assert_equal Tilt::TypeScriptTemplate, Tilt['test.tsx'] end - test "compiles and evaluates the template on #render" do + it "compiles and evaluates the template on #render" do template = Tilt::TypeScriptTemplate.new { @ts } assert_match @js, template.render end - test "supports source map" do + it "supports source map" do template = Tilt::TypeScriptTemplate.new(inlineSourceMap: true) { @ts } assert_match %r(sourceMappingURL), template.render end - test "can be rendered more than once" do + it "can be rendered more than once" do template = Tilt::TypeScriptTemplate.new { @ts } 3.times { assert_match @js, template.render } end diff --git a/test/tilt_wikiclothtemplate_test.rb b/test/tilt_wikiclothtemplate_test.rb index f87a1026..c8ade1f7 100644 --- a/test/tilt_wikiclothtemplate_test.rb +++ b/test/tilt_wikiclothtemplate_test.rb @@ -3,25 +3,25 @@ begin require 'tilt/wikicloth' - class WikiClothTemplateTest < Minitest::Test - test "is registered for '.mediawiki' files" do + describe 'tilt/wikicloth' do + it "is registered for '.mediawiki' files" do assert_equal Tilt::WikiClothTemplate, Tilt['test.mediawiki'] end - test "is registered for '.mw' files" do + it "is registered for '.mw' files" do assert_equal Tilt::WikiClothTemplate, Tilt['test.mw'] end - test "is registered for '.wiki' files" do + it "is registered for '.wiki' files" do assert_equal Tilt::WikiClothTemplate, Tilt['test.wiki'] end - test "compiles and evaluates the template on #render" do + it "compiles and evaluates the template on #render" do template = Tilt::WikiClothTemplate.new { |t| "= Hello World! =" } assert_match(/

    .*Hello World!.*<\/h1>/m, template.render) end - test "can be rendered more than once" do + it "can be rendered more than once" do template = Tilt::WikiClothTemplate.new { |t| "= Hello World! =" } 3.times { assert_match(/

    .*Hello World!.*<\/h1>/m, template.render) } end diff --git a/test/tilt_yajltemplate_test.rb b/test/tilt_yajltemplate_test.rb index d391bf0f..5f3fb566 100644 --- a/test/tilt_yajltemplate_test.rb +++ b/test/tilt_yajltemplate_test.rb @@ -3,12 +3,12 @@ begin require 'tilt/yajl' - class YajlTemplateTest < Minitest::Test - test "is registered for '.yajl' files" do + describe 'tilt/yajl' do + it "is registered for '.yajl' files" do assert_equal Tilt::YajlTemplate, Tilt['test.yajl'] end - test "compiles and evaluates the template on #render" do + it "compiles and evaluates the template on #render" do template = Tilt::YajlTemplate.new { "json = { :integer => 3, :string => 'hello' }" } output = template.render result = Yajl::Parser.parse(output) @@ -16,7 +16,7 @@ class YajlTemplateTest < Minitest::Test assert_equal expect, result end - test "can be rendered more than once" do + it "can be rendered more than once" do template = Tilt::YajlTemplate.new { "json = { :integer => 3, :string => 'hello' }" } expect = {"integer" => 3,"string" => "hello"} 3.times do @@ -26,30 +26,30 @@ class YajlTemplateTest < Minitest::Test end end - test "evaluating ruby code" do + it "evaluating ruby code" do template = Tilt::YajlTemplate.new { "json = { :integer => (3 * 2) }" } assert_equal '{"integer":6}', template.render end - test "evaluating in an object scope" do + it "evaluating in an object scope" do template = Tilt::YajlTemplate.new { "json = { :string => 'Hey ' + @name + '!' }" } scope = Object.new scope.instance_variable_set :@name, 'Joe' assert_equal '{"string":"Hey Joe!"}', template.render(scope) end - test "passing locals" do + it "passing locals" do template = Tilt::YajlTemplate.new { "json = { :string => 'Hey ' + name + '!' }" } assert_equal '{"string":"Hey Joe!"}', template.render(Object.new, :name => 'Joe') end - test "passing a block for yield" do + it "passing a block for yield" do template = Tilt::YajlTemplate.new { "json = { :string => 'Hey ' + yield + '!' }" } assert_equal '{"string":"Hey Joe!"}', template.render { 'Joe' } assert_equal '{"string":"Hey Moe!"}', template.render { 'Moe' } end - test "template multiline" do + it "template multiline" do template = Tilt::YajlTemplate.new { %Q{ json = { :string => "hello" @@ -58,12 +58,12 @@ class YajlTemplateTest < Minitest::Test assert_equal '{"string":"hello"}', template.render end - test "template can reuse existing json buffer" do + it "template can reuse existing json buffer" do template = Tilt::YajlTemplate.new { "json.merge! :string => 'hello'" } assert_equal '{"string":"hello"}', template.render end - test "template can end with any statement" do + it "template can end with any statement" do template = Tilt::YajlTemplate.new { %Q{ json = { :string => "hello" @@ -76,19 +76,19 @@ class YajlTemplateTest < Minitest::Test assert( (result == '{"string":"hello","integer":4}') || (result == '{"integer":4,"string":"hello"}') ) end - test "option callback" do + it "option callback" do options = { :callback => 'foo' } template = Tilt::YajlTemplate.new(nil, options) { "json = { :string => 'hello' }" } assert_equal 'foo({"string":"hello"});', template.render end - test "option variable" do + it "option variable" do options = { :variable => 'output' } template = Tilt::YajlTemplate.new(nil, options) { "json = { :string => 'hello' }" } assert_equal 'var output = {"string":"hello"};', template.render end - test "option callback and variable" do + it "option callback and variable" do options = { :callback => 'foo', :variable => 'output' } template = Tilt::YajlTemplate.new(nil, options) { "json = { :string => 'hello' }" } assert_equal 'var output = {"string":"hello"}; foo(output);', template.render