/
formatter.cr
99 lines (79 loc) · 1.64 KB
/
formatter.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
90
91
92
93
94
95
96
97
98
99
module Spec
# :nodoc:
abstract class Formatter
def push(context)
end
def pop
end
def before_example(description)
end
def report(result)
end
def finish
end
end
# :nodoc:
class DotFormatter < Formatter
def report(result)
print Spec.color(LETTERS[result.kind], result.kind)
end
def finish
puts
end
end
# :nodoc:
class VerboseFormatter < Formatter
class Item
def initialize(@indent : Int32, @description : String)
@printed = false
end
def print
return if @printed
@printed = true
VerboseFormatter.print_indent(@indent)
puts @description
end
end
def initialize
@indent = 0
@last_description = ""
@items = [] of Item
end
def push(context)
@items << Item.new(@indent, context.description)
@indent += 1
end
def pop
@items.pop
@indent -= 1
end
def print_indent
self.class.print_indent(@indent)
end
def self.print_indent(indent)
indent.times { print " " }
end
def before_example(description)
@items.each &.print
print_indent
print description
@last_description = description
end
def report(result)
print '\r'
print_indent
puts Spec.color(@last_description, result.kind)
end
end
@@formatters = [Spec::DotFormatter.new] of Spec::Formatter
# :nodoc:
def self.formatters
@@formatters
end
def self.override_default_formatter(formatter)
@@formatters[0] = formatter
end
def self.add_formatter(formatter)
@@formatters << formatter
end
end