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 /src | |
parent | ae5597c1b27982aabe74eb16d5b0c890802730d9 (diff) | |
download | gleam_stdlib-8cfa606f3834cf7d05f5011bc68295a9d84263dc.tar.gz gleam_stdlib-8cfa606f3834cf7d05f5011bc68295a9d84263dc.zip |
stdlib: Split out tests
Diffstat (limited to 'src')
-rw-r--r-- | src/any.gleam | 259 | ||||
-rw-r--r-- | src/atom.gleam | 47 | ||||
-rw-r--r-- | src/bool.gleam | 61 | ||||
-rw-r--r-- | src/gleam__stdlib.erl | 5 | ||||
-rw-r--r-- | src/iodata.gleam | 93 | ||||
-rw-r--r-- | src/list.gleam | 179 | ||||
-rw-r--r-- | src/map.gleam | 190 | ||||
-rw-r--r-- | src/map_dict.gleam | 60 | ||||
-rw-r--r-- | src/order.gleam | 50 | ||||
-rw-r--r-- | src/result.gleam | 79 | ||||
-rw-r--r-- | src/str.gleam | 129 | ||||
-rw-r--r-- | src/tuple.gleam | 44 |
12 files changed, 73 insertions, 1123 deletions
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 -} |