Skip to content

Commit 829b947

Browse files
committed
test: update TS declarations test
Signed-off-by: Andrey <[email protected]>
1 parent 1350c61 commit 829b947

File tree

4 files changed

+174
-12
lines changed

4 files changed

+174
-12
lines changed

test/typescript_declarations/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ clean:
55
.PHONY: test
66
test:
77
cargo run --quiet -- build
8-
tsc ./build/dev/javascript/typescript_declarations/typescript_declarations.d.mts --noEmit --lib es2015
8+
tsc ./main.ts --noEmit --lib es2015,dom
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# typescript_declarations
22

3-
Check that generated TypeScript declarartions are correct. This requires TypeScript installed and be in PATH.
3+
Check that generated TypeScript declarartions are correct. This requires `tsc` installed and be in PATH.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import type { Option$ } from "./build/dev/javascript/gleam_stdlib/gleam/option.d.mts";
2+
import { List, Ok, Result } from "./build/dev/javascript/typescript_declarations/gleam.mjs";
3+
import * as Gleam from "./build/dev/javascript/typescript_declarations/typescript_declarations.mjs"
4+
5+
// Check add and add_alias
6+
const sum: number = Gleam.add_alias(Gleam.add(1, 2), 908);
7+
8+
// Check ID lists constants
9+
const count_of_moderators: number = Gleam.moders.countLength();
10+
const count_of_administrators: number = Gleam.admins.countLength();
11+
12+
// Check add_one closure
13+
const add_one: (_: number) => number = Gleam.add_one()
14+
const two: number = add_one(1)
15+
16+
// Check UserId type alias
17+
const first_moderator: Gleam.UserId = Gleam.moders[ 0];
18+
const first_moderator_num: number = first_moderator;
19+
const me: UserID = 84738;
20+
type UserID = Gleam.UserId;
21+
22+
// Check is_divisible_by
23+
const is_five_divisible_by_two: boolean = Gleam.is_divisible_by(5, 2);
24+
25+
// Check extern alert function if we are in browser target
26+
if (typeof window != undefined) {
27+
Gleam.js_alert("Hello!");
28+
}
29+
30+
// Check generic twice function
31+
const twice_number: number = Gleam.twice(45, add_one);
32+
33+
// Check recursive sum_list
34+
const sum_of_list: number = Gleam.sum_list(List.fromArray([10, 25, 65383, 8910, 1893]), 0);
35+
36+
// Check results
37+
const result_ok: Result<number, number> = new Ok(10);
38+
const is_ok: boolean = Gleam.is_ok_result(result_ok);
39+
40+
// Check name_description tuple
41+
const name: string = Gleam.name_description[0];
42+
const description: string = Gleam.name_description[1];
43+
44+
// Check User and related functions
45+
const user: Gleam.User$ = new Gleam.User("King", 83874, new Gleam.PlainUser());
46+
const guest: Gleam.User$ = new Gleam.Guest();
47+
const user_username: Option$<string> = Gleam.user_name(user);
48+
const guest_username: Option$<string> = Gleam.user_name(guest);
49+
const plain_user_string: string = Gleam.role_string(user.withFields["role"]);
50+
51+
// Check Either type
52+
const left_either: Gleam.Either$<string, number> = new Gleam.Left("Hello!");
53+
const right_either: Gleam.Either$<string, number> = new Gleam.Right(3747);
54+
55+
// This added since TypeScript will give warnings about unused things otherwise
56+
void [
57+
sum,
58+
count_of_administrators,
59+
count_of_moderators,
60+
add_one,
61+
two,
62+
first_moderator,
63+
first_moderator_num,
64+
me,
65+
is_five_divisible_by_two,
66+
twice_number,
67+
sum_of_list,
68+
result_ok,
69+
is_ok,
70+
name,
71+
description,
72+
user,
73+
guest,
74+
user_username,
75+
guest_username,
76+
plain_user_string,
77+
left_either,
78+
right_either,
79+
]
Lines changed: 93 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,102 @@
1-
//// Test TS declarartions
1+
import gleam/option.{type Option}
2+
import gleam/result
23

3-
/// Answer for all questions in the world
4-
pub const answer = 256
4+
/// Greeting for user
5+
pub const greeting: String = "Hello, dear user!"
56

7+
/// Add two numbers
8+
pub fn add(first a: Int, second b: Int) -> Int {
9+
a + b
10+
}
11+
12+
/// Returns true if number is divisible by another
13+
pub fn is_divisible_by(number num: Int, divider div: Int) -> Bool {
14+
case num % div {
15+
0 -> True
16+
_ -> False
17+
}
18+
}
19+
20+
/// ID of specific user
21+
pub type UserId =
22+
Int
23+
24+
/// Alias to add function
25+
pub const add_alias = add
26+
27+
/// Return function that will add one to given number
28+
pub fn add_one() -> fn(Int) -> Int {
29+
let func = add(1, _)
30+
func
31+
}
32+
33+
/// IDs of administrators
34+
pub const admins: List(UserId) = [00_010, 00_000, 00_001, 00_002, 29_373]
35+
36+
/// IDs of moderators
37+
pub const moders = [01_013, 36_371, 74_839, 18_930, 36_373]
38+
39+
/// Create alert on browser target
40+
@external(javascript, "globalThis", "alert")
41+
pub fn js_alert(text: String) -> Nil
42+
43+
/// Twice value via given function
44+
pub fn twice(val: a, function: fn(a) -> a) -> a {
45+
function(function(val))
46+
}
47+
48+
/// Recursively sum list of numbers
49+
pub fn sum_list(list: List(Int), total: Int) -> Int {
50+
case list {
51+
[first, ..rest] -> sum_list(rest, total + first)
52+
[] -> total
53+
}
54+
}
55+
56+
/// Returns true if result is ok
57+
pub fn is_ok_result(res: Result(a, b)) -> Bool {
58+
result.is_ok(res)
59+
}
60+
61+
/// First value is name and second is description
62+
pub const name_description = #("MyApp", "My Awesome Application")
63+
64+
/// Role of user
65+
pub type UserRole {
66+
/// Administrator
67+
Administrator
68+
/// Moderator
69+
Moderator
70+
/// Just a user
71+
PlainUser
72+
}
73+
74+
/// Represents user
675
pub type User {
7-
// Registered user
8-
User(name: String, email: String)
9-
/// Unregistered guest
76+
/// Represents registered user
77+
User(username: String, id: UserId, role: UserRole)
78+
/// Represents guest without any data
1079
Guest
1180
}
1281

13-
/// Get name of user. Returns "Guest" if user is guest
14-
pub fn get_name(user) {
82+
/// Get name of user, None if user is guest
83+
pub fn user_name(user: User) -> Option(String) {
1584
case user {
16-
User(name, ..) -> name
17-
Guest -> "Guest"
85+
User(name, ..) -> option.Some(name)
86+
Guest -> option.None
87+
}
88+
}
89+
90+
/// Format user role as string
91+
pub fn role_string(role: UserRole) -> String {
92+
case role {
93+
PlainUser -> "user"
94+
Moderator -> "moderator"
95+
Administrator -> "admin"
1896
}
1997
}
98+
99+
pub type Either(a, b) {
100+
Left(a)
101+
Right(b)
102+
}

0 commit comments

Comments
 (0)