File: pipeline_test.rb

package info (click to toggle)
ruby-html-pipeline 2.14.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 424 kB
  • sloc: ruby: 2,265; sh: 13; makefile: 6
file content (76 lines) | stat: -rw-r--r-- 2,374 bytes parent folder | download | duplicates (3)
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
# frozen_string_literal: true

require 'test_helper'
require 'helpers/mocked_instrumentation_service'

class HTML::PipelineTest < Minitest::Test
  Pipeline = HTML::Pipeline
  class TestFilter
    def self.call(input, _context, _result)
      input.reverse
    end
  end

  def setup
    @context = {}
    @result_class = Hash
    @pipeline = Pipeline.new [TestFilter], @context, @result_class
  end

  def test_filter_instrumentation
    service = MockedInstrumentationService.new
    events = service.subscribe 'call_filter.html_pipeline'
    @pipeline.instrumentation_service = service
    filter(body = 'hello')
    event, payload, res = events.pop
    assert event, 'event expected'
    assert_equal 'call_filter.html_pipeline', event
    assert_equal TestFilter.name, payload[:filter]
    assert_equal @pipeline.class.name, payload[:pipeline]
    assert_equal body.reverse, payload[:result][:output]
  end

  def test_pipeline_instrumentation
    service = MockedInstrumentationService.new
    events = service.subscribe 'call_pipeline.html_pipeline'
    @pipeline.instrumentation_service = service
    filter(body = 'hello')
    event, payload, res = events.pop
    assert event, 'event expected'
    assert_equal 'call_pipeline.html_pipeline', event
    assert_equal @pipeline.filters.map(&:name), payload[:filters]
    assert_equal @pipeline.class.name, payload[:pipeline]
    assert_equal body.reverse, payload[:result][:output]
  end

  def test_default_instrumentation_service
    service = 'default'
    Pipeline.default_instrumentation_service = service
    pipeline = Pipeline.new [], @context, @result_class
    assert_equal service, pipeline.instrumentation_service
  ensure
    Pipeline.default_instrumentation_service = nil
  end

  def test_setup_instrumentation
    assert_nil @pipeline.instrumentation_service

    service = MockedInstrumentationService.new
    events = service.subscribe 'call_pipeline.html_pipeline'
    @pipeline.setup_instrumentation name = 'foo', service

    assert_equal service, @pipeline.instrumentation_service
    assert_equal name, @pipeline.instrumentation_name

    filter(body = 'foo')

    event, payload, res = events.pop
    assert event, 'expected event'
    assert_equal name, payload[:pipeline]
    assert_equal body.reverse, payload[:result][:output]
  end

  def filter(input)
    @pipeline.call(input)
  end
end