Fork of daniellemaywood.uk/gleam — Wasm codegen work
Something went wrong. Try again.
9.6 kB · 627 lines
Rust
at wasm
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628// SPDX-License-Identifier: Apache-2.0// SPDX-FileCopyrightText: 2021 The Gleam contributors
use crate::assert_js;
#[test]fn referencing_pattern_var() { assert_js!( r#"pub fn main(xs) { case xs { #(x) if x -> 1 _ -> 0 }}"#, );}
#[test]fn rebound_var() { assert_js!( r#"pub fn main() { let x = False let x = True case x { _ if x -> 1 _ -> 0 }}"#, );}
#[test]fn bitarray_with_var() { assert_js!( r#"pub fn main() { case 5 { z if <<z>> == <<z>> -> Nil _ -> Nil }}"#, )}
// https://github.com/gleam-lang/gleam/issues/3004#[test]fn keyword_var() { assert_js!( r#"pub const function = 5pub const do = 10pub fn main() { let class = 5 let while = 10 let var = 7 case var { _ if class == while -> True _ if [class] == [5] -> True function if #(function) == #(5) -> False _ if do == function -> True while if while > 5 -> False class -> False }}"#, );}
#[test]fn operator_wrapping_right() { assert_js!( r#"pub fn main(xs, y: Bool, z: Bool) { case xs { #(x) if x == { y == z } -> 1 _ -> 0 }}"#, );}
#[test]fn operator_wrapping_left() { assert_js!( r#"pub fn main(xs, y: Bool, z: Bool) { case xs { #(x) if { x == y } == z -> 1 _ -> 0 }}"#, );}
#[test]fn eq_scalar() { assert_js!( r#"pub fn main(xs, y: Int) { case xs { #(x) if x == y -> 1 _ -> 0 }}"#, );}
#[test]fn not_eq_scalar() { assert_js!( r#"pub fn main(xs, y: Int) { case xs { #(x) if x != y -> 1 _ -> 0 }}"#, );}
#[test]fn tuple_index() { assert_js!( r#"pub fn main(x, xs: #(Bool, Bool, Bool)) { case x { _ if xs.2 -> 1 _ -> 0 }}"#, );}
#[test]fn not_eq_complex() { assert_js!( r#"pub fn main(xs, y) { case xs { #(x) if xs != y -> x _ -> 0 }}"#, );}
#[test]fn eq_complex() { assert_js!( r#"pub fn main(xs, y) { case xs { #(x) if xs == y -> x _ -> 0 }}"#, );}
#[test]fn constant() { assert_js!( r#"pub fn main(xs) { case xs { #(x) if x == 1 -> x _ -> 0 }}"#, );}
#[test]fn alternative_patterns() { assert_js!( r#"pub fn main(xs) { case xs { 1 | 2 -> 0 _ -> 1 }}"#, );}
#[test]fn alternative_patterns_list() { assert_js!( r#"pub fn main(xs) -> Int { case xs { [1] | [1, 2] -> 0 _ -> 1 }}"#, );}
#[test]fn alternative_patterns_assignment() { assert_js!( r#"pub fn main(xs) -> Int { case xs { [x] | [_, x] -> x _ -> 1 }}"#, );}
#[test]fn alternative_patterns_guard() { assert_js!( r#"pub fn main(xs) -> Int { case xs { [x] | [_, x] if x == 1 -> x _ -> 0 }}"#, );}
#[test]fn field_access() { assert_js!( r#" pub type Person { Person(username: String, name: String, age: Int) } pub fn main() { let given_name = "jack" let raiden = Person("raiden", "jack", 31) case given_name { name if name == raiden.name -> "It's jack" _ -> "It's not jack" } } "# )}
#[test]fn nested_record_access() { assert_js!( r#"pub type A { A(b: B)}
pub type B { B(c: C)}
pub type C { C(d: Bool)}
pub fn a(a: A) { case a { _ if a.b.c.d -> 1 _ -> 0 }}"# );}
#[test]fn module_string_access() { assert_js!( ( "package", "hero", r#" pub const ironman = "Tony Stark" "# ), r#" import hero pub fn main() { let name = "Tony Stark" case name { n if n == hero.ironman -> True _ -> False } } "# );}
#[test]fn module_list_access() { assert_js!( ( "package", "hero", r#" pub const heroes = ["Tony Stark", "Bruce Wayne"] "# ), r#" import hero pub fn main() { let names = ["Tony Stark", "Bruce Wayne"] case names { n if n == hero.heroes -> True _ -> False } } "# );}
#[test]fn module_tuple_access() { assert_js!( ( "package", "hero", r#" pub const hero = #("ironman", "Tony Stark") "# ), r#" import hero pub fn main() { let name = "Tony Stark" case name { n if n == hero.hero.1 -> True _ -> False } } "# );}
#[test]fn module_access() { assert_js!( ( "package", "hero", r#" pub type Hero { Hero(name: String) } pub const ironman = Hero("Tony Stark") "# ), r#" import hero pub fn main() { let name = "Tony Stark" case name { n if n == hero.ironman.name -> True _ -> False } } "# );}
#[test]fn module_access_submodule() { assert_js!( ( "package", "hero/submodule", r#" pub type Hero { Hero(name: String) } pub const ironman = Hero("Tony Stark") "# ), r#" import hero/submodule pub fn main() { let name = "Tony Stark" case name { n if n == submodule.ironman.name -> True _ -> False } } "# );}
#[test]fn module_access_aliased() { assert_js!( ( "package", "hero/submodule", r#" pub type Hero { Hero(name: String) } pub const ironman = Hero("Tony Stark") "# ), r#" import hero/submodule as myhero pub fn main() { let name = "Tony Stark" case name { n if n == myhero.ironman.name -> True _ -> False } } "# );}
#[test]fn module_nested_access() { assert_js!( ( "package", "hero", r#" pub type Person { Person(name: String) } pub type Hero { Hero(secret_identity: Person) } const bruce = Person("Bruce Wayne") pub const batman = Hero(bruce) "# ), r#" import hero pub fn main() { let name = "Bruce Wayne" case name { n if n == hero.batman.secret_identity.name -> True _ -> False } } "# );}
#[test]fn not() { assert_js!( r#"pub fn main(x, y) { case x { _ if !y -> 0 _ -> 1 }}"#, );}
#[test]fn not_two() { assert_js!( r#"pub fn main(x, y) { case x { _ if !y && !x -> 0 _ -> 1 }}"#, );}
#[test]fn custom_type_constructor_imported_and_aliased() { assert_js!( ("package", "other_module", "pub type T { A }"), r#"import other_module.{A as B}pub fn func() { case B { x if x == B -> True _ -> False }}"#, );}
#[test]fn imported_aliased_ok() { assert_js!( r#"import gleam.{Ok as Y}pub type X { Ok}pub fn func() { case Y { y if y == Y -> True _ -> False }}"#, );}
#[test]fn imported_ok() { assert_js!( r#"import gleampub type X { Ok}pub fn func(x) { case gleam.Ok { _ if [] == [ gleam.Ok ] -> True _ -> False }}"#, );}
// Variant of https://github.com/lpil/decode/pull/6#[test]fn constructor_function_in_guard() { assert_js!( r#"pub fn func(x) { case [] { _ if [] == [ Ok ] -> True _ -> False }} "#, );}
// https://github.com/gleam-lang/gleam/issues/4241#[test]fn int_division() { assert_js!( r#"pub fn main() { case 5 / 2 { x if x == 5 / 2 -> True _ -> False }}"#, );}
// https://github.com/gleam-lang/gleam/issues/4241#[test]fn float_division() { assert_js!( r#"pub fn main() { case 5.1 /. 0.0 { x if x == 5.1 /. 0.0 -> True _ -> False }}"#, );}
// https://github.com/gleam-lang/gleam/issues/4241#[test]fn int_remainder() { assert_js!( r#"pub fn main() { case 4 % 0 { x if x == 4 % 0 -> True _ -> False }}"#, );}
// https://github.com/gleam-lang/gleam/issues/5094#[test]fn guard_pattern_does_not_shadow_outer_scope() { assert_js!( r#"pub type Option(a) { Some(a) None}
pub type Container { Container(x: Option(Int))}
pub fn main() { let x: Option(Int) = Some(42) case Some(1) { Some(x) if x < 0 -> Container(None) _ -> { Container(x:) } }}"#, );}
// https://github.com/gleam-lang/gleam/issues/5214#[test]fn bit_array_referencing_shadowed_variable() { assert_js!( "pub fn main() { let a = 1 let a = 2
case Nil { _ if <<a>> == <<1>> -> False _ if <<a>> == <<2>> -> True _ -> False }}" );}