mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-04 12:18:19 +00:00
added map_try! + walk_try! test (#7770)
* map_try! + walk_try! test * update mono tests
This commit is contained in:
parent
e89bb6eb50
commit
882577bebc
51 changed files with 1510 additions and 1352 deletions
|
@ -61,14 +61,14 @@ mod cli_tests {
|
|||
.status()
|
||||
.unwrap();
|
||||
|
||||
let cli_build = ExecCli::new(
|
||||
let cli_dev = ExecCli::new(
|
||||
roc_cli::CMD_DEV,
|
||||
file_from_root("crates/cli/tests/platform-switching", "roc_loves_rust.roc"),
|
||||
);
|
||||
|
||||
let expected_output = "Roc <3 Rust!\n";
|
||||
|
||||
let output = cli_build.run();
|
||||
let output = cli_dev.run();
|
||||
|
||||
output.assert_clean_stdout(expected_output);
|
||||
}
|
||||
|
@ -1022,6 +1022,34 @@ mod cli_tests {
|
|||
|
||||
cli_build.check_build_and_run(expected_output, ALLOW_VALGRIND, None, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(windows, ignore)]
|
||||
fn effectful_walk_try() {
|
||||
build_platform_host();
|
||||
|
||||
let cli_dev = ExecCli::new(
|
||||
roc_cli::CMD_DEV,
|
||||
file_from_root("crates/cli/tests/test-projects/effectful", "walk_try.roc"),
|
||||
);
|
||||
|
||||
let cli_dev_out = cli_dev.run();
|
||||
cli_dev_out.assert_clean_success();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(windows, ignore)]
|
||||
fn effectful_map_try() {
|
||||
build_platform_host();
|
||||
|
||||
let cli_dev = ExecCli::new(
|
||||
roc_cli::CMD_DEV,
|
||||
file_from_root("crates/cli/tests/test-projects/effectful", "map_try.roc"),
|
||||
);
|
||||
|
||||
let cli_dev_out = cli_dev.run();
|
||||
cli_dev_out.assert_clean_success();
|
||||
}
|
||||
}
|
||||
|
||||
// this is for testing the benchmarks (on small inputs), to perform proper benchmarks see crates/cli/benches/README.md
|
||||
|
|
33
crates/cli/tests/test-projects/effectful/map_try.roc
Normal file
33
crates/cli/tests/test-projects/effectful/map_try.roc
Normal file
|
@ -0,0 +1,33 @@
|
|||
app [main!] { pf: platform "../test-platform-effects-zig/main.roc" }
|
||||
|
||||
import pf.Effect
|
||||
|
||||
main! : {} => {}
|
||||
main! = \{} ->
|
||||
sanity_check = err_if_zero!(1)
|
||||
expect sanity_check == Ok(1)
|
||||
|
||||
good = List.map_try!([1, 2, 3], |num| err_if_zero!(num))
|
||||
expect good == Ok([1, 2, 3])
|
||||
|
||||
good_empty = List.map_try!([], |num| err_if_zero!(num))
|
||||
expect good_empty == Ok([])
|
||||
|
||||
bad_a = List.map_try!([1, 2, 3, 0], |num| err_if_zero!(num))
|
||||
expect bad_a == Err({})
|
||||
|
||||
bad_b = List.map_try!([0, 1, 2, 3], |num| err_if_zero!(num))
|
||||
expect bad_b == Err({})
|
||||
|
||||
bad_c = List.map_try!([1, 0, 2, 3], |num| err_if_zero!(num))
|
||||
expect bad_c == Err({})
|
||||
|
||||
{}
|
||||
|
||||
err_if_zero! : U64 => Result U64 {}
|
||||
err_if_zero! = \num ->
|
||||
if num != 0 then
|
||||
Ok(Effect.id_effectful!(num))
|
||||
else
|
||||
_ = Effect.id_effectful!(num)
|
||||
Err({})
|
69
crates/cli/tests/test-projects/effectful/walk_try.roc
Normal file
69
crates/cli/tests/test-projects/effectful/walk_try.roc
Normal file
|
@ -0,0 +1,69 @@
|
|||
app [main!] { pf: platform "../test-platform-effects-zig/main.roc" }
|
||||
|
||||
import pf.Effect
|
||||
|
||||
main! : {} => {}
|
||||
main! = \{} ->
|
||||
# Sanity check for our test helper
|
||||
sanity_check = add_non_zero!(0, 1)
|
||||
expect sanity_check == Ok(1)
|
||||
|
||||
# Test successful walk_try! with non-zero elements
|
||||
good = List.walk_try!([1, 2, 3], 0, \sum, num -> add_non_zero!(sum, num))
|
||||
expect good == Ok(6)
|
||||
|
||||
# Test walk_try! with empty list
|
||||
good_empty = List.walk_try!([], 10, \sum, num -> add_non_zero!(sum, num))
|
||||
expect good_empty == Ok(10)
|
||||
|
||||
# Test walk_try! that encounters a zero at different positions
|
||||
bad_a = List.walk_try!([1, 2, 0, 3], 0, \sum, num -> add_non_zero!(sum, num))
|
||||
expect bad_a == Err({})
|
||||
|
||||
bad_b = List.walk_try!([0, 1, 2, 3], 0, \sum, num -> add_non_zero!(sum, num))
|
||||
expect bad_b == Err({})
|
||||
|
||||
bad_c = List.walk_try!([1, 0, 2, 3], 0, \sum, num -> add_non_zero!(sum, num))
|
||||
expect bad_c == Err({})
|
||||
|
||||
# Test more complex state accumulation
|
||||
# Accumulate pairs of [state, elem] but error on odd numbers
|
||||
complex_walk = List.walk_try!(
|
||||
[2, 4, 6, 8],
|
||||
[],
|
||||
\pairs, num ->
|
||||
if num % 2 == 0 then
|
||||
new_state = List.len(pairs) * 10
|
||||
result = Effect.id_effectful!(new_state)
|
||||
Ok(List.append(pairs, [result, num]))
|
||||
else
|
||||
_ = Effect.id_effectful!(num)
|
||||
Err({})
|
||||
)
|
||||
expect complex_walk == Ok([[0, 2], [10, 4], [20, 6], [30, 8]])
|
||||
|
||||
# Test complex state walk that fails
|
||||
complex_walk_fail = List.walk_try!(
|
||||
[2, 4, 5, 8],
|
||||
[],
|
||||
\pairs, num ->
|
||||
if num % 2 == 0 then
|
||||
new_state = List.len(pairs) * 10
|
||||
result = Effect.id_effectful!(new_state)
|
||||
Ok(List.append(pairs, [result, num]))
|
||||
else
|
||||
_ = Effect.id_effectful!(num)
|
||||
Err({})
|
||||
)
|
||||
expect complex_walk_fail == Err({})
|
||||
|
||||
{}
|
||||
|
||||
# Helper that returns Ok(sum) if num is non-zero, or Err({}) if num is zero
|
||||
add_non_zero! : U64, U64 => Result U64 {}
|
||||
add_non_zero! = \sum, num ->
|
||||
if num != 0 then
|
||||
Ok(sum + Effect.id_effectful!(num))
|
||||
else
|
||||
_ = Effect.id_effectful!(num)
|
||||
Err({})
|
|
@ -1,5 +1,7 @@
|
|||
hosted [put_line!, get_line!]
|
||||
hosted [put_line!, get_line!, id_effectful!]
|
||||
|
||||
put_line! : Str => {}
|
||||
|
||||
get_line! : {} => Str
|
||||
|
||||
id_effectful! : U64 => U64
|
||||
|
|
|
@ -181,3 +181,8 @@ fn roc_fx_get_int_help() !i64 {
|
|||
|
||||
return std.fmt.parseInt(i64, line, 10);
|
||||
}
|
||||
|
||||
/// Just return the input for testing purposes
|
||||
pub export fn roc_fx_id_effectful(input_num: u64) u64 {
|
||||
return input_num;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue