Fork of daniellemaywood.uk/gleam — Wasm codegen work
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2023 The Gleam contributors
3
4use crate::assert_format;
5
6#[test]
7fn field_access() {
8 assert_format!(
9 r#"pub fn main() {
10 case x {
11 _ if a.b -> 1
12 _ -> 0
13 }
14}
15"#
16 );
17}
18
19#[test]
20fn nested_field_access() {
21 assert_format!(
22 r#"pub fn main() {
23 case x {
24 _ if a.b.c.d -> 1
25 _ -> 0
26 }
27}
28"#
29 );
30}
31
32#[test]
33fn operators_in_guard() {
34 assert_format!(
35 r#"pub fn main() {
36 case list.map(codepoints, string.utf_codepoint_to_int) {
37 [drive, colon, slash]
38 if { slash == 47 || slash == 92 }
39 && colon == 58
40 && drive >= 65
41 && drive <= 90
42 || drive >= 97
43 && drive <= 122
44 -> {
45 1
46 |> 2
47 }
48 }
49}
50"#
51 );
52}
53
54#[test]
55fn commented_operators_in_guard() {
56 assert_format!(
57 r#"pub fn main() {
58 case list.map(codepoints, string.utf_codepoint_to_int) {
59 [drive, colon, slash]
60 if { slash == 47 || slash == 92 }
61 && colon == 58
62 // Hello
63 && drive >= 65
64 && drive <= 90
65 // This is a comment
66 || drive >= 97
67 // And another one
68 && drive <= 122
69 -> {
70 1
71 |> 2
72 }
73 }
74}
75"#
76 );
77}
78
79#[test]
80fn a_comment_before_a_guard_doesnt_force_it_to_break() {
81 assert_format!(
82 r#"pub fn main() {
83 case wibble {
84 // Apparently this comment breaks everything
85 _ if wobble -> Ok(state.newest)
86 }
87}
88"#
89 );
90}
91
92#[test]
93fn long_guard_with_alternative_patterns() {
94 assert_format!(
95 r#"pub fn main() {
96 case wibble {
97 Wibble(first_one)
98 | Wibble(another_one)
99 | Wibble(
100 this_is_extra_long_to_go_over_the_line_limit,
101 this_gets_broken_as_well,
102 )
103 if True
104 -> Ok(wibble)
105 }
106}
107"#
108 );
109}
110
111#[test]
112fn guard_block_is_not_removed_even_if_redundant() {
113 assert_format!(
114 r#"pub fn main() {
115 case todo {
116 _ if { True && True } && True -> 1
117 _ -> 2
118 }
119}
120"#
121 );
122}
123
124#[test]
125fn nested_guard_block_is_not_removed_even_if_redundant() {
126 assert_format!(
127 r#"pub fn main() {
128 case todo {
129 _ if { True && { True && False } } && True -> 1
130 _ -> 2
131 }
132}
133"#
134 );
135}