1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
require_relative 'helper'
class PartialTest < Minitest::Test
def test_view_partial
assert_equal <<-end_partial.strip, PartialWithModule.render
<h1>Welcome</h1>
Hello Bob
You have just won $100000!
<h3>Fair enough, right?</h3>
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_partial_inlining
view = Mustache.new :inline_partials_at_compile_time => true
view.template = '{{> test/fixtures/inner_partial}}'
view[:title] = 'success'
# Test the rendered result first
assert_equal "Again, success!", view.render
# Now the template should be compiled.
# There should be no :partial instruction as the partial has been in-lined.
assert_equal false, view.template.tokens.flatten.include?(:partial)
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
<h1>Context Test</h1>
<ul>
{{#titles}}
<li>{{>inner_partial}}</li>
{{/titles}}
</ul>
end_template
assert_equal <<-end_partial, view.render
<h1>Context Test</h1>
<ul>
<li>Again, One!</li>
<li>Again, Two!</li>
</ul>
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
<h1>Context Test</h1>
<ul>
{{#titles}}
<li>{{>inner_partial}}</li>
{{/titles}}
</ul>
end_template
assert_equal <<-end_partial, view.render
<h1>Context Test</h1>
<ul>
<li>Again, One!</li>
<li>Again, Two!</li>
</ul>
end_partial
end
def test_template_partial
assert_equal <<-end_partial.strip, TemplatePartial.render
<h1>Welcome</h1>
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
<html>
<body>
<ul>
<li>
1
<ul>
<li>
2
<ul>
<li>
3
<ul>
</ul>
</li>
</ul>
</li>
<li>
4
<ul>
<li>
5
<ul>
<li>
6
<ul>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</body>
</html>
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
|