You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bat/tests/syntax-tests/highlighted/Erlang/bat_erlang.erl

73 lines
15 KiB
Erlang

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

-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.