-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
formatter.cr
145 lines (115 loc) · 2.5 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
module Spec
# :nodoc:
abstract class Formatter
def initialize(@io : IO = STDOUT)
end
def push(context)
end
def pop
end
def before_example(description)
end
def report(result)
end
def finish(elapsed_time, aborted)
end
def should_print_summary?
false
end
end
# :nodoc:
class DotFormatter < Formatter
@count = 0
@split = 0
def initialize(*args)
super
if split = ENV["SPEC_SPLIT_DOTS"]?
@split = split.to_i
end
end
def report(result)
@io << Spec.color(LETTERS[result.kind], result.kind)
split_lines
@io.flush
end
private def split_lines
return unless @split > 0
if (@count += 1) >= @split
@io.puts
@count = 0
end
end
def finish(elapsed_time, aborted)
@io.puts
end
def should_print_summary?
true
end
end
# :nodoc:
class VerboseFormatter < Formatter
class Item
def initialize(@indent : Int32, @description : String)
@printed = false
end
def print(io)
return if @printed
@printed = true
VerboseFormatter.print_indent(io, @indent)
io.puts @description
end
end
@indent = 0
@last_description = ""
@items = [] of Item
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(@io, @indent)
end
def self.print_indent(io, indent)
indent.times { io << " " }
end
def before_example(description)
@items.each &.print(@io)
print_indent
@io << description
@last_description = description
end
def report(result)
@io << '\r'
print_indent
@io.puts Spec.color(@last_description, result.kind)
end
def should_print_summary?
true
end
end
# :nodoc:
class CLI
@formatters = [Spec::DotFormatter.new] of Spec::Formatter
def formatters
@formatters
end
def override_default_formatter(formatter)
@formatters[0] = formatter
end
def add_formatter(formatter)
@formatters << formatter
end
end
@[Deprecated("This is an internal API.")]
def self.override_default_formatter(formatter)
@@cli.override_default_formatter(formatter)
end
@[Deprecated("This is an internal API.")]
def self.add_formatter(formatter)
@@cli.add_formatter(formatter)
end
end