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 | |
parent | ae5597c1b27982aabe74eb16d5b0c890802730d9 (diff) | |
download | gleam_stdlib-8cfa606f3834cf7d05f5011bc68295a9d84263dc.tar.gz gleam_stdlib-8cfa606f3834cf7d05f5011bc68295a9d84263dc.zip |
stdlib: Split out tests
60 files changed, 2214 insertions, 2297 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. diff --git a/rebar.config b/rebar.config index 25588d6..ff3b0a2 100644 --- a/rebar.config +++ b/rebar.config @@ -1,5 +1,11 @@ {erl_opts, [debug_info]}. -{src_dirs, ["src", "gen"]}. +{src_dirs, ["src", "gen/src"]}. + +{profiles, [ + {test, [ + {src_dirs, ["gen/test"]} + ]} +]}. {pre_hooks, [{"(linux|darwin|solaris|win32)", compile, "gleam build ."}]}. diff --git a/src/any.gleam b/src/any.gleam index de37ca0..a2fa418 100644 --- a/src/any.gleam +++ b/src/any.gleam @@ -1,17 +1,11 @@ import list import atom -import tuple import result -import expect fn list_module() { list } -fn tuple_module() { - tuple -} - // `Any` data is data that we don"t know the type of yet. // We likely get data like this from interop with Erlang, or from // IO with the outside world. @@ -32,160 +26,21 @@ pub external fn unsafeCoerce(a) -> b = "gleam__stdlib" "identity"; pub external fn string(Any) -> Result(String, String) = "gleam__stdlib" "decode_string" -test string { - "" - |> from - |> string - |> expect:equal(_, Ok("")) - - "Hello" - |> from - |> string - |> expect:equal(_, Ok("Hello")) - - 1 - |> from - |> string - |> expect:equal(_, Error("Expected a String, got `1`")) - - [] - |> from - |> string - |> expect:equal(_, Error("Expected a String, got `[]`")) -} - pub external fn int(Any) -> Result(Int, String) = "gleam__stdlib" "decode_int" -test int { - 1 - |> from - |> int - |> expect:equal(_, Ok(1)) - - 2 - |> from - |> int - |> expect:equal(_, Ok(2)) - - 1.0 - |> from - |> int - |> expect:equal(_, Error("Expected an Int, got `1.0`")) - - [] - |> from - |> int - |> expect:equal(_, Error("Expected an Int, got `[]`")) -} - pub external fn float(Any) -> Result(Float, String) = "gleam__stdlib" "decode_float" -test float { - 1.0 - |> from - |> float - |> expect:equal(_, Ok(1.0)) - - 2.2 - |> from - |> float - |> expect:equal(_, Ok(2.2)) - - 1 - |> from - |> float - |> expect:equal(_, Error("Expected a Float, got `1`")) - - [] - |> from - |> float - |> expect:equal(_, Error("Expected a Float, got `[]`")) -} - -// TODO -// pub external fn atom(Any) -> Result(Atom, String) -// = "gleam__stdlib" "decode_atom" - -// test atom { -// make an atom here -// |> from -// |> atom -// |> expect:equal(_, Ok("")) - -// make an atom here -// |> from -// |> atom -// |> expect:equal(_, Ok("ok")) - -// 1 -// |> from -// |> atom -// |> expect:is_error - -// [] -// |> from -// |> atom -// |> expect:is_error -// } +pub external fn atom(Any) -> Result(atom:Atom, String) + = "gleam__stdlib" "decode_atom" pub external fn bool(Any) -> Result(Bool, String) = "gleam__stdlib" "decode_bool" -test bool { - True - |> from - |> bool - |> expect:equal(_, Ok(True)) - - False - |> from - |> bool - |> expect:equal(_, Ok(False)) - - 1 - |> from - |> bool - |> expect:equal(_, Error("Expected a Bool, got `1`")) - - [] - |> from - |> bool - |> expect:equal(_, Error("Expected a Bool, got `[]`")) -} - pub external fn thunk(Any) -> Result(fn() -> Any, String) = "gleam__stdlib" "decode_thunk" -test thunk { - fn() { 1 } - |> from - |> thunk - |> expect:is_ok - - fn() { 1 } - |> from - |> thunk - |> result:map(_, fn(f) { f() }) - |> expect:equal(_, Ok(from(1))) - - fn(x) { x } - |> from - |> thunk - |> expect:is_error - - 1 - |> from - |> thunk - |> expect:is_error - - [] - |> from - |> thunk - |> expect:is_error -} - external fn list_any(Any) -> Result(List(Any), String) = "gleam__stdlib" "decode_list" @@ -195,118 +50,8 @@ pub fn list(any, decode) { |> result:then(_, list_module():traverse(_, decode)) } -test list { - [] - |> from - |> list(_, string) - |> expect:equal(_, Ok([])) - - [] - |> from - |> list(_, int) - |> expect:equal(_, Ok([])) - - [1, 2, 3] - |> from - |> list(_, int) - |> expect:equal(_, Ok([1, 2, 3])) - - [[1], [2], [3]] - |> from - |> list(_, list(_, int)) - |> expect:equal(_, Ok([[1], [2], [3]])) - - 1 - |> from - |> list(_, string) - |> expect:is_error - - 1.0 - |> from - |> list(_, int) - |> expect:is_error - - [""] - |> from - |> list(_, int) - |> expect:is_error - - [from(1), from("not an int")] - |> from - |> list(_, int) - |> expect:is_error -} - pub external fn tuple(Any) -> Result({Any, Any}, String) = "gleam__stdlib" "decode_tuple" -test tuple { - {1, []} - |> from - |> tuple - |> expect:equal(_, Ok({from(1), from([])})) - - {"ok", "ok"} - |> from - |> tuple - |> expect:equal(_, Ok({from("ok"), from("ok")})) - - {1} - |> from - |> tuple - |> expect:is_error - - {1, 2, 3} - |> from - |> tuple - |> expect:is_error - - {1, 2.0} - |> from - |> tuple - |> result:then(_, fn(x) { - x - |> tuple_module():first - |> int - |> result:map(_, fn(f) { {f, tuple_module():second(x)} }) - }) - |> result:then(_, fn(x) { - x - |> tuple_module():second - |> float - |> result:map(_, fn(f) { {tuple_module():first(x), f} }) - }) - |> expect:equal(_, Ok({1, 2.0})) -} - pub external fn field(Any, a) -> Result(Any, String) = "gleam__stdlib" "decode_field" - -test field { - let Ok(ok_atom) = atom:from_string("ok") - - {ok = 1} - |> from - |> field(_, ok_atom) - |> expect:equal(_, Ok(from(1))) - - {earlier = 2, ok = 3} - |> from - |> field(_, ok_atom) - |> expect:equal(_, Ok(from(3))) - - {} - |> from - |> field(_, ok_atom) - |> expect:is_error - - 1 - |> from - |> field(_, ok_atom) - |> expect:is_error - - [] - |> from - |> field(_, []) - |> expect:is_error -} diff --git a/src/atom.gleam b/src/atom.gleam index e887a01..67d040d 100644 --- a/src/atom.gleam +++ b/src/atom.gleam @@ -1,5 +1,3 @@ -import expect - pub external type Atom; pub enum AtomNotLoaded = @@ -8,20 +6,6 @@ pub enum AtomNotLoaded = pub external fn from_string(String) -> Result(Atom, AtomNotLoaded) = "gleam__stdlib" "atom_from_string"; -test from_string { - "ok" - |> from_string - |> expect:is_ok - - "expect" - |> from_string - |> expect:is_ok - - "this is not an atom we have seen before" - |> from_string - |> expect:equal(_, Error(AtomNotLoaded)) -} - // This function can create a new atom if one does not already exist for // the given string. Atoms are not garbage collected so this can result // in a memory leak if called over time on new values @@ -29,36 +13,5 @@ test from_string { pub external fn create_from_string(String) -> Atom = "gleam__stdlib" "atom_create_from_string"; -test create_from_string { - let ok = fn(x) { Ok(x) } - - "ok" - |> create_from_string - |> ok - |> expect:equal(_, from_string("ok")) - - "expect" - |> create_from_string - |> ok - |> expect:equal(_, from_string("expect")) - - "this is another atom we have not seen before" - |> create_from_string - |> ok - |> expect:equal(_, from_string("this is another atom we have not seen before")) -} - pub external fn to_string(Atom) -> String = "gleam__stdlib" "atom_to_string"; - -test to_string { - "ok" - |> create_from_string - |> to_string - |> expect:equal(_, "ok") - - "expect" - |> create_from_string - |> to_string - |> expect:equal(_, "expect") -} diff --git a/src/bool.gleam b/src/bool.gleam index b4e5287..d1d9ac4 100644 --- a/src/bool.gleam +++ b/src/bool.gleam @@ -1,5 +1,4 @@ -import expect -import order +// import order pub fn negate(bool) { case bool { @@ -8,14 +7,6 @@ pub fn negate(bool) { } } -test negate { - negate(True) - |> expect:false - - negate(False) - |> expect:true -} - // pub fn compare(a, b) { // case {a, b} { // | {True, True} -> order:Eq @@ -25,20 +16,6 @@ test negate { // } // } -// test compare { -// compare(True, True) -// |> expect:equal(_, order:Eq) - -// compare(True, False) -// |> expect:equal(_, order:Gt) - -// compare(False, False) -// |> expect:equal(_, order:Lt) - -// compare(False, True) -// |> expect:equal(_, order:Gt) -// } - pub fn max(a, b) { case a { | True -> True @@ -46,20 +23,6 @@ pub fn max(a, b) { } } -test max { - max(True, True) - |> expect:equal(_, True) - - max(True, False) - |> expect:equal(_, True) - - max(False, False) - |> expect:equal(_, False) - - max(False, True) - |> expect:equal(_, True) -} - pub fn min(a, b) { case a { | False -> False @@ -67,31 +30,9 @@ pub fn min(a, b) { } } -test min { - min(True, True) - |> expect:equal(_, True) - - min(True, False) - |> expect:equal(_, False) - - min(False, False) - |> expect:equal(_, False) - - min(False, True) - |> expect:equal(_, False) -} - pub fn to_int(bool) { case bool { | False -> 0 | True -> 1 } } - -test to_int { - to_int(True) - |> expect:equal(_, 1) - - to_int(False) - |> expect:equal(_, 0) -} diff --git a/src/gleam__stdlib.erl b/src/gleam__stdlib.erl index 05623c6..5640e70 100644 --- a/src/gleam__stdlib.erl +++ b/src/gleam__stdlib.erl @@ -5,7 +5,7 @@ expect_is_ok/1, expect_is_error/1, atom_from_string/1, atom_create_from_string/1, atom_to_string/1, map_fetch/2, iodata_append/2, iodata_prepend/2, identity/1, decode_int/1, - decode_string/1, decode_bool/1, decode_float/1, decode_thunk/1, + decode_string/1, decode_bool/1, decode_float/1, decode_thunk/1, decode_atom/1, decode_tuple/1, decode_list/1, decode_field/2, parse_int/1, parse_float/1]). expect_equal(Actual, Expected) -> ?assertEqual(Expected, Actual). @@ -40,6 +40,9 @@ identity(X) -> X. decode_error_msg(Type, Data) -> {error, iolist_to_binary(io_lib:format("Expected ~s, got `~p`", [Type, Data]))}. +decode_atom(Data) when is_atom(Data) -> {ok, Data}; +decode_atom(Data) -> decode_error_msg("an Atom", Data). + decode_string(Data) when is_binary(Data) -> {ok, Data}; decode_string(Data) -> decode_error_msg("a String", Data). diff --git a/src/iodata.gleam b/src/iodata.gleam index 81329d0..56efc65 100644 --- a/src/iodata.gleam +++ b/src/iodata.gleam @@ -1,8 +1,3 @@ -import expect - -// concat should work on List(Iodata) -// need a name for the string version - pub external type Iodata; pub external fn prepend(Iodata, String) -> Iodata = @@ -35,54 +30,10 @@ pub external fn byte_size(Iodata) -> Int = pub external fn from_float(Float) -> Iodata = "io_lib_format" "fwrite_g"; -test iodata { - let iodata = new("ello") - |> append(_, ",") - |> append(_, " world!") - |> prepend(_, "H") - - iodata - |> to_string - |> expect:equal(_, "Hello, world!") - - iodata - |> byte_size - |> expect:equal(_, 13) - - let iodata = new("ello") - |> append_iodata(_, new(",")) - |> append_iodata(_, concat([new(" wo"), new("rld!")])) - |> prepend_iodata(_, new("H")) - - iodata - |> to_string - |> expect:equal(_, "Hello, world!") - - iodata - |> byte_size - |> expect:equal(_, 13) -} - pub external fn lowercase(Iodata) -> Iodata = "string" "lowercase" -test lowercase { - ["Gleam", "Gleam"] - |> from_strings - |> lowercase - |> to_string - |> expect:equal(_, "gleamgleam") -} - pub external fn uppercase(Iodata) -> Iodata = "string" "uppercase" -test uppercase { - ["Gleam", "Gleam"] - |> from_strings - |> uppercase - |> to_string - |> expect:equal(_, "GLEAMGLEAM") -} - pub external fn reverse(Iodata) -> Iodata = "string" "reverse" enum Direction = @@ -95,18 +46,6 @@ pub fn split(iodata, on) { erl_split(iodata, on, All) } -test split { - "Gleam,Erlang,Elixir" - |> new - |> split(_, ",") - |> expect:equal(_, [new("Gleam"), new("Erlang"), new("Elixir")]) - - ["Gleam, Erl", "ang,Elixir"] - |> from_strings - |> split(_, ", ") - |> expect:equal(_, [new("Gleam"), from_strings(["Erl", "ang,Elixir"])]) -} - external fn erl_replace(Iodata, String, String, Direction) -> Iodata = "string" "replace" @@ -116,36 +55,4 @@ pub fn replace(iodata, pattern, replacement) { pub external fn is_equal(Iodata, Iodata) -> Bool = "string" "equal" -test is_equal { - new("12") - |> is_equal(_, from_strings(["1", "2"])) - |> expect:true - - new("12") - |> is_equal(_, new("12")) - |> expect:true - - new("12") - |> is_equal(_, new("2")) - |> expect:false -} - pub external fn is_empty(Iodata) -> Bool = "string" "is_empty" - -test is_empty { - new("") - |> is_empty - |> expect:true - - new("12") - |> is_empty - |> expect:false - - from_strings([]) - |> is_empty - |> expect:true - - from_strings(["", ""]) - |> is_empty - |> expect:true -} diff --git a/src/list.gleam b/src/list.gleam index 5ccdda6..22da117 100644 --- a/src/list.gleam +++ b/src/list.gleam @@ -1,11 +1,8 @@ -import expect - // TODO: all // TODO: any // TODO: at // TODO: concat // TODO: index_map -// TODO: index_map // TODO: intersperse // TODO: sort // TODO: unique @@ -21,44 +18,21 @@ pub enum NotFound = // pub external fn length(List(a)) -> Int = "erlang" "length" -test length { - length([]) |> expect:equal(_, 0) - length([1]) |> expect:equal(_, 1) - length([1, 1]) |> expect:equal(_, 2) - length([1, 1, 1]) |> expect:equal(_, 3) -} - // Using the Erlang C BIF implementation. // pub external fn reverse(List(a)) -> List(a) = "lists" "reverse" -test reverse { - length([]) |> expect:equal(_, 0) - length([1, 2, 3, 4, 5]) |> expect:equal(_, 5) -} - pub fn is_empty(list) { list == [] } -test is_empty { - is_empty([]) |> expect:true - is_empty([1]) |> expect:false -} - -pub fn has_member(list, elem) { +pub fn contains(list, elem) { case list { | [] -> False - | [head | rest] -> head == elem || has_member(rest, elem) + | [head | rest] -> head == elem || contains(rest, elem) } } -test has_member { - has_member([0, 4, 5, 1], 1) |> expect:true - has_member([0, 4, 5, 7], 1) |> expect:false - has_member([], 1) |> expect:false -} - pub fn head(list) { case list { | [] -> Error(Empty) @@ -66,14 +40,6 @@ pub fn head(list) { } } -test head { - head([0, 4, 5, 7]) - |> expect:equal(_, Ok(0)) - - head([]) - |> expect:equal(_, Error(Empty)) -} - pub fn tail(list) { case list { | [] -> Error(Empty) @@ -81,17 +47,6 @@ pub fn tail(list) { } } -test tail { - tail([0, 4, 5, 7]) - |> expect:equal(_, Ok([4, 5, 7])) - - tail([0]) - |> expect:equal(_, Ok([])) - - tail([]) - |> expect:equal(_, Error(Empty)) -} - fn do_filter(list, fun, acc) { case list { | [] -> reverse(acc) @@ -109,24 +64,6 @@ pub fn filter(list, fun) { do_filter(list, fun, []) } -test filter { - [] - |> filter(_, fn(_) { True }) - |> expect:equal(_, []) - - [0, 4, 5, 7, 3] - |> filter(_, fn(_) { True }) - |> expect:equal(_, [0, 4, 5, 7, 3]) - - [0, 4, 5, 7, 3] - |> filter(_, fn(x) { x > 4 }) - |> expect:equal(_, [5, 7]) - - [0, 4, 5, 7, 3] - |> filter(_, fn(x) { x < 4 }) - |> expect:equal(_, [0, 3]) -} - fn do_map(list, fun, acc) { case list { | [] -> reverse(acc) @@ -138,16 +75,6 @@ pub fn map(list, fun) { do_map(list, fun, []) } -test map { - [] - |> map(_, fn(x) { x * 2 }) - |> expect:equal(_, []) - - [0, 4, 5, 7, 3] - |> map(_, fn(x) { x * 2 }) - |> expect:equal(_, [0, 8, 10, 14, 6]) -} - fn do_traverse(list, fun, acc) { case list { | [] -> Ok(reverse(acc)) @@ -163,24 +90,6 @@ pub fn traverse(list, fun) { do_traverse(list, fun, []) } -test traverse { - let fun = fn(x) { - case x == 6 || x == 5 || x == 4 { - | True -> Ok(x * 2) - | False -> Error(x) - } - } - - [5, 6, 5, 6] - |> traverse(_, fun) - |> expect:equal(_, Ok([10, 12, 10, 12])) - - [4, 6, 5, 7, 3] - |> traverse(_, fun) - |> expect:equal(_, Error(7)) -} - - pub fn drop(list, n) { case n <= 0 { | True -> list @@ -192,16 +101,6 @@ pub fn drop(list, n) { } } -test drop { - [] - |> drop(_, 5) - |> expect:equal(_, []) - - [1, 2, 3, 4, 5, 6, 7, 8] - |> drop(_, 5) - |> expect:equal(_, [6, 7, 8]) -} - fn do_take(list, n, acc) { case n <= 0 { | True -> reverse(acc) @@ -217,32 +116,12 @@ pub fn take(list, n) { do_take(list, n, []) } -test take { - [] - |> take(_, 5) - |> expect:equal(_, []) - [1, 2, 3, 4, 5, 6, 7, 8] - |> take(_, 5) - |> expect:equal(_, [1, 2, 3, 4, 5]) -} - pub fn new() { [] } -test new { - new() |> expect:equal(_, []) -} - pub external fn append(List(a), List(a)) -> List(a) = "lists" "append"; -test append { - expect:equal( - append([1], [2, 3]), - [1, 2, 3], - ) -} - fn do_flatten(lists, acc) { case lists { | [] -> acc @@ -254,46 +133,20 @@ pub fn flatten(lists) { do_flatten(lists, []) } -test flatten { - flatten([]) - |> expect:equal(_, []) - - flatten([[]]) - |> expect:equal(_, []) - - flatten([[], [], []]) - |> expect:equal(_, []) - - flatten([[1, 2], [], [3, 4]]) - |> expect:equal(_, [1, 2, 3, 4]) -} - -pub fn foldl(list, acc, fun) { +pub fn fold(list, acc, fun) { case list { | [] -> acc - | [x | rest] -> foldl(rest, fun(x, acc), fun) + | [x | rest] -> fold(rest, fun(x, acc), fun) } } -test foldl { - [1, 2, 3] - |> foldl(_, [], fn(x, acc) { [x | acc] }) - |> expect:equal(_, [3, 2, 1]) -} - -pub fn foldr(list, acc, fun) { +pub fn fold_right(list, acc, fun) { case list { | [] -> acc - | [x | rest] -> fun(x, foldr(rest, acc, fun)) + | [x | rest] -> fun(x, fold_right(rest, acc, fun)) } } -test foldr { - [1, 2, 3] - |> foldr(_, [], fn(x, acc) { [x | acc] }) - |> expect:equal(_, [1, 2, 3]) -} - pub fn find(haystack, f) { case haystack { | [] -> Error(NotFound) @@ -305,23 +158,3 @@ pub fn find(haystack, f) { } } -test find { - let f = fn(x) { - case x { - | 2 -> Ok(4) - | _ -> Error(NotFound) - } - } - - [1, 2, 3] - |> find(_, f) - |> expect:equal(_, Ok(4)) - - [1, 3, 2] - |> find(_, f) - |> expect:equal(_, Ok(4)) - - [1, 3] - |> find(_, f) - |> expect:equal(_, Error(NotFound)) -} diff --git a/src/map.gleam b/src/map.gleam deleted file mode 100644 index 31b8726..0000000 --- a/src/map.gleam +++ /dev/null @@ -1,190 +0,0 @@ -import any -import result -import expect - -// TODO: drop -// TODO: take -// TODO: update :: fn(Map(k, v), k, fn(Result(v, NotFound)) -> v) -> Map(k, v) - -pub external type Map(key, value); - -pub enum NotFound = - | NotFound - -pub external fn size(Map(k, v)) -> Int - = "maps" "size" - -pub external fn to_list(Map(key, value)) -> List({key, value}) - = "maps" "to_list" - -pub external fn from_list(List({key, value})) -> Map(key, value) - = "maps" "from_list" - -test from_list { - let proplist = [ - {4, 0}, - {1, 0}, - ] - let map = from_list(proplist) - - map - |> size - |> expect:equal(_, 2) -} - - -external fn is_key(key, Map(key, v)) -> Bool - = "maps" "is_key" - -pub fn has_key(map, key) { - is_key(key, map) -} - -test has_key { - [] - |> from_list - |> has_key(_, 1) - |> expect:false - - [ - {1, 0}, - ] - |> from_list - |> has_key(_, 1) - |> expect:true - - [ - {4, 0}, - {1, 0}, - ] - |> from_list - |> has_key(_, 1) - |> expect:true - - [ - {4, 0}, - {1, 0}, - ] - |> from_list - |> has_key(_, 0) - |> expect:false -} - -pub external fn new() -> Map(key, value) - = "maps" "new" - -test new { - new() - |> size - |> expect:equal(_, 0) - - new() - |> to_list - |> expect:equal(_, []) -} - -// pub fn from_record(record: {r | }) -> Map(Atom, any:Any) { -// any:unsafeCoerce(record) -// } - -// test from_record { -// let map = from_record({ name = "Jane" }) - -// map -// |> size -// |> expect:equal(_, 1) - -// map -// |> expect:equal(_, from_list([{"name", "Jane"}])) -// } - -pub external fn fetch(Map(key, value), key) -> Result(value, NotFound) - = "gleam__stdlib" "map_fetch"; - -test fetch { - let proplist = [ - {4, 0}, - {1, 1}, - ] - let map = from_list(proplist) - - map - |> fetch(_, 4) - |> expect:equal(_, Ok(0)) - - map - |> fetch(_, 1) - |> expect:equal(_, Ok(1)) - - map - |> fetch(_, 2) - |> expect:equal(_, Error(NotFound)) -} - -external fn erl_put(key, value, Map(key, value)) -> Map(key, value) - = "maps" "put"; - -pub fn put(map, key, value) { - erl_put(key, value, map) -} - -test put { - new() - |> put(_, "a", 0) - |> put(_, "b", 1) - |> put(_, "c", 2) - |> expect:equal(_, from_list([{"a", 0}, {"b", 1}, {"c", 2}])) -} - -external fn erl_map_values(fn(key, value) -> value, Map(key, value)) -> Map(key, value) - = "maps" "map"; - -pub fn map_values(map, fun) { - erl_map_values(fun, map) -} - -test map_values { - [ - {1, 0}, - {2, 1}, - {3, 2}, - ] - |> from_list - |> map_values(_, fn(k, v) { k + v }) - |> expect:equal(_, from_list([{1, 1}, {2, 3}, {3, 5}])) -} - -pub external fn keys(Map(keys, v)) -> List(keys) - = "maps" "keys" - -test keys { - [ - {"a", 0}, - {"b", 1}, - {"c", 2}, - ] - |> from_list - |> keys - |> expect:equal(_, ["a", "b", "c"]) -} - -pub external fn values(Map(k, values)) -> List(values) - = "maps" "values" - -test values { - [ - {"a", 0}, - {"b", 1}, - {"c", 2}, - ] - |> from_list - |> values - |> expect:equal(_, [0, 1, 2]) -} - -external fn erl_filter(fn(key, value) -> Bool, Map(key, value)) -> Map(key, value) - = "maps" "filter"; - -pub fn filter(map, fun) { - erl_filter(fun, map) -} diff --git a/src/map_dict.gleam b/src/map_dict.gleam new file mode 100644 index 0000000..e6f5d3f --- /dev/null +++ b/src/map_dict.gleam @@ -0,0 +1,60 @@ +import any +import result + +// TODO: drop +// TODO: take +// TODO: update :: fn(Map(k, v), k, fn(Result(v, NotFound)) -> v) -> Map(k, v) + +pub external type Map(key, value); + +pub enum NotFound = + | NotFound + +pub external fn size(Map(k, v)) -> Int + = "maps" "size" + +pub external fn to_list(Map(key, value)) -> List({key, value}) + = "maps" "to_list" + +pub external fn from_list(List({key, value})) -> Map(key, value) + = "maps" "from_list" + +external fn is_key(key, Map(key, v)) -> Bool + = "maps" "is_key" + +pub fn has_key(map, key) { + is_key(key, map) +} + +pub external fn new() -> Map(key, value) + = "maps" "new" + +pub external fn fetch(Map(key, value), key) -> Result(value, NotFound) + = "gleam__stdlib" "map_fetch"; + +external fn erl_put(key, value, Map(key, value)) -> Map(key, value) + = "maps" "put"; + +pub fn put(map, key, value) { + erl_put(key, value, map) +} + +external fn erl_map_values(fn(key, value) -> value, Map(key, value)) -> Map(key, value) + = "maps" "map"; + +pub fn map_values(map, fun) { + erl_map_values(fun, map) +} + +pub external fn keys(Map(keys, v)) -> List(keys) + = "maps" "keys" + +pub external fn values(Map(k, values)) -> List(values) + = "maps" "values" + +external fn erl_filter(fn(key, value) -> Bool, Map(key, value)) -> Map(key, value) + = "maps" "filter"; + +pub fn filter(map, fun) { + erl_filter(fun, map) +} diff --git a/src/order.gleam b/src/order.gleam index 9e69114..4d39705 100644 --- a/src/order.gleam +++ b/src/order.gleam @@ -1,5 +1,3 @@ -import expect - pub enum Order = | Lt | Eq @@ -14,12 +12,6 @@ pub fn reverse(order) { } } -test reverse { - reverse(Lt) |> expect:equal(_, Gt) - reverse(Eq) |> expect:equal(_, Eq) - reverse(Gt) |> expect:equal(_, Lt) -} - pub fn to_int(order) { case order { | Lt -> -1 @@ -28,12 +20,6 @@ pub fn to_int(order) { } } -test to_int { - to_int(Lt) |> expect:equal(_, -1) - to_int(Eq) |> expect:equal(_, 0) - to_int(Gt) |> expect:equal(_, 1) -} - pub fn compare(a, b) { case {a, b} { | {Lt, Lt} -> Eq @@ -45,18 +31,6 @@ pub fn compare(a, b) { } } -test compare { - compare(Lt, Lt) |> expect:equal(_, Eq) - compare(Lt, Eq) |> expect:equal(_, Lt) - compare(Lt, Gt) |> expect:equal(_, Lt) - compare(Eq, Lt) |> expect:equal(_, Gt) - compare(Eq, Eq) |> expect:equal(_, Eq) - compare(Eq, Gt) |> expect:equal(_, Lt) - compare(Gt, Lt) |> expect:equal(_, Gt) - compare(Gt, Eq) |> expect:equal(_, Gt) - compare(Gt, Gt) |> expect:equal(_, Eq) -} - pub fn max(a, b) { case {a, b} { | {Gt, _} -> Gt @@ -65,18 +39,6 @@ pub fn max(a, b) { } } -test max { - max(Lt, Lt) |> expect:equal(_, Lt) - max(Lt, Eq) |> expect:equal(_, Eq) - max(Lt, Gt) |> expect:equal(_, Gt) - max(Eq, Lt) |> expect:equal(_, Eq) - max(Eq, Eq) |> expect:equal(_, Eq) - max(Eq, Gt) |> expect:equal(_, Gt) - max(Gt, Lt) |> expect:equal(_, Gt) - max(Gt, Eq) |> expect:equal(_, Gt) - max(Gt, Gt) |> expect:equal(_, Gt) -} - pub fn min(a, b) { case {a, b} { | {Lt, _} -> Lt @@ -84,15 +46,3 @@ pub fn min(a, b) { | _ -> b } } - -test min { - min(Lt, Lt) |> expect:equal(_, Lt) - min(Lt, Eq) |> expect:equal(_, Lt) - min(Lt, Gt) |> expect:equal(_, Lt) - min(Eq, Lt) |> expect:equal(_, Lt) - min(Eq, Eq) |> expect:equal(_, Eq) - min(Eq, Gt) |> expect:equal(_, Eq) - min(Gt, Lt) |> expect:equal(_, Lt) - min(Gt, Eq) |> expect:equal(_, Eq) - min(Gt, Gt) |> expect:equal(_, Gt) -} diff --git a/src/result.gleam b/src/result.gleam index a2bd330..133d9d5 100644 --- a/src/result.gleam +++ b/src/result.gleam @@ -1,5 +1,3 @@ -import expect - // Result represents the result of something that may succeed or fail. // `Ok` means it was successful, `Error` means it failed. @@ -10,11 +8,6 @@ pub fn is_ok(result) { } } -test is_ok { - is_ok(Ok(1)) |> expect:true - is_ok(Error(1)) |> expect:false -} - pub fn is_error(result) { case result { | Ok(_) -> False @@ -22,14 +15,6 @@ pub fn is_error(result) { } } -test is_error { - is_error(Ok(1)) - |> expect:false - - is_error(Error(1)) - |> expect:true -} - pub fn map(result, fun) { case result { | Ok(x) -> Ok(fun(x)) @@ -37,20 +22,6 @@ pub fn map(result, fun) { } } -test map { - Ok(1) - |> map(_, fn(x) { x + 1 }) - |> expect:equal(_, Ok(2)) - - Ok(1) - |> map(_, fn(_) { "2" }) - |> expect:equal(_, Ok("2")) - - Error(1) - |> map(_, fn(x) { x + 1 }) - |> expect:equal(_, Error(1)) -} - pub fn map_error(result, fun) { case result { | Ok(_) -> result @@ -58,16 +29,6 @@ pub fn map_error(result, fun) { } } -test map_error { - Ok(1) - |> map_error(_, fn(x) { x + 1 }) - |> expect:equal(_, Ok(1)) - - Error(1) - |> map_error(_, fn(x) { x + 1 }) - |> expect:equal(_, Error(2)) -} - pub fn flatten(result) { case result { | Ok(x) -> x @@ -75,20 +36,6 @@ pub fn flatten(result) { } } -test flatten { - flatten(Ok(Ok(1))) - |> expect:equal(_, Ok(1)) - - flatten(Ok(Error(1))) - |> expect:equal(_, Error(1)) - - flatten(Error(1)) - |> expect:equal(_, Error(1)) - - flatten(Error(Error(1))) - |> expect:equal(_, Error(Error(1))) -} - pub fn then(result, fun) { case result { | Ok(x) -> fun(x) @@ -96,35 +43,9 @@ pub fn then(result, fun) { } } -test then { - Error(1) - |> then(_, fn(x) { Ok(x + 1) }) - |> expect:equal(_, Error(1)) - - Ok(1) - |> then(_, fn(x) { Ok(x + 1) }) - |> expect:equal(_, Ok(2)) - - Ok(1) - |> then(_, fn(_) { Ok("type change") }) - |> expect:equal(_, Ok("type change")) - - Ok(1) - |> then(_, fn(_) { Error(1) }) - |> expect:equal(_, Error(1)) -} - pub fn unwrap(result, default) { case result { | Ok(v) -> v | Error(_) -> default } } - -test unwrap { - unwrap(Ok(1), 50) - |> expect:equal(_, 1) - - unwrap(Error("nope"), 50) - |> expect:equal(_, 50) -} diff --git a/src/str.gleam b/src/str.gleam index bd027a1..00268f8 100644 --- a/src/str.gleam +++ b/src/str.gleam @@ -1,7 +1,6 @@ // Named str to avoid name collisions with the Erlang string module. // Will rename later once we have namespaces for modules. -import expect import iodata import list @@ -10,31 +9,10 @@ pub external fn length(String) -> Int = "string" "length" pub enum ParseError = | ParseError -test length { - length("ß↑e̊") - |> expect:equal(_, 3) - - length("Gleam") - |> expect:equal(_, 5) - - length("") - |> expect:equal(_, 0) -} - pub external fn lowercase(String) -> String = "string" "lowercase" -test lowercase { - lowercase("Gleam") - |> expect:equal(_, "gleam") -} - pub external fn uppercase(String) -> String = "string" "uppercase" -test uppercase { - uppercase("Gleam") - |> expect:equal(_, "GLEAM") -} - pub fn reverse(string) { string |> iodata:new @@ -42,11 +20,6 @@ pub fn reverse(string) { |> iodata:to_string } -test reverse { - reverse("Gleam") - |> expect:equal(_, "maelG") -} - pub fn split(string, on) { string |> iodata:new @@ -54,16 +27,6 @@ pub fn split(string, on) { |> list:map(_, iodata:to_string) } -test split { - "Gleam,Erlang,Elixir" - |> split(_, ",") - |> expect:equal(_, ["Gleam", "Erlang", "Elixir"]) - - "Gleam, Erlang,Elixir" - |> split(_, ", ") - |> expect:equal(_, ["Gleam", "Erlang,Elixir"]) -} - pub fn replace(string, pattern, with) { string |> iodata:new @@ -71,108 +34,16 @@ pub fn replace(string, pattern, with) { |> iodata:to_string } -test replace { - "Gleam,Erlang,Elixir" - |> replace(_, ",", "++") - |> expect:equal(_, "Gleam++Erlang++Elixir") -} - pub external fn from_int(Int) -> String = "erlang" "integer_to_binary" -test from_int { - 123 - |> from_int - |> expect:equal(_, "123") - - -123 - |> from_int - |> expect:equal(_, "-123") - - 0123 - |> from_int - |> expect:equal(_, "123") -} - pub external fn parse_int(String) -> Result(Int, ParseError) = "gleam__stdlib" "parse_int"; -test parse_int { - "123" - |> parse_int - |> expect:equal(_, Ok(123)) - - "-123" - |> parse_int - |> expect:equal(_, Ok(-123)) - - "0123" - |> parse_int - |> expect:equal(_, Ok(123)) - - "" - |> parse_int - |> expect:equal(_, Error(ParseError)) - - "what" - |> parse_int - |> expect:equal(_, Error(ParseError)) - - "1.23" - |> parse_int - |> expect:equal(_, Error(ParseError)) -} - pub external fn parse_float(String) -> Result(Float, ParseError) = "gleam__stdlib" "parse_float"; -test parse_float { - "1.23" - |> parse_float - |> expect:equal(_, Ok(1.23)) - - "5.0" - |> parse_float - |> expect:equal(_, Ok(5.0)) - - "0.123456789" - |> parse_float - |> expect:equal(_, Ok(0.123456789)) - - "" - |> parse_float - |> expect:equal(_, Error(ParseError)) - - "what" - |> parse_float - |> expect:equal(_, Error(ParseError)) - - "1" - |> parse_float - |> expect:equal(_, Error(ParseError)) -} - pub external fn base_from_int(Int, Int) -> String = "erlang" "integer_to_binary" -test base_from_int { - 100 - |> base_from_int(_, 16) - |> expect:equal(_, "64") - - -100 - |> base_from_int(_, 16) - |> expect:equal(_, "-64") -} - pub fn from_float(f) { f |> iodata:from_float |> iodata:to_string } - -test from_float { - 123.0 - |> from_float - |> expect:equal(_, "123.0") - - -8.1 - |> from_float - |> expect:equal(_, "-8.1") -} diff --git a/src/tuple.gleam b/src/tuple.gleam index 418c5bc..41902db 100644 --- a/src/tuple.gleam +++ b/src/tuple.gleam @@ -1,52 +1,24 @@ -import expect -import result import list pub fn new(a, b) { {a, b} } -test new { - new(1, 2) - |> expect:equal(_, {1, 2}) - - new(2, "3") - |> expect:equal(_, {2, "3"}) -} - pub fn first(tup) { let {a, _} = tup a } -test first { - {1, 2} - |> first - |> expect:equal(_, 1) -} - pub fn second(tup) { let {_, a} = tup a } -test second { - {1, 2} - |> second - |> expect:equal(_, 2) -} - pub fn swap(tup) { let {a, b} = tup {b, a} } -test swap { - {1, "2"} - |> swap - |> expect:equal(_, {"2", 1}) -} - pub fn fetch(haystack, needle) { list:find(haystack, fn(tuple) { case first(tuple) == needle { @@ -55,19 +27,3 @@ pub fn fetch(haystack, needle) { } }) } - -test fetch { - let proplist = [{0, "1"}, {1, "2"}] - - proplist - |> fetch(_, 0) - |> expect:equal(_, Ok("1")) - - proplist - |> fetch(_, 1) - |> expect:equal(_, Ok("2")) - - proplist - |> fetch(_, 2) - |> expect:is_error -} diff --git a/test/any_test.gleam b/test/any_test.gleam new file mode 100644 index 0000000..9c9d236 --- /dev/null +++ b/test/any_test.gleam @@ -0,0 +1,278 @@ +import any +import atom +import list +import tuple +import expect +import result + +pub fn string_test() { + "" + |> any:from + |> any:string + |> expect:equal(_, Ok("")) + + "Hello" + |> any:from + |> any:string + |> expect:equal(_, Ok("Hello")) + + 1 + |> any:from + |> any:string + |> expect:equal(_, Error("Expected a String, got `1`")) + + [] + |> any:from + |> any:string + |> expect:equal(_, Error("Expected a String, got `[]`")) +} + +pub fn int_test() { + 1 + |> any:from + |> any:int + |> expect:equal(_, Ok(1)) + + 2 + |> any:from + |> any:int + |> expect:equal(_, Ok(2)) + + 1.0 + |> any:from + |> any:int + |> expect:equal(_, Error("Expected an Int, got `1.0`")) + + [] + |> any:from + |> any:int + |> expect:equal(_, Error("Expected an Int, got `[]`")) +} + +pub fn float_test() { + 1.0 + |> any:from + |> any:float + |> expect:equal(_, Ok(1.0)) + + 2.2 + |> any:from + |> any:float + |> expect:equal(_, Ok(2.2)) + + 1 + |> any:from + |> any:float + |> expect:equal(_, Error("Expected a Float, got `1`")) + + [] + |> any:from + |> any:float + |> expect:equal(_, Error("Expected a Float, got `[]`")) +} + +// pub fn atom_test() { +// make an atom here +// |> any:from +// |> atom +// |> expect:equal(_, Ok("")) + +// make an atom here +// |> any:from +// |> atom +// |> expect:equal(_, Ok("ok")) + +// 1 +// |> any:from +// |> atom +// |> expect:is_error + +// [] +// |> any:from +// |> atom +// |> expect:is_error +// } + +pub fn thunk_test() { + fn() { 1 } + |> any:from + |> any:thunk + |> expect:is_ok + + fn() { 1 } + |> any:from + |> any:thunk + |> result:map(_, fn(f) { f() }) + |> expect:equal(_, Ok(any:from(1))) + + fn(x) { x } + |> any:from + |> any:thunk + |> expect:is_error + + 1 + |> any:from + |> any:thunk + |> expect:is_error + + [] + |> any:from + |> any:thunk + |> expect:is_error +} + +pub fn bool_test() { + True + |> any:from + |> any:bool + |> expect:equal(_, Ok(True)) + + False + |> any:from + |> any:bool + |> expect:equal(_, Ok(False)) + + 1 + |> any:from + |> any:bool + |> expect:equal(_, Error("Expected a Bool, got `1`")) + + [] + |> any:from + |> any:bool + |> expect:equal(_, Error("Expected a Bool, got `[]`")) +} + +pub fn atom_test() { + "" + |> atom:create_from_string + |> any:from + |> any:atom + |> expect:equal(_, Ok(atom:create_from_string(""))) + + "ok" + |> atom:create_from_string + |> any:from + |> any:atom + |> expect:equal(_, Ok(atom:create_from_string("ok"))) + + 1 + |> any:from + |> any:atom + |> expect:is_error + + [] + |> any:from + |> any:atom + |> expect:is_error +} + +pub fn list_test() { + [] + |> any:from + |> any:list(_, any:string) + |> expect:equal(_, Ok([])) + + [] + |> any:from + |> any:list(_, any:int) + |> expect:equal(_, Ok([])) + + [1, 2, 3] + |> any:from + |> any:list(_, any:int) + |> expect:equal(_, Ok([1, 2, 3])) + + [[1], [2], [3]] + |> any:from + |> any:list(_, any:list(_, any:int)) + |> expect:equal(_, Ok([[1], [2], [3]])) + + 1 + |> any:from + |> any:list(_, any:string) + |> expect:is_error + + 1.0 + |> any:from + |> any:list(_, any:int) + |> expect:is_error + + [""] + |> any:from + |> any:list(_, any:int) + |> expect:is_error + + [any:from(1), any:from("not an int")] + |> any:from + |> any:list(_, any:int) + |> expect:is_error +} + +pub fn tuple_test() { + {1, []} + |> any:from + |> any:tuple + |> expect:equal(_, Ok({any:from(1), any:from([])})) + + {"ok", "ok"} + |> any:from + |> any:tuple + |> expect:equal(_, Ok({any:from("ok"), any:from("ok")})) + + {1} + |> any:from + |> any:tuple + |> expect:is_error + + {1, 2, 3} + |> any:from + |> any:tuple + |> expect:is_error + + {1, 2.0} + |> any:from + |> any:tuple + |> result:then(_, fn(x) { + x + |> tuple:first + |> any:int + |> result:map(_, fn(f) { {f, tuple:second(x)} }) + }) + |> result:then(_, fn(x) { + x + |> tuple:second + |> any:float + |> result:map(_, fn(f) { {tuple:first(x), f} }) + }) + |> expect:equal(_, Ok({1, 2.0})) +} + +pub fn field_test() { + let Ok(ok_atom) = atom:from_string("ok") + + {ok = 1} + |> any:from + |> any:field(_, ok_atom) + |> expect:equal(_, Ok(any:from(1))) + + {earlier = 2, ok = 3} + |> any:from + |> any:field(_, ok_atom) + |> expect:equal(_, Ok(any:from(3))) + + {} + |> any:from + |> any:field(_, ok_atom) + |> expect:is_error + + 1 + |> any:from + |> any:field(_, ok_atom) + |> expect:is_error + + [] + |> any:from + |> any:field(_, []) + |> expect:is_error +} diff --git a/test/atom_test.gleam b/test/atom_test.gleam new file mode 100644 index 0000000..60686ab --- /dev/null +++ b/test/atom_test.gleam @@ -0,0 +1,46 @@ +import atom +import expect + +pub fn from_string_test() { + "ok" + |> atom:from_string + |> expect:is_ok + + "expect" + |> atom:from_string + |> expect:is_ok + + "this is not an atom we have seen before" + |> atom:from_string + // |> expect:equal(_, Error(AtomNotLoaded)) + |> expect:is_error +} + +pub fn create_from_string_test() { + "ok" + |> atom:create_from_string + |> Ok + |> expect:equal(_, atom:from_string("ok")) + + "expect" + |> atom:create_from_string + |> Ok + |> expect:equal(_, atom:from_string("expect")) + + "this is another atom we have not seen before" + |> atom:create_from_string + |> Ok + |> expect:equal(_, atom:from_string("this is another atom we have not seen before")) +} + +pub fn to_string_test() { + "ok" + |> atom:create_from_string + |> atom:to_string + |> expect:equal(_, "ok") + + "expect" + |> atom:create_from_string + |> atom:to_string + |> expect:equal(_, "expect") +} diff --git a/test/bool_test.gleam b/test/bool_test.gleam new file mode 100644 index 0000000..78c711b --- /dev/null +++ b/test/bool_test.gleam @@ -0,0 +1,61 @@ +import bool +// import order +import expect + +pub fn negate_test() { + bool:negate(True) + |> expect:false + + bool:negate(False) + |> expect:true +} + +// pub fn compare_test() { +// bool:compare(True, True) +// |> expect:equal(_, order:Eq) + +// bool:compare(True, False) +// |> expect:equal(_, order:Gt) + +// bool:compare(False, False) +// |> expect:equal(_, order:Lt) + +// bool:compare(False, True) +// |> expect:equal(_, order:Gt) +// } + +pub fn max_test() { + bool:max(True, True) + |> expect:equal(_, True) + + bool:max(True, False) + |> expect:equal(_, True) + + bool:max(False, False) + |> expect:equal(_, False) + + bool:max(False, True) + |> expect:equal(_, True) +} + +pub fn min_test() { + bool:min(True, True) + |> expect:equal(_, True) + + bool:min(True, False) + |> expect:equal(_, False) + + bool:min(False, False) + |> expect:equal(_, False) + + bool:min(False, True) + |> expect:equal(_, False) +} + +pub fn to_int_test() { + bool:to_int(True) + |> expect:equal(_, 1) + + bool:to_int(False) + |> expect:equal(_, 0) +} diff --git a/test/http_test.gleam b/test/http_test.gleam new file mode 100644 index 0000000..9941d56 --- /dev/null +++ b/test/http_test.gleam @@ -0,0 +1 @@ +// Nothing here yet... diff --git a/test/iodata_test.gleam b/test/iodata_test.gleam new file mode 100644 index 0000000..e57c8cc --- /dev/null +++ b/test/iodata_test.gleam @@ -0,0 +1,90 @@ +import expect +import iodata + +pub fn iodata_test() { + let data = iodata:new("ello") + |> iodata:append(_, ",") + |> iodata:append(_, " world!") + |> iodata:prepend(_, "H") + + data + |> iodata:to_string + |> expect:equal(_, "Hello, world!") + + data + |> iodata:byte_size + |> expect:equal(_, 13) + + let data = iodata:new("ello") + |> iodata:append_iodata(_, iodata:new(",")) + |> iodata:append_iodata(_, iodata:concat([iodata:new(" wo"), iodata:new("rld!")])) + |> iodata:prepend_iodata(_, iodata:new("H")) + + data + |> iodata:to_string + |> expect:equal(_, "Hello, world!") + + data + |> iodata:byte_size + |> expect:equal(_, 13) +} + +pub fn lowercase_test() { + ["Gleam", "Gleam"] + |> iodata:from_strings + |> iodata:lowercase + |> iodata:to_string + |> expect:equal(_, "gleamgleam") +} + +pub fn uppercase_test() { + ["Gleam", "Gleam"] + |> iodata:from_strings + |> iodata:uppercase + |> iodata:to_string + |> expect:equal(_, "GLEAMGLEAM") +} + +pub fn split_test() { + "Gleam,Erlang,Elixir" + |> iodata:new + |> iodata:split(_, ",") + |> expect:equal(_, [iodata:new("Gleam"), iodata:new("Erlang"), iodata:new("Elixir")]) + + ["Gleam, Erl", "ang,Elixir"] + |> iodata:from_strings + |> iodata:split(_, ", ") + |> expect:equal(_, [iodata:new("Gleam"), iodata:from_strings(["Erl", "ang,Elixir"])]) +} + +pub fn is_equal_test() { + iodata:new("12") + |> iodata:is_equal(_, iodata:from_strings(["1", "2"])) + |> expect:true + + iodata:new("12") + |> iodata:is_equal(_, iodata:new("12")) + |> expect:true + + iodata:new("12") + |> iodata:is_equal(_, iodata:new("2")) + |> expect:false +} + +pub fn is_empty_test() { + iodata:new("") + |> iodata:is_empty + |> expect:true + + iodata:new("12") + |> iodata:is_empty + |> expect:false + + iodata:from_strings([]) + |> iodata:is_empty + |> expect:true + + iodata:from_strings(["", ""]) + |> iodata:is_empty + |> expect:true +} diff --git a/test/list_test.gleam b/test/list_test.gleam new file mode 100644 index 0000000..c0a16ae --- /dev/null +++ b/test/list_test.gleam @@ -0,0 +1,164 @@ +import expect +import list + +pub fn length_test() { + list:length([]) |> expect:equal(_, 0) + list:length([1]) |> expect:equal(_, 1) + list:length([1, 1]) |> expect:equal(_, 2) + list:length([1, 1, 1]) |> expect:equal(_, 3) +} + +pub fn reverse_test() { + list:reverse([]) |> expect:equal(_, []) + list:reverse([1, 2, 3, 4, 5]) |> expect:equal(_, [5, 4, 3, 2, 1]) +} + +pub fn is_empty_test() { + list:is_empty([]) |> expect:true + list:is_empty([1]) |> expect:false +} + +pub fn contains_test() { + list:contains([0, 4, 5, 1], 1) |> expect:true + list:contains([0, 4, 5, 7], 1) |> expect:false + list:contains([], 1) |> expect:false +} + +pub fn head_test() { + list:head([0, 4, 5, 7]) + |> expect:equal(_, Ok(0)) + + list:head([]) + |> expect:is_error +} + +pub fn tail_test() { + list:tail([0, 4, 5, 7]) + |> expect:equal(_, Ok([4, 5, 7])) + + list:tail([0]) + |> expect:equal(_, Ok([])) + + list:tail([]) + |> expect:is_error +} + +pub fn filter_test() { + [] + |> list:filter(_, fn(_) { True }) + |> expect:equal(_, []) + + [0, 4, 5, 7, 3] + |> list:filter(_, fn(_) { True }) + |> expect:equal(_, [0, 4, 5, 7, 3]) + + [0, 4, 5, 7, 3] + |> list:filter(_, fn(x) { x > 4 }) + |> expect:equal(_, [5, 7]) + + [0, 4, 5, 7, 3] + |> list:filter(_, fn(x) { x < 4 }) + |> expect:equal(_, [0, 3]) +} + +pub fn map_test() { + [] + |> list:map(_, fn(x) { x * 2 }) + |> expect:equal(_, []) + + [0, 4, 5, 7, 3] + |> list:map(_, fn(x) { x * 2 }) + |> expect:equal(_, [0, 8, 10, 14, 6]) +} + +pub fn traverse_test() { + let fun = fn(x) { + case x == 6 || x == 5 || x == 4 { + | True -> Ok(x * 2) + | False -> Error(x) + } + } + + [5, 6, 5, 6] + |> list:traverse(_, fun) + |> expect:equal(_, Ok([10, 12, 10, 12])) + + [4, 6, 5, 7, 3] + |> list:traverse(_, fun) + |> expect:equal(_, Error(7)) +} + +pub fn drop_test() { + [] + |> list:drop(_, 5) + |> expect:equal(_, []) + + [1, 2, 3, 4, 5, 6, 7, 8] + |> list:drop(_, 5) + |> expect:equal(_, [6, 7, 8]) +} + +pub fn take_test() { + [] + |> list:take(_, 5) + |> expect:equal(_, []) + [1, 2, 3, 4, 5, 6, 7, 8] + |> list:take(_, 5) + |> expect:equal(_, [1, 2, 3, 4, 5]) +} + +pub fn new_test() { + list:new() |> expect:equal(_, []) +} + +pub fn append_test() { + list:append([1], [2, 3]) + |> expect:equal(_, [1, 2, 3]) +} + +pub fn flatten_test() { + list:flatten([]) + |> expect:equal(_, []) + + list:flatten([[]]) + |> expect:equal(_, []) + + list:flatten([[], [], []]) + |> expect:equal(_, []) + + list:flatten([[1, 2], [], [3, 4]]) + |> expect:equal(_, [1, 2, 3, 4]) +} + +pub fn fold_test() { + [1, 2, 3] + |> list:fold(_, [], fn(x, acc) { [x | acc] }) + |> expect:equal(_, [3, 2, 1]) +} + +pub fn fold_right_test() { + [1, 2, 3] + |> list:fold_right(_, [], fn(x, acc) { [x | acc] }) + |> expect:equal(_, [1, 2, 3]) +} + +pub fn find_test() { + let f = fn(x) { + case x { + | 2 -> Ok(4) + | _ -> Error(0) + } + } + + [1, 2, 3] + |> list:find(_, f) + |> expect:equal(_, Ok(4)) + + [1, 3, 2] + |> list:find(_, f) + |> expect:equal(_, Ok(4)) + + [1, 3] + |> list:find(_, f) + |> expect:is_error +} diff --git a/test/map_dict_test.gleam b/test/map_dict_test.gleam new file mode 100644 index 0000000..02b7072 --- /dev/null +++ b/test/map_dict_test.gleam @@ -0,0 +1,113 @@ +import expect +import map_dict + +pub fn from_list_test() { + [ + {4, 0}, + {1, 0}, + ] + |> map_dict:from_list + |> map_dict:size + |> expect:equal(_, 2) +} + +pub fn has_key_test() { + [] + |> map_dict:from_list + |> map_dict:has_key(_, 1) + |> expect:false + + [ + {1, 0}, + ] + |> map_dict:from_list + |> map_dict:has_key(_, 1) + |> expect:true + + [ + {4, 0}, + {1, 0}, + ] + |> map_dict:from_list + |> map_dict:has_key(_, 1) + |> expect:true + + [ + {4, 0}, + {1, 0}, + ] + |> map_dict:from_list + |> map_dict:has_key(_, 0) + |> expect:false +} + +pub fn new_test() { + map_dict:new() + |> map_dict:size + |> expect:equal(_, 0) + + map_dict:new() + |> map_dict:to_list + |> expect:equal(_, []) +} + +pub fn fetch_test() { + let proplist = [ + {4, 0}, + {1, 1}, + ] + let m = map_dict:from_list(proplist) + + m + |> map_dict:fetch(_, 4) + |> expect:equal(_, Ok(0)) + + m + |> map_dict:fetch(_, 1) + |> expect:equal(_, Ok(1)) + + m + |> map_dict:fetch(_, 2) + |> expect:is_error +} + +pub fn put_test() { + map_dict:new() + |> map_dict:put(_, "a", 0) + |> map_dict:put(_, "b", 1) + |> map_dict:put(_, "c", 2) + |> expect:equal(_, map_dict:from_list([{"a", 0}, {"b", 1}, {"c", 2}])) +} + +pub fn map_values_test() { + [ + {1, 0}, + {2, 1}, + {3, 2}, + ] + |> map_dict:from_list + |> map_dict:map_values(_, fn(k, v) { k + v }) + |> expect:equal(_, map_dict:from_list([{1, 1}, {2, 3}, {3, 5}])) +} + +pub fn keys_test() { + [ + {"a", 0}, + {"b", 1}, + {"c", 2}, + ] + |> map_dict:from_list + |> map_dict:keys + |> expect:equal(_, ["a", "b", "c"]) +} + +pub fn values_test() { + [ + {"a", 0}, + {"b", 1}, + {"c", 2}, + ] + |> map_dict:from_list + |> map_dict:values + |> expect:equal(_, [0, 1, 2]) +} diff --git a/test/order_test.gleam b/test/order_test.gleam new file mode 100644 index 0000000..07b527c --- /dev/null +++ b/test/order_test.gleam @@ -0,0 +1,52 @@ +import expect +import order + +// TODO: https://github.com/lpil/gleam/issues/141 + +// pub fn reverse_test() { +// order:reverse(Lt) |> expect:equal(_, Gt) +// order:reverse(Eq) |> expect:equal(_, Eq) +// order:reverse(Gt) |> expect:equal(_, Lt) +// } + +// pub fn to_int_test() { +// order:to_int(Lt) |> expect:equal(_, -1) +// order:to_int(Eq) |> expect:equal(_, 0) +// order:to_int(Gt) |> expect:equal(_, 1) +// } + +// pub fn compare_test() { +// order:compare(Lt, Lt) |> expect:equal(_, Eq) +// order:compare(Lt, Eq) |> expect:equal(_, Lt) +// order:compare(Lt, Gt) |> expect:equal(_, Lt) +// order:compare(Eq, Lt) |> expect:equal(_, Gt) +// order:compare(Eq, Eq) |> expect:equal(_, Eq) +// order:compare(Eq, Gt) |> expect:equal(_, Lt) +// order:compare(Gt, Lt) |> expect:equal(_, Gt) +// order:compare(Gt, Eq) |> expect:equal(_, Gt) +// order:compare(Gt, Gt) |> expect:equal(_, Eq) +// } + +// pub fn max_test() { +// order:max(Lt, Lt) |> expect:equal(_, Lt) +// order:max(Lt, Eq) |> expect:equal(_, Eq) +// order:max(Lt, Gt) |> expect:equal(_, Gt) +// order:max(Eq, Lt) |> expect:equal(_, Eq) +// order:max(Eq, Eq) |> expect:equal(_, Eq) +// order:max(Eq, Gt) |> expect:equal(_, Gt) +// order:max(Gt, Lt) |> expect:equal(_, Gt) +// order:max(Gt, Eq) |> expect:equal(_, Gt) +// order:max(Gt, Gt) |> expect:equal(_, Gt) +// } + +// pub fn min_test() { +// order:min(Lt, Lt) |> expect:equal(_, Lt) +// order:min(Lt, Eq) |> expect:equal(_, Lt) +// order:min(Lt, Gt) |> expect:equal(_, Lt) +// order:min(Eq, Lt) |> expect:equal(_, Lt) +// order:min(Eq, Eq) |> expect:equal(_, Eq) +// order:min(Eq, Gt) |> expect:equal(_, Eq) +// order:min(Gt, Lt) |> expect:equal(_, Lt) +// order:min(Gt, Eq) |> expect:equal(_, Eq) +// order:min(Gt, Gt) |> expect:equal(_, Gt) +// } diff --git a/test/result_test.gleam b/test/result_test.gleam new file mode 100644 index 0000000..36d208e --- /dev/null +++ b/test/result_test.gleam @@ -0,0 +1,85 @@ +import expect +import result + +pub fn is_ok_test() { + result:is_ok(Ok(1)) |> expect:true + result:is_ok(Error(1)) |> expect:false +} + +pub fn is_error_test() { + result:is_error(Ok(1)) + |> expect:false + + result:is_error(Error(1)) + |> expect:true +} + +pub fn map_test() { + Ok(1) + |> result:map(_, fn(x) { x + 1 }) + |> expect:equal(_, Ok(2)) + + Ok(1) + |> result:map(_, fn(_) { "2" }) + |> expect:equal(_, Ok("2")) + + Error(1) + |> result:map(_, fn(x) { x + 1 }) + |> expect:equal(_, Error(1)) +} + +pub fn map_error_test() { + Ok(1) + |> result:map_error(_, fn(x) { x + 1 }) + |> expect:equal(_, Ok(1)) + + Error(1) + |> result:map_error(_, fn(x) { x + 1 }) + |> expect:equal(_, Error(2)) +} + +pub fn flatten_test() { + Ok(Ok(1)) + |> result:flatten + |> expect:equal(_, Ok(1)) + + Ok(Error(1)) + |> result:flatten + |> expect:equal(_, Error(1)) + + Error(1) + |> result:flatten + |> expect:equal(_, Error(1)) + + Error(Error(1)) + |> result:flatten + |> expect:equal(_, Error(Error(1))) +} + +pub fn then_test() { + Error(1) + |> result:then(_, fn(x) { Ok(x + 1) }) + |> expect:equal(_, Error(1)) + + Ok(1) + |> result:then(_, fn(x) { Ok(x + 1) }) + |> expect:equal(_, Ok(2)) + + Ok(1) + |> result:then(_, fn(_) { Ok("type change") }) + |> expect:equal(_, Ok("type change")) + + Ok(1) + |> result:then(_, fn(_) { Error(1) }) + |> expect:equal(_, Error(1)) +} + +pub fn unwrap_test() { + Ok(1) + |> result:unwrap(_, 50) + |> expect:equal(_, 1) + + Error("nope") + |> result:unwrap(_, 50) + |> expect:equal(_, 50) +} diff --git a/test/set_test.gleam b/test/set_test.gleam new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/set_test.gleam diff --git a/test/str_test.gleam b/test/str_test.gleam new file mode 100644 index 0000000..13d781c --- /dev/null +++ b/test/str_test.gleam @@ -0,0 +1,130 @@ +import str +import expect + +pub fn length_test() { + str:length("ß↑e̊") + |> expect:equal(_, 3) + + str:length("Gleam") + |> expect:equal(_, 5) + + str:length("") + |> expect:equal(_, 0) +} + +pub fn lowercase_test() { + str:lowercase("Gleam") + |> expect:equal(_, "gleam") +} + +pub fn uppercase_test() { + str:uppercase("Gleam") + |> expect:equal(_, "GLEAM") +} + +pub fn reverse_test() { + str:reverse("Gleam") + |> expect:equal(_, "maelG") +} + +pub fn split_test() { + "Gleam,Erlang,Elixir" + |> str:split(_, ",") + |> expect:equal(_, ["Gleam", "Erlang", "Elixir"]) + + "Gleam, Erlang,Elixir" + |> str:split(_, ", ") + |> expect:equal(_, ["Gleam", "Erlang,Elixir"]) +} + +pub fn replace_test() { + "Gleam,Erlang,Elixir" + |> str:replace(_, ",", "++") + |> expect:equal(_, "Gleam++Erlang++Elixir") +} + +pub fn from_int_test() { + 123 + |> str:from_int + |> expect:equal(_, "123") + + -123 + |> str:from_int + |> expect:equal(_, "-123") + + 0123 + |> str:from_int + |> expect:equal(_, "123") +} + +pub fn parse_int_test() { + "123" + |> str:parse_int + |> expect:equal(_, Ok(123)) + + "-123" + |> str:parse_int + |> expect:equal(_, Ok(-123)) + + "0123" + |> str:parse_int + |> expect:equal(_, Ok(123)) + + "" + |> str:parse_int + |> expect:is_error + + "what" + |> str:parse_int + |> expect:is_error + + "1.23" + |> str:parse_int + |> expect:is_error +} + +pub fn parse_float_test() { + "1.23" + |> str:parse_float + |> expect:equal(_, Ok(1.23)) + + "5.0" + |> str:parse_float + |> expect:equal(_, Ok(5.0)) + + "0.123456789" + |> str:parse_float + |> expect:equal(_, Ok(0.123456789)) + + "" + |> str:parse_float + |> expect:is_error + + "what" + |> str:parse_float + |> expect:is_error + + "1" + |> str:parse_float + |> expect:is_error +} + +pub fn base_from_int_test() { + 100 + |> str:base_from_int(_, 16) + |> expect:equal(_, "64") + + -100 + |> str:base_from_int(_, 16) + |> expect:equal(_, "-64") +} + +pub fn from_float_test() { + 123.0 + |> str:from_float + |> expect:equal(_, "123.0") + + -8.1 + |> str:from_float + |> expect:equal(_, "-8.1") +} diff --git a/test/tuple_test.gleam b/test/tuple_test.gleam new file mode 100644 index 0000000..1c0463a --- /dev/null +++ b/test/tuple_test.gleam @@ -0,0 +1,44 @@ +import expect +import tuple + +pub fn new_test() { + tuple:new(1, 2) + |> expect:equal(_, {1, 2}) + + tuple:new(2, "3") + |> expect:equal(_, {2, "3"}) +} + +pub fn first_test() { + {1, 2} + |> tuple:first + |> expect:equal(_, 1) +} + +pub fn second_test() { + {1, 2} + |> tuple:second + |> expect:equal(_, 2) +} + +pub fn swap_test() { + {1, "2"} + |> tuple:swap + |> expect:equal(_, {"2", 1}) +} + +pub fn fetch_test() { + let proplist = [{0, "1"}, {1, "2"}] + + proplist + |> tuple:fetch(_, 0) + |> expect:equal(_, Ok("1")) + + proplist + |> tuple:fetch(_, 1) + |> expect:equal(_, Ok("2")) + + proplist + |> tuple:fetch(_, 2) + |> expect:is_error +} |