diff options
author | Louis Pilfold <louis@lpil.uk> | 2019-04-14 13:03:24 +0000 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2019-04-14 13:03:32 +0000 |
commit | 8cfa606f3834cf7d05f5011bc68295a9d84263dc (patch) | |
tree | fa7edc8ea53426b072f35d3d61e0ab821c8d5feb /gen | |
parent | ae5597c1b27982aabe74eb16d5b0c890802730d9 (diff) | |
download | gleam_stdlib-8cfa606f3834cf7d05f5011bc68295a9d84263dc.tar.gz gleam_stdlib-8cfa606f3834cf7d05f5011bc68295a9d84263dc.zip |
stdlib: Split out tests
Diffstat (limited to 'gen')
35 files changed, 1070 insertions, 1173 deletions
diff --git a/gen/any.erl b/gen/any.erl deleted file mode 100644 index aa59a22..0000000 --- a/gen/any.erl +++ /dev/null @@ -1,138 +0,0 @@ --module(any). --compile(no_auto_import). --include_lib("eunit/include/eunit.hrl"). - --export([from/1, unsafeCoerce/1, string/1, int/1, float/1, bool/1, thunk/1, list/2, tuple/1, field/2]). - -list_module() -> - list. - -tuple_module() -> - tuple. - -from(A) -> - gleam__stdlib:identity(A). - -unsafeCoerce(A) -> - gleam__stdlib:identity(A). - -string(A) -> - gleam__stdlib:decode_string(A). - --ifdef(TEST). -string_test() -> - expect:equal(string(from(<<"">>)), {ok, <<"">>}), - expect:equal(string(from(<<"Hello">>)), {ok, <<"Hello">>}), - expect:equal(string(from(1)), {error, <<"Expected a String, got `1`">>}), - expect:equal(string(from([])), {error, <<"Expected a String, got `[]`">>}). --endif. - -int(A) -> - gleam__stdlib:decode_int(A). - --ifdef(TEST). -int_test() -> - expect:equal(int(from(1)), {ok, 1}), - expect:equal(int(from(2)), {ok, 2}), - expect:equal(int(from(1.0)), {error, <<"Expected an Int, got `1.0`">>}), - expect:equal(int(from([])), {error, <<"Expected an Int, got `[]`">>}). --endif. - -float(A) -> - gleam__stdlib:decode_float(A). - --ifdef(TEST). -float_test() -> - expect:equal(float(from(1.0)), {ok, 1.0}), - expect:equal(float(from(2.2)), {ok, 2.2}), - expect:equal(float(from(1)), {error, <<"Expected a Float, got `1`">>}), - expect:equal(float(from([])), {error, <<"Expected a Float, got `[]`">>}). --endif. - -bool(A) -> - gleam__stdlib:decode_bool(A). - --ifdef(TEST). -bool_test() -> - expect:equal(bool(from(true)), {ok, true}), - expect:equal(bool(from(false)), {ok, false}), - expect:equal(bool(from(1)), {error, <<"Expected a Bool, got `1`">>}), - expect:equal(bool(from([])), {error, <<"Expected a Bool, got `[]`">>}). --endif. - -thunk(A) -> - gleam__stdlib:decode_thunk(A). - --ifdef(TEST). -thunk_test() -> - expect:is_ok(thunk(from(fun() -> 1 end))), - expect:equal(result:map(thunk(from(fun() -> 1 end)), fun(F) -> F() end), - {ok, from(1)}), - expect:is_error(thunk(from(fun(X) -> X end))), - expect:is_error(thunk(from(1))), - expect:is_error(thunk(from([]))). --endif. - -list_any(A) -> - gleam__stdlib:decode_list(A). - -list(Any, Decode) -> - result:then(list_any(Any), - fun(Capture1) -> - (list_module()):traverse(Capture1, Decode) - end). - --ifdef(TEST). -list_test() -> - expect:equal(list(from([]), fun string/1), {ok, []}), - expect:equal(list(from([]), fun int/1), {ok, []}), - expect:equal(list(from([1, 2, 3]), fun int/1), {ok, [1, 2, 3]}), - expect:equal(list(from([[1], [2], [3]]), - fun(Capture1) -> list(Capture1, fun int/1) end), - {ok, [[1], [2], [3]]}), - expect:is_error(list(from(1), fun string/1)), - expect:is_error(list(from(1.0), fun int/1)), - expect:is_error(list(from([<<"">>]), fun int/1)), - expect:is_error(list(from([from(1), from(<<"not an int">>)]), fun int/1)). --endif. - -tuple(A) -> - gleam__stdlib:decode_tuple(A). - --ifdef(TEST). -tuple_test() -> - expect:equal(tuple(from({1, []})), {ok, {from(1), from([])}}), - expect:equal(tuple(from({<<"ok">>, <<"ok">>})), - {ok, {from(<<"ok">>), from(<<"ok">>)}}), - expect:is_error(tuple(from({1}))), - expect:is_error(tuple(from({1, 2, 3}))), - expect:equal(result:then(result:then(tuple(from({1, 2.0})), - fun(X) -> - result:map(int((tuple_module()):first(X)), - fun(F) -> - {F, - (tuple_module()):second(X)} - end) - end), - fun(X) -> - result:map(float((tuple_module()):second(X)), - fun(F) -> - {(tuple_module()):first(X), F} - end) - end), - {ok, {1, 2.0}}). --endif. - -field(A, B) -> - gleam__stdlib:decode_field(A, B). - --ifdef(TEST). -field_test() -> - {ok, OkAtom} = atom:from_string(<<"ok">>), - expect:equal(field(from(#{}#{ok => 1}), OkAtom), {ok, from(1)}), - expect:equal(field(from(#{}#{ok => 3}#{earlier => 2}), OkAtom), - {ok, from(3)}), - expect:is_error(field(from(#{}), OkAtom)), - expect:is_error(field(from(1), OkAtom)), - expect:is_error(field(from([]), [])). --endif. diff --git a/gen/atom.erl b/gen/atom.erl deleted file mode 100644 index 70c899b..0000000 --- a/gen/atom.erl +++ /dev/null @@ -1,38 +0,0 @@ --module(atom). --compile(no_auto_import). --include_lib("eunit/include/eunit.hrl"). - --export([from_string/1, create_from_string/1, to_string/1]). - -from_string(A) -> - gleam__stdlib:atom_from_string(A). - --ifdef(TEST). -from_string_test() -> - expect:is_ok(from_string(<<"ok">>)), - expect:is_ok(from_string(<<"expect">>)), - expect:equal(from_string(<<"this is not an atom we have seen before">>), - {error, atom_not_loaded}). --endif. - -create_from_string(A) -> - gleam__stdlib:atom_create_from_string(A). - --ifdef(TEST). -create_from_string_test() -> - Ok = fun(X) -> {ok, X} end, - expect:equal(Ok(create_from_string(<<"ok">>)), from_string(<<"ok">>)), - expect:equal(Ok(create_from_string(<<"expect">>)), - from_string(<<"expect">>)), - expect:equal(Ok(create_from_string(<<"this is another atom we have not seen before">>)), - from_string(<<"this is another atom we have not seen before">>)). --endif. - -to_string(A) -> - gleam__stdlib:atom_to_string(A). - --ifdef(TEST). -to_string_test() -> - expect:equal(to_string(create_from_string(<<"ok">>)), <<"ok">>), - expect:equal(to_string(create_from_string(<<"expect">>)), <<"expect">>). --endif. diff --git a/gen/bool.erl b/gen/bool.erl deleted file mode 100644 index a02edc8..0000000 --- a/gen/bool.erl +++ /dev/null @@ -1,69 +0,0 @@ --module(bool). --compile(no_auto_import). --include_lib("eunit/include/eunit.hrl"). - --export([negate/1, max/2, min/2, to_int/1]). - -negate(Bool) -> - case Bool of - true -> - false; - - false -> - true - end. - --ifdef(TEST). -negate_test() -> - expect:false(negate(true)), - expect:true(negate(false)). --endif. - -max(A, B) -> - case A of - true -> - true; - - false -> - B - end. - --ifdef(TEST). -max_test() -> - expect:equal(max(true, true), true), - expect:equal(max(true, false), true), - expect:equal(max(false, false), false), - expect:equal(max(false, true), true). --endif. - -min(A, B) -> - case A of - false -> - false; - - true -> - B - end. - --ifdef(TEST). -min_test() -> - expect:equal(min(true, true), true), - expect:equal(min(true, false), false), - expect:equal(min(false, false), false), - expect:equal(min(false, true), false). --endif. - -to_int(Bool) -> - case Bool of - false -> - 0; - - true -> - 1 - end. - --ifdef(TEST). -to_int_test() -> - expect:equal(to_int(true), 1), - expect:equal(to_int(false), 0). --endif. diff --git a/gen/iodata.erl b/gen/iodata.erl deleted file mode 100644 index 3401d3f..0000000 --- a/gen/iodata.erl +++ /dev/null @@ -1,114 +0,0 @@ --module(iodata). --compile(no_auto_import). --include_lib("eunit/include/eunit.hrl"). - --export([prepend/2, append/2, prepend_iodata/2, append_iodata/2, from_strings/1, concat/1, new/1, to_string/1, byte_size/1, from_float/1, lowercase/1, uppercase/1, reverse/1, split/2, replace/3, is_equal/2, is_empty/1]). - -prepend(A, B) -> - gleam__stdlib:iodata_prepend(A, B). - -append(A, B) -> - gleam__stdlib:iodata_append(A, B). - -prepend_iodata(A, B) -> - gleam__stdlib:iodata_prepend(A, B). - -append_iodata(A, B) -> - gleam__stdlib:iodata_append(A, B). - -from_strings(A) -> - gleam__stdlib:identity(A). - -concat(A) -> - gleam__stdlib:identity(A). - -new(A) -> - gleam__stdlib:identity(A). - -to_string(A) -> - erlang:iolist_to_binary(A). - -byte_size(A) -> - erlang:iolist_size(A). - -from_float(A) -> - io_lib_format:fwrite_g(A). - --ifdef(TEST). -iodata_test() -> - Iodata = prepend(append(append(new(<<"ello">>), <<",">>), <<" world!">>), - <<"H">>), - expect:equal(to_string(Iodata), <<"Hello, world!">>), - expect:equal(byte_size(Iodata), 13), - Iodata1 = prepend_iodata(append_iodata(append_iodata(new(<<"ello">>), - new(<<",">>)), - concat([new(<<" wo">>), - new(<<"rld!">>)])), - new(<<"H">>)), - expect:equal(to_string(Iodata1), <<"Hello, world!">>), - expect:equal(byte_size(Iodata1), 13). --endif. - -lowercase(A) -> - string:lowercase(A). - --ifdef(TEST). -lowercase_test() -> - expect:equal(to_string(lowercase(from_strings([<<"Gleam">>, <<"Gleam">>]))), - <<"gleamgleam">>). --endif. - -uppercase(A) -> - string:uppercase(A). - --ifdef(TEST). -uppercase_test() -> - expect:equal(to_string(uppercase(from_strings([<<"Gleam">>, <<"Gleam">>]))), - <<"GLEAMGLEAM">>). --endif. - -reverse(A) -> - string:reverse(A). - -erl_split(A, B, C) -> - string:split(A, B, C). - -split(Iodata, On) -> - erl_split(Iodata, On, all). - --ifdef(TEST). -split_test() -> - expect:equal(split(new(<<"Gleam,Erlang,Elixir">>), <<",">>), - [new(<<"Gleam">>), new(<<"Erlang">>), new(<<"Elixir">>)]), - expect:equal(split(from_strings([<<"Gleam, Erl">>, <<"ang,Elixir">>]), - <<", ">>), - [new(<<"Gleam">>), - from_strings([<<"Erl">>, <<"ang,Elixir">>])]). --endif. - -erl_replace(A, B, C, D) -> - string:replace(A, B, C, D). - -replace(Iodata, Pattern, Replacement) -> - erl_replace(Iodata, Pattern, Replacement, all). - -is_equal(A, B) -> - string:equal(A, B). - --ifdef(TEST). -is_equal_test() -> - expect:true(is_equal(new(<<"12">>), from_strings([<<"1">>, <<"2">>]))), - expect:true(is_equal(new(<<"12">>), new(<<"12">>))), - expect:false(is_equal(new(<<"12">>), new(<<"2">>))). --endif. - -is_empty(A) -> - string:is_empty(A). - --ifdef(TEST). -is_empty_test() -> - expect:true(is_empty(new(<<"">>))), - expect:false(is_empty(new(<<"12">>))), - expect:true(is_empty(from_strings([]))), - expect:true(is_empty(from_strings([<<"">>, <<"">>]))). --endif. diff --git a/gen/list.erl b/gen/list.erl deleted file mode 100644 index 1567b98..0000000 --- a/gen/list.erl +++ /dev/null @@ -1,295 +0,0 @@ --module(list). --compile(no_auto_import). --include_lib("eunit/include/eunit.hrl"). - --export([length/1, reverse/1, is_empty/1, has_member/2, head/1, tail/1, filter/2, map/2, traverse/2, drop/2, take/2, new/0, append/2, flatten/1, foldl/3, foldr/3, find/2]). - -length(A) -> - erlang:length(A). - --ifdef(TEST). -length_test() -> - expect:equal(length([]), 0), - expect:equal(length([1]), 1), - expect:equal(length([1, 1]), 2), - expect:equal(length([1, 1, 1]), 3). --endif. - -reverse(A) -> - lists:reverse(A). - --ifdef(TEST). -reverse_test() -> - expect:equal(length([]), 0), - expect:equal(length([1, 2, 3, 4, 5]), 5). --endif. - -is_empty(List) -> - List =:= []. - --ifdef(TEST). -is_empty_test() -> - expect:true(is_empty([])), - expect:false(is_empty([1])). --endif. - -has_member(List, Elem) -> - case List of - [] -> - false; - - [Head | Rest] -> - Head =:= Elem orelse has_member(Rest, Elem) - end. - --ifdef(TEST). -has_member_test() -> - expect:true(has_member([0, 4, 5, 1], 1)), - expect:false(has_member([0, 4, 5, 7], 1)), - expect:false(has_member([], 1)). --endif. - -head(List) -> - case List of - [] -> - {error, empty}; - - [X | _] -> - {ok, X} - end. - --ifdef(TEST). -head_test() -> - expect:equal(head([0, 4, 5, 7]), {ok, 0}), - expect:equal(head([]), {error, empty}). --endif. - -tail(List) -> - case List of - [] -> - {error, empty}; - - [_ | Xs] -> - {ok, Xs} - end. - --ifdef(TEST). -tail_test() -> - expect:equal(tail([0, 4, 5, 7]), {ok, [4, 5, 7]}), - expect:equal(tail([0]), {ok, []}), - expect:equal(tail([]), {error, empty}). --endif. - -do_filter(List, Fun, Acc) -> - case List of - [] -> - reverse(Acc); - - [X | Xs] -> - NewAcc = case Fun(X) of - true -> - [X | Acc]; - - false -> - Acc - end, - do_filter(Xs, Fun, NewAcc) - end. - -filter(List, Fun) -> - do_filter(List, Fun, []). - --ifdef(TEST). -filter_test() -> - expect:equal(filter([], fun(_) -> true end), []), - expect:equal(filter([0, 4, 5, 7, 3], fun(_) -> true end), [0, 4, 5, 7, 3]), - expect:equal(filter([0, 4, 5, 7, 3], fun(X) -> X > 4 end), [5, 7]), - expect:equal(filter([0, 4, 5, 7, 3], fun(X) -> X < 4 end), [0, 3]). --endif. - -do_map(List, Fun, Acc) -> - case List of - [] -> - reverse(Acc); - - [X | Xs] -> - do_map(Xs, Fun, [Fun(X) | Acc]) - end. - -map(List, Fun) -> - do_map(List, Fun, []). - --ifdef(TEST). -map_test() -> - expect:equal(map([], fun(X) -> X * 2 end), []), - expect:equal(map([0, 4, 5, 7, 3], fun(X) -> X * 2 end), [0, 8, 10, 14, 6]). --endif. - -do_traverse(List, Fun, Acc) -> - case List of - [] -> - {ok, reverse(Acc)}; - - [X | Xs] -> - case Fun(X) of - {ok, Y} -> - do_traverse(Xs, Fun, [Y | Acc]); - - {error, Error} -> - {error, Error} - end - end. - -traverse(List, Fun) -> - do_traverse(List, Fun, []). - --ifdef(TEST). -traverse_test() -> - Fun = fun(X) -> case X =:= 6 orelse X =:= 5 orelse X =:= 4 of - true -> - {ok, X * 2}; - - false -> - {error, X} - end end, - expect:equal(traverse([5, 6, 5, 6], Fun), {ok, [10, 12, 10, 12]}), - expect:equal(traverse([4, 6, 5, 7, 3], Fun), {error, 7}). --endif. - -drop(List, N) -> - case N =< 0 of - true -> - List; - - false -> - case List of - [] -> - []; - - [_ | Xs] -> - drop(Xs, N - 1) - end - end. - --ifdef(TEST). -drop_test() -> - expect:equal(drop([], 5), []), - expect:equal(drop([1, 2, 3, 4, 5, 6, 7, 8], 5), [6, 7, 8]). --endif. - -do_take(List, N, Acc) -> - case N =< 0 of - true -> - reverse(Acc); - - false -> - case List of - [] -> - reverse(Acc); - - [X | Xs] -> - do_take(Xs, N - 1, [X | Acc]) - end - end. - -take(List, N) -> - do_take(List, N, []). - --ifdef(TEST). -take_test() -> - expect:equal(take([], 5), []), - expect:equal(take([1, 2, 3, 4, 5, 6, 7, 8], 5), [1, 2, 3, 4, 5]). --endif. - -new() -> - []. - --ifdef(TEST). -new_test() -> - expect:equal(new(), []). --endif. - -append(A, B) -> - lists:append(A, B). - --ifdef(TEST). -append_test() -> - expect:equal(append([1], [2, 3]), [1, 2, 3]). --endif. - -do_flatten(Lists, Acc) -> - case Lists of - [] -> - Acc; - - [L | Rest] -> - do_flatten(Rest, append(Acc, L)) - end. - -flatten(Lists) -> - do_flatten(Lists, []). - --ifdef(TEST). -flatten_test() -> - expect:equal(flatten([]), []), - expect:equal(flatten([[]]), []), - expect:equal(flatten([[], [], []]), []), - expect:equal(flatten([[1, 2], [], [3, 4]]), [1, 2, 3, 4]). --endif. - -foldl(List, Acc, Fun) -> - case List of - [] -> - Acc; - - [X | Rest] -> - foldl(Rest, Fun(X, Acc), Fun) - end. - --ifdef(TEST). -foldl_test() -> - expect:equal(foldl([1, 2, 3], [], fun(X, Acc) -> [X | Acc] end), [3, 2, 1]). --endif. - -foldr(List, Acc, Fun) -> - case List of - [] -> - Acc; - - [X | Rest] -> - Fun(X, foldr(Rest, Acc, Fun)) - end. - --ifdef(TEST). -foldr_test() -> - expect:equal(foldr([1, 2, 3], [], fun(X, Acc) -> [X | Acc] end), [1, 2, 3]). --endif. - -find(Haystack, F) -> - case Haystack of - [] -> - {error, not_found}; - - [X | Rest] -> - case F(X) of - {ok, X1} -> - {ok, X1}; - - _ -> - find(Rest, F) - end - end. - --ifdef(TEST). -find_test() -> - F = fun(X) -> case X of - 2 -> - {ok, 4}; - - _ -> - {error, not_found} - end end, - expect:equal(find([1, 2, 3], F), {ok, 4}), - expect:equal(find([1, 3, 2], F), {ok, 4}), - expect:equal(find([1, 3], F), {error, not_found}). --endif. diff --git a/gen/map.erl b/gen/map.erl deleted file mode 100644 index ef57f07..0000000 --- a/gen/map.erl +++ /dev/null @@ -1,105 +0,0 @@ --module(map). --compile(no_auto_import). --include_lib("eunit/include/eunit.hrl"). - --export([size/1, to_list/1, from_list/1, has_key/2, new/0, fetch/2, put/3, map_values/2, keys/1, values/1, filter/2]). - -size(A) -> - maps:size(A). - -to_list(A) -> - maps:to_list(A). - -from_list(A) -> - maps:from_list(A). - --ifdef(TEST). -from_list_test() -> - Proplist = [{4, 0}, {1, 0}], - Map = from_list(Proplist), - expect:equal(size(Map), 2). --endif. - -is_key(A, B) -> - maps:is_key(A, B). - -has_key(Map, Key) -> - is_key(Key, Map). - --ifdef(TEST). -has_key_test() -> - expect:false(has_key(from_list([]), 1)), - expect:true(has_key(from_list([{1, 0}]), 1)), - expect:true(has_key(from_list([{4, 0}, {1, 0}]), 1)), - expect:false(has_key(from_list([{4, 0}, {1, 0}]), 0)). --endif. - -new() -> - maps:new(). - --ifdef(TEST). -new_test() -> - expect:equal(size(new()), 0), - expect:equal(to_list(new()), []). --endif. - -fetch(A, B) -> - gleam__stdlib:map_fetch(A, B). - --ifdef(TEST). -fetch_test() -> - Proplist = [{4, 0}, {1, 1}], - Map = from_list(Proplist), - expect:equal(fetch(Map, 4), {ok, 0}), - expect:equal(fetch(Map, 1), {ok, 1}), - expect:equal(fetch(Map, 2), {error, not_found}). --endif. - -erl_put(A, B, C) -> - maps:put(A, B, C). - -put(Map, Key, Value) -> - erl_put(Key, Value, Map). - --ifdef(TEST). -put_test() -> - expect:equal(put(put(put(new(), <<"a">>, 0), <<"b">>, 1), <<"c">>, 2), - from_list([{<<"a">>, 0}, {<<"b">>, 1}, {<<"c">>, 2}])). --endif. - -erl_map_values(A, B) -> - maps:map(A, B). - -map_values(Map, Fun) -> - erl_map_values(Fun, Map). - --ifdef(TEST). -map_values_test() -> - expect:equal(map_values(from_list([{1, 0}, {2, 1}, {3, 2}]), - fun(K, V) -> K + V end), - from_list([{1, 1}, {2, 3}, {3, 5}])). --endif. - -keys(A) -> - maps:keys(A). - --ifdef(TEST). -keys_test() -> - expect:equal(keys(from_list([{<<"a">>, 0}, {<<"b">>, 1}, {<<"c">>, 2}])), - [<<"a">>, <<"b">>, <<"c">>]). --endif. - -values(A) -> - maps:values(A). - --ifdef(TEST). -values_test() -> - expect:equal(values(from_list([{<<"a">>, 0}, {<<"b">>, 1}, {<<"c">>, 2}])), - [0, 1, 2]). --endif. - -erl_filter(A, B) -> - maps:filter(A, B). - -filter(Map, Fun) -> - erl_filter(Fun, Map). diff --git a/gen/order.erl b/gen/order.erl deleted file mode 100644 index 92e6f13..0000000 --- a/gen/order.erl +++ /dev/null @@ -1,127 +0,0 @@ --module(order). --compile(no_auto_import). --include_lib("eunit/include/eunit.hrl"). - --export([reverse/1, to_int/1, compare/2, max/2, min/2]). - -reverse(Order) -> - case Order of - lt -> - gt; - - eq -> - eq; - - gt -> - lt - end. - --ifdef(TEST). -reverse_test() -> - expect:equal(reverse(lt), gt), - expect:equal(reverse(eq), eq), - expect:equal(reverse(gt), lt). --endif. - -to_int(Order) -> - case Order of - lt -> - -1; - - eq -> - 0; - - gt -> - 1 - end. - --ifdef(TEST). -to_int_test() -> - expect:equal(to_int(lt), -1), - expect:equal(to_int(eq), 0), - expect:equal(to_int(gt), 1). --endif. - -compare(A, B) -> - case {A, B} of - {lt, lt} -> - eq; - - {lt, _} -> - lt; - - {eq, eq} -> - eq; - - {gt, gt} -> - eq; - - {eq, gt} -> - lt; - - _ -> - gt - end. - --ifdef(TEST). -compare_test() -> - expect:equal(compare(lt, lt), eq), - expect:equal(compare(lt, eq), lt), - expect:equal(compare(lt, gt), lt), - expect:equal(compare(eq, lt), gt), - expect:equal(compare(eq, eq), eq), - expect:equal(compare(eq, gt), lt), - expect:equal(compare(gt, lt), gt), - expect:equal(compare(gt, eq), gt), - expect:equal(compare(gt, gt), eq). --endif. - -max(A, B) -> - case {A, B} of - {gt, _} -> - gt; - - {eq, lt} -> - eq; - - _ -> - B - end. - --ifdef(TEST). -max_test() -> - expect:equal(max(lt, lt), lt), - expect:equal(max(lt, eq), eq), - expect:equal(max(lt, gt), gt), - expect:equal(max(eq, lt), eq), - expect:equal(max(eq, eq), eq), - expect:equal(max(eq, gt), gt), - expect:equal(max(gt, lt), gt), - expect:equal(max(gt, eq), gt), - expect:equal(max(gt, gt), gt). --endif. - -min(A, B) -> - case {A, B} of - {lt, _} -> - lt; - - {eq, gt} -> - eq; - - _ -> - B - end. - --ifdef(TEST). -min_test() -> - expect:equal(min(lt, lt), lt), - expect:equal(min(lt, eq), lt), - expect:equal(min(lt, gt), lt), - expect:equal(min(eq, lt), lt), - expect:equal(min(eq, eq), eq), - expect:equal(min(eq, gt), eq), - expect:equal(min(gt, lt), lt), - expect:equal(min(gt, eq), eq), - expect:equal(min(gt, gt), gt). --endif. diff --git a/gen/result.erl b/gen/result.erl deleted file mode 100644 index e793c6b..0000000 --- a/gen/result.erl +++ /dev/null @@ -1,116 +0,0 @@ --module(result). --compile(no_auto_import). --include_lib("eunit/include/eunit.hrl"). - --export([is_ok/1, is_error/1, map/2, map_error/2, flatten/1, then/2, unwrap/2]). - -is_ok(Result) -> - case Result of - {error, _} -> - false; - - {ok, _} -> - true - end. - --ifdef(TEST). -is_ok_test() -> - expect:true(is_ok({ok, 1})), - expect:false(is_ok({error, 1})). --endif. - -is_error(Result) -> - case Result of - {ok, _} -> - false; - - {error, _} -> - true - end. - --ifdef(TEST). -is_error_test() -> - expect:false(is_error({ok, 1})), - expect:true(is_error({error, 1})). --endif. - -map(Result, Fun) -> - case Result of - {ok, X} -> - {ok, Fun(X)}; - - {error, E} -> - {error, E} - end. - --ifdef(TEST). -map_test() -> - expect:equal(map({ok, 1}, fun(X) -> X + 1 end), {ok, 2}), - expect:equal(map({ok, 1}, fun(_) -> <<"2">> end), {ok, <<"2">>}), - expect:equal(map({error, 1}, fun(X) -> X + 1 end), {error, 1}). --endif. - -map_error(Result, Fun) -> - case Result of - {ok, _} -> - Result; - - {error, Error} -> - {error, Fun(Error)} - end. - --ifdef(TEST). -map_error_test() -> - expect:equal(map_error({ok, 1}, fun(X) -> X + 1 end), {ok, 1}), - expect:equal(map_error({error, 1}, fun(X) -> X + 1 end), {error, 2}). --endif. - -flatten(Result) -> - case Result of - {ok, X} -> - X; - - {error, Error} -> - {error, Error} - end. - --ifdef(TEST). -flatten_test() -> - expect:equal(flatten({ok, {ok, 1}}), {ok, 1}), - expect:equal(flatten({ok, {error, 1}}), {error, 1}), - expect:equal(flatten({error, 1}), {error, 1}), - expect:equal(flatten({error, {error, 1}}), {error, {error, 1}}). --endif. - -then(Result, Fun) -> - case Result of - {ok, X} -> - Fun(X); - - {error, E} -> - {error, E} - end. - --ifdef(TEST). -then_test() -> - expect:equal(then({error, 1}, fun(X) -> {ok, X + 1} end), {error, 1}), - expect:equal(then({ok, 1}, fun(X) -> {ok, X + 1} end), {ok, 2}), - expect:equal(then({ok, 1}, fun(_) -> {ok, <<"type change">>} end), - {ok, <<"type change">>}), - expect:equal(then({ok, 1}, fun(_) -> {error, 1} end), {error, 1}). --endif. - -unwrap(Result, Default) -> - case Result of - {ok, V} -> - V; - - {error, _} -> - Default - end. - --ifdef(TEST). -unwrap_test() -> - expect:equal(unwrap({ok, 1}, 50), 1), - expect:equal(unwrap({error, <<"nope">>}, 50), 50). --endif. diff --git a/gen/src/any.erl b/gen/src/any.erl new file mode 100644 index 0000000..62f1a77 --- /dev/null +++ b/gen/src/any.erl @@ -0,0 +1,46 @@ +-module(any). +-compile(no_auto_import). + +-export([from/1, unsafeCoerce/1, string/1, int/1, float/1, atom/1, bool/1, thunk/1, list/2, tuple/1, field/2]). + +list_module() -> + list. + +from(A) -> + gleam__stdlib:identity(A). + +unsafeCoerce(A) -> + gleam__stdlib:identity(A). + +string(A) -> + gleam__stdlib:decode_string(A). + +int(A) -> + gleam__stdlib:decode_int(A). + +float(A) -> + gleam__stdlib:decode_float(A). + +atom(A) -> + gleam__stdlib:decode_atom(A). + +bool(A) -> + gleam__stdlib:decode_bool(A). + +thunk(A) -> + gleam__stdlib:decode_thunk(A). + +list_any(A) -> + gleam__stdlib:decode_list(A). + +list(Any, Decode) -> + result:then(list_any(Any), + fun(Capture1) -> + (list_module()):traverse(Capture1, Decode) + end). + +tuple(A) -> + gleam__stdlib:decode_tuple(A). + +field(A, B) -> + gleam__stdlib:decode_field(A, B). diff --git a/gen/src/atom.erl b/gen/src/atom.erl new file mode 100644 index 0000000..450245a --- /dev/null +++ b/gen/src/atom.erl @@ -0,0 +1,13 @@ +-module(atom). +-compile(no_auto_import). + +-export([from_string/1, create_from_string/1, to_string/1]). + +from_string(A) -> + gleam__stdlib:atom_from_string(A). + +create_from_string(A) -> + gleam__stdlib:atom_create_from_string(A). + +to_string(A) -> + gleam__stdlib:atom_to_string(A). diff --git a/gen/src/bool.erl b/gen/src/bool.erl new file mode 100644 index 0000000..e4e1108 --- /dev/null +++ b/gen/src/bool.erl @@ -0,0 +1,40 @@ +-module(bool). +-compile(no_auto_import). + +-export([negate/1, max/2, min/2, to_int/1]). + +negate(Bool) -> + case Bool of + true -> + false; + + false -> + true + end. + +max(A, B) -> + case A of + true -> + true; + + false -> + B + end. + +min(A, B) -> + case A of + false -> + false; + + true -> + B + end. + +to_int(Bool) -> + case Bool of + false -> + 0; + + true -> + 1 + end. diff --git a/gen/expect.erl b/gen/src/expect.erl index 602f3fb..602f3fb 100644 --- a/gen/expect.erl +++ b/gen/src/expect.erl diff --git a/gen/http.erl b/gen/src/http.erl index 6d2e42a..6d2e42a 100644 --- a/gen/http.erl +++ b/gen/src/http.erl diff --git a/gen/src/iodata.erl b/gen/src/iodata.erl new file mode 100644 index 0000000..540af1b --- /dev/null +++ b/gen/src/iodata.erl @@ -0,0 +1,61 @@ +-module(iodata). +-compile(no_auto_import). + +-export([prepend/2, append/2, prepend_iodata/2, append_iodata/2, from_strings/1, concat/1, new/1, to_string/1, byte_size/1, from_float/1, lowercase/1, uppercase/1, reverse/1, split/2, replace/3, is_equal/2, is_empty/1]). + +prepend(A, B) -> + gleam__stdlib:iodata_prepend(A, B). + +append(A, B) -> + gleam__stdlib:iodata_append(A, B). + +prepend_iodata(A, B) -> + gleam__stdlib:iodata_prepend(A, B). + +append_iodata(A, B) -> + gleam__stdlib:iodata_append(A, B). + +from_strings(A) -> + gleam__stdlib:identity(A). + +concat(A) -> + gleam__stdlib:identity(A). + +new(A) -> + gleam__stdlib:identity(A). + +to_string(A) -> + erlang:iolist_to_binary(A). + +byte_size(A) -> + erlang:iolist_size(A). + +from_float(A) -> + io_lib_format:fwrite_g(A). + +lowercase(A) -> + string:lowercase(A). + +uppercase(A) -> + string:uppercase(A). + +reverse(A) -> + string:reverse(A). + +erl_split(A, B, C) -> + string:split(A, B, C). + +split(Iodata, On) -> + erl_split(Iodata, On, all). + +erl_replace(A, B, C, D) -> + string:replace(A, B, C, D). + +replace(Iodata, Pattern, Replacement) -> + erl_replace(Iodata, Pattern, Replacement, all). + +is_equal(A, B) -> + string:equal(A, B). + +is_empty(A) -> + string:is_empty(A). diff --git a/gen/src/list.erl b/gen/src/list.erl new file mode 100644 index 0000000..b606f22 --- /dev/null +++ b/gen/src/list.erl @@ -0,0 +1,173 @@ +-module(list). +-compile(no_auto_import). + +-export([length/1, reverse/1, is_empty/1, contains/2, head/1, tail/1, filter/2, map/2, traverse/2, drop/2, take/2, new/0, append/2, flatten/1, fold/3, fold_right/3, find/2]). + +length(A) -> + erlang:length(A). + +reverse(A) -> + lists:reverse(A). + +is_empty(List) -> + List =:= []. + +contains(List, Elem) -> + case List of + [] -> + false; + + [Head | Rest] -> + Head =:= Elem orelse contains(Rest, Elem) + end. + +head(List) -> + case List of + [] -> + {error, empty}; + + [X | _] -> + {ok, X} + end. + +tail(List) -> + case List of + [] -> + {error, empty}; + + [_ | Xs] -> + {ok, Xs} + end. + +do_filter(List, Fun, Acc) -> + case List of + [] -> + reverse(Acc); + + [X | Xs] -> + NewAcc = case Fun(X) of + true -> + [X | Acc]; + + false -> + Acc + end, + do_filter(Xs, Fun, NewAcc) + end. + +filter(List, Fun) -> + do_filter(List, Fun, []). + +do_map(List, Fun, Acc) -> + case List of + [] -> + reverse(Acc); + + [X | Xs] -> + do_map(Xs, Fun, [Fun(X) | Acc]) + end. + +map(List, Fun) -> + do_map(List, Fun, []). + +do_traverse(List, Fun, Acc) -> + case List of + [] -> + {ok, reverse(Acc)}; + + [X | Xs] -> + case Fun(X) of + {ok, Y} -> + do_traverse(Xs, Fun, [Y | Acc]); + + {error, Error} -> + {error, Error} + end + end. + +traverse(List, Fun) -> + do_traverse(List, Fun, []). + +drop(List, N) -> + case N =< 0 of + true -> + List; + + false -> + case List of + [] -> + []; + + [_ | Xs] -> + drop(Xs, N - 1) + end + end. + +do_take(List, N, Acc) -> + case N =< 0 of + true -> + reverse(Acc); + + false -> + case List of + [] -> + reverse(Acc); + + [X | Xs] -> + do_take(Xs, N - 1, [X | Acc]) + end + end. + +take(List, N) -> + do_take(List, N, []). + +new() -> + []. + +append(A, B) -> + lists:append(A, B). + +do_flatten(Lists, Acc) -> + case Lists of + [] -> + Acc; + + [L | Rest] -> + do_flatten(Rest, append(Acc, L)) + end. + +flatten(Lists) -> + do_flatten(Lists, []). + +fold(List, Acc, Fun) -> + case List of + [] -> + Acc; + + [X | Rest] -> + fold(Rest, Fun(X, Acc), Fun) + end. + +fold_right(List, Acc, Fun) -> + case List of + [] -> + Acc; + + [X | Rest] -> + Fun(X, fold_right(Rest, Acc, Fun)) + end. + +find(Haystack, F) -> + case Haystack of + [] -> + {error, not_found}; + + [X | Rest] -> + case F(X) of + {ok, X1} -> + {ok, X1}; + + _ -> + find(Rest, F) + end + end. diff --git a/gen/src/map_dict.erl b/gen/src/map_dict.erl new file mode 100644 index 0000000..56fb686 --- /dev/null +++ b/gen/src/map_dict.erl @@ -0,0 +1,49 @@ +-module(map_dict). +-compile(no_auto_import). + +-export([size/1, to_list/1, from_list/1, has_key/2, new/0, fetch/2, put/3, map_values/2, keys/1, values/1, filter/2]). + +size(A) -> + maps:size(A). + +to_list(A) -> + maps:to_list(A). + +from_list(A) -> + maps:from_list(A). + +is_key(A, B) -> + maps:is_key(A, B). + +has_key(Map, Key) -> + is_key(Key, Map). + +new() -> + maps:new(). + +fetch(A, B) -> + gleam__stdlib:map_fetch(A, B). + +erl_put(A, B, C) -> + maps:put(A, B, C). + +put(Map, Key, Value) -> + erl_put(Key, Value, Map). + +erl_map_values(A, B) -> + maps:map(A, B). + +map_values(Map, Fun) -> + erl_map_values(Fun, Map). + +keys(A) -> + maps:keys(A). + +values(A) -> + maps:values(A). + +erl_filter(A, B) -> + maps:filter(A, B). + +filter(Map, Fun) -> + erl_filter(Fun, Map). diff --git a/gen/src/order.erl b/gen/src/order.erl new file mode 100644 index 0000000..7921740 --- /dev/null +++ b/gen/src/order.erl @@ -0,0 +1,73 @@ +-module(order). +-compile(no_auto_import). + +-export([reverse/1, to_int/1, compare/2, max/2, min/2]). + +reverse(Order) -> + case Order of + lt -> + gt; + + eq -> + eq; + + gt -> + lt + end. + +to_int(Order) -> + case Order of + lt -> + -1; + + eq -> + 0; + + gt -> + 1 + end. + +compare(A, B) -> + case {A, B} of + {lt, lt} -> + eq; + + {lt, _} -> + lt; + + {eq, eq} -> + eq; + + {gt, gt} -> + eq; + + {eq, gt} -> + lt; + + _ -> + gt + end. + +max(A, B) -> + case {A, B} of + {gt, _} -> + gt; + + {eq, lt} -> + eq; + + _ -> + B + end. + +min(A, B) -> + case {A, B} of + {lt, _} -> + lt; + + {eq, gt} -> + eq; + + _ -> + B + end. diff --git a/gen/src/result.erl b/gen/src/result.erl new file mode 100644 index 0000000..cd66908 --- /dev/null +++ b/gen/src/result.erl @@ -0,0 +1,67 @@ +-module(result). +-compile(no_auto_import). + +-export([is_ok/1, is_error/1, map/2, map_error/2, flatten/1, then/2, unwrap/2]). + +is_ok(Result) -> + case Result of + {error, _} -> + false; + + {ok, _} -> + true + end. + +is_error(Result) -> + case Result of + {ok, _} -> + false; + + {error, _} -> + true + end. + +map(Result, Fun) -> + case Result of + {ok, X} -> + {ok, Fun(X)}; + + {error, E} -> + {error, E} + end. + +map_error(Result, Fun) -> + case Result of + {ok, _} -> + Result; + + {error, Error} -> + {error, Fun(Error)} + end. + +flatten(Result) -> + case Result of + {ok, X} -> + X; + + {error, Error} -> + {error, Error} + end. + +then(Result, Fun) -> + case Result of + {ok, X} -> + Fun(X); + + {error, E} -> + {error, E} + end. + +unwrap(Result, Default) -> + case Result of + {ok, V} -> + V; + + {error, _} -> + Default + end. diff --git a/gen/set.erl b/gen/src/set.erl index ca52d4b..ca52d4b 100644 --- a/gen/set.erl +++ b/gen/src/set.erl diff --git a/gen/src/str.erl b/gen/src/str.erl new file mode 100644 index 0000000..b492c86 --- /dev/null +++ b/gen/src/str.erl @@ -0,0 +1,37 @@ +-module(str). +-compile(no_auto_import). + +-export([length/1, lowercase/1, uppercase/1, reverse/1, split/2, replace/3, from_int/1, parse_int/1, parse_float/1, base_from_int/2, from_float/1]). + +length(A) -> + string:length(A). + +lowercase(A) -> + string:lowercase(A). + +uppercase(A) -> + string:uppercase(A). + +reverse(String) -> + iodata:to_string(iodata:reverse(iodata:new(String))). + +split(String, On) -> + list:map(iodata:split(iodata:new(String), On), fun iodata:to_string/1). + +replace(String, Pattern, With) -> + iodata:to_string(iodata:replace(iodata:new(String), Pattern, With)). + +from_int(A) -> + erlang:integer_to_binary(A). + +parse_int(A) -> + gleam__stdlib:parse_int(A). + +parse_float(A) -> + gleam__stdlib:parse_float(A). + +base_from_int(A, B) -> + erlang:integer_to_binary(A, B). + +from_float(F) -> + iodata:to_string(iodata:from_float(F)). diff --git a/gen/src/tuple.erl b/gen/src/tuple.erl new file mode 100644 index 0000000..fe3ce03 --- /dev/null +++ b/gen/src/tuple.erl @@ -0,0 +1,28 @@ +-module(tuple). +-compile(no_auto_import). + +-export([new/2, first/1, second/1, swap/1, fetch/2]). + +new(A, B) -> + {A, B}. + +first(Tup) -> + {A, _} = Tup, + A. + +second(Tup) -> + {_, A} = Tup, + A. + +swap(Tup) -> + {A, B} = Tup, + {B, A}. + +fetch(Haystack, Needle) -> + list:find(Haystack, fun(Tuple) -> case first(Tuple) =:= Needle of + true -> + {ok, second(Tuple)}; + + false -> + {error, []} + end end). diff --git a/gen/str.erl b/gen/str.erl deleted file mode 100644 index b403187..0000000 --- a/gen/str.erl +++ /dev/null @@ -1,113 +0,0 @@ --module(str). --compile(no_auto_import). --include_lib("eunit/include/eunit.hrl"). - --export([length/1, lowercase/1, uppercase/1, reverse/1, split/2, replace/3, from_int/1, parse_int/1, parse_float/1, base_from_int/2, from_float/1]). - -length(A) -> - string:length(A). - --ifdef(TEST). -length_test() -> - expect:equal(length(<<"ß↑e̊">>), 3), - expect:equal(length(<<"Gleam">>), 5), - expect:equal(length(<<"">>), 0). --endif. - -lowercase(A) -> - string:lowercase(A). - --ifdef(TEST). -lowercase_test() -> - expect:equal(lowercase(<<"Gleam">>), <<"gleam">>). --endif. - -uppercase(A) -> - string:uppercase(A). - --ifdef(TEST). -uppercase_test() -> - expect:equal(uppercase(<<"Gleam">>), <<"GLEAM">>). --endif. - -reverse(String) -> - iodata:to_string(iodata:reverse(iodata:new(String))). - --ifdef(TEST). -reverse_test() -> - expect:equal(reverse(<<"Gleam">>), <<"maelG">>). --endif. - -split(String, On) -> - list:map(iodata:split(iodata:new(String), On), fun iodata:to_string/1). - --ifdef(TEST). -split_test() -> - expect:equal(split(<<"Gleam,Erlang,Elixir">>, <<",">>), - [<<"Gleam">>, <<"Erlang">>, <<"Elixir">>]), - expect:equal(split(<<"Gleam, Erlang,Elixir">>, <<", ">>), - [<<"Gleam">>, <<"Erlang,Elixir">>]). --endif. - -replace(String, Pattern, With) -> - iodata:to_string(iodata:replace(iodata:new(String), Pattern, With)). - --ifdef(TEST). -replace_test() -> - expect:equal(replace(<<"Gleam,Erlang,Elixir">>, <<",">>, <<"++">>), - <<"Gleam++Erlang++Elixir">>). --endif. - -from_int(A) -> - erlang:integer_to_binary(A). - --ifdef(TEST). -from_int_test() -> - expect:equal(from_int(123), <<"123">>), - expect:equal(from_int(-123), <<"-123">>), - expect:equal(from_int(123), <<"123">>). --endif. - -parse_int(A) -> - gleam__stdlib:parse_int(A). - --ifdef(TEST). -parse_int_test() -> - expect:equal(parse_int(<<"123">>), {ok, 123}), - expect:equal(parse_int(<<"-123">>), {ok, -123}), - expect:equal(parse_int(<<"0123">>), {ok, 123}), - expect:equal(parse_int(<<"">>), {error, parse_error}), - expect:equal(parse_int(<<"what">>), {error, parse_error}), - expect:equal(parse_int(<<"1.23">>), {error, parse_error}). --endif. - -parse_float(A) -> - gleam__stdlib:parse_float(A). - --ifdef(TEST). -parse_float_test() -> - expect:equal(parse_float(<<"1.23">>), {ok, 1.23}), - expect:equal(parse_float(<<"5.0">>), {ok, 5.0}), - expect:equal(parse_float(<<"0.123456789">>), {ok, 0.123456789}), - expect:equal(parse_float(<<"">>), {error, parse_error}), - expect:equal(parse_float(<<"what">>), {error, parse_error}), - expect:equal(parse_float(<<"1">>), {error, parse_error}). --endif. - -base_from_int(A, B) -> - erlang:integer_to_binary(A, B). - --ifdef(TEST). -base_from_int_test() -> - expect:equal(base_from_int(100, 16), <<"64">>), - expect:equal(base_from_int(-100, 16), <<"-64">>). --endif. - -from_float(F) -> - iodata:to_string(iodata:from_float(F)). - --ifdef(TEST). -from_float_test() -> - expect:equal(from_float(123.0), <<"123.0">>), - expect:equal(from_float(-8.1), <<"-8.1">>). --endif. diff --git a/gen/test/any_test.erl b/gen/test/any_test.erl new file mode 100644 index 0000000..6e5bd73 --- /dev/null +++ b/gen/test/any_test.erl @@ -0,0 +1,97 @@ +-module(any_test). +-compile(no_auto_import). + +-export([string_test/0, int_test/0, float_test/0, thunk_test/0, bool_test/0, atom_test/0, list_test/0, tuple_test/0, field_test/0]). + +string_test() -> + expect:equal(any:string(any:from(<<"">>)), {ok, <<"">>}), + expect:equal(any:string(any:from(<<"Hello">>)), {ok, <<"Hello">>}), + expect:equal(any:string(any:from(1)), + {error, <<"Expected a String, got `1`">>}), + expect:equal(any:string(any:from([])), + {error, <<"Expected a String, got `[]`">>}). + +int_test() -> + expect:equal(any:int(any:from(1)), {ok, 1}), + expect:equal(any:int(any:from(2)), {ok, 2}), + expect:equal(any:int(any:from(1.0)), + {error, <<"Expected an Int, got `1.0`">>}), + expect:equal(any:int(any:from([])), + {error, <<"Expected an Int, got `[]`">>}). + +float_test() -> + expect:equal(any:float(any:from(1.0)), {ok, 1.0}), + expect:equal(any:float(any:from(2.2)), {ok, 2.2}), + expect:equal(any:float(any:from(1)), + {error, <<"Expected a Float, got `1`">>}), + expect:equal(any:float(any:from([])), + {error, <<"Expected a Float, got `[]`">>}). + +thunk_test() -> + expect:is_ok(any:thunk(any:from(fun() -> 1 end))), + expect:equal(result:map(any:thunk(any:from(fun() -> 1 end)), + fun(F) -> F() end), + {ok, any:from(1)}), + expect:is_error(any:thunk(any:from(fun(X) -> X end))), + expect:is_error(any:thunk(any:from(1))), + expect:is_error(any:thunk(any:from([]))). + +bool_test() -> + expect:equal(any:bool(any:from(true)), {ok, true}), + expect:equal(any:bool(any:from(false)), {ok, false}), + expect:equal(any:bool(any:from(1)), + {error, <<"Expected a Bool, got `1`">>}), + expect:equal(any:bool(any:from([])), + {error, <<"Expected a Bool, got `[]`">>}). + +atom_test() -> + expect:equal(any:atom(any:from(atom:create_from_string(<<"">>))), + {ok, atom:create_from_string(<<"">>)}), + expect:equal(any:atom(any:from(atom:create_from_string(<<"ok">>))), + {ok, atom:create_from_string(<<"ok">>)}), + expect:is_error(any:atom(any:from(1))), + expect:is_error(any:atom(any:from([]))). + +list_test() -> + expect:equal(any:list(any:from([]), fun any:string/1), {ok, []}), + expect:equal(any:list(any:from([]), fun any:int/1), {ok, []}), + expect:equal(any:list(any:from([1, 2, 3]), fun any:int/1), {ok, [1, 2, 3]}), + expect:equal(any:list(any:from([[1], [2], [3]]), + fun(Capture1) -> + any:list(Capture1, fun any:int/1) + end), + {ok, [[1], [2], [3]]}), + expect:is_error(any:list(any:from(1), fun any:string/1)), + expect:is_error(any:list(any:from(1.0), fun any:int/1)), + expect:is_error(any:list(any:from([<<"">>]), fun any:int/1)), + expect:is_error(any:list(any:from([any:from(1), any:from(<<"not an int">>)]), + fun any:int/1)). + +tuple_test() -> + expect:equal(any:tuple(any:from({1, []})), + {ok, {any:from(1), any:from([])}}), + expect:equal(any:tuple(any:from({<<"ok">>, <<"ok">>})), + {ok, {any:from(<<"ok">>), any:from(<<"ok">>)}}), + expect:is_error(any:tuple(any:from({1}))), + expect:is_error(any:tuple(any:from({1, 2, 3}))), + expect:equal(result:then(result:then(any:tuple(any:from({1, 2.0})), + fun(X) -> + result:map(any:int(tuple:first(X)), + fun(F) -> + {F, tuple:second(X)} + end) + end), + fun(X) -> + result:map(any:float(tuple:second(X)), + fun(F) -> {tuple:first(X), F} end) + end), + {ok, {1, 2.0}}). + +field_test() -> + {ok, OkAtom} = atom:from_string(<<"ok">>), + expect:equal(any:field(any:from(#{}#{ok => 1}), OkAtom), {ok, any:from(1)}), + expect:equal(any:field(any:from(#{}#{ok => 3}#{earlier => 2}), OkAtom), + {ok, any:from(3)}), + expect:is_error(any:field(any:from(#{}), OkAtom)), + expect:is_error(any:field(any:from(1), OkAtom)), + expect:is_error(any:field(any:from([]), [])). diff --git a/gen/test/atom_test.erl b/gen/test/atom_test.erl new file mode 100644 index 0000000..ae9f76b --- /dev/null +++ b/gen/test/atom_test.erl @@ -0,0 +1,23 @@ +-module(atom_test). +-compile(no_auto_import). + +-export([from_string_test/0, create_from_string_test/0, to_string_test/0]). + +from_string_test() -> + expect:is_ok(atom:from_string(<<"ok">>)), + expect:is_ok(atom:from_string(<<"expect">>)), + expect:is_error(atom:from_string(<<"this is not an atom we have seen before">>)). + +create_from_string_test() -> + expect:equal({ok, atom:create_from_string(<<"ok">>)}, + atom:from_string(<<"ok">>)), + expect:equal({ok, atom:create_from_string(<<"expect">>)}, + atom:from_string(<<"expect">>)), + expect:equal({ok, + atom:create_from_string(<<"this is another atom we have not seen before">>)}, + atom:from_string(<<"this is another atom we have not seen before">>)). + +to_string_test() -> + expect:equal(atom:to_string(atom:create_from_string(<<"ok">>)), <<"ok">>), + expect:equal(atom:to_string(atom:create_from_string(<<"expect">>)), + <<"expect">>). diff --git a/gen/test/bool_test.erl b/gen/test/bool_test.erl new file mode 100644 index 0000000..c38f780 --- /dev/null +++ b/gen/test/bool_test.erl @@ -0,0 +1,24 @@ +-module(bool_test). +-compile(no_auto_import). + +-export([negate_test/0, max_test/0, min_test/0, to_int_test/0]). + +negate_test() -> + expect:false(bool:negate(true)), + expect:true(bool:negate(false)). + +max_test() -> + expect:equal(bool:max(true, true), true), + expect:equal(bool:max(true, false), true), + expect:equal(bool:max(false, false), false), + expect:equal(bool:max(false, true), true). + +min_test() -> + expect:equal(bool:min(true, true), true), + expect:equal(bool:min(true, false), false), + expect:equal(bool:min(false, false), false), + expect:equal(bool:min(false, true), false). + +to_int_test() -> + expect:equal(bool:to_int(true), 1), + expect:equal(bool:to_int(false), 0). diff --git a/gen/test/http_test.erl b/gen/test/http_test.erl new file mode 100644 index 0000000..306f916 --- /dev/null +++ b/gen/test/http_test.erl @@ -0,0 +1,6 @@ +-module(http_test). +-compile(no_auto_import). + +-export([]). + + diff --git a/gen/test/iodata_test.erl b/gen/test/iodata_test.erl new file mode 100644 index 0000000..b996bec --- /dev/null +++ b/gen/test/iodata_test.erl @@ -0,0 +1,52 @@ +-module(iodata_test). +-compile(no_auto_import). + +-export([iodata_test/0, lowercase_test/0, uppercase_test/0, split_test/0, is_equal_test/0, is_empty_test/0]). + +iodata_test() -> + Data = iodata:prepend(iodata:append(iodata:append(iodata:new(<<"ello">>), + <<",">>), + <<" world!">>), + <<"H">>), + expect:equal(iodata:to_string(Data), <<"Hello, world!">>), + expect:equal(iodata:byte_size(Data), 13), + Data1 = iodata:prepend_iodata(iodata:append_iodata(iodata:append_iodata(iodata:new(<<"ello">>), + iodata:new(<<",">>)), + iodata:concat([iodata:new(<<" wo">>), + iodata:new(<<"rld!">>)])), + iodata:new(<<"H">>)), + expect:equal(iodata:to_string(Data1), <<"Hello, world!">>), + expect:equal(iodata:byte_size(Data1), 13). + +lowercase_test() -> + expect:equal(iodata:to_string(iodata:lowercase(iodata:from_strings([<<"Gleam">>, + <<"Gleam">>]))), + <<"gleamgleam">>). + +uppercase_test() -> + expect:equal(iodata:to_string(iodata:uppercase(iodata:from_strings([<<"Gleam">>, + <<"Gleam">>]))), + <<"GLEAMGLEAM">>). + +split_test() -> + expect:equal(iodata:split(iodata:new(<<"Gleam,Erlang,Elixir">>), <<",">>), + [iodata:new(<<"Gleam">>), + iodata:new(<<"Erlang">>), + iodata:new(<<"Elixir">>)]), + expect:equal(iodata:split(iodata:from_strings([<<"Gleam, Erl">>, + <<"ang,Elixir">>]), + <<", ">>), + [iodata:new(<<"Gleam">>), + iodata:from_strings([<<"Erl">>, <<"ang,Elixir">>])]). + +is_equal_test() -> + expect:true(iodata:is_equal(iodata:new(<<"12">>), + iodata:from_strings([<<"1">>, <<"2">>]))), + expect:true(iodata:is_equal(iodata:new(<<"12">>), iodata:new(<<"12">>))), + expect:false(iodata:is_equal(iodata:new(<<"12">>), iodata:new(<<"2">>))). + +is_empty_test() -> + expect:true(iodata:is_empty(iodata:new(<<"">>))), + expect:false(iodata:is_empty(iodata:new(<<"12">>))), + expect:true(iodata:is_empty(iodata:from_strings([]))), + expect:true(iodata:is_empty(iodata:from_strings([<<"">>, <<"">>]))). diff --git a/gen/test/list_test.erl b/gen/test/list_test.erl new file mode 100644 index 0000000..2505a6e --- /dev/null +++ b/gen/test/list_test.erl @@ -0,0 +1,95 @@ +-module(list_test). +-compile(no_auto_import). + +-export([length_test/0, reverse_test/0, is_empty_test/0, contains_test/0, head_test/0, tail_test/0, filter_test/0, map_test/0, traverse_test/0, drop_test/0, take_test/0, new_test/0, append_test/0, flatten_test/0, fold_test/0, fold_right_test/0, find_test/0]). + +length_test() -> + expect:equal(list:length([]), 0), + expect:equal(list:length([1]), 1), + expect:equal(list:length([1, 1]), 2), + expect:equal(list:length([1, 1, 1]), 3). + +reverse_test() -> + expect:equal(list:reverse([]), []), + expect:equal(list:reverse([1, 2, 3, 4, 5]), [5, 4, 3, 2, 1]). + +is_empty_test() -> + expect:true(list:is_empty([])), + expect:false(list:is_empty([1])). + +contains_test() -> + expect:true(list:contains([0, 4, 5, 1], 1)), + expect:false(list:contains([0, 4, 5, 7], 1)), + expect:false(list:contains([], 1)). + +head_test() -> + expect:equal(list:head([0, 4, 5, 7]), {ok, 0}), + expect:is_error(list:head([])). + +tail_test() -> + expect:equal(list:tail([0, 4, 5, 7]), {ok, [4, 5, 7]}), + expect:equal(list:tail([0]), {ok, []}), + expect:is_error(list:tail([])). + +filter_test() -> + expect:equal(list:filter([], fun(_) -> true end), []), + expect:equal(list:filter([0, 4, 5, 7, 3], fun(_) -> true end), + [0, 4, 5, 7, 3]), + expect:equal(list:filter([0, 4, 5, 7, 3], fun(X) -> X > 4 end), [5, 7]), + expect:equal(list:filter([0, 4, 5, 7, 3], fun(X) -> X < 4 end), [0, 3]). + +map_test() -> + expect:equal(list:map([], fun(X) -> X * 2 end), []), + expect:equal(list:map([0, 4, 5, 7, 3], fun(X) -> X * 2 end), + [0, 8, 10, 14, 6]). + +traverse_test() -> + Fun = fun(X) -> case X =:= 6 orelse X =:= 5 orelse X =:= 4 of + true -> + {ok, X * 2}; + + false -> + {error, X} + end end, + expect:equal(list:traverse([5, 6, 5, 6], Fun), {ok, [10, 12, 10, 12]}), + expect:equal(list:traverse([4, 6, 5, 7, 3], Fun), {error, 7}). + +drop_test() -> + expect:equal(list:drop([], 5), []), + expect:equal(list:drop([1, 2, 3, 4, 5, 6, 7, 8], 5), [6, 7, 8]). + +take_test() -> + expect:equal(list:take([], 5), []), + expect:equal(list:take([1, 2, 3, 4, 5, 6, 7, 8], 5), [1, 2, 3, 4, 5]). + +new_test() -> + expect:equal(list:new(), []). + +append_test() -> + expect:equal(list:append([1], [2, 3]), [1, 2, 3]). + +flatten_test() -> + expect:equal(list:flatten([]), []), + expect:equal(list:flatten([[]]), []), + expect:equal(list:flatten([[], [], []]), []), + expect:equal(list:flatten([[1, 2], [], [3, 4]]), [1, 2, 3, 4]). + +fold_test() -> + expect:equal(list:fold([1, 2, 3], [], fun(X, Acc) -> [X | Acc] end), + [3, 2, 1]). + +fold_right_test() -> + expect:equal(list:fold_right([1, 2, 3], [], fun(X, Acc) -> [X | Acc] end), + [1, 2, 3]). + +find_test() -> + F = fun(X) -> case X of + 2 -> + {ok, 4}; + + _ -> + {error, 0} + end end, + expect:equal(list:find([1, 2, 3], F), {ok, 4}), + expect:equal(list:find([1, 3, 2], F), {ok, 4}), + expect:is_error(list:find([1, 3], F)). diff --git a/gen/test/map_dict_test.erl b/gen/test/map_dict_test.erl new file mode 100644 index 0000000..e4560d9 --- /dev/null +++ b/gen/test/map_dict_test.erl @@ -0,0 +1,55 @@ +-module(map_dict_test). +-compile(no_auto_import). + +-export([from_list_test/0, has_key_test/0, new_test/0, fetch_test/0, put_test/0, map_values_test/0, keys_test/0, values_test/0]). + +from_list_test() -> + expect:equal(map_dict:size(map_dict:from_list([{4, 0}, {1, 0}])), 2). + +has_key_test() -> + expect:false(map_dict:has_key(map_dict:from_list([]), 1)), + expect:true(map_dict:has_key(map_dict:from_list([{1, 0}]), 1)), + expect:true(map_dict:has_key(map_dict:from_list([{4, 0}, {1, 0}]), 1)), + expect:false(map_dict:has_key(map_dict:from_list([{4, 0}, {1, 0}]), 0)). + +new_test() -> + expect:equal(map_dict:size(map_dict:new()), 0), + expect:equal(map_dict:to_list(map_dict:new()), []). + +fetch_test() -> + Proplist = [{4, 0}, {1, 1}], + M = map_dict:from_list(Proplist), + expect:equal(map_dict:fetch(M, 4), {ok, 0}), + expect:equal(map_dict:fetch(M, 1), {ok, 1}), + expect:is_error(map_dict:fetch(M, 2)). + +put_test() -> + expect:equal(map_dict:put(map_dict:put(map_dict:put(map_dict:new(), + <<"a">>, + 0), + <<"b">>, + 1), + <<"c">>, + 2), + map_dict:from_list([{<<"a">>, 0}, + {<<"b">>, 1}, + {<<"c">>, 2}])). + +map_values_test() -> + expect:equal(map_dict:map_values(map_dict:from_list([{1, 0}, + {2, 1}, + {3, 2}]), + fun(K, V) -> K + V end), + map_dict:from_list([{1, 1}, {2, 3}, {3, 5}])). + +keys_test() -> + expect:equal(map_dict:keys(map_dict:from_list([{<<"a">>, 0}, + {<<"b">>, 1}, + {<<"c">>, 2}])), + [<<"a">>, <<"b">>, <<"c">>]). + +values_test() -> + expect:equal(map_dict:values(map_dict:from_list([{<<"a">>, 0}, + {<<"b">>, 1}, + {<<"c">>, 2}])), + [0, 1, 2]). diff --git a/gen/test/order_test.erl b/gen/test/order_test.erl new file mode 100644 index 0000000..bd30c4d --- /dev/null +++ b/gen/test/order_test.erl @@ -0,0 +1,6 @@ +-module(order_test). +-compile(no_auto_import). + +-export([]). + + diff --git a/gen/test/result_test.erl b/gen/test/result_test.erl new file mode 100644 index 0000000..090a744 --- /dev/null +++ b/gen/test/result_test.erl @@ -0,0 +1,39 @@ +-module(result_test). +-compile(no_auto_import). + +-export([is_ok_test/0, is_error_test/0, map_test/0, map_error_test/0, flatten_test/0, then_test/0, unwrap_test/0]). + +is_ok_test() -> + expect:true(result:is_ok({ok, 1})), + expect:false(result:is_ok({error, 1})). + +is_error_test() -> + expect:false(result:is_error({ok, 1})), + expect:true(result:is_error({error, 1})). + +map_test() -> + expect:equal(result:map({ok, 1}, fun(X) -> X + 1 end), {ok, 2}), + expect:equal(result:map({ok, 1}, fun(_) -> <<"2">> end), {ok, <<"2">>}), + expect:equal(result:map({error, 1}, fun(X) -> X + 1 end), {error, 1}). + +map_error_test() -> + expect:equal(result:map_error({ok, 1}, fun(X) -> X + 1 end), {ok, 1}), + expect:equal(result:map_error({error, 1}, fun(X) -> X + 1 end), {error, 2}). + +flatten_test() -> + expect:equal(result:flatten({ok, {ok, 1}}), {ok, 1}), + expect:equal(result:flatten({ok, {error, 1}}), {error, 1}), + expect:equal(result:flatten({error, 1}), {error, 1}), + expect:equal(result:flatten({error, {error, 1}}), {error, {error, 1}}). + +then_test() -> + expect:equal(result:then({error, 1}, fun(X) -> {ok, X + 1} end), + {error, 1}), + expect:equal(result:then({ok, 1}, fun(X) -> {ok, X + 1} end), {ok, 2}), + expect:equal(result:then({ok, 1}, fun(_) -> {ok, <<"type change">>} end), + {ok, <<"type change">>}), + expect:equal(result:then({ok, 1}, fun(_) -> {error, 1} end), {error, 1}). + +unwrap_test() -> + expect:equal(result:unwrap({ok, 1}, 50), 1), + expect:equal(result:unwrap({error, <<"nope">>}, 50), 50). diff --git a/gen/test/set_test.erl b/gen/test/set_test.erl new file mode 100644 index 0000000..cdff846 --- /dev/null +++ b/gen/test/set_test.erl @@ -0,0 +1,6 @@ +-module(set_test). +-compile(no_auto_import). + +-export([]). + + diff --git a/gen/test/str_test.erl b/gen/test/str_test.erl new file mode 100644 index 0000000..0048ce8 --- /dev/null +++ b/gen/test/str_test.erl @@ -0,0 +1,57 @@ +-module(str_test). +-compile(no_auto_import). + +-export([length_test/0, lowercase_test/0, uppercase_test/0, reverse_test/0, split_test/0, replace_test/0, from_int_test/0, parse_int_test/0, parse_float_test/0, base_from_int_test/0, from_float_test/0]). + +length_test() -> + expect:equal(str:length(<<"ß↑e̊">>), 3), + expect:equal(str:length(<<"Gleam">>), 5), + expect:equal(str:length(<<"">>), 0). + +lowercase_test() -> + expect:equal(str:lowercase(<<"Gleam">>), <<"gleam">>). + +uppercase_test() -> + expect:equal(str:uppercase(<<"Gleam">>), <<"GLEAM">>). + +reverse_test() -> + expect:equal(str:reverse(<<"Gleam">>), <<"maelG">>). + +split_test() -> + expect:equal(str:split(<<"Gleam,Erlang,Elixir">>, <<",">>), + [<<"Gleam">>, <<"Erlang">>, <<"Elixir">>]), + expect:equal(str:split(<<"Gleam, Erlang,Elixir">>, <<", ">>), + [<<"Gleam">>, <<"Erlang,Elixir">>]). + +replace_test() -> + expect:equal(str:replace(<<"Gleam,Erlang,Elixir">>, <<",">>, <<"++">>), + <<"Gleam++Erlang++Elixir">>). + +from_int_test() -> + expect:equal(str:from_int(123), <<"123">>), + expect:equal(str:from_int(-123), <<"-123">>), + expect:equal(str:from_int(123), <<"123">>). + +parse_int_test() -> + expect:equal(str:parse_int(<<"123">>), {ok, 123}), + expect:equal(str:parse_int(<<"-123">>), {ok, -123}), + expect:equal(str:parse_int(<<"0123">>), {ok, 123}), + expect:is_error(str:parse_int(<<"">>)), + expect:is_error(str:parse_int(<<"what">>)), + expect:is_error(str:parse_int(<<"1.23">>)). + +parse_float_test() -> + expect:equal(str:parse_float(<<"1.23">>), {ok, 1.23}), + expect:equal(str:parse_float(<<"5.0">>), {ok, 5.0}), + expect:equal(str:parse_float(<<"0.123456789">>), {ok, 0.123456789}), + expect:is_error(str:parse_float(<<"">>)), + expect:is_error(str:parse_float(<<"what">>)), + expect:is_error(str:parse_float(<<"1">>)). + +base_from_int_test() -> + expect:equal(str:base_from_int(100, 16), <<"64">>), + expect:equal(str:base_from_int(-100, 16), <<"-64">>). + +from_float_test() -> + expect:equal(str:from_float(123.0), <<"123.0">>), + expect:equal(str:from_float(-8.1), <<"-8.1">>). diff --git a/gen/test/tuple_test.erl b/gen/test/tuple_test.erl new file mode 100644 index 0000000..2f6a1ce --- /dev/null +++ b/gen/test/tuple_test.erl @@ -0,0 +1,23 @@ +-module(tuple_test). +-compile(no_auto_import). + +-export([new_test/0, first_test/0, second_test/0, swap_test/0, fetch_test/0]). + +new_test() -> + expect:equal(tuple:new(1, 2), {1, 2}), + expect:equal(tuple:new(2, <<"3">>), {2, <<"3">>}). + +first_test() -> + expect:equal(tuple:first({1, 2}), 1). + +second_test() -> + expect:equal(tuple:second({1, 2}), 2). + +swap_test() -> + expect:equal(tuple:swap({1, <<"2">>}), {<<"2">>, 1}). + +fetch_test() -> + Proplist = [{0, <<"1">>}, {1, <<"2">>}], + expect:equal(tuple:fetch(Proplist, 0), {ok, <<"1">>}), + expect:equal(tuple:fetch(Proplist, 1), {ok, <<"2">>}), + expect:is_error(tuple:fetch(Proplist, 2)). diff --git a/gen/tuple.erl b/gen/tuple.erl deleted file mode 100644 index ec38901..0000000 --- a/gen/tuple.erl +++ /dev/null @@ -1,58 +0,0 @@ --module(tuple). --compile(no_auto_import). --include_lib("eunit/include/eunit.hrl"). - --export([new/2, first/1, second/1, swap/1, fetch/2]). - -new(A, B) -> - {A, B}. - --ifdef(TEST). -new_test() -> - expect:equal(new(1, 2), {1, 2}), - expect:equal(new(2, <<"3">>), {2, <<"3">>}). --endif. - -first(Tup) -> - {A, _} = Tup, - A. - --ifdef(TEST). -first_test() -> - expect:equal(first({1, 2}), 1). --endif. - -second(Tup) -> - {_, A} = Tup, - A. - --ifdef(TEST). -second_test() -> - expect:equal(second({1, 2}), 2). --endif. - -swap(Tup) -> - {A, B} = Tup, - {B, A}. - --ifdef(TEST). -swap_test() -> - expect:equal(swap({1, <<"2">>}), {<<"2">>, 1}). --endif. - -fetch(Haystack, Needle) -> - list:find(Haystack, fun(Tuple) -> case first(Tuple) =:= Needle of - true -> - {ok, second(Tuple)}; - - false -> - {error, []} - end end). - --ifdef(TEST). -fetch_test() -> - Proplist = [{0, <<"1">>}, {1, <<"2">>}], - expect:equal(fetch(Proplist, 0), {ok, <<"1">>}), - expect:equal(fetch(Proplist, 1), {ok, <<"2">>}), - expect:is_error(fetch(Proplist, 2)). --endif. |