Skip to content

Commit cd2dd0e

Browse files
authored
Replace some lookup hashes in Spec with exhaustive cases (#15879)
1 parent 148e1b3 commit cd2dd0e

File tree

3 files changed

+25
-23
lines changed

3 files changed

+25
-23
lines changed

src/spec/context.cr

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,24 @@ module Spec
116116
Fail
117117
Error
118118
Pending
119+
120+
def color : Colorize::Color
121+
case self
122+
in Success then Colorize::ColorANSI::Green
123+
in Fail then Colorize::ColorANSI::Red
124+
in Error then Colorize::ColorANSI::Red
125+
in Pending then Colorize::ColorANSI::Yellow
126+
end
127+
end
128+
129+
def letter : Char
130+
case self
131+
in Success then '.'
132+
in Fail then 'F'
133+
in Error then 'E'
134+
in Pending then '*'
135+
end
136+
end
119137
end
120138

121139
# :nodoc:

src/spec/dsl.cr

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,20 @@ module Spec
1111
Comment
1212
Focus
1313
Order
14-
end
1514

16-
private STATUS_COLORS = {
17-
Status::Success => :green,
18-
Status::Fail => :red,
19-
Status::Error => :red,
20-
Status::Pending => :yellow,
21-
}
22-
23-
private INFO_COLORS = {
24-
InfoKind::Comment => :cyan,
25-
InfoKind::Focus => :cyan,
26-
InfoKind::Order => :cyan,
27-
}
28-
29-
private LETTERS = {
30-
Status::Success => '.',
31-
Status::Fail => 'F',
32-
Status::Error => 'E',
33-
Status::Pending => '*',
34-
}
15+
def color : Colorize::Color
16+
Colorize::ColorANSI::Cyan
17+
end
18+
end
3519

3620
# :nodoc:
3721
def self.color(str, status : Status)
38-
str.colorize(STATUS_COLORS[status])
22+
str.colorize(status.color)
3923
end
4024

4125
# :nodoc:
4226
def self.color(str, kind : InfoKind)
43-
str.colorize(INFO_COLORS[kind])
27+
str.colorize(kind.color)
4428
end
4529

4630
# :nodoc:

src/spec/formatter.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ module Spec
3838
end
3939

4040
def report(result)
41-
@io << Spec.color(LETTERS[result.kind], result.kind)
41+
@io << Spec.color(result.kind.letter, result.kind)
4242
split_lines
4343
@io.flush
4444
end

0 commit comments

Comments
 (0)