Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 70 lines (58 sloc) 2.332 kb
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
require 'contest'
require 'tilt'

begin
  require 'mustache'
  raise LoadError, "mustache version must be > 0.2.2" if !Mustache.respond_to?(:compiled?)

  module Views
    class Foo < Mustache
      attr_reader :foo
    end
  end

  class MustacheTemplateTest < Test::Unit::TestCase
    test "registered for '.mustache' files" do
      assert_equal Tilt::MustacheTemplate, Tilt['test.mustache']
    end

    test "preparing and evaluating templates on #render" do
      template = Tilt::MustacheTemplate.new { |t| "Hello World!" }
      assert_equal "Hello World!", template.render
    end

    test "passing locals" do
      template = Tilt::MustacheTemplate.new { "<p>Hey {{name}}!</p>" }
      assert_equal "<p>Hey Joe!</p>", template.render(nil, :name => 'Joe')
    end

    test "passing a block for yield" do
      template = Tilt::MustacheTemplate.new { "<p>Hey {{yield}}!</p>" }
      assert_equal "<p>Hey Joe!</p>", template.render { 'Joe' }
    end

    test "locating views defined at the top-level" do
      template = Tilt::MustacheTemplate.new('foo.mustache') { "<p>Hey {{foo}}!</p>" }
      assert_equal Views::Foo, template.engine
    end

    module Bar
      module Views
        class Bizzle < Mustache
        end
      end
    end

    test "locating views defined in a custom namespace" do
      template = Tilt::MustacheTemplate.new('bizzle.mustache', :namespace => Bar) { "<p>Hello World!</p>" }
      assert_equal Bar::Views::Bizzle, template.engine
      assert_equal "<p>Hello World!</p>", template.render
    end

    test "locating views in files" do
      view_path = File.expand_path('../tilt_mustache_views', __FILE__)
      template = Tilt::MustacheTemplate.new('external.mustache', :view_path => view_path) { "<p>{{hello}}!</p>" }
      assert defined?(Views::External), "external.rb should have been required"
      assert_equal Views::External, template.engine
      assert_equal "<p>Stached!</p>", template.render
    end

    test "copying instance variables from scope object" do
      template = Tilt::MustacheTemplate.new('foo.mustache') { "<p>Hey {{foo}}!</p>" }
      scope = Object.new
      scope.instance_variable_set(:@foo, 'Jane!')
      assert_equal "<p>Hey Jane!!</p>", template.render(scope)
    end
  end

rescue LoadError => boom
  warn "Tilt::MustacheTemplate (disabled)\n"
end
Something went wrong with that request. Please try again.