Skip to content

Commit 882577b

Browse files
authored
added map_try! + walk_try! test (#7770)
* map_try! + walk_try! test * update mono tests
1 parent e89bb6e commit 882577b

File tree

51 files changed

+1525
-1367
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1525
-1367
lines changed

crates/cli/tests/cli_tests.rs

+30-2
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ mod cli_tests {
6161
.status()
6262
.unwrap();
6363

64-
let cli_build = ExecCli::new(
64+
let cli_dev = ExecCli::new(
6565
roc_cli::CMD_DEV,
6666
file_from_root("crates/cli/tests/platform-switching", "roc_loves_rust.roc"),
6767
);
6868

6969
let expected_output = "Roc <3 Rust!\n";
7070

71-
let output = cli_build.run();
71+
let output = cli_dev.run();
7272

7373
output.assert_clean_stdout(expected_output);
7474
}
@@ -1022,6 +1022,34 @@ mod cli_tests {
10221022

10231023
cli_build.check_build_and_run(expected_output, ALLOW_VALGRIND, None, None);
10241024
}
1025+
1026+
#[test]
1027+
#[cfg_attr(windows, ignore)]
1028+
fn effectful_walk_try() {
1029+
build_platform_host();
1030+
1031+
let cli_dev = ExecCli::new(
1032+
roc_cli::CMD_DEV,
1033+
file_from_root("crates/cli/tests/test-projects/effectful", "walk_try.roc"),
1034+
);
1035+
1036+
let cli_dev_out = cli_dev.run();
1037+
cli_dev_out.assert_clean_success();
1038+
}
1039+
1040+
#[test]
1041+
#[cfg_attr(windows, ignore)]
1042+
fn effectful_map_try() {
1043+
build_platform_host();
1044+
1045+
let cli_dev = ExecCli::new(
1046+
roc_cli::CMD_DEV,
1047+
file_from_root("crates/cli/tests/test-projects/effectful", "map_try.roc"),
1048+
);
1049+
1050+
let cli_dev_out = cli_dev.run();
1051+
cli_dev_out.assert_clean_success();
1052+
}
10251053
}
10261054

10271055
// this is for testing the benchmarks (on small inputs), to perform proper benchmarks see crates/cli/benches/README.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
app [main!] { pf: platform "../test-platform-effects-zig/main.roc" }
2+
3+
import pf.Effect
4+
5+
main! : {} => {}
6+
main! = \{} ->
7+
sanity_check = err_if_zero!(1)
8+
expect sanity_check == Ok(1)
9+
10+
good = List.map_try!([1, 2, 3], |num| err_if_zero!(num))
11+
expect good == Ok([1, 2, 3])
12+
13+
good_empty = List.map_try!([], |num| err_if_zero!(num))
14+
expect good_empty == Ok([])
15+
16+
bad_a = List.map_try!([1, 2, 3, 0], |num| err_if_zero!(num))
17+
expect bad_a == Err({})
18+
19+
bad_b = List.map_try!([0, 1, 2, 3], |num| err_if_zero!(num))
20+
expect bad_b == Err({})
21+
22+
bad_c = List.map_try!([1, 0, 2, 3], |num| err_if_zero!(num))
23+
expect bad_c == Err({})
24+
25+
{}
26+
27+
err_if_zero! : U64 => Result U64 {}
28+
err_if_zero! = \num ->
29+
if num != 0 then
30+
Ok(Effect.id_effectful!(num))
31+
else
32+
_ = Effect.id_effectful!(num)
33+
Err({})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
app [main!] { pf: platform "../test-platform-effects-zig/main.roc" }
2+
3+
import pf.Effect
4+
5+
main! : {} => {}
6+
main! = \{} ->
7+
# Sanity check for our test helper
8+
sanity_check = add_non_zero!(0, 1)
9+
expect sanity_check == Ok(1)
10+
11+
# Test successful walk_try! with non-zero elements
12+
good = List.walk_try!([1, 2, 3], 0, \sum, num -> add_non_zero!(sum, num))
13+
expect good == Ok(6)
14+
15+
# Test walk_try! with empty list
16+
good_empty = List.walk_try!([], 10, \sum, num -> add_non_zero!(sum, num))
17+
expect good_empty == Ok(10)
18+
19+
# Test walk_try! that encounters a zero at different positions
20+
bad_a = List.walk_try!([1, 2, 0, 3], 0, \sum, num -> add_non_zero!(sum, num))
21+
expect bad_a == Err({})
22+
23+
bad_b = List.walk_try!([0, 1, 2, 3], 0, \sum, num -> add_non_zero!(sum, num))
24+
expect bad_b == Err({})
25+
26+
bad_c = List.walk_try!([1, 0, 2, 3], 0, \sum, num -> add_non_zero!(sum, num))
27+
expect bad_c == Err({})
28+
29+
# Test more complex state accumulation
30+
# Accumulate pairs of [state, elem] but error on odd numbers
31+
complex_walk = List.walk_try!(
32+
[2, 4, 6, 8],
33+
[],
34+
\pairs, num ->
35+
if num % 2 == 0 then
36+
new_state = List.len(pairs) * 10
37+
result = Effect.id_effectful!(new_state)
38+
Ok(List.append(pairs, [result, num]))
39+
else
40+
_ = Effect.id_effectful!(num)
41+
Err({})
42+
)
43+
expect complex_walk == Ok([[0, 2], [10, 4], [20, 6], [30, 8]])
44+
45+
# Test complex state walk that fails
46+
complex_walk_fail = List.walk_try!(
47+
[2, 4, 5, 8],
48+
[],
49+
\pairs, num ->
50+
if num % 2 == 0 then
51+
new_state = List.len(pairs) * 10
52+
result = Effect.id_effectful!(new_state)
53+
Ok(List.append(pairs, [result, num]))
54+
else
55+
_ = Effect.id_effectful!(num)
56+
Err({})
57+
)
58+
expect complex_walk_fail == Err({})
59+
60+
{}
61+
62+
# Helper that returns Ok(sum) if num is non-zero, or Err({}) if num is zero
63+
add_non_zero! : U64, U64 => Result U64 {}
64+
add_non_zero! = \sum, num ->
65+
if num != 0 then
66+
Ok(sum + Effect.id_effectful!(num))
67+
else
68+
_ = Effect.id_effectful!(num)
69+
Err({})
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
hosted [put_line!, get_line!]
1+
hosted [put_line!, get_line!, id_effectful!]
22

33
put_line! : Str => {}
44

55
get_line! : {} => Str
6+
7+
id_effectful! : U64 => U64

crates/cli/tests/test-projects/test-platform-effects-zig/host.zig

+5
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,8 @@ fn roc_fx_get_int_help() !i64 {
181181

182182
return std.fmt.parseInt(i64, line, 10);
183183
}
184+
185+
/// Just return the input for testing purposes
186+
pub export fn roc_fx_id_effectful(input_num: u64) u64 {
187+
return input_num;
188+
}

0 commit comments

Comments
 (0)