Class: MethodFormatter

Inherits:
DottedFormatter show all
Defined in:
lib/extensions/mspec/mspec/runner/formatters/method.rb

Instance Attribute Summary collapse

Attributes inherited from DottedFormatter

#exceptions, #tally, #timer

Instance Method Summary collapse

Methods inherited from DottedFormatter

#abort, #exception, #exception?, #failure?, #print, #register

Constructor Details

#initialize(out = nil) ⇒ MethodFormatter

Returns a new instance of MethodFormatter.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/extensions/mspec/mspec/runner/formatters/method.rb', line 6

def initialize(out=nil)
  super
  @methods = Hash.new do |h, k|
    hash = {}
    hash[:examples]     = 0
    hash[:expectations] = 0
    hash[:failures]     = 0
    hash[:errors]       = 0
    hash[:exceptions]   = []
    h[k] = hash
  end
end

Instance Attribute Details

#methodsObject

Returns the value of attribute methods.



4
5
6
# File 'lib/extensions/mspec/mspec/runner/formatters/method.rb', line 4

def methods
  @methods
end

Instance Method Details

#after(state) ⇒ Object

Callback for the MSpec :after event. Sets or adds to tallies for the example block.



63
64
65
66
67
68
69
70
71
72
# File 'lib/extensions/mspec/mspec/runner/formatters/method.rb', line 63

def after(state)
  h = methods[@key]
  h[:examples]     += tally.counter.examples
  h[:expectations] += tally.counter.expectations
  h[:failures]     += tally.counter.failures
  h[:errors]       += tally.counter.errors
  @exceptions.each do |exc|
    h[:exceptions] << "#{exc.message}\n#{exc.backtrace}\n"
  end
end

#before(state) ⇒ Object

Callback for the MSpec :before event. Parses the describe string into class and method if possible. Resets the tallies so the counts are only for this example.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/extensions/mspec/mspec/runner/formatters/method.rb', line 36

def before(state)
  super

  # The pattern for a method name is not correctly
  # restrictive but it is simplistic and useful
  # for our purpose.
  /^([A-Za-z_]+\w*)(\.|#|::)([^ ]+)/ =~ state.describe
  @key = $1 && $2 && $3 ? "#{$1}#{$2}#{$3}" : state.describe

  unless methods.key? @key
    h = methods[@key]
    h[:class]       = "#{$1}"
    h[:method]      = "#{$3}"
    h[:type]        = method_type $2
    h[:description] = state.description
  end

  tally.counter.examples     = 0
  tally.counter.expectations = 0
  tally.counter.failures     = 0
  tally.counter.errors       = 0

  @exceptions = []
end

#finishObject

Callback for the MSpec :finish event. Prints out the summary information in YAML format for all the methods.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/extensions/mspec/mspec/runner/formatters/method.rb', line 76

def finish
  print "---\n"

  methods.each do |key, hash|
    print key.inspect, ":\n"
    print "  class: ",        hash[:class].inspect,        "\n"
    print "  method: ",       hash[:method].inspect,       "\n"
    print "  type: ",         hash[:type],                 "\n"
    print "  description: ",  hash[:description].inspect,  "\n"
    print "  examples: ",     hash[:examples],             "\n"
    print "  expectations: ", hash[:expectations],         "\n"
    print "  failures: ",     hash[:failures],             "\n"
    print "  errors: ",       hash[:errors],               "\n"
    print "  exceptions:\n"
    hash[:exceptions].each { |exc| print "  - ", exc.inspect, "\n" }
  end
end

#method_type(sep) ⇒ Object

Returns the type of method as a “class”, “instance”, or “unknown”.



21
22
23
24
25
26
27
28
29
30
# File 'lib/extensions/mspec/mspec/runner/formatters/method.rb', line 21

def method_type(sep)
  case sep
  when '.', '::'
    "class"
  when '#'
    "instance"
  else
    "unknown"
  end
end