Commit b9709686 authored by Matt Todd's avatar Matt Todd

Instrument filtering, test

parent 696ba4d3
......@@ -84,10 +84,23 @@ module HTML
context = @default_context.merge(context)
context = context.freeze
result ||= @result_class.new
result[:output] = @filters.inject(html) { |doc, filter| filter.call(doc, context, result) }
result[:output] =
@filters.inject(html) do |doc, filter|
instrument "call_filter.html_pipeline", :filter => filter.name do
filter.call(doc, context, result)
end
end
result
end
def instrument(event, payload = nil, &block)
return yield unless instrumentation_service
instrumentation_service.instrument event, payload do
yield
end
end
attr_accessor :instrumentation_service
# Like call but guarantee the value returned is a DocumentFragment.
# Pipelines may return a DocumentFragment or a String. Callers that need a
# DocumentFragment should use this method.
......
class MockedInstrumentationService
attr_reader :events
def initialize(events = [])
@events = events
end
def instrument(event, payload = nil)
res = yield
events << [event, payload, res]
res
end
end
require "test_helper"
require "helpers/mocked_instrumentation_service"
class HTML::PipelineTest < Test::Unit::TestCase
Pipeline = HTML::Pipeline
class TestFilter
def self.call(input, context, result)
input
end
end
def setup
@context = {}
@result_class = Hash
@pipeline = Pipeline.new [TestFilter], @context, @result_class
end
def test_filter_instrumentation
service = MockedInstrumentationService.new
@pipeline.instrumentation_service = service
filter("hello")
event, payload, res = service.events.pop
assert event, "event expected"
assert_equal "call_filter.html_pipeline", event
assert_equal TestFilter.name, payload[:filter]
end
def filter(input)
@pipeline.call(input)
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment