Class: BareTest::Status

Inherits:
Object
  • Object
show all
Defined in:
lib/baretest/status.rb

Overview

An assertion or suite has 9 possible states:

:success

The assertion passed. This means the block returned a trueish value.

:failure

The assertion failed. This means the block returned a falsish value. Alternatively it raised an Assertion::Failure. The latter has the advantage that it can provide nicer diagnostics.

:pending

No block given to the assertion to be run

:skipped

If one of the parent suites is missing a dependency, its assertions will be skipped Alternatively it raised an Assertion::Skip.

:error

The assertion errored out. This means the block raised an exception

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity, status, context = nil, skip_reason = nil, failure_reason = nil, exception = nil) ⇒ Status

entity

The suite or Assertion this Status belongs to

status

The status identifier

skip_reason

Why the Assertion or Suite failed. Array, String or nil.

failure_reason

Why the Assertion or Suite was skipped. Array, String or nil.



50
51
52
53
54
55
56
57
# File 'lib/baretest/status.rb', line 50

def initialize(entity, status, context=nil, skip_reason=nil, failure_reason=nil, exception=nil)
  @entity         = entity
  @status         = status
  @context        = context
  @skip_reason    = skip_reason
  @failure_reason = failure_reason
  @exception      = exception
end

Instance Attribute Details

#contextObject (readonly)

The assertions execute context.



29
30
31
# File 'lib/baretest/status.rb', line 29

def context
  @context
end

#entityObject (readonly)

The assertion or suite this status belongs to. Assertion or Suite.



26
27
28
# File 'lib/baretest/status.rb', line 26

def entity
  @entity
end

#exceptionObject (readonly)

If an exception occured in Assertion#execute, this will contain the Exception object raised.



42
43
44
# File 'lib/baretest/status.rb', line 42

def exception
  @exception
end

#failure_reasonObject (readonly)

Detailed reason for failing. Array or nil.



38
39
40
# File 'lib/baretest/status.rb', line 38

def failure_reason
  @failure_reason
end

#skip_reasonObject (readonly)

Detailed reason for skipping. Array or nil.



35
36
37
# File 'lib/baretest/status.rb', line 35

def skip_reason
  @skip_reason
end

#statusObject (readonly)

The status identifier, see BareTest::Status. Symbol.



32
33
34
# File 'lib/baretest/status.rb', line 32

def status
  @status
end

Instance Method Details

#inspectObject

:nodoc:



82
83
84
85
86
87
88
89
90
91
# File 'lib/baretest/status.rb', line 82

def inspect # :nodoc:
  sprintf "#<%s:0x%08x status=%p exception=%p skip_reason=%p failure_reason=%p entity=%p>",
    self.class,
    object_id>>1,
    @status,
    @exception,
    @skip_reason,
    @failure_reason,
    @entity
end

#reason(opt = nil) ⇒ Object

The failure/error/skipping/pending reason. Returns nil if there’s no reason, a string otherwise Options:

:default

Reason to return if no reason is present

:separator

String used to separate multiple reasons

:indent

A String, the indentation to use. Prefixes every line.

:first_indent

A String, used to indent the first line only (replaces indent).



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/baretest/status.rb', line 66

def reason(opt=nil)
  if opt then
    default, separator, indent, first_indent = 
      *opt.values_at(:default, :separator, :indent, :first_indent)
    reason = @skip_reason || @failure_reason || default
    return nil unless reason
    reason = reason.kind_of?(Array) ? reason : [reason]
    reason = reason.join(separator || "\n")
    reason = reason.gsub(/^/, indent) if indent
    reason = reason.gsub(/\A#{Regexp.escape(indent)}/, first_indent) if first_indent
    reason
  else
    @reason.empty? ? nil : @reason.join("\n")
  end
end