Skip to content

Instantly share code, notes, and snippets.

@judofyr
Created April 20, 2011 15:19
Show Gist options
  • Save judofyr/931605 to your computer and use it in GitHub Desktop.
Save judofyr/931605 to your computer and use it in GitHub Desktop.
require 'test/unit'
require 'tilt'
# See below for example of usage
module RenderTests
class Scope
String = 1
end
# This method should returned a cached template. The "template" in this case
# is just a Ruby expression.
def cache(ruby) end
# This method takes the cache above and renders it under the given `scope`
# with a local varible named `var` and `yield` should call the `blk`.
def evaluate(cache, scope, var, &blk)
end
def render(tmpl, scope = Object.new, var = :var, &blk)
evaluate(cache(tmpl), scope, var, &blk)
end
def assert_render(result, *args, &blk)
assert_equal result, render(*args, &blk)
end
def test_scope
assert_render 1, 'self', 1
end
def test_var
assert_render :hello, 'var', self, :hello
end
def test_yield
assert_render :inner, 'yield' do
:inner
end
end
def test_constant
assert_render 1, 'String', Scope.new
end
end
# This one fails a few tests :-(
class ProcRender < Test::Unit::TestCase
include RenderTests
def cache(ruby)
eval("Proc.new{|var|#{ruby}}")
end
def evaluate(p, scope, var, &blk)
scope.instance_exec(var, &p)
end
end
# Tilt works fine!
class TiltRender < Test::Unit::TestCase
include RenderTests
class Foo < Tilt::Template
def prepare
end
def precompiled_template(*)
data
end
end
def cache(ruby)
Foo.new { ruby }
end
def evaluate(inst, scope, var, &blk)
inst.render(scope, :var => var, &blk)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment