summaryrefslogtreecommitdiffstats
path: root/tests/syntax-tests/source/Erlang/bat_erlang.erl
blob: 77d3fffb7f945e631a9907640f4039ae6353c681 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
-module(bat_erlang).

-export([main/0]).

-record(test, {
    name :: list(),
    data :: binary()
}).

-define(TESTMACRO, "testmacro").

-spec main() -> ok.
main() ->
    %% Handling Lists and Numbers
    List = [1, 2, 3, 4, $6, 2#00111],
    _Sum = lists:sum(List),
    _ = [(N * N) + N / N - N || N <- List, N > 2],
    [_Head, _SecondHead | _Tail] = List,
    _ = [1, atom, [list], <<"binary">>, {tuple, tuple}, #{map => key}, #test{name = "record"}],

    %% Handling Binaries 
    BinHelloWorld = <<"Hello World">>,
    <<X || <<X:1/binary>> <= BinHelloWorld >>,
    <<0,0,0,0,0,0,0,151>> = <<151:64/signed-integer>>,

    %% Handling Boolean and Atoms
    true = true andalso true,
    true = false orelse true,
    _ = true =:= true,
    _ = false =/= true,
    _ = 'HELLO' /= hello,
    _ = hello == world,

    %% Handling Maps and Records
    TestMap = #{a => 1, b => 2, c => 3},
    #{a := _Value, c := _} = TestMap,
    _ = TestMap#{d => 4},
    Record = #test{name = ?TESTMACRO},
    _ = Record#test.name,

    %% Conditionals
    case TestMap of
        #{b := B} ->
            B;
        _ ->
            ok
    end,
    if
        erlang:is_map(TestMap) ->
            map;
        true ->
            test_function(1)
    end,

    %% Messaging
    Self = erlang:self(),
    Self ! hello_world,
    receive
        hello_world ->
            ok;
        _ ->
            io:format("unknown message")
    after 1000 ->
        timeout
    end,
    ok.

test_function(N) when erlang:is_integer(N) -> integer;
test_function([_|_]) -> list;
test_function(<<_/binary>>) ->  binary;
test_function(_) ->
    undefined.