Class: Cucumber::Formatter::MessageBuilder

Inherits:
Object
  • Object
show all
Includes:
Io, Messages::Helpers::TimeConversion
Defined in:
lib/cucumber/formatter/message_builder.rb

Direct Known Subclasses

HTML, Message, Rerun

Instance Method Summary collapse

Methods included from Io

ensure_dir, ensure_file, ensure_io, included, io?, url?

Constructor Details

#initialize(config) ⇒ MessageBuilder

Returns a new instance of MessageBuilder.



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
# File 'lib/cucumber/formatter/message_builder.rb', line 18

def initialize(config)
  @config = config

  @hook_by_test_step = Query::HookByTestStep.new(config)
  @pickle_by_test = Query::PickleByTest.new(config)
  @step_definitions_by_test_step = Query::StepDefinitionsByTestStep.new(config)
  @test_case_started_by_test_case = Query::TestCaseStartedByTestCase.new(config)

  @repository = Cucumber::Repository.new
  @query = Cucumber::Query.new(@repository)

  @test_run_started_id = config.id_generator.new_id
  @test_case_by_step_id = {}
  @pickle_id_step_by_test_step_id = {}

  # Ensure all handlers for events occur after all ivars are instantiated

  config.on_event :envelope, &method(:on_envelope)

  config.on_event :gherkin_source_parsed, &method(:on_gherkin_source_parsed)
  config.on_event :gherkin_source_read, &method(:on_gherkin_source_read)

  config.on_event :hook_test_step_created, &method(:on_hook_test_step_created)

  config.on_event :step_activated, &method(:on_step_activated)
  config.on_event :step_definition_registered, &method(:on_step_definition_registered)

  # TODO: Handle TestCaseCreated
  config.on_event :test_case_ready, &method(:on_test_case_ready)
  config.on_event :test_case_started, &method(:on_test_case_started)
  config.on_event :test_case_finished, &method(:on_test_case_finished)

  config.on_event :test_run_started, &method(:on_test_run_started)
  config.on_event :test_run_finished, &method(:on_test_run_finished)

  config.on_event :test_run_hook_started, &method(:on_test_run_hook_started)
  config.on_event :test_run_hook_finished, &method(:on_test_run_hook_finished)

  config.on_event :test_step_created, &method(:on_test_step_created)
  config.on_event :test_step_started, &method(:on_test_step_started)
  config.on_event :test_step_finished, &method(:on_test_step_finished)

  config.on_event :undefined_parameter_type, &method(:on_undefined_parameter_type)
end

Instance Method Details

#attach(src, media_type, filename) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cucumber/formatter/message_builder.rb', line 63

def attach(src, media_type, filename)
  attachment_data = {
    test_step_id: @current_test_step_id,
    test_case_started_id: @current_test_case_started_id,
    media_type: media_type,
    file_name: filename,
    timestamp: time_to_timestamp(Time.now)
  }

  if media_type&.start_with?('text/')
    attachment_data[:content_encoding] = Cucumber::Messages::AttachmentContentEncoding::IDENTITY
    attachment_data[:body] = src
  else
    body = src.respond_to?(:read) ? src.read : src
    attachment_data[:content_encoding] = Cucumber::Messages::AttachmentContentEncoding::BASE64
    attachment_data[:body] = Base64.strict_encode64(body)
  end

  message = Cucumber::Messages::Envelope.new(attachment: Cucumber::Messages::Attachment.new(**attachment_data))
  output_envelope(message)
end