Skip to content

Commit

Permalink
Switch to minitest/spec
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jeremyevans authored and judofyr committed Aug 12, 2022
1 parent 1ba7821 commit e3e5279
Show file tree
Hide file tree
Showing 45 changed files with 917 additions and 981 deletions.
57 changes: 1 addition & 56 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
26 changes: 13 additions & 13 deletions test/tilt_asciidoctor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,46 @@
begin
require 'tilt/asciidoc'

class AsciidoctorTemplateTest < Minitest::Test
HTML5_OUTPUT = "<div class=\"sect1\"><h2 id=\"_hello_world\">Hello World!</h2><div class=\"sectionbody\"></div></div>"
DOCBOOK5_OUTPUT = "<section xml:id=\"_hello_world\"><title>Hello World!</title></section>"
describe 'tilt/asciidoctor' do
html5_output = "<div class=\"sect1\"><h2 id=\"_hello_world\">Hello World!</h2><div class=\"sectionbody\"></div></div>"
docbook5_output = "<section xml:id=\"_hello_world\"><title>Hello World!</title></section>"

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
Expand Down
12 changes: 6 additions & 6 deletions test/tilt_babeltemplate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 { "<Awesome ness={true} />" }
assert_match "React.createElement", template.render
Expand Down
10 changes: 5 additions & 5 deletions test/tilt_blueclothtemplate_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<h1>Hello World!</h1>", 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 "<h1>Hello World!</h1>", 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 "<p>OKAY &mdash; &lsquo;Smarty Pants&rsquo;</p>",
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 <blink>WORLD</blink>" }
assert_equal "<p>HELLO &lt;blink>WORLD&lt;/blink></p>", template.render
Expand Down
23 changes: 12 additions & 11 deletions test/tilt_buildertemplate_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,62 @@

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 "<em>Hello World!</em>\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 "<em>Hello World!</em>\n", template.render }
end

test "passing locals" do
it "passing locals" do
template = Tilt::BuilderTemplate.new { "xml.em('Hey ' + name + '!')" }
assert_equal "<em>Hey Joe!</em>\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 "<em>Hey Joe!</em>\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 "<em>Hey Joe!</em>\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!') }
end
assert_equal "<em>Hey Joe!</em>\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 "<div><em>Hey</em></div>", 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
assert_equal "<em>world</em>", template.render(self, locals)
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
Expand Down
12 changes: 6 additions & 6 deletions test/tilt_cache_test.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
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
result = @cache.fetch('hello') { fail 'should be cached' }
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
result = @cache.fetch('hello', {:foo => 'bar', :baz => 'bizzle'}) { fail 'should be cached' }
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
Expand All @@ -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
Expand Down
Loading

0 comments on commit e3e5279

Please sign in to comment.