Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 55 lines (43 sloc) 1.168 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
require 'sprockets_test'
require 'sprockets/engines'

class AlertTemplate
  def self.default_mime_type
    'application/javascript'
  end

  def initialize(file, &block)
    @data = block.call
  end

  def render(context)
    "alert(#{@data.inspect});"
  end
end

class StringTemplate
  def initialize(file, &block)
    @data = block.call
  end

  def render(context)
    @data.gsub(/#\{.*?\}/, "moo")
  end
end

class TestEngines < Sprockets::TestCase
  ORIGINAL_ENGINES = Sprockets.instance_variable_get(:@engines)

  def setup
    Sprockets.instance_variable_set(:@engines, ORIGINAL_ENGINES.dup)
  end

  def teardown
    Sprockets.instance_variable_set(:@engines, ORIGINAL_ENGINES)
  end

  test "registering a global engine" do
    Sprockets.register_engine ".alert", AlertTemplate
    assert_equal 'AlertTemplate', Sprockets.engines(".alert").name

    env = new_environment
    asset = env["hello.alert"]
    assert_equal 'alert("Hello world!\n");', asset.to_s
    assert_equal 'application/javascript', asset.content_type
  end

  def new_environment
    Sprockets::Environment.new.tap do |env|
      env.append_path(fixture_path('engines'))
    end
  end
end
Something went wrong with that request. Please try again.