aboutsummaryrefslogtreecommitdiff
path: root/gen/test/gleam@generic_test.erl
blob: eecf6501ce111311a100762cfb53f5e9d696268b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
-module(gleam@generic_test).
-compile(no_auto_import).

-export([identity_test/0, always_test/0, flip_test/0, compose_test/0]).

identity_test() ->
    gleam@expect:equal(gleam@generic:identity(1), 1).

always_test() ->
    gleam@expect:equal(gleam@generic:always(1, 2), 2).

flip_test() ->
    Fun = fun(String, Int) ->
        gleam@string:append(
            gleam@string:append(
                gleam@string:append(
                    gleam@string:append(<<"String: '">>, String),
                    <<"', Int: '">>
                ),
                gleam@int:to_string(Int)
            ),
            <<"'">>
        )
    end,
    FlippedFun = gleam@generic:flip(Fun),
    gleam@expect:equal(Fun(<<"Bob">>, 1), <<"String: 'Bob', Int: '1'">>),
    gleam@expect:equal(
        FlippedFun(2, <<"Alice">>),
        <<"String: 'Alice', Int: '2'">>
    ).

compose_test() ->
    AddTwo = fun(Int) -> Int + 2 end,
    AddThree = fun(Int1) -> Int1 + 3 end,
    AddFive = gleam@generic:compose(AddTwo, AddThree),
    gleam@expect:equal(AddFive(1), 6),
    HeadToString = gleam@generic:compose(
        fun gleam@list:head/1,
        fun(IntResult) ->
            gleam@int:to_string(gleam@result:unwrap(IntResult, 0))
        end
    ),
    gleam@expect:equal(HeadToString([1]), <<"1">>),
    gleam@expect:equal(HeadToString([]), <<"0">>).