Fork of daniellemaywood.uk/gleam — Wasm codegen work
3.3 kB
170 lines
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2022 The Gleam contributors
3
4use crate::assert_js;
5
6#[test]
7fn record_accessors() {
8 // We can use record accessors for types with only one constructor
9 assert_js!(
10 r#"
11pub type Person { Person(name: String, age: Int) }
12pub fn get_age(person: Person) { person.age }
13pub fn get_name(person: Person) { person.name }
14"#
15 );
16}
17
18#[test]
19fn record_accessor_multiple_variants() {
20 // We can access fields on custom types with multiple variants
21 assert_js!(
22 "
23pub type Person {
24 Teacher(name: String, title: String)
25 Student(name: String, age: Int)
26}
27pub fn get_name(person: Person) { person.name }"
28 );
29}
30
31#[test]
32fn record_accessor_multiple_variants_positions_other_than_first() {
33 // We can access fields on custom types with multiple variants
34 // In positions other than the 1st field
35 assert_js!(
36 "
37pub type Person {
38 Teacher(name: String, age: Int, title: String)
39 Student(name: String, age: Int)
40}
41pub fn get_name(person: Person) { person.name }
42pub fn get_age(person: Person) { person.age }"
43 );
44}
45
46#[test]
47fn record_accessor_multiple_with_first_position_different_types() {
48 // We can access fields on custom types with multiple variants
49 // In positions other than the 1st field
50 assert_js!(
51 "
52pub type Person {
53 Teacher(name: Nil, age: Int)
54 Student(name: String, age: Int)
55}
56pub fn get_age(person: Person) { person.age }"
57 );
58}
59
60#[test]
61fn record_accessor_multiple_variants_parameterised_types() {
62 // We can access fields on custom types with multiple variants
63 // In positions other than the 1st field
64 assert_js!(
65 "
66pub type Person {
67 Teacher(name: String, age: List(Int), title: String)
68 Student(name: String, age: List(Int))
69}
70pub fn get_name(person: Person) { person.name }
71pub fn get_age(person: Person) { person.age }"
72 );
73}
74
75// https://github.com/gleam-lang/gleam/issues/4603
76#[test]
77fn field_named_x0() {
78 assert_js!(
79 "
80pub type Wibble {
81 Wibble(Int, x0: String)
82}
83"
84 );
85}
86
87#[test]
88fn field_named_then_is_escaped() {
89 assert_js!(
90 "
91pub type Wibble {
92 Wibble(then: fn() -> Int)
93}
94"
95 );
96}
97
98#[test]
99fn field_named_constructor_is_escaped() {
100 assert_js!(
101 "
102pub type Wibble {
103 Wibble(constructor: fn() -> Wibble)
104}
105"
106 );
107}
108
109#[test]
110fn field_named_prototype_is_escaped() {
111 assert_js!(
112 "
113pub type Wibble {
114 Wibble(prototype: String)
115}
116"
117 );
118}
119
120#[test]
121fn const_record_update_generic_respecialization() {
122 assert_js!(
123 "
124pub type Box(a) {
125 Box(name: String, value: a)
126}
127
128pub const base = Box(\"score\", 50)
129pub const updated = Box(..base, value: \"Hello\")
130
131pub fn main() {
132 #(base, updated)
133}
134",
135 );
136}
137
138#[test]
139fn record_update_with_unlabelled_fields() {
140 assert_js!(
141 r#"
142pub type Wibble {
143 Wibble(Int, Float, b: Bool, s: String)
144}
145
146pub fn main() {
147 let record = Wibble(1, 3.14, True, "Hello")
148 Wibble(..record, b: False)
149}
150"#
151 );
152}
153
154#[test]
155fn constant_record_update_with_unlabelled_fields() {
156 assert_js!(
157 r#"
158pub type Wibble {
159 Wibble(Int, Float, b: Bool, s: String)
160}
161
162pub const record = Wibble(1, 3.14, True, "Hello")
163pub const updated = Wibble(..record, b: False)
164
165pub fn main() {
166 updated
167}
168"#
169 );
170}