Fork of daniellemaywood.uk/gleam — Wasm codegen work
Something went wrong. Try again.
3.3 kB · 170 lines
Rust
at wasm
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171// SPDX-License-Identifier: Apache-2.0// SPDX-FileCopyrightText: 2022 The Gleam contributors
use crate::assert_js;
#[test]fn record_accessors() { // We can use record accessors for types with only one constructor assert_js!( r#"pub type Person { Person(name: String, age: Int) }pub fn get_age(person: Person) { person.age }pub fn get_name(person: Person) { person.name }"# );}
#[test]fn record_accessor_multiple_variants() { // We can access fields on custom types with multiple variants assert_js!( "pub type Person { Teacher(name: String, title: String) Student(name: String, age: Int)}pub fn get_name(person: Person) { person.name }" );}
#[test]fn record_accessor_multiple_variants_positions_other_than_first() { // We can access fields on custom types with multiple variants // In positions other than the 1st field assert_js!( "pub type Person { Teacher(name: String, age: Int, title: String) Student(name: String, age: Int)}pub fn get_name(person: Person) { person.name }pub fn get_age(person: Person) { person.age }" );}
#[test]fn record_accessor_multiple_with_first_position_different_types() { // We can access fields on custom types with multiple variants // In positions other than the 1st field assert_js!( "pub type Person { Teacher(name: Nil, age: Int) Student(name: String, age: Int)}pub fn get_age(person: Person) { person.age }" );}
#[test]fn record_accessor_multiple_variants_parameterised_types() { // We can access fields on custom types with multiple variants // In positions other than the 1st field assert_js!( "pub type Person { Teacher(name: String, age: List(Int), title: String) Student(name: String, age: List(Int))}pub fn get_name(person: Person) { person.name }pub fn get_age(person: Person) { person.age }" );}
// https://github.com/gleam-lang/gleam/issues/4603#[test]fn field_named_x0() { assert_js!( "pub type Wibble { Wibble(Int, x0: String)}" );}
#[test]fn field_named_then_is_escaped() { assert_js!( "pub type Wibble { Wibble(then: fn() -> Int)}" );}
#[test]fn field_named_constructor_is_escaped() { assert_js!( "pub type Wibble { Wibble(constructor: fn() -> Wibble)}" );}
#[test]fn field_named_prototype_is_escaped() { assert_js!( "pub type Wibble { Wibble(prototype: String)}" );}
#[test]fn const_record_update_generic_respecialization() { assert_js!( "pub type Box(a) { Box(name: String, value: a)}
pub const base = Box(\"score\", 50)pub const updated = Box(..base, value: \"Hello\")
pub fn main() { #(base, updated)}", );}
#[test]fn record_update_with_unlabelled_fields() { assert_js!( r#"pub type Wibble { Wibble(Int, Float, b: Bool, s: String)}
pub fn main() { let record = Wibble(1, 3.14, True, "Hello") Wibble(..record, b: False)}"# );}
#[test]fn constant_record_update_with_unlabelled_fields() { assert_js!( r#"pub type Wibble { Wibble(Int, Float, b: Bool, s: String)}
pub const record = Wibble(1, 3.14, True, "Hello")pub const updated = Wibble(..record, b: False)
pub fn main() { updated}"# );}