require_relative 'helper'
class PartialTest < Minitest::Test
def test_view_partial
assert_equal <<-end_partial.strip, PartialWithModule.render
Welcome
Hello Bob
You have just won $100000!
Fair enough, right?
end_partial
end
def test_partial_with_slashes
klass = Class.new(Mustache)
klass.template = '{{> test/fixtures/inner_partial}}'
view = klass.new
view[:title] = 'success'
assert_equal "Again, success!", view.render
end
def test_view_partial_inherits_context
klass = Class.new(TemplatePartial)
view = klass.new
view.template_path = File.dirname(__FILE__) + '/fixtures'
view[:titles] = [{:title => :One}, {:title => :Two}]
view.template = <<-end_template
Context Test
{{#titles}}
- {{>inner_partial}}
{{/titles}}
end_template
assert_equal <<-end_partial, view.render
Context Test
end_partial
end
def test_view_partial_inherits_context_of_class_methods
klass = Class.new(TemplatePartial)
klass.template_path = File.dirname(__FILE__) + '/fixtures'
klass.send(:define_method, :titles) do
[{:title => :One}, {:title => :Two}]
end
view = klass.new
view.template = <<-end_template
Context Test
{{#titles}}
- {{>inner_partial}}
{{/titles}}
end_template
assert_equal <<-end_partial, view.render
Context Test
end_partial
end
def test_template_partial
assert_equal <<-end_partial.strip, TemplatePartial.render
Welcome
Again, Welcome!
end_partial
end
def test_template_partial_with_custom_extension
partial = Class.new(TemplatePartial)
partial.template_extension = 'txt'
partial.template_path = File.dirname(__FILE__) + '/fixtures'
assert_equal <<-end_partial.strip, partial.render.strip
Welcome
-------
## Again, Welcome! ##
end_partial
end
def test_recursive_partials
assert_equal <<-end_partial, Recursive.render
It works!
end_partial
end
def test_crazy_recursive_partials
assert_equal <<-end_partial.strip, CrazyRecursive.render
end_partial
end
def test_partials_use_proper_context
assert_equal "OuterThing OuterThing", OuterThing.render('{{name}} {{> p}}')
assert_equal "InnerThing InnerThing", InnerThing.render('{{name}} {{> p}}')
assert_equal "OuterThing InnerThing InnerThing",
OuterThing.render('{{name}} {{#inner}}{{name}} {{> p}}{{/inner}}')
end
def test_partials_render_returned_strings
assert_equal "ok", MiddleThing.render('{{> some_partial }}')
end
end
class InnerThing < Mustache
def partial(p) self.class end
def name; self.class end
end
class OuterThing < Mustache
def inner
InnerThing.new
end
def partial(p) self.class end
def name; self.class end
end
class MiddleThing < Mustache
def partial(name) "{{#{name}}}" end
def some_partial; "ok" end
end