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.



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

def initialize spec, raw, source
  @spec = spec
  @raw = raw
  @source = source
end

Dynamic Method Handling

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

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



12
13
14
# File 'lib/uspec/result.rb', line 12

def raw
  @raw
end

#sourceObject (readonly)

Returns the value of attribute source.



12
13
14
# File 'lib/uspec/result.rb', line 12

def source
  @source
end

#specObject (readonly)

Returns the value of attribute spec.



12
13
14
# File 'lib/uspec/result.rb', line 12

def spec
  @spec
end

Instance Method Details

#ancestor_klassesObject



97
98
99
# File 'lib/uspec/result.rb', line 97

def ancestor_klasses
  @ancestor_klasses ||= ancestors.select{|a| a.is_a? Class}
end

#ancestorsObject

Collects the ancestors of an object



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

def ancestors
  @ancestors ||= safe_send singleton, :ancestors
end

#get_idObject

Gets the object ID of an object



88
89
90
# File 'lib/uspec/result.rb', line 88

def get_id
  raw.__id__.to_s(16) rescue 0
end

#inspectObject



111
112
113
# File 'lib/uspec/result.rb', line 111

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

#inspectorObject

Attempts to inspect an object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/uspec/result.rb', line 52

def inspector
  klass && klass.public_method_defined?(:inspect) ? raw.inspect : "#<#{klass}:0x#{get_id}>"
rescue Exception => error
  return "#<#{klass}:0x#{get_id}>" if error.message.include? 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

#klassObject

Returns the class of the object if it isn’t already a class



78
79
80
# File 'lib/uspec/result.rb', line 78

def klass
  Module === raw ? raw : ancestor_klasses[1]
end

#klassinfoObject



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

def klassinfo
  superklass ? "#{klass} < #{superklass}: " : "#{klass}: "
end

#messageObject



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

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

#prettyObject



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

def pretty
  if raw == true then
    green raw
  elsif raw == false then
    red raw
  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('Unknown Result'), vspace,
      hspace, 'Spec did not return a boolean value ', newline,
      hspace, 'in spec at ', source.first, vspace,
      hspace, red(klassinfo), inspector, newline
    ].join
  end
end

#safe_send(object, method, *args, &block) ⇒ Object

Works around BasicObject and other objects that are missing/overwrite important methods



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

def safe_send object, method, *args, &block
  (Module === object ? Module : Object).instance_method(method).bind(object).call(*args, &block)
end

#singletonObject

Obtain the singleton class of an object



93
94
95
# File 'lib/uspec/result.rb', line 93

def singleton
  @singleton ||= (class << raw; self; end) rescue raw.class
end

#superklassObject

Returns the superclass of the object



83
84
85
# File 'lib/uspec/result.rb', line 83

def superklass
  ancestor_klasses[2]
end

#traceObject



37
38
39
40
41
# File 'lib/uspec/result.rb', line 37

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