aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorRJ Dellecese <rjdellecese@gmail.com>2019-12-28 21:29:11 -0500
committerLouis Pilfold <louis@lpil.uk>2019-12-30 22:36:04 +0000
commit9292225d996c2092647fae15726a9f9bb2a691c8 (patch)
tree3e2b4edfcbe34c54eb279cd59924e9fc0ce13590 /test
parenta37c8fcf7b8ed522bc7c23dc25c3a804f7728adb (diff)
downloadgleam_stdlib-9292225d996c2092647fae15726a9f9bb2a691c8.tar.gz
gleam_stdlib-9292225d996c2092647fae15726a9f9bb2a691c8.zip
Add generic module
Containing identity, always, flip, and compose functions.
Diffstat (limited to 'test')
-rw-r--r--test/gleam/generic_test.gleam66
1 files changed, 66 insertions, 0 deletions
diff --git a/test/gleam/generic_test.gleam b/test/gleam/generic_test.gleam
new file mode 100644
index 0000000..a65e931
--- /dev/null
+++ b/test/gleam/generic_test.gleam
@@ -0,0 +1,66 @@
+import gleam/expect
+import gleam/generic.{identity, always, flip, compose}
+import gleam/int as int_mod
+import gleam/list
+import gleam/result
+import gleam/string as string_mod
+
+pub fn identity_test() {
+ 1
+ |> identity
+ |> expect.equal(_, 1)
+}
+
+pub fn always_test() {
+ 1
+ |> always(_, 2)
+ |> expect.equal(_, 2)
+}
+
+pub fn flip_test() {
+ let fun = fn(string: String, int: Int) {
+ string
+ |> string_mod.append("String: '", _)
+ |> string_mod.append(_, "', Int: '")
+ |> string_mod.append(_, int_mod.to_string(int))
+ |> string_mod.append(_, "'")
+ }
+
+ let flipped_fun = flip(fun)
+
+ fun("Bob", 1)
+ |> expect.equal(_, "String: 'Bob', Int: '1'")
+
+ flipped_fun(2, "Alice")
+ |> expect.equal(_, "String: 'Alice', Int: '2'")
+}
+
+pub fn compose_test() {
+ let add_two = fn(int: Int) { int + 2 }
+ let add_three = fn(int: Int) { int + 3 }
+
+ let add_five = compose(add_two, add_three)
+
+ 1
+ |> add_five
+ |> expect.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 =
+ compose(
+ list.head,
+ fn(int_result: Result(Int, Nil)) {
+ result.unwrap(int_result, 0)
+ |> int_mod.to_string
+ }
+ )
+
+ [1]
+ |> head_to_string
+ |> expect.equal(_, "1")
+
+ []
+ |> head_to_string
+ |> expect.equal(_, "0")
+}