Class: Uspec::Result

Inherits:
Object
  • Object
show all
Includes:
Terminal
Defined in:
lib/uspec/result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Terminal

color, colors, esc, hspace, method_missing, newline, normal, vspace

Constructor Details

#initialize(spec, raw, source) ⇒ Result

Returns a new instance of Result.



8
9
10
11
12
13
# File 'lib/uspec/result.rb', line 8

def initialize spec, raw, source
  @spec = spec
  @raw = raw
  @source = source
  @handler = ::TOISB.wrap raw
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Uspec::Terminal

Instance Attribute Details

#handlerObject (readonly)

Returns the value of attribute handler.



14
15
16
# File 'lib/uspec/result.rb', line 14

def handler
  @handler
end

#rawObject (readonly)

Returns the value of attribute raw.



14
15
16
# File 'lib/uspec/result.rb', line 14

def raw
  @raw
end

#sourceObject (readonly)

Returns the value of attribute source.



14
15
16
# File 'lib/uspec/result.rb', line 14

def source
  @source
end

#specObject (readonly)

Returns the value of attribute spec.



14
15
16
# File 'lib/uspec/result.rb', line 14

def spec
  @spec
end

Instance Method Details

#failure?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/uspec/result.rb', line 95

def failure?
  raw != true && !@pending
end

#inspectObject



107
108
109
# File 'lib/uspec/result.rb', line 107

def inspect
  "#{self.class} for `#{spec}` -> #{pretty}"
end

#inspectorObject

Attempts to inspect an object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/uspec/result.rb', line 56

def inspector
  if String === raw && raw.include?(?\n) then
    # if object is a multiline string, display it unescaped
    [
      vspace,
      hspace, yellow('"""'), newline,
      raw, normal, newline,
      hspace, yellow('"""')
    ].join
  else
    handler.inspector!
  end
rescue Exception => error
  return handler.simple_inspector if error.message.include? handler.get_id

  error_file, error_line, _ = error.backtrace[4].split ?:

  <<-MSG

  #{error.class} : #{error.message}

  Uspec detected a bug in your source code!
  Calling #inspect on an object will recusively call #inspect on its instance variables and contents.
  If one of those contained objects does not have an #inspect method you will see this message.
  You will also get this message if your #inspect method or one of its callees raises an exception.
  This is most likely to happen with BasicObject and its subclasses.

  If you think this is a bug in Uspec please report it: https://github.com/acook/uspec/issues/new

  Error may have occured in test `#{spec}` in file `#{error_file}` on line ##{error_line}.

\t#{error.backtrace.join "\n\t"}
  MSG
end

#messageObject



47
48
49
# File 'lib/uspec/result.rb', line 47

def message
  "#{red subklassinfo}#{raw.message}"
end

#pending!Object



103
104
105
# File 'lib/uspec/result.rb', line 103

def pending!
  @pending = true
end

#pending?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/uspec/result.rb', line 99

def pending?
  !!@pending
end

#prettyObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/uspec/result.rb', line 16

def pretty
  if raw == true then
    green raw
  elsif raw == false then
    red raw
  elsif pending? then
    yellow 'pending'
  elsif Exception === raw then
    [
      red('Exception'), vspace,
      hspace, 'Spec encountered an Exception ', newline,
      hspace, 'in spec at ', source.first, vspace,
      hspace, message, vspace,
      white(trace)
    ].join
  else
    [
      red('Failed'), vspace,
      hspace, 'Spec did not return a boolean value ', newline,
      hspace, 'in spec at ', source.first, vspace,
      hspace, red(subklassinfo), inspector, newline
    ].join
  end
end

#subklassinfoObject



51
52
53
# File 'lib/uspec/result.rb', line 51

def subklassinfo
  "#{handler.subklassinfo}: "
end

#success?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/uspec/result.rb', line 91

def success?
  raw == true
end

#traceObject



41
42
43
44
45
# File 'lib/uspec/result.rb', line 41

def trace
  raw.backtrace.inject(String.new) do |text, line|
    text << "#{hspace}#{line}#{newline}"
  end
end