1use crate::assert_js;
2
3#[test]
4fn record_accessors() {
5 // We can use record accessors for types with only one constructor
6 assert_js!(
7 r#"
8pub type Person { Person(name: String, age: Int) }
9pub fn get_age(person: Person) { person.age }
10pub fn get_name(person: Person) { person.name }
11"#
12 );
13}
14
15#[test]
16fn record_accessor_multiple_variants() {
17 // We can access fields on custom types with multiple variants
18 assert_js!(
19 "
20pub type Person {
21 Teacher(name: String, title: String)
22 Student(name: String, age: Int)
23}
24pub fn get_name(person: Person) { person.name }"
25 );
26}
27
28#[test]
29fn record_accessor_multiple_variants_positions_other_than_first() {
30 // We can access fields on custom types with multiple variants
31 // In positions other than the 1st field
32 assert_js!(
33 "
34pub type Person {
35 Teacher(name: String, age: Int, title: String)
36 Student(name: String, age: Int)
37}
38pub fn get_name(person: Person) { person.name }
39pub fn get_age(person: Person) { person.age }"
40 );
41}
42
43#[test]
44fn record_accessor_multiple_with_first_position_different_types() {
45 // We can access fields on custom types with multiple variants
46 // In positions other than the 1st field
47 assert_js!(
48 "
49pub type Person {
50 Teacher(name: Nil, age: Int)
51 Student(name: String, age: Int)
52}
53pub fn get_age(person: Person) { person.age }"
54 );
55}
56
57#[test]
58fn record_accessor_multiple_variants_parameterised_types() {
59 // We can access fields on custom types with multiple variants
60 // In positions other than the 1st field
61 assert_js!(
62 "
63pub type Person {
64 Teacher(name: String, age: List(Int), title: String)
65 Student(name: String, age: List(Int))
66}
67pub fn get_name(person: Person) { person.name }
68pub fn get_age(person: Person) { person.age }"
69 );
70}
71
72// https://github.com/gleam-lang/gleam/issues/4603
73#[test]
74fn field_named_x0() {
75 assert_js!(
76 "
77pub type Wibble {
78 Wibble(Int, x0: String)
79}
80"
81 );
82}