diff options
author | Louis Pilfold <louis@lpil.uk> | 2021-07-18 22:35:47 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2021-07-18 22:35:47 +0100 |
commit | e657d78fbadad6e02e2cd3e2427e79a54ddb343e (patch) | |
tree | 4a659896bd8a1beb5d7e110a04cb366b3eb84633 /test | |
parent | eb2f87a814ba3235a4e98c201e8e82e25da2e9d0 (diff) | |
download | gleam_stdlib-e657d78fbadad6e02e2cd3e2427e79a54ddb343e.tar.gz gleam_stdlib-e657d78fbadad6e02e2cd3e2427e79a54ddb343e.zip |
Result functions on JS
Diffstat (limited to 'test')
-rw-r--r-- | test/gleam/function_test.gleam | 183 | ||||
-rw-r--r-- | test/gleam/result_test.gleam | 338 |
2 files changed, 261 insertions, 260 deletions
diff --git a/test/gleam/function_test.gleam b/test/gleam/function_test.gleam index 0b3bd8c..e4a368c 100644 --- a/test/gleam/function_test.gleam +++ b/test/gleam/function_test.gleam @@ -1,109 +1,112 @@ -import gleam/should -import gleam/dynamic -import gleam/function -import gleam/int -import gleam/list -import gleam/result -import gleam/string - -pub fn compose_test() { - let add_two = fn(int: Int) { int + 2 } - let add_three = fn(int: Int) { int + 3 } - - let add_five = function.compose(add_two, add_three) - - 1 - |> add_five - |> should.equal(6) - - // Takes a list of ints and returns the head as a string (if there is one, or - // else "0" if there is not) - let head_to_string = - list.head - |> function.compose(result.unwrap(_, 0)) - |> function.compose(int.to_string) - - [1] - |> head_to_string - |> should.equal("1") - - [] - |> head_to_string - |> should.equal("0") -} - -pub fn curry2_test() { - let fun = fn(a, b) { a + b } - let curried = function.curry2(fun) +if erlang { + // TODO: JavaScript + import gleam/should + import gleam/dynamic + import gleam/function + import gleam/int + import gleam/list + import gleam/result + import gleam/string + + pub fn compose_test() { + let add_two = fn(int: Int) { int + 2 } + let add_three = fn(int: Int) { int + 3 } + + let add_five = function.compose(add_two, add_three) + + 1 + |> add_five + |> should.equal(6) + + // Takes a list of ints and returns the head as a string (if there is one, or + // else "0" if there is not) + let head_to_string = + list.head + |> function.compose(result.unwrap(_, 0)) + |> function.compose(int.to_string) + + [1] + |> head_to_string + |> should.equal("1") + + [] + |> head_to_string + |> should.equal("0") + } - curried(1)(2) - |> should.equal(3) -} + pub fn curry2_test() { + let fun = fn(a, b) { a + b } + let curried = function.curry2(fun) -pub fn curry3_test() { - let fun = fn(a, b, c) { a + b + c } - let curried = function.curry3(fun) + curried(1)(2) + |> should.equal(3) + } - curried(1)(2)(4) - |> should.equal(7) -} + pub fn curry3_test() { + let fun = fn(a, b, c) { a + b + c } + let curried = function.curry3(fun) -pub fn curry4_test() { - let fun = fn(a, b, c, d) { a + b + c + d } - let curried = function.curry4(fun) + curried(1)(2)(4) + |> should.equal(7) + } - curried(1)(2)(4)(8) - |> should.equal(15) -} + pub fn curry4_test() { + let fun = fn(a, b, c, d) { a + b + c + d } + let curried = function.curry4(fun) -pub fn curry5_test() { - let fun = fn(a, b, c, d, e) { a + b + c + d + e } - let curried = function.curry5(fun) + curried(1)(2)(4)(8) + |> should.equal(15) + } - curried(1)(2)(4)(8)(16) - |> should.equal(31) -} + pub fn curry5_test() { + let fun = fn(a, b, c, d, e) { a + b + c + d + e } + let curried = function.curry5(fun) -pub fn curry6_test() { - let fun = fn(a, b, c, d, e, f) { a + b + c + d + e + f } - let curried = function.curry6(fun) + curried(1)(2)(4)(8)(16) + |> should.equal(31) + } - curried(1)(2)(4)(8)(16)(32) - |> should.equal(63) -} + pub fn curry6_test() { + let fun = fn(a, b, c, d, e, f) { a + b + c + d + e + f } + let curried = function.curry6(fun) -pub fn flip_test() { - let fun = fn(s: String, i: Int) { - s - |> string.append("String: '", _) - |> string.append("', Int: '") - |> string.append(int.to_string(i)) - |> string.append("'") + curried(1)(2)(4)(8)(16)(32) + |> should.equal(63) } - let flipped_fun = function.flip(fun) + pub fn flip_test() { + let fun = fn(s: String, i: Int) { + s + |> string.append("String: '", _) + |> string.append("', Int: '") + |> string.append(int.to_string(i)) + |> string.append("'") + } - fun("Bob", 1) - |> should.equal("String: 'Bob', Int: '1'") + let flipped_fun = function.flip(fun) - flipped_fun(2, "Alice") - |> should.equal("String: 'Alice', Int: '2'") -} + fun("Bob", 1) + |> should.equal("String: 'Bob', Int: '1'") + + flipped_fun(2, "Alice") + |> should.equal("String: 'Alice', Int: '2'") + } -pub fn identity_test() { - 1 - |> function.identity - |> should.equal(1) + pub fn identity_test() { + 1 + |> function.identity + |> should.equal(1) - "" - |> function.identity - |> should.equal("") + "" + |> function.identity + |> should.equal("") - [] - |> function.identity - |> should.equal([]) + [] + |> function.identity + |> should.equal([]) - #(1, 2.0) - |> function.identity - |> should.equal(#(1, 2.0)) + #(1, 2.0) + |> function.identity + |> should.equal(#(1, 2.0)) + } } diff --git a/test/gleam/result_test.gleam b/test/gleam/result_test.gleam index 464aa2b..a2bd6ff 100644 --- a/test/gleam/result_test.gleam +++ b/test/gleam/result_test.gleam @@ -1,171 +1,169 @@ -if erlang { - import gleam/should - import gleam/result - - pub fn is_ok_test() { - result.is_ok(Ok(1)) - |> should.be_true - - result.is_ok(Error(1)) - |> should.be_false - } - - pub fn is_error_test() { - result.is_error(Ok(1)) - |> should.be_false - - result.is_error(Error(1)) - |> should.be_true - } - - pub fn map_test() { - Ok(1) - |> result.map(fn(x) { x + 1 }) - |> should.equal(Ok(2)) - - Ok(1) - |> result.map(fn(_) { "2" }) - |> should.equal(Ok("2")) - - Error(1) - |> result.map(fn(x) { x + 1 }) - |> should.equal(Error(1)) - } - - pub fn map_error_test() { - Ok(1) - |> result.map_error(fn(x) { x + 1 }) - |> should.equal(Ok(1)) - - Error(1) - |> result.map_error(fn(x) { #("ok", x + 1) }) - |> should.equal(Error(#("ok", 2))) - } - - pub fn flatten_test() { - Ok(Ok(1)) - |> result.flatten - |> should.equal(Ok(1)) - - Ok(Error(1)) - |> result.flatten - |> should.equal(Error(1)) - - Error(1) - |> result.flatten - |> should.equal(Error(1)) - - Error(Error(1)) - |> result.flatten - |> should.equal(Error(Error(1))) - } - - pub fn then_test() { - Error(1) - |> result.then(fn(x) { Ok(x + 1) }) - |> should.equal(Error(1)) - - Ok(1) - |> result.then(fn(x) { Ok(x + 1) }) - |> should.equal(Ok(2)) - - Ok(1) - |> result.then(fn(_) { Ok("type change") }) - |> should.equal(Ok("type change")) - - Ok(1) - |> result.then(fn(_) { Error(1) }) - |> should.equal(Error(1)) - } - - pub fn unwrap_test() { - Ok(1) - |> result.unwrap(50) - |> should.equal(1) - - Error("nope") - |> result.unwrap(50) - |> should.equal(50) - } - - pub fn lazy_unwrap_test() { - Ok(1) - |> result.lazy_unwrap(fn() { 50 }) - |> should.equal(1) - - Error("nope") - |> result.lazy_unwrap(fn() { 50 }) - |> should.equal(50) - } - - pub fn nil_error_test() { - Error("error_string") - |> result.nil_error - |> should.equal(Error(Nil)) - - Error(123) - |> result.nil_error - |> should.equal(Error(Nil)) - - Ok(1) - |> result.nil_error - |> should.equal(Ok(1)) - } - - pub fn or_test() { - Ok(1) - |> result.or(Ok(2)) - |> should.equal(Ok(1)) - - Ok(1) - |> result.or(Error("Error 2")) - |> should.equal(Ok(1)) - - Error("Error 1") - |> result.or(Ok(2)) - |> should.equal(Ok(2)) - - Error("Error 1") - |> result.or(Error("Error 2")) - |> should.equal(Error("Error 2")) - } - - pub fn lazy_or_test() { - Ok(1) - |> result.lazy_or(fn() { Ok(2) }) - |> should.equal(Ok(1)) - - Ok(1) - |> result.lazy_or(fn() { Error("Error 2") }) - |> should.equal(Ok(1)) - - Error("Error 1") - |> result.lazy_or(fn() { Ok(2) }) - |> should.equal(Ok(2)) - - Error("Error 1") - |> result.lazy_or(fn() { Error("Error 2") }) - |> should.equal(Error("Error 2")) - } - - pub fn all_test() { - [Ok(1), Ok(2), Ok(3)] - |> result.all - |> should.equal(Ok([1, 2, 3])) - - [Ok(1), Error("a"), Error("b"), Ok(3)] - |> result.all - |> should.equal(Error("a")) - } - - pub fn replace_error_test() { - Error(Nil) - |> result.replace_error("Invalid") - |> should.equal(Error("Invalid")) - } - - pub fn values_test() { - result.values([Ok(1), Error(""), Ok(3)]) - |> should.equal([1, 3]) - } +import gleam/should +import gleam/result + +pub fn is_ok_test() { + result.is_ok(Ok(1)) + |> should.be_true + + result.is_ok(Error(1)) + |> should.be_false +} + +pub fn is_error_test() { + result.is_error(Ok(1)) + |> should.be_false + + result.is_error(Error(1)) + |> should.be_true +} + +pub fn map_test() { + Ok(1) + |> result.map(fn(x) { x + 1 }) + |> should.equal(Ok(2)) + + Ok(1) + |> result.map(fn(_) { "2" }) + |> should.equal(Ok("2")) + + Error(1) + |> result.map(fn(x) { x + 1 }) + |> should.equal(Error(1)) +} + +pub fn map_error_test() { + Ok(1) + |> result.map_error(fn(x) { x + 1 }) + |> should.equal(Ok(1)) + + Error(1) + |> result.map_error(fn(x) { #("ok", x + 1) }) + |> should.equal(Error(#("ok", 2))) +} + +pub fn flatten_test() { + Ok(Ok(1)) + |> result.flatten + |> should.equal(Ok(1)) + + Ok(Error(1)) + |> result.flatten + |> should.equal(Error(1)) + + Error(1) + |> result.flatten + |> should.equal(Error(1)) + + Error(Error(1)) + |> result.flatten + |> should.equal(Error(Error(1))) +} + +pub fn then_test() { + Error(1) + |> result.then(fn(x) { Ok(x + 1) }) + |> should.equal(Error(1)) + + Ok(1) + |> result.then(fn(x) { Ok(x + 1) }) + |> should.equal(Ok(2)) + + Ok(1) + |> result.then(fn(_) { Ok("type change") }) + |> should.equal(Ok("type change")) + + Ok(1) + |> result.then(fn(_) { Error(1) }) + |> should.equal(Error(1)) +} + +pub fn unwrap_test() { + Ok(1) + |> result.unwrap(50) + |> should.equal(1) + + Error("nope") + |> result.unwrap(50) + |> should.equal(50) +} + +pub fn lazy_unwrap_test() { + Ok(1) + |> result.lazy_unwrap(fn() { 50 }) + |> should.equal(1) + + Error("nope") + |> result.lazy_unwrap(fn() { 50 }) + |> should.equal(50) +} + +pub fn nil_error_test() { + Error("error_string") + |> result.nil_error + |> should.equal(Error(Nil)) + + Error(123) + |> result.nil_error + |> should.equal(Error(Nil)) + + Ok(1) + |> result.nil_error + |> should.equal(Ok(1)) +} + +pub fn or_test() { + Ok(1) + |> result.or(Ok(2)) + |> should.equal(Ok(1)) + + Ok(1) + |> result.or(Error("Error 2")) + |> should.equal(Ok(1)) + + Error("Error 1") + |> result.or(Ok(2)) + |> should.equal(Ok(2)) + + Error("Error 1") + |> result.or(Error("Error 2")) + |> should.equal(Error("Error 2")) +} + +pub fn lazy_or_test() { + Ok(1) + |> result.lazy_or(fn() { Ok(2) }) + |> should.equal(Ok(1)) + + Ok(1) + |> result.lazy_or(fn() { Error("Error 2") }) + |> should.equal(Ok(1)) + + Error("Error 1") + |> result.lazy_or(fn() { Ok(2) }) + |> should.equal(Ok(2)) + + Error("Error 1") + |> result.lazy_or(fn() { Error("Error 2") }) + |> should.equal(Error("Error 2")) +} + +pub fn all_test() { + [Ok(1), Ok(2), Ok(3)] + |> result.all + |> should.equal(Ok([1, 2, 3])) + + [Ok(1), Error("a"), Error("b"), Ok(3)] + |> result.all + |> should.equal(Error("a")) +} + +pub fn replace_error_test() { + Error(Nil) + |> result.replace_error("Invalid") + |> should.equal(Error("Invalid")) +} + +pub fn values_test() { + result.values([Ok(1), Error(""), Ok(3)]) + |> should.equal([1, 3]) } |