Class: Uspec::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/uspec/formatter.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/uspec/formatter.rb', line 81

def method_missing name, *args, &block
  if colors.keys.include? name then
    color name, *args
  else
    super
  end
end

Instance Method Details

#classify(object) ⇒ Object



61
62
63
# File 'lib/uspec/formatter.rb', line 61

def classify object
  object.is_a?(Module) ? object : object.class
end

#classinfo(object) ⇒ Object



57
58
59
# File 'lib/uspec/formatter.rb', line 57

def classinfo object
  "#{classify object} < #{superclass object}: "
end

#color(hue, text = nil) ⇒ Object



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

def color hue, text = nil
  esc("3#{colors[hue]};1") + "#{text}#{normal}"
end

#colorize(result, source) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/uspec/formatter.rb', line 24

def colorize result, source
  if result == true then
    green result
  elsif result == false then
    red result
  elsif result.is_a? Exception then
    [
      red('Exception'), vspace,
      hspace, 'Spec encountered an Exception ', newline,
      hspace, 'in spec at ', source.first, vspace,
      hspace, message(result), vspace,
      white(trace result)
    ].join
  else
    [
      red('Unknown Result'), vspace,
      hspace, 'Spec did not return a boolean value ', newline,
      hspace, 'in spec at ', source.first, vspace,
      hspace, red(classinfo(result)), result.inspect, newline
    ].join
  end
end

#colorsObject



3
4
5
6
7
8
9
10
# File 'lib/uspec/formatter.rb', line 3

def colors
  {
    red: 1,
    green: 2,
    yellow: 3,
    white: 7
  }
end

#esc(seq) ⇒ Object



16
17
18
# File 'lib/uspec/formatter.rb', line 16

def esc seq
  "\e[#{seq}m"
end

#hspaceObject



69
70
71
# File 'lib/uspec/formatter.rb', line 69

def hspace
  '    '
end

#message(error) ⇒ Object



53
54
55
# File 'lib/uspec/formatter.rb', line 53

def message error
  red(classinfo error) + error.message
end

#newlineObject



77
78
79
# File 'lib/uspec/formatter.rb', line 77

def newline
  $/
end

#normal(text = nil) ⇒ Object



20
21
22
# File 'lib/uspec/formatter.rb', line 20

def normal text=nil
  esc(0) + text.to_s
end

#superclass(object) ⇒ Object



65
66
67
# File 'lib/uspec/formatter.rb', line 65

def superclass object
  classify(object).superclass
end

#trace(error) ⇒ Object



47
48
49
50
51
# File 'lib/uspec/formatter.rb', line 47

def trace error
  error.backtrace.inject(String.new) do |text, line|
    text << hspace + line + newline
  end
end

#vspaceObject



73
74
75
# File 'lib/uspec/formatter.rb', line 73

def vspace
  newline + newline
end