-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
78 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,92 @@ | ||
import "std"; | ||
import "debug"; | ||
|
||
test "ternary" { | ||
int value = if (true) 12 else 0; | ||
| test "ternary" { | ||
| int value = if (true) 12 else 0; | ||
|
||
assert(value == 12, message: "could use constant inline if"); | ||
| assert(value == 12, message: "could use constant inline if"); | ||
|
||
value = if ("hello".len() == 2) 0 else 12; | ||
| value = if ("hello".len() == 2) 0 else 12; | ||
|
||
assert(value == 12, message: "could use inline if"); | ||
} | ||
| assert(value == 12, message: "could use inline if"); | ||
| } | ||
|
||
test "multiple branches" { | ||
const value = 12; | ||
| test "multiple branches" { | ||
| const value = 12; | ||
|
||
const expr = if (value == 14) | ||
"hello" | ||
else if (value == 12) | ||
"yolo" | ||
else | ||
null; | ||
| const expr = if (value == 14) | ||
| "hello" | ||
| else if (value == 12) | ||
| "yolo" | ||
| else | ||
| null; | ||
|
||
assert(expr == "yolo", message: "Could use multiple branches with inline if"); | ||
} | ||
| assert(expr == "yolo", message: "Could use multiple branches with inline if"); | ||
| } | ||
|
||
test "unwrap" { | ||
str? opt = "hello"; | ||
| test "unwrap" { | ||
| str? opt = "hello"; | ||
|
||
const another = 12; | ||
| const _ = 12; | ||
|
||
const expr = if (opt -> hello) | ||
hello | ||
else | ||
"bye"; | ||
| const expr = if (opt -> hello) | ||
| hello | ||
| else | ||
| "bye"; | ||
|
||
assert(expr == "hello", message: "Could unwrap optional in inline if"); | ||
} | ||
| assert(expr == "hello", message: "Could unwrap optional in inline if"); | ||
| } | ||
|
||
test "cast" { | ||
const any unknown = "hello"; | ||
| test "cast" { | ||
| const any unknown = "hello"; | ||
|
||
const expr = if (unknown as str hello) | ||
hello | ||
else | ||
"bye"; | ||
| const expr = if (unknown as str hello) | ||
| hello | ||
| else | ||
| "bye"; | ||
|
||
assert(expr == "hello", message: "Could cast in inline if"); | ||
} | ||
| assert(expr == "hello", message: "Could cast in inline if"); | ||
| } | ||
|
||
test "cast to unwrap null" { | ||
const str? unknown = null; | ||
| test "cast to unwrap null" { | ||
| const str? unknown = null; | ||
|
||
const expr = if (unknown as str hello) | ||
hello | ||
else | ||
"hello"; | ||
| const expr = if (unknown as str hello) | ||
| hello | ||
| else | ||
| "hello"; | ||
|
||
assert(expr == "hello", message: "Could unwrap optional using cas in inline if"); | ||
} | ||
| assert(expr == "hello", message: "Could unwrap optional using cas in inline if"); | ||
| } | ||
|
||
test "cast to unwrap null" { | ||
const str? unknown = "hello"; | ||
| test "cast to unwrap null" { | ||
| const str? unknown = "hello"; | ||
|
||
const expr = if (unknown as str hello) | ||
hello | ||
else | ||
"bye"; | ||
| const expr = if (unknown as str hello) | ||
| hello | ||
| else | ||
| "bye"; | ||
|
||
assert(expr == "hello", message: "Could unwrap optional using cast in inline if"); | ||
} | ||
| assert(expr == "hello", message: "Could unwrap optional using cast in inline if"); | ||
| } | ||
|
||
test "not in a var decl" { | ||
str? opt = "hello"; | ||
|
||
const another = 12; | ||
const _ = 12; | ||
|
||
| assert( | ||
| (if (opt -> hello) | ||
| hello | ||
| else | ||
| "bye") == "hello", | ||
| message: "Could unwrap optional in inline if in expression" | ||
| ); | ||
|
||
assert( | ||
(if (opt -> hello) | ||
const condition = if (opt -> hello) | ||
hello | ||
else | ||
"bye") == "hello", | ||
message: "Could unwrap optional in inline if in expression" | ||
); | ||
"bye"; | ||
|
||
dump(condition); | ||
} |