diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/any_test.gleam | 278 | ||||
-rw-r--r-- | test/atom_test.gleam | 46 | ||||
-rw-r--r-- | test/bool_test.gleam | 61 | ||||
-rw-r--r-- | test/http_test.gleam | 1 | ||||
-rw-r--r-- | test/iodata_test.gleam | 90 | ||||
-rw-r--r-- | test/list_test.gleam | 164 | ||||
-rw-r--r-- | test/map_dict_test.gleam | 113 | ||||
-rw-r--r-- | test/order_test.gleam | 52 | ||||
-rw-r--r-- | test/result_test.gleam | 85 | ||||
-rw-r--r-- | test/set_test.gleam | 0 | ||||
-rw-r--r-- | test/str_test.gleam | 130 | ||||
-rw-r--r-- | test/tuple_test.gleam | 44 |
12 files changed, 1064 insertions, 0 deletions
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 +} |