Fork of daniellemaywood.uk/gleam — Wasm codegen work
2

Configure Feed

Select the types of activity you want to include in your feed.

gleam / compiler-core / src / erlang / tests / patterns.rs
2.0 kB 127 lines
1// SPDX-License-Identifier: Apache-2.0 2// SPDX-FileCopyrightText: 2021 The Gleam contributors 3 4use crate::assert_erl; 5 6#[test] 7fn alternative_patterns() { 8 // reassigning name in alternative patterns 9 assert_erl!( 10 r#" 11pub fn main() { 12 let duplicate_name = 1 13 14 case 1 { 15 1 | 2 -> { 16 let duplicate_name = duplicate_name + 1 17 duplicate_name 18 } 19 _ -> 0 20 } 21}"# 22 ); 23} 24 25#[test] 26fn alternative_patterns1() { 27 // Alternative patterns with a clause containing vars 28 assert_erl!( 29 r#" 30pub fn main() { 31 case Ok(1) { 32 Ok(duplicate_name) | Error(duplicate_name) -> duplicate_name 33 } 34}"# 35 ); 36} 37 38#[test] 39fn alternative_patterns2() { 40 // Alternative patterns with a guard clause containing vars 41 assert_erl!( 42 r#" 43pub fn main() { 44 let duplicate_name = 1 45 46 case 1 { 47 1 | 2 if duplicate_name == 1 -> duplicate_name 48 _ -> 0 49 } 50}"# 51 ); 52} 53 54#[test] 55fn alternative_patterns3() { 56 assert_erl!( 57 r#" 58pub const constant = Ok(1) 59 60pub fn main(arg) { 61 let _ = constant 62 case arg { 63 _ if arg == constant -> 1 64 _ -> 0 65 } 66} 67"# 68 ); 69} 70 71#[test] 72fn pattern_as() { 73 assert_erl!( 74 "pub fn a(x) { 75 case x { 76 Ok(1 as y) -> 1 77 _ -> 0 78 } 79}" 80 ); 81} 82 83#[test] 84fn string_prefix_as_pattern_with_multiple_subjects() { 85 assert_erl!( 86 "pub fn a(x) { 87 case x, x { 88 _, \"a\" as a <> _ -> a 89 _, _ -> \"a\" 90 } 91}" 92 ); 93} 94 95#[test] 96fn string_prefix_as_pattern_with_multiple_subjects_and_guard() { 97 assert_erl!( 98 "pub fn a(x) { 99 case x, x { 100 _, \"a\" as a <> rest if rest == \"a\" -> a 101 _, _ -> \"a\" 102 } 103}" 104 ); 105} 106 107#[test] 108fn string_prefix_as_pattern_with_list() { 109 assert_erl!( 110 "pub fn a(x) { 111 case x { 112 [\"a\" as a <> _, \"b\" as b <> _] -> a <> b 113 _ -> \"\" 114 } 115}" 116 ); 117} 118 119#[test] 120fn string_prefix_as_pattern_with_assertion() { 121 assert_erl!( 122 "pub fn a(x) { 123 let assert \"a\" as a <> rest = \"wibble\" 124 a 125}" 126 ); 127}