Skip to content

Commit

Permalink
Add test_gen tests for early returns
Browse files Browse the repository at this point in the history
  • Loading branch information
smores56 committed Oct 25, 2024
1 parent ca76212 commit 8a0cc10
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 2 deletions.
112 changes: 112 additions & 0 deletions crates/compiler/test_gen/src/gen_return.rs
Original file line number Diff line number Diff line change
@@ -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
);
}
1 change: 1 addition & 0 deletions crates/compiler/test_gen/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions crates/reporting/src/error/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,8 @@ fn to_expr_report<'b>(
alloc.reflow("But I need every "),
alloc.keyword("expect"),
alloc.reflow(" condition to evaluate to a "),
alloc.reflow("Bool—either "),
alloc.type_str("Bool"),
alloc.reflow("—either "),
alloc.tag("Bool.true".into()),
alloc.reflow(" or "),
alloc.tag("Bool.false".into()),
Expand Down Expand Up @@ -957,7 +958,8 @@ fn to_expr_report<'b>(
alloc.reflow("But I need every "),
alloc.keyword("if"),
alloc.reflow(" condition to evaluate to a "),
alloc.reflow("Bool—either "),
alloc.type_str("Bool"),
alloc.reflow("—either "),
alloc.tag("Bool.true".into()),
alloc.reflow(" or "),
alloc.tag("Bool.false".into()),
Expand Down Expand Up @@ -995,6 +997,7 @@ fn to_expr_report<'b>(
alloc.reflow("But I need every "),
alloc.keyword("if"),
alloc.reflow(" guard condition to evaluate to a "),
alloc.type_str("Bool"),
alloc.reflow("—either "),
alloc.tag("Bool.true".into()),
alloc.reflow(" or "),
Expand Down

0 comments on commit 8a0cc10

Please sign in to comment.