Add test_gen tests for early returns

This commit is contained in:
Sam Mohr 2024-10-24 23:31:34 -07:00
parent ca762127e5
commit 8a0cc10c93
No known key found for this signature in database
GPG key ID: EA41D161A3C1BC99
3 changed files with 118 additions and 2 deletions

View file

@ -0,0 +1,112 @@
#![cfg(not(feature = "gen-wasm"))]
#[cfg(feature = "gen-llvm")]
use crate::helpers::llvm::assert_evals_to;
#[cfg(feature = "gen-dev")]
use crate::helpers::dev::assert_evals_to;
#[allow(unused_imports)]
use indoc::indoc;
#[allow(unused_imports)]
use roc_std::{RocList, RocResult, RocStr, I128, U128};
#[test]
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))]
fn early_return_nested_ifs() {
assert_evals_to!(
indoc!(
r#"
app "test" provides [main] to "./platform"
displayN = \n ->
first = Num.toStr n
second =
if n == 1 then
return "early 1"
else
third = Num.toStr (n + 1)
if n == 2 then
return "early 2"
else
third
"$(first), $(second)"
main : List Str
main = List.map [1, 2, 3] displayN
"#
),
RocList::from_slice(&[
RocStr::from("early 1"),
RocStr::from("early 2"),
RocStr::from("3, 4")
]),
RocList<RocStr>
);
}
#[test]
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))]
fn early_return_nested_whens() {
assert_evals_to!(
indoc!(
r#"
app "test" provides [main] to "./platform"
displayN = \n ->
first = Num.toStr n
second =
when n is
1 ->
return "early 1"
_ ->
third = Num.toStr (n + 1)
when n is
2 ->
return "early 2"
_ ->
third
"$(first), $(second)"
main : List Str
main = List.map [1, 2, 3] displayN
"#
),
RocList::from_slice(&[
RocStr::from("early 1"),
RocStr::from("early 2"),
RocStr::from("3, 4")
]),
RocList<RocStr>
);
}
#[test]
#[cfg(any(feature = "gen-llvm", feature = "gen-dev", feature = "gen-wasm"))]
fn early_return_solo() {
assert_evals_to!(
r#"
identity = \x ->
return x
identity "abc"
"#,
RocStr::from("abc"),
RocStr
);
assert_evals_to!(
r#"
identity = \x ->
return x
identity 123
"#,
123,
i64
);
}

View file

@ -16,6 +16,7 @@ pub mod gen_primitives;
pub mod gen_records;
pub mod gen_refcount;
pub mod gen_result;
pub mod gen_return;
pub mod gen_set;
pub mod gen_str;
pub mod gen_tags;