diff options
Diffstat (limited to 'test/std')
-rw-r--r-- | test/std/any_test.gleam | 278 | ||||
-rw-r--r-- | test/std/atom_test.gleam | 46 | ||||
-rw-r--r-- | test/std/bool_test.gleam | 61 | ||||
-rw-r--r-- | test/std/float_test.gleam | 118 | ||||
-rw-r--r-- | test/std/http_test.gleam | 1 | ||||
-rw-r--r-- | test/std/int_test.gleam | 73 | ||||
-rw-r--r-- | test/std/iodata_test.gleam | 90 | ||||
-rw-r--r-- | test/std/list_test.gleam | 351 | ||||
-rw-r--r-- | test/std/map_dict_test.gleam | 233 | ||||
-rw-r--r-- | test/std/order_test.gleam | 111 | ||||
-rw-r--r-- | test/std/result_test.gleam | 88 | ||||
-rw-r--r-- | test/std/string_test.gleam | 50 | ||||
-rw-r--r-- | test/std/tuple_test.gleam | 44 |
13 files changed, 1544 insertions, 0 deletions
diff --git a/test/std/any_test.gleam b/test/std/any_test.gleam new file mode 100644 index 0000000..dce4a44 --- /dev/null +++ b/test/std/any_test.gleam @@ -0,0 +1,278 @@ +import std/any +import std/atom +import std/list +import std/tuple +import std/expect +import std/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/std/atom_test.gleam b/test/std/atom_test.gleam new file mode 100644 index 0000000..54e88da --- /dev/null +++ b/test/std/atom_test.gleam @@ -0,0 +1,46 @@ +import std/atom +import std/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/std/bool_test.gleam b/test/std/bool_test.gleam new file mode 100644 index 0000000..d2da7f9 --- /dev/null +++ b/test/std/bool_test.gleam @@ -0,0 +1,61 @@ +import std/bool +import std/order +import std/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:Eq) + + bool:compare(False, True) + |> expect:equal(_, order:Lt) +} + +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/std/float_test.gleam b/test/std/float_test.gleam new file mode 100644 index 0000000..0847973 --- /dev/null +++ b/test/std/float_test.gleam @@ -0,0 +1,118 @@ +import std/expect +import std/float + +pub fn parse_test() { + "1.23" + |> float:parse + |> expect:equal(_, Ok(1.23)) + + "5.0" + |> float:parse + |> expect:equal(_, Ok(5.0)) + + "0.123456789" + |> float:parse + |> expect:equal(_, Ok(0.123456789)) + + "" + |> float:parse + |> expect:is_error + + "what" + |> float:parse + |> expect:is_error + + "1" + |> float:parse + |> expect:is_error +} + +pub fn to_string_test() { + 123.0 + |> float:to_string + |> expect:equal(_, "123.0") + + -8.1 + |> float:to_string + |> expect:equal(_, "-8.1") +} + +pub fn ceiling_test() { + 8.1 + |> float:ceiling + |> expect:equal(_, 9.0) + + -8.1 + |> float:ceiling + |> expect:equal(_, -8.0) + + -8.0 + |> float:ceiling + |> expect:equal(_, -8.0) +} + +pub fn floor_test() { + 8.1 + |> float:floor + |> expect:equal(_, 8.0) + + -8.1 + |> float:floor + |> expect:equal(_, -9.0) + + -8.0 + |> float:floor + |> expect:equal(_, -8.0) +} + +pub fn round_test() { + 8.1 + |> float:round + |> expect:equal(_, 8) + + 8.4 + |> float:round + |> expect:equal(_, 8) + + 8.499 + |> float:round + |> expect:equal(_, 8) + + 8.5 + |> float:round + |> expect:equal(_, 9) + + -8.1 + |> float:round + |> expect:equal(_, -8) + + -7.5 + |> float:round + |> expect:equal(_, -8) +} + +pub fn truncate_test() { + 8.1 + |> float:truncate + |> expect:equal(_, 8) + + 8.4 + |> float:truncate + |> expect:equal(_, 8) + + 8.499 + |> float:truncate + |> expect:equal(_, 8) + + 8.5 + |> float:truncate + |> expect:equal(_, 8) + + -8.1 + |> float:truncate + |> expect:equal(_, -8) + + -7.5 + |> float:truncate + |> expect:equal(_, -7) +} diff --git a/test/std/http_test.gleam b/test/std/http_test.gleam new file mode 100644 index 0000000..9941d56 --- /dev/null +++ b/test/std/http_test.gleam @@ -0,0 +1 @@ +// Nothing here yet... diff --git a/test/std/int_test.gleam b/test/std/int_test.gleam new file mode 100644 index 0000000..63e9d50 --- /dev/null +++ b/test/std/int_test.gleam @@ -0,0 +1,73 @@ +import std/expect +import std/int +import std/order + +pub fn to_string() { + 123 + |> int:to_string + |> expect:equal(_, "123") + + -123 + |> int:to_string + |> expect:equal(_, "-123") + + 0123 + |> int:to_string + |> expect:equal(_, "123") +} + +pub fn parse() { + "123" + |> int:parse + |> expect:equal(_, Ok(123)) + + "-123" + |> int:parse + |> expect:equal(_, Ok(-123)) + + "0123" + |> int:parse + |> expect:equal(_, Ok(123)) + + "" + |> int:parse + |> expect:is_error + + "what" + |> int:parse + |> expect:is_error + + "1.23" + |> int:parse + |> expect:is_error +} + +pub fn to_base_string() { + 100 + |> int:to_base_string(_, 16) + |> expect:equal(_, "64") + + -100 + |> int:to_base_string(_, 16) + |> expect:equal(_, "-64") +} + +pub fn compare_test() { + int:compare(0, 0) + |> expect:equal(_, order:Eq) + + int:compare(1, 1) + |> expect:equal(_, order:Eq) + + int:compare(0, 1) + |> expect:equal(_, order:Lt) + + int:compare(-2, -1) + |> expect:equal(_, order:Lt) + + int:compare(2, 1) + |> expect:equal(_, order:Gt) + + int:compare(-1, -2) + |> expect:equal(_, order:Gt) +} diff --git a/test/std/iodata_test.gleam b/test/std/iodata_test.gleam new file mode 100644 index 0000000..1a90e51 --- /dev/null +++ b/test/std/iodata_test.gleam @@ -0,0 +1,90 @@ +import std/expect +import std/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/std/list_test.gleam b/test/std/list_test.gleam new file mode 100644 index 0000000..9e27d22 --- /dev/null +++ b/test/std/list_test.gleam @@ -0,0 +1,351 @@ +import std/expect +import std/list +import std/int +import std/string + +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 +} + +pub fn all_test() { + list:all([1, 2, 3, 4, 5], fn(x) { x > 0 }) + |> expect:equal(_, True) + + list:all([1, 2, 3, 4, 5], fn(x) { x < 0 }) + |> expect:equal(_, False) + + list:all([], fn(_) { False }) + |> expect:equal(_, True) +} + +pub fn any_test() { + list:any([1, 2, 3, 4, 5], fn(x) { x == 2 }) + |> expect:equal(_, True) + + list:any([1, 2, 3, 4, 5], fn(x) { x < 0 }) + |> expect:equal(_, False) + + list:any([], fn(_) { False }) + |> expect:equal(_, False) +} + +pub fn zip_test() { + list:zip([], [1, 2, 3]) + |> expect:equal(_, []) + + list:zip([1, 2], []) + |> expect:equal(_, []) + + list:zip([1, 2, 3], [4, 5, 6]) + |> expect:equal(_, [{1, 4}, {2, 5}, {3, 6}]) + + list:zip([5, 6], [1, 2, 3]) + |> expect:equal(_, [{5, 1}, {6, 2}]) + + list:zip([5, 6, 7], [1, 2]) + |> expect:equal(_, [{5, 1}, {6, 2}]) +} + +pub fn strict_zip_test() { + list:strict_zip([], [1, 2, 3]) + |> expect:is_error + + list:strict_zip([1, 2], []) + |> expect:is_error + + list:strict_zip([1, 2, 3], [4, 5, 6]) + |> expect:equal(_, Ok([{1, 4}, {2, 5}, {3, 6}])) + + list:strict_zip([5, 6], [1, 2, 3]) + |> expect:is_error + + list:strict_zip([5, 6, 7], [1, 2]) + |> expect:is_error +} + +pub fn intersperse_test() { + list:intersperse([1, 2, 3], 4) + |> expect:equal(_, [1, 4, 2, 4, 3]) + + list:intersperse([], 2) + |> expect:equal(_, []) +} + +pub fn at_test() { + list:at([1, 2, 3], 2) + |> expect:equal(_, Ok(3)) + + list:at([1, 2, 3], 5) + |> expect:is_error + + list:at([], 0) + |> expect:is_error + + list:at([1, 2, 3, 4, 5, 6], -1) + |> expect:is_error +} + +pub fn unique_test() { + list:unique([1, 1, 2, 3, 4, 4, 4, 5, 6]) + |> expect:equal(_, [1, 2, 3, 4, 5, 6]) + + list:unique([7, 1, 45, 6, 2, 47, 2, 7, 5]) + |> expect:equal(_, [7, 1, 45, 6, 2, 47, 5]) + + list:unique([3, 4, 5]) + |> expect:equal(_, [3, 4, 5]) + + list:unique([]) + |> expect:equal(_, []) +} + +pub fn sort_test() { + list:sort([4, 3, 6, 5, 4]) + |> expect:equal(_, [3, 4, 4, 5, 6]) + + list:sort([]) + |> expect:equal(_, []) + + list:sort([{1, 2}, {4, 5}, {3, 2}]) + |> expect:equal(_, [{1, 2}, {3, 2}, {4, 5}]) +} + +pub fn index_map_test() { + list:index_map([3, 4, 5], fn(i, x) { {i, x} }) + |> expect:equal(_, [{0, 3}, {1, 4}, {2, 5}]) + + let f = fn(i, x) { + string:append(x, int:to_string(i)) + } + list:index_map(["a", "b", "c"], f) + |> expect:equal(_, ["a0", "b1", "c2"]) +} + +pub fn range_test() { + list:range(0, 0) + |> expect:equal(_, []) + + list:range(1, 1) + |> expect:equal(_, []) + + list:range(-1, -1) + |> expect:equal(_, []) + + list:range(0, 1) + |> expect:equal(_, [0]) + + list:range(0, 5) + |> expect:equal(_, [0, 1, 2, 3, 4]) + + list:range(1, -5) + |> expect:equal(_, [1, 0, -1, -2, -3, -4]) +} + +pub fn repeat_test() { + list:repeat(1, -10) + |> expect:equal(_, []) + + list:repeat(1, 0) + |> expect:equal(_, []) + + list:repeat(2, 3) + |> expect:equal(_, [2, 2, 2]) + + list:repeat("x", 5) + |> expect:equal(_, ["x", "x", "x", "x", "x"]) +} + +pub fn split_test() { + list:split([], 0) + |> expect:equal(_, {[], []}) + + list:split([0, 1, 2, 3, 4], 0) + |> expect:equal(_, {[], [0, 1, 2, 3, 4]}) + + list:split([0, 1, 2, 3, 4], -2) + |> expect:equal(_, {[], [0, 1, 2, 3, 4]}) + + list:split([0, 1, 2, 3, 4], 1) + |> expect:equal(_, {[0], [1, 2, 3, 4]}) + + list:split([0, 1, 2, 3, 4], 3) + |> expect:equal(_, {[0, 1, 2], [3, 4]}) + + list:split([0, 1, 2, 3, 4], 9) + |> expect:equal(_, {[0, 1, 2, 3, 4], []}) +} + +pub fn split_while_test() { + list:split_while([], fn(x) { x <= 5 }) + |> expect:equal(_, {[], []}) + + list:split_while([1, 2, 3, 4, 5], fn(x) { x <= 5 }) + |> expect:equal(_, {[1, 2, 3, 4, 5], []}) + + list:split_while([1, 2, 3, 4, 5], fn(x) { x == 2 }) + |> expect:equal(_, {[], [1, 2, 3, 4, 5]}) + + list:split_while([1, 2, 3, 4, 5], fn(x) { x <= 3 }) + |> expect:equal(_, {[1, 2, 3], [4, 5]}) + + list:split_while([1, 2, 3, 4, 5], fn(x) { x <= -3 }) + |> expect:equal(_, {[], [1, 2, 3, 4, 5]}) +} diff --git a/test/std/map_dict_test.gleam b/test/std/map_dict_test.gleam new file mode 100644 index 0000000..892bc5d --- /dev/null +++ b/test/std/map_dict_test.gleam @@ -0,0 +1,233 @@ +import std/string +import std/expect +import std/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]) +} + +pub fn take_test() { + [ + {"a", 0}, + {"b", 1}, + {"c", 2}, + ] + |> map_dict:from_list + |> map_dict:take(_, ["a", "b", "d"]) + |> expect:equal(_, map_dict:from_list([{"a", 0}, {"b", 1}])) +} + +pub fn drop_test() { + [ + {"a", 0}, + {"b", 1}, + {"c", 2}, + ] + |> map_dict:from_list + |> map_dict:drop(_, ["a", "b", "d"]) + |> expect:equal(_, map_dict:from_list([{"c", 2}])) +} + +pub fn merge_test() { + let a = map_dict:from_list([ + {"a", 2}, + {"c", 4}, + {"d", 3}, + ]) + let b = map_dict:from_list([ + {"a", 0}, + {"b", 1}, + {"c", 2}, + ]) + + map_dict:merge(a, b) + |> expect:equal(_, map_dict:from_list([ + {"a", 0}, + {"b", 1}, + {"c", 2}, + {"d", 3}, + ])) + + map_dict:merge(b, a) + |> expect:equal(_, map_dict:from_list([ + {"a", 2}, + {"b", 1}, + {"c", 4}, + {"d", 3}, + ])) +} + +pub fn delete_test() { + [ + {"a", 0}, + {"b", 1}, + {"c", 2}, + ] + |> map_dict:from_list + |> map_dict:delete(_, "a") + |> map_dict:delete(_, "d") + |> expect:equal(_, map_dict:from_list([{"b", 1}, {"c", 2}])) +} + +pub fn update_test() { + let dict = map_dict:from_list([ + {"a", 0}, + {"b", 1}, + {"c", 2}, + ]) + + let inc_or_zero = fn(x) { + case x { + | Ok(i) -> i + 1 + | Error(_) -> 0 + } + } + + dict + |> map_dict:update(_, "a", inc_or_zero) + |> expect:equal(_, map_dict:from_list([{"a", 1}, {"b", 1}, {"c", 2}])) + + dict + |> map_dict:update(_, "b", inc_or_zero) + |> expect:equal(_, map_dict:from_list([{"a", 0}, {"b", 2}, {"c", 2}])) + + dict + |> map_dict:update(_, "z", inc_or_zero) + |> expect:equal(_, map_dict:from_list([{"a", 0}, {"b", 1}, {"c", 2}, {"z", 0}])) +} + +pub fn fold_test() { + let dict = map_dict:from_list([ + {"a", 0}, + {"b", 1}, + {"c", 2}, + {"d", 3}, + ]) + + let add = fn(_, v, acc) { + v + acc + } + + dict + |> map_dict:fold(_, 0, add) + |> expect:equal(_, 6) + + let concat = fn(k, _, acc) { + string:append(acc, k) + } + + dict + |> map_dict:fold(_, "", concat) + |> expect:equal(_, "abcd") + + map_dict:from_list([]) + |> map_dict:fold(_, 0, add) + |> expect:equal(_, 0) +} diff --git a/test/std/order_test.gleam b/test/std/order_test.gleam new file mode 100644 index 0000000..aac0aae --- /dev/null +++ b/test/std/order_test.gleam @@ -0,0 +1,111 @@ +import std/expect +import std/order + +pub fn reverse_test() { + order:reverse(order:Lt) + |> expect:equal(_, order:Gt) + + order:reverse(order:Eq) + |> expect:equal(_, order:Eq) + + order:reverse(order:Gt) + |> expect:equal(_, order:Lt) +} + +pub fn to_int_test() { + order:to_int(order:Lt) + |> expect:equal(_, -1) + + order:to_int(order:Eq) + |> expect:equal(_, 0) + + order:to_int(order:Gt) + |> expect:equal(_, 1) +} + +pub fn compare_test() { + order:compare(order:Lt, order:Lt) + |> expect:equal(_, order:Eq) + + order:compare(order:Lt, order:Eq) + |> expect:equal(_, order:Lt) + + order:compare(order:Lt, order:Gt) + |> expect:equal(_, order:Lt) + + order:compare(order:Eq, order:Lt) + |> expect:equal(_, order:Gt) + + order:compare(order:Eq, order:Eq) + |> expect:equal(_, order:Eq) + + order:compare(order:Eq, order:Gt) + |> expect:equal(_, order:Lt) + + order:compare(order:Gt, order:Lt) + |> expect:equal(_, order:Gt) + + order:compare(order:Gt, order:Eq) + |> expect:equal(_, order:Gt) + + order:compare(order:Gt, order:Gt) + |> expect:equal(_, order:Eq) +} + +pub fn max_test() { + order:max(order:Lt, order:Lt) + |> expect:equal(_, order:Lt) + + order:max(order:Lt, order:Eq) + |> expect:equal(_, order:Eq) + + order:max(order:Lt, order:Gt) + |> expect:equal(_, order:Gt) + + order:max(order:Eq, order:Lt) + |> expect:equal(_, order:Eq) + + order:max(order:Eq, order:Eq) + |> expect:equal(_, order:Eq) + + order:max(order:Eq, order:Gt) + |> expect:equal(_, order:Gt) + + order:max(order:Gt, order:Lt) + |> expect:equal(_, order:Gt) + + order:max(order:Gt, order:Eq) + |> expect:equal(_, order:Gt) + + order:max(order:Gt, order:Gt) + |> expect:equal(_, order:Gt) +} + +pub fn min_test() { + order:min(order:Lt, order:Lt) + |> expect:equal(_, order:Lt) + + order:min(order:Lt, order:Eq) + |> expect:equal(_, order:Lt) + + order:min(order:Lt, order:Gt) + |> expect:equal(_, order:Lt) + + order:min(order:Eq, order:Lt) + |> expect:equal(_, order:Lt) + + order:min(order:Eq, order:Eq) + |> expect:equal(_, order:Eq) + + order:min(order:Eq, order:Gt) + |> expect:equal(_, order:Eq) + + order:min(order:Gt, order:Lt) + |> expect:equal(_, order:Lt) + + order:min(order:Gt, order:Eq) + |> expect:equal(_, order:Eq) + + order:min(order:Gt, order:Gt) + |> expect:equal(_, order:Gt) +} diff --git a/test/std/result_test.gleam b/test/std/result_test.gleam new file mode 100644 index 0000000..41fd808 --- /dev/null +++ b/test/std/result_test.gleam @@ -0,0 +1,88 @@ +import std/expect +import std/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/std/string_test.gleam b/test/std/string_test.gleam new file mode 100644 index 0000000..dd8a00e --- /dev/null +++ b/test/std/string_test.gleam @@ -0,0 +1,50 @@ +import std/string +import std/expect + +pub fn length_test() { + string:length("ß↑e̊") + |> expect:equal(_, 3) + + string:length("Gleam") + |> expect:equal(_, 5) + + string:length("") + |> expect:equal(_, 0) +} + +pub fn lowercase_test() { + string:lowercase("Gleam") + |> expect:equal(_, "gleam") +} + +pub fn uppercase_test() { + string:uppercase("Gleam") + |> expect:equal(_, "GLEAM") +} + +pub fn reverse_test() { + string:reverse("Gleam") + |> expect:equal(_, "maelG") +} + +pub fn split_test() { + "Gleam,Erlang,Elixir" + |> string:split(_, ",") + |> expect:equal(_, ["Gleam", "Erlang", "Elixir"]) + + "Gleam, Erlang,Elixir" + |> string:split(_, ", ") + |> expect:equal(_, ["Gleam", "Erlang,Elixir"]) +} + +pub fn replace_test() { + "Gleam,Erlang,Elixir" + |> string:replace(_, ",", "++") + |> expect:equal(_, "Gleam++Erlang++Elixir") +} + +pub fn append_test() { + "Test" + |> string:append(_, " Me") + |> expect:equal(_, "Test Me") +} diff --git a/test/std/tuple_test.gleam b/test/std/tuple_test.gleam new file mode 100644 index 0000000..31f1179 --- /dev/null +++ b/test/std/tuple_test.gleam @@ -0,0 +1,44 @@ +import std/expect +import std/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 +} |