Skip to content

Commit 38b64ee

Browse files
committed
Extract formatting functions into Fmt class
1 parent e8d3015 commit 38b64ee

File tree

1 file changed

+45
-46
lines changed

1 file changed

+45
-46
lines changed

project/test/util/test_runner.gd

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ var _included_tests := PackedStringArray()
4545

4646
## Initialize test execution
4747
func init_runner() -> void:
48-
print_rich(_step(), _prominent("Initializing test runner..."))
48+
print_rich(Fmt.step(), Fmt.prominent("Initializing test runner..."))
4949
_test_cases = _discover_tests()
5050
if _test_cases.is_empty():
51-
print_rich(_error("No test cases found!"))
51+
print_rich(Fmt.error("No test cases found!"))
5252
_finish(Result.TESTS_NOT_FOUND)
5353
return
5454
_state = RUN
@@ -84,9 +84,9 @@ func _discover_tests() -> Array[GdUnitTestCase]:
8484
for path in _included_tests:
8585
var scripts := scanner.scan(path)
8686
for script in scripts:
87-
print_rich(_step(), "Scanning: ", script.resource_path)
87+
print_rich(Fmt.step(), "Scanning: ", Fmt.suite(script.resource_path))
8888
GdUnitTestDiscoverer.discover_tests(script, func(test: GdUnitTestCase) -> void:
89-
print_rich(_substep(), "Discovered %s" % test.display_name)
89+
print_rich(Fmt.substep(), "Discovered %s" % Fmt.case(test.display_name))
9090
_test_cases.append(test)
9191
gdunit_test_discover_added.emit(test)
9292
)
@@ -108,20 +108,20 @@ func _finish(code: int) -> void:
108108
func _on_gdunit_event(event: GdUnitEvent) -> void:
109109
match event.type():
110110
GdUnitEvent.INIT:
111-
print_rich(_step(), _prominent("Initializing..."))
111+
print_rich(Fmt.step(), Fmt.prominent("Initializing..."))
112112
stats.clear()
113113
suite_stats.clear()
114114

115115
GdUnitEvent.STOP:
116-
print_rich(_step(), _prominent("Finished all tests."))
116+
print_rich(Fmt.step(), Fmt.prominent("Finished all tests."))
117117
_print_stats(stats, "Overall Summary")
118118

119119
GdUnitEvent.TESTSUITE_BEFORE:
120-
print_rich(_step(), _prominent("Loading: "), _suite(event.resource_path()))
120+
print_rich(Fmt.step(), Fmt.prominent("Loading: "), Fmt.suite(event.resource_path()))
121121
suite_stats.clear()
122122

123123
GdUnitEvent.TESTSUITE_AFTER:
124-
print_rich(_substep(), _prominent("Finished: "), _suite(event.resource_path()))
124+
print_rich(Fmt.substep(), Fmt.prominent("Finished: "), Fmt.suite(event.resource_path()))
125125
_print_failure_report(event.reports())
126126
_print_stats(suite_stats, "Summary")
127127
stats.num_total += suite_stats.num_total
@@ -133,8 +133,8 @@ func _on_gdunit_event(event: GdUnitEvent) -> void:
133133

134134
GdUnitEvent.TESTCASE_BEFORE:
135135
var test := _test_session.find_test_by_id(event.guid())
136-
print_rich(_substep(), _prominent("Started: "),
137-
_suite(test.suite_name), " > ", _case(test.display_name))
136+
print_rich(Fmt.substep(), Fmt.prominent("Started: "),
137+
Fmt.suite(test.suite_name), " > ", Fmt.case(test.display_name))
138138

139139
GdUnitEvent.TESTCASE_AFTER:
140140
suite_stats.num_total += 1
@@ -146,45 +146,22 @@ func _on_gdunit_event(event: GdUnitEvent) -> void:
146146

147147
var test := _test_session.find_test_by_id(event.guid())
148148
if event.is_success():
149-
_print_result(_bold(_success("PASSED")), test.suite_name, test.display_name)
149+
_print_result(Fmt.bold(Fmt.success("PASSED")), test.suite_name, test.display_name)
150150
elif event.is_skipped():
151-
_print_result(_bold(_muted("SKIPPED")), test.suite_name, test.display_name)
151+
_print_result(Fmt.bold(Fmt.muted("SKIPPED")), test.suite_name, test.display_name)
152152
elif event.is_failed() or event.is_error():
153-
_print_result(_bold(_error("FAILED")), test.suite_name, test.display_name)
153+
_print_result(Fmt.bold(Fmt.error("FAILED")), test.suite_name, test.display_name)
154154
_print_failure_report(event.reports())
155155
elif event.is_warning():
156-
_print_result(_bold(_warning("WARNING")), test.suite_name, test.display_name)
156+
_print_result(Fmt.bold(Fmt.warning("WARNING")), test.suite_name, test.display_name)
157157
_print_failure_report(event.reports())
158158

159159

160160
# *** CONSOLE OUTPUT
161161

162-
static func _colored(text: String, color: Color) -> String:
163-
return "[color=%s]%s[/color]" % [color.to_html(), text]
164-
165-
166-
static func _bold(text: String) -> String:
167-
return "[b]%s[/b]" % text
168-
169-
170-
static func _step() -> String: return _bold(_colored("==> ", Color.WHITE))
171-
static func _substep() -> String: return _bold(_colored("--> ", Color.WHITE))
172-
static func _prominent(text: String) -> String: return _bold(_colored(text, Color.WHITE))
173-
174-
static func _primary(text: String) -> String: return _colored(text, Color.WHITE)
175-
static func _accent(text: String) -> String: return _colored(text, Color.DODGER_BLUE)
176-
static func _muted(text: String) -> String: return _colored(text, Color.GRAY)
177-
static func _success(text: String) -> String: return _colored(text, Color.GREEN)
178-
static func _warning(text: String) -> String: return _colored(text, Color.GOLDENROD)
179-
static func _error(text: String) -> String: return _colored(text, Color.RED)
180-
181-
static func _suite(text: String) -> String: return _colored(text, Color.DARK_CYAN)
182-
static func _case(text: String) -> String: return _colored(text, Color.CYAN)
183-
184-
185162
static func _print_result(status: String, test_suite: String, test_case: String) -> void:
186-
print_rich(_substep(), status, ": ",
187-
_suite(test_suite), " > ", _case(test_case))
163+
print_rich(Fmt.substep(), status, ": ",
164+
Fmt.suite(test_suite), " > ", Fmt.case(test_case))
188165

189166

190167
static func _print_failure_report(reports: Array[GdUnitReport]) -> void:
@@ -206,11 +183,33 @@ static func _print_stats(p_stats: Stats, p_header: String) -> void:
206183
var skipped: String = " %d skipped" % p_stats.num_skipped
207184
var flaky: String = " %d flaky" % p_stats.num_flaky
208185
print_rich(
209-
_substep(),
210-
_bold(_accent(p_header)), ": ",
211-
_primary(total),
212-
_error(errors) if p_stats.num_errors > 0 else _primary(errors),
213-
_error(failed) if p_stats.num_failed > 0 else _primary(failed),
214-
_primary(skipped),
215-
_warning(flaky) if p_stats.num_flaky > 0 else _primary(flaky)
186+
Fmt.substep(),
187+
Fmt.bold(Fmt.accent(p_header)), ": ",
188+
Fmt.primary(total),
189+
Fmt.error(errors) if p_stats.num_errors > 0 else Fmt.primary(errors),
190+
Fmt.error(failed) if p_stats.num_failed > 0 else Fmt.primary(failed),
191+
Fmt.primary(skipped),
192+
Fmt.warning(flaky) if p_stats.num_flaky > 0 else Fmt.primary(flaky)
216193
)
194+
195+
196+
class Fmt:
197+
static func colored(text: String, color: Color) -> String:
198+
return "[color=%s]%s[/color]" % [color.to_html(), text]
199+
200+
static func bold(text: String) -> String:
201+
return "[b]%s[/b]" % text
202+
203+
static func step() -> String: return bold(colored("==> ", Color.WHITE))
204+
static func substep() -> String: return bold(colored("--> ", Color.WHITE))
205+
static func prominent(text: String) -> String: return bold(colored(text, Color.WHITE))
206+
207+
static func primary(text: String) -> String: return colored(text, Color.WHITE)
208+
static func accent(text: String) -> String: return colored(text, Color.DODGER_BLUE)
209+
static func muted(text: String) -> String: return colored(text, Color.GRAY)
210+
static func success(text: String) -> String: return colored(text, Color.GREEN)
211+
static func warning(text: String) -> String: return colored(text, Color.GOLDENROD)
212+
static func error(text: String) -> String: return colored(text, Color.RED)
213+
214+
static func suite(text: String) -> String: return colored(text, Color.DARK_CYAN)
215+
static func case(text: String) -> String: return colored(text, Color.CYAN)

0 commit comments

Comments
 (0)