class Inspec::Object::Describe

Constants

Test

Internal helper to structure test objects. Should not be exposed to the user as it is hidden behind `add_test`, `to_hash`, and `to_ruby` in Inspec::Object::Describe

Attributes

qualifier[RW]

A qualifier describing the resource that will be tested. It may consist of the resource identification and multiple accessors to pinpoint the data the user wants to test.

skip[RW]

Optional method to skip this describe block altogether. If `skip` is defined it takes precendence and will print the skip statement instead of adding other tests.

tests[RW]

An array of individual tests for the qualifier. Every entry will be translated into an `it` or `its` clause.

variables[RW]

Optional variables which are used by tests.

Public Class Methods

new() click to toggle source
# File lib/inspec/objects/describe.rb, line 52
def initialize
  @qualifier = []
  @tests = []
  @variables = []
end

Public Instance Methods

add_test(its, matcher, expectation, opts = {}) click to toggle source
# File lib/inspec/objects/describe.rb, line 58
def add_test(its, matcher, expectation, opts = {})
  test = Inspec::Object::Describe::Test.new(its, matcher, expectation, opts[:negated])
  tests.push(test)
  test
end
resource() click to toggle source
# File lib/inspec/objects/describe.rb, line 74
def resource
  return nil if qualifier.empty? || qualifier[0].empty? || qualifier[0][0].empty?

  qualifier[0][0]
end
to_hash() click to toggle source
# File lib/inspec/objects/describe.rb, line 70
def to_hash
  { qualifier: qualifier, tests: tests.map(&:to_h), variables: variables, skip: skip }
end
to_ruby() click to toggle source
# File lib/inspec/objects/describe.rb, line 64
def to_ruby
  return rb_skip unless skip.nil?

  rb_describe
end

Private Instance Methods

rb_describe() click to toggle source
# File lib/inspec/objects/describe.rb, line 82
def rb_describe
  vars = variables.map(&:to_ruby).join("\n")
  vars += "\n" unless vars.empty?

  objarr = @qualifier
  objarr = [["unknown object".inspect]] if objarr.nil? || objarr.empty?
  obj = objarr.map { |q| ruby_qualifier(q) }.join(".")

  rbtests = tests.map(&:to_ruby).join("\n  ")
  format("%sdescribe %s do\n  %s\nend", vars, obj, rbtests)
end
rb_skip() click to toggle source
# File lib/inspec/objects/describe.rb, line 94
def rb_skip
  obj = @qualifier || skip.inspect
  format("describe %s do\n  skip %s\nend", obj, skip.inspect)
end