Method: InspecRspecCli#format_example

Defined in:
lib/inspec/rspec_json_formatter.rb

#format_example(example) ⇒ Object

This method is called through the RSpec Formatter interface for every example found in the test suite.

Within #format_example we are getting an example and:

* if this is an example, within a control, within a profile then we want
  to display the profile header, display the control, and then display
  the example.
* if this is another example, within the same control, within the same
  profile we want to display the example.
* if this is an example that does not map to a control (anonymous) then
  we want to store it for later to displayed at the end of a profile.


362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/inspec/rspec_json_formatter.rb', line 362

def format_example(example)
  example_data = super(example)
  control = create_or_find_control(example_data)

  # If we are switching to a new control then we want to print the control
  # we were previously collecting examples unless the last control is
  # anonymous (no control). Anonymous controls and their examples are handled
  # later on the profile change.

  if switching_to_new_control?(control)
    print_last_control_with_examples unless last_control_is_anonymous?
  end

  store_last_control(control)

  # Each profile may have zero or more anonymous examples. These are examples
  # that defined in a profile but outside of a control. They may be defined
  # at the start, in-between, or end of list of examples. To display them
  # at the very end of a profile, which means we have to wait for the profile
  # to change to know we are done with a profile.

  if switching_to_new_profile?(control.profile)
    output.puts ''
    print_anonymous_examples_associated_with_last_profile
    clear_anonymous_examples_associated_with_last_profile
  end

  print_profile(control.profile)
  store_last_profile(control.profile)

  # The anonymous controls should be added to a hash that we will display
  # when we are done examining all the examples within this profile.

  if control.anonymous?
    add_anonymous_example_within_this_profile(control.as_hash)
  end

  @all_controls.push(control.as_hash)
  example_data
end