Skip to content

Commit 05b3052

Browse files
committed
refactor: use qualified type access and rename CliOptions
1 parent 92a71c6 commit 05b3052

10 files changed

Lines changed: 144 additions & 132 deletions

File tree

src/unitest.gleam

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@ import gleam_community/ansi
1919
import prng/random
2020
import simplifile
2121
import spinner
22-
import unitest/internal/cli.{type Reporter, type SortOrder}
23-
import unitest/internal/discover.{type Test}
22+
import unitest/internal/cli
23+
import unitest/internal/discover
2424
import unitest/internal/format_table
25-
import unitest/internal/runner.{
26-
type PoolResult, type Progress, type Report, type TestResult,
27-
}
25+
import unitest/internal/runner
2826

2927
const yield_every_n_tests = 5
3028

@@ -53,7 +51,7 @@ pub type ResolvedExecutionMode {
5351

5452
@internal
5553
pub type CliAction {
56-
RunWithCliOptions(cli.CliOptions)
54+
RunWithCliOptions(cli.Options)
5755
ShowCliMessage(message: String, exit_code: Int)
5856
}
5957

@@ -274,7 +272,7 @@ fn wants_help(args: List(String)) -> Bool {
274272
list.contains(args, "--help") || list.contains(args, "-h")
275273
}
276274

277-
fn run_with_cli_opts(cli_opts: cli.CliOptions, options: Options) -> Nil {
275+
fn run_with_cli_opts(cli_opts: cli.Options, options: Options) -> Nil {
278276
let use_color = should_use_color(cli_opts.no_color)
279277
let tests = discover.discover_from_fs(options.test_directory)
280278

@@ -362,8 +360,8 @@ fn execute_and_finish(
362360
seed: Int,
363361
mode: ResolvedExecutionMode,
364362
use_color: Bool,
365-
reporter: Reporter,
366-
sort_order: SortOrder,
363+
reporter: cli.Reporter,
364+
sort_order: cli.SortOrder,
367365
sort_reversed: Bool,
368366
check_results: Bool,
369367
) -> Nil {
@@ -426,20 +424,24 @@ fn execute_and_finish(
426424
}
427425

428426
type OnResultCallback =
429-
fn(TestResult, Progress, fn() -> Nil) -> Nil
427+
fn(runner.TestResult, runner.Progress, fn() -> Nil) -> Nil
430428

431429
type CleanupCallback =
432430
fn(runner.ExecuteResult) -> Nil
433431

434432
fn build_on_result_callback(
435-
reporter: Reporter,
433+
reporter: cli.Reporter,
436434
use_color: Bool,
437-
sort_order: SortOrder,
435+
sort_order: cli.SortOrder,
438436
sort_reversed: Bool,
439437
) -> #(OnResultCallback, CleanupCallback) {
440438
case reporter {
441439
cli.DotReporter -> {
442-
let on_result = fn(result: TestResult, _progress: Progress, continue) {
440+
let on_result = fn(
441+
result: runner.TestResult,
442+
_progress: runner.Progress,
443+
continue,
444+
) {
443445
io.print(runner.outcome_char(result.outcome, use_color))
444446
continue()
445447
}
@@ -452,7 +454,11 @@ fn build_on_result_callback(
452454
|> spinner.with_colour(ansi.cyan)
453455
|> spinner.start
454456

455-
let on_result = fn(_result: TestResult, progress: Progress, continue) {
457+
let on_result = fn(
458+
_result: runner.TestResult,
459+
progress: runner.Progress,
460+
continue,
461+
) {
456462
let text =
457463
"Running tests... "
458464
<> int.to_string(progress.current)
@@ -482,7 +488,7 @@ fn build_on_result_callback(
482488
}
483489

484490
@internal
485-
pub fn exit_code(report: Report) -> Int {
491+
pub fn exit_code(report: runner.Report) -> Int {
486492
case report.failed > 0 {
487493
True -> 1
488494
False -> 0
@@ -500,7 +506,7 @@ fn now_ms() -> Int
500506
@external(erlang, "unitest_ffi", "run_test_async")
501507
@external(javascript, "./unitest_ffi.mjs", "runTestAsync")
502508
fn run_test_async(
503-
t: Test,
509+
t: discover.Test,
504510
package_name: String,
505511
check_results: Bool,
506512
k: fn(runner.TestRunResult) -> Nil,
@@ -509,15 +515,15 @@ fn run_test_async(
509515
@external(erlang, "unitest_ffi", "start_module_pool")
510516
@external(javascript, "./unitest_ffi.mjs", "startModulePool")
511517
fn start_module_pool(
512-
module_groups: List(List(Test)),
518+
module_groups: List(List(discover.Test)),
513519
package_name: String,
514520
check_results: Bool,
515521
workers: Int,
516522
) -> Nil
517523

518524
@external(erlang, "unitest_ffi", "receive_pool_result")
519525
@external(javascript, "./unitest_ffi.mjs", "receivePoolResult")
520-
fn receive_pool_result(callback: fn(PoolResult) -> Nil) -> Nil
526+
fn receive_pool_result(callback: fn(runner.PoolResult) -> Nil) -> Nil
521527

522528
@target(erlang)
523529
fn yield_then(next: fn() -> Nil) -> Nil {

src/unitest/internal/cli.gleam

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ pub type SortOrder {
3030
NameSort
3131
}
3232

33-
pub type CliOptions {
34-
CliOptions(
33+
pub type Options {
34+
Options(
3535
seed: Option(Int),
3636
filter: Filter,
3737
no_color: Bool,
@@ -42,7 +42,7 @@ pub type CliOptions {
4242
)
4343
}
4444

45-
pub fn parse(args: List(String)) -> Result(CliOptions, String) {
45+
pub fn parse(args: List(String)) -> Result(Options, String) {
4646
let command =
4747
clip.command({
4848
use seed_result <- clip.parameter
@@ -149,7 +149,7 @@ pub fn parse(args: List(String)) -> Result(CliOptions, String) {
149149
use reporter <- result.try(parse_reporter(reporter_str))
150150
use sort_order <- result.try(parse_sort_order(sort_str))
151151
use validated_workers <- result.try(validate_workers(workers))
152-
Ok(CliOptions(
152+
Ok(Options(
153153
seed:,
154154
filter:,
155155
no_color:,

src/unitest/internal/discover.gleam

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import glance.{
2-
type Attribute, type Expression, type Function, type Span, type Statement,
3-
}
1+
import glance
42
import gleam/bit_array
53
import gleam/list
64
import gleam/result
@@ -22,7 +20,7 @@ pub type Test {
2220
}
2321

2422
pub type ParsedTest {
25-
ParsedTest(name: String, tags: List(String), byte_span: Span)
23+
ParsedTest(name: String, tags: List(String), byte_span: glance.Span)
2624
}
2725

2826
pub fn path_to_module(path: String, base_path: String) -> String {
@@ -105,7 +103,10 @@ pub fn parse_module_for_target(
105103
})
106104
}
107105

108-
fn is_available_for_target(attributes: List(Attribute), target: String) -> Bool {
106+
fn is_available_for_target(
107+
attributes: List(glance.Attribute),
108+
target: String,
109+
) -> Bool {
109110
let target_attrs = list.filter(attributes, fn(attr) { attr.name == "target" })
110111

111112
case target_attrs {
@@ -114,7 +115,7 @@ fn is_available_for_target(attributes: List(Attribute), target: String) -> Bool
114115
}
115116
}
116117

117-
fn matches_target(attr: Attribute, target: String) -> Bool {
118+
fn matches_target(attr: glance.Attribute, target: String) -> Bool {
118119
case attr.arguments {
119120
[glance.Variable(name: attr_target, ..)] -> attr_target == target
120121
_ -> True
@@ -131,24 +132,24 @@ fn current_target() -> String {
131132
"javascript"
132133
}
133134

134-
fn parse_function(func: Function) -> ParsedTest {
135+
fn parse_function(func: glance.Function) -> ParsedTest {
135136
let tags = extract_tags_from_body(func.body)
136137
ParsedTest(name: func.name, tags: list.unique(tags), byte_span: func.location)
137138
}
138139

139-
fn extract_tags_from_body(statements: List(Statement)) -> List(String) {
140+
fn extract_tags_from_body(statements: List(glance.Statement)) -> List(String) {
140141
list.flat_map(statements, extract_tags_from_statement)
141142
}
142143

143-
fn extract_tags_from_statement(stmt: Statement) -> List(String) {
144+
fn extract_tags_from_statement(stmt: glance.Statement) -> List(String) {
144145
case stmt {
145146
glance.Use(function: func, ..) -> extract_tags_from_expr(func)
146147
glance.Expression(expr) -> extract_tags_from_expr(expr)
147148
_ -> []
148149
}
149150
}
150151

151-
fn extract_tags_from_expr(expr: Expression) -> List(String) {
152+
fn extract_tags_from_expr(expr: glance.Expression) -> List(String) {
152153
case expr {
153154
// unitest.tag("slow")
154155
glance.Call(
@@ -176,14 +177,18 @@ fn extract_tags_from_expr(expr: Expression) -> List(String) {
176177
}
177178
}
178179

179-
fn extract_single_tag(args: List(glance.Field(Expression))) -> List(String) {
180+
fn extract_single_tag(
181+
args: List(glance.Field(glance.Expression)),
182+
) -> List(String) {
180183
case args {
181184
[glance.UnlabelledField(glance.String(value: tag, ..))] -> [tag]
182185
_ -> []
183186
}
184187
}
185188

186-
fn extract_tags_list(args: List(glance.Field(Expression))) -> List(String) {
189+
fn extract_tags_list(
190+
args: List(glance.Field(glance.Expression)),
191+
) -> List(String) {
187192
case args {
188193
[glance.UnlabelledField(glance.List(elements: elements, ..))] ->
189194
list.filter_map(elements, fn(elem) {

src/unitest/internal/format_table.gleam

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import gleam/order
44
import gleam/string
55
import gleam_community/ansi
66
import tobble
7-
import unitest/internal/cli.{type SortOrder}
8-
import unitest/internal/runner.{type TestResult}
7+
import unitest/internal/cli
8+
import unitest/internal/runner
99

1010
pub fn render_table(
11-
results: List(TestResult),
11+
results: List(runner.TestResult),
1212
use_color: Bool,
13-
sort_order: SortOrder,
13+
sort_order: cli.SortOrder,
1414
sort_reversed: Bool,
1515
) -> String {
1616
let sorted_results = sort_results(results, sort_order, sort_reversed)
@@ -64,10 +64,10 @@ fn format_duration(outcome: runner.Outcome, duration_ms: Int) -> String {
6464
}
6565

6666
fn sort_results(
67-
results: List(TestResult),
68-
sort_order: SortOrder,
67+
results: List(runner.TestResult),
68+
sort_order: cli.SortOrder,
6969
sort_reversed: Bool,
70-
) -> List(TestResult) {
70+
) -> List(runner.TestResult) {
7171
case sort_order, sort_reversed {
7272
cli.NativeSort, False -> results
7373
cli.NativeSort, True -> list.reverse(results)
@@ -79,12 +79,16 @@ fn sort_results(
7979
}
8080
}
8181

82-
fn compare_by_name(a: TestResult, b: TestResult) -> order.Order {
82+
fn compare_by_name(a: runner.TestResult, b: runner.TestResult) -> order.Order {
8383
string.compare(a.item.module, b.item.module)
8484
|> order.lazy_break_tie(fn() { string.compare(a.item.name, b.item.name) })
8585
}
8686

87-
fn compare_by_time(a: TestResult, b: TestResult, reversed: Bool) -> order.Order {
87+
fn compare_by_time(
88+
a: runner.TestResult,
89+
b: runner.TestResult,
90+
reversed: Bool,
91+
) -> order.Order {
8892
case a.outcome, b.outcome {
8993
runner.Skipped, runner.Skipped -> order.Eq
9094
runner.Skipped, runner.Passed | runner.Skipped, runner.Failed(_) -> order.Gt

src/unitest/internal/js_decode.gleam

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import gleam/dynamic.{type Dynamic}
22
import gleam/dynamic/decode
3-
import unitest/internal/discover.{type Test}
4-
import unitest/internal/runner.{type PoolResult, type TestRunResult}
5-
import unitest/internal/test_failure.{
6-
type AssertKind, type AssertedExpr, type ExprKind, type PanicKind,
7-
type TestFailure,
8-
}
3+
import unitest/internal/discover
4+
import unitest/internal/runner
5+
import unitest/internal/test_failure.{type TestFailure}
96

10-
pub fn decode_test_run_result(raw: Dynamic) -> TestRunResult {
7+
pub fn decode_test_run_result(raw: Dynamic) -> runner.TestRunResult {
118
let decoder = {
129
use tag <- decode.field("kind", decode.string)
1310
case tag {
@@ -28,14 +25,14 @@ pub fn decode_test_run_result(raw: Dynamic) -> TestRunResult {
2825
}
2926

3027
pub fn wrap_pool_result(
31-
item: Test,
32-
result: TestRunResult,
28+
item: discover.Test,
29+
result: runner.TestRunResult,
3330
duration_ms: Int,
34-
) -> PoolResult {
31+
) -> runner.PoolResult {
3532
runner.PoolResult(item:, result:, duration_ms:)
3633
}
3734

38-
pub fn make_crash_error(message: String) -> TestRunResult {
35+
pub fn make_crash_error(message: String) -> runner.TestRunResult {
3936
runner.RunError(test_failure.TestFailure(
4037
message:,
4138
file: "",
@@ -67,7 +64,7 @@ fn decode_test_failure() -> decode.Decoder(TestFailure) {
6764
))
6865
}
6966

70-
fn decode_panic_kind_inner() -> decode.Decoder(PanicKind) {
67+
fn decode_panic_kind_inner() -> decode.Decoder(test_failure.PanicKind) {
7168
use tag <- decode.field("type", decode.string)
7269
case tag {
7370
"assert" -> {
@@ -101,7 +98,7 @@ fn decode_panic_kind_inner() -> decode.Decoder(PanicKind) {
10198
}
10299
}
103100

104-
fn decode_assert_kind_inner() -> decode.Decoder(AssertKind) {
101+
fn decode_assert_kind_inner() -> decode.Decoder(test_failure.AssertKind) {
105102
let default_expr = test_failure.AssertedExpr(0, 0, test_failure.Unevaluated)
106103

107104
use tag <- decode.field("type", decode.string)
@@ -140,14 +137,14 @@ fn decode_assert_kind_inner() -> decode.Decoder(AssertKind) {
140137
}
141138
}
142139

143-
fn decode_asserted_expr_value() -> decode.Decoder(AssertedExpr) {
140+
fn decode_asserted_expr_value() -> decode.Decoder(test_failure.AssertedExpr) {
144141
use start <- decode.optional_field("start", 0, decode.int)
145142
use end <- decode.optional_field("end", 0, decode.int)
146143
use kind <- decode.then(decode_expr_kind())
147144
decode.success(test_failure.AssertedExpr(start:, end:, kind:))
148145
}
149146

150-
fn decode_expr_kind() -> decode.Decoder(ExprKind) {
147+
fn decode_expr_kind() -> decode.Decoder(test_failure.ExprKind) {
151148
use tag <- decode.optional_field("kind", "", decode.string)
152149
case tag {
153150
"literal" -> {

0 commit comments

Comments
 (0)