Sha256: 9f492ce1fbcddbb74e080990e25b4dcb0eb76fd2323b2d2f8c05ab22ad900097

Contents?: true

Size: 1.99 KB

Versions: 1

Compression:

Stored size: 1.99 KB

Contents

require_relative '../spec_helper'

require 'wright/util/mustache_renderer'

describe Wright::Util::MustacheRenderer do
  describe 'render' do
    it 'should render mustache templates' do
      template = "foo: {{foo}}\n"
      hash = { foo: 'FOO' }
      renderer = Wright::Util::MustacheRenderer.new(hash)
      expected = "foo: FOO\n"
      actual = renderer.render(template)
      actual.must_equal expected
    end

    it 'should throw exceptions when encountering undefined names' do
      skip unless defined?(Mustache)
      template = '{{undefined_name}}'
      hash = {}
      renderer = Wright::Util::MustacheRenderer.new(hash)
      -> { renderer.render(template) }.must_raise Mustache::ContextMiss
    end

    it 'should delegate rendering templates to Mustache' do
      template = "foo: {{foo}}\n"
      hash = { foo: 'FOO' }
      mustache_class_double = Minitest::Mock.new
      mustache_object_double = Minitest::Mock.new
      mustache_class_double.expect(:new, mustache_object_double)
      mustache_object_double.expect(:raise_on_context_miss=, true, [true])
      mustache_object_double.expect(:render, nil, [template, hash])

      Object.stub_const(:Mustache, mustache_class_double) do
        Wright::Util::MustacheRenderer.new(hash).render(template)
      end
      mustache_class_double.verify
      mustache_object_double.verify
    end

    it 'should fall back to pencil_mustache if Mustache is not available' do
      template = "foo: {{foo}}\n"
      hash = { foo: 'FOO' }
      mustache_class_double = Minitest::Mock.new
      mustache_object_double = Minitest::Mock.new
      mustache_class_double.expect(:new, mustache_object_double)
      mustache_object_double.expect(:render, nil, [template, hash])

      Object.stub_remove_const(:Mustache) do
        Wright::Util.stub_const(:PencilMustache, mustache_class_double) do
          Wright::Util::MustacheRenderer.new(hash).render(template)
        end
      end
      mustache_class_double.verify
      mustache_object_double.verify
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wright-0.5.0 spec/util/mustache_renderer_spec.rb