diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/gleam/atom_test.gleam | 90 | ||||
-rw-r--r-- | test/gleam/base_test.gleam | 192 | ||||
-rw-r--r-- | test/gleam/bit_builder_test.gleam | 126 | ||||
-rw-r--r-- | test/gleam/bit_string_test.gleam | 189 | ||||
-rw-r--r-- | test/gleam/bool_test.gleam | 170 | ||||
-rw-r--r-- | test/gleam/dynamic_test.gleam | 1488 | ||||
-rw-r--r-- | test/gleam/float_test.gleam | 446 | ||||
-rw-r--r-- | test/gleam/function_test.gleam | 206 | ||||
-rw-r--r-- | test/gleam/int_test.gleam | 338 | ||||
-rw-r--r-- | test/gleam/iterator_test.gleam | 706 | ||||
-rw-r--r-- | test/gleam/list_test.gleam | 1144 | ||||
-rw-r--r-- | test/gleam/map_test.gleam | 278 | ||||
-rw-r--r-- | test/gleam/option_test.gleam | 230 | ||||
-rw-r--r-- | test/gleam/order_test.gleam | 156 | ||||
-rw-r--r-- | test/gleam/os_test.gleam | 80 | ||||
-rw-r--r-- | test/gleam/pair_test.gleam | 116 | ||||
-rw-r--r-- | test/gleam/queue_test.gleam | 534 | ||||
-rw-r--r-- | test/gleam/regex_test.gleam | 120 | ||||
-rw-r--r-- | test/gleam/result_test.gleam | 338 | ||||
-rw-r--r-- | test/gleam/set_test.gleam | 160 | ||||
-rw-r--r-- | test/gleam/string_builder_test.gleam | 204 | ||||
-rw-r--r-- | test/gleam/string_test.gleam | 664 | ||||
-rw-r--r-- | test/gleam/uri_test.gleam | 586 |
23 files changed, 4307 insertions, 4254 deletions
diff --git a/test/gleam/atom_test.gleam b/test/gleam/atom_test.gleam index 228791d..ccc34ed 100644 --- a/test/gleam/atom_test.gleam +++ b/test/gleam/atom_test.gleam @@ -1,45 +1,47 @@ -import gleam/atom -import gleam/should - -pub fn from_string_test() { - atom.create_from_string("this is an existing atom") - - "this is an existing atom" - |> atom.from_string - |> should.be_ok - - "this is not an atom we have seen before" - |> atom.from_string - |> should.equal(Error(atom.AtomNotLoaded)) -} - -pub fn create_from_string_test() { - "ok" - |> atom.create_from_string - |> Ok - |> should.equal(atom.from_string("ok")) - - "expect" - |> atom.create_from_string - |> Ok - |> should.equal(atom.from_string("expect")) - - "this is another atom we have not seen before" - |> atom.create_from_string - |> Ok - |> should.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 - |> should.equal("ok") - - "expect" - |> atom.create_from_string - |> atom.to_string - |> should.equal("expect") +if erlang { + import gleam/atom + import gleam/should + + pub fn from_string_test() { + atom.create_from_string("this is an existing atom") + + "this is an existing atom" + |> atom.from_string + |> should.be_ok + + "this is not an atom we have seen before" + |> atom.from_string + |> should.equal(Error(atom.AtomNotLoaded)) + } + + pub fn create_from_string_test() { + "ok" + |> atom.create_from_string + |> Ok + |> should.equal(atom.from_string("ok")) + + "expect" + |> atom.create_from_string + |> Ok + |> should.equal(atom.from_string("expect")) + + "this is another atom we have not seen before" + |> atom.create_from_string + |> Ok + |> should.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 + |> should.equal("ok") + + "expect" + |> atom.create_from_string + |> atom.to_string + |> should.equal("expect") + } } diff --git a/test/gleam/base_test.gleam b/test/gleam/base_test.gleam index 3d2b7c6..80f0024 100644 --- a/test/gleam/base_test.gleam +++ b/test/gleam/base_test.gleam @@ -1,96 +1,98 @@ -import gleam/base -import gleam/bit_string.{BitString} -import gleam/io -import gleam/list -import gleam/should - -external fn list_to_binary(List(Int)) -> BitString = - "erlang" "list_to_binary" - -pub fn encode64_test() { - [255, 127, 254, 252] - |> list_to_binary() - |> base.encode64(True) - |> should.equal("/3/+/A==") - - [255, 127, 254, 252] - |> list_to_binary() - |> base.encode64(False) - |> should.equal("/3/+/A") - - [0, 0, 0] - |> list_to_binary() - |> base.encode64(True) - |> should.equal("AAAA") - - [] - |> list_to_binary() - |> base.encode64(True) - |> should.equal("") -} - -pub fn decode64_test() { - "/3/+/A==" - |> base.decode64() - |> should.equal(Ok(list_to_binary([255, 127, 254, 252]))) - - "/3/+/A" - |> base.decode64() - |> should.equal(Ok(list_to_binary([255, 127, 254, 252]))) - - "AAAA" - |> base.decode64() - |> should.equal(Ok(list_to_binary([0, 0, 0]))) - - "" - |> base.decode64() - |> should.equal(Ok(list_to_binary([]))) - - ")!" - |> base.decode64() - |> should.equal(Error(Nil)) -} - -pub fn url_encode64_test() { - [255, 127, 254, 252] - |> list_to_binary() - |> base.url_encode64(True) - |> should.equal("_3_-_A==") - - [255, 127, 254, 252] - |> list_to_binary() - |> base.url_encode64(False) - |> should.equal("_3_-_A") - - [0, 0, 0] - |> list_to_binary() - |> base.url_encode64(True) - |> should.equal("AAAA") - - [] - |> list_to_binary() - |> base.url_encode64(True) - |> should.equal("") -} - -pub fn url_decode64_test() { - "_3_-_A==" - |> base.url_decode64() - |> should.equal(Ok(list_to_binary([255, 127, 254, 252]))) - - "_3_-_A" - |> base.url_decode64() - |> should.equal(Ok(list_to_binary([255, 127, 254, 252]))) - - "AAAA" - |> base.url_decode64() - |> should.equal(Ok(list_to_binary([0, 0, 0]))) - - "" - |> base.url_decode64() - |> should.equal(Ok(list_to_binary([]))) - - ")!" - |> base.url_decode64() - |> should.equal(Error(Nil)) +if erlang { + import gleam/base + import gleam/bit_string.{BitString} + import gleam/io + import gleam/list + import gleam/should + + external fn list_to_binary(List(Int)) -> BitString = + "erlang" "list_to_binary" + + pub fn encode64_test() { + [255, 127, 254, 252] + |> list_to_binary() + |> base.encode64(True) + |> should.equal("/3/+/A==") + + [255, 127, 254, 252] + |> list_to_binary() + |> base.encode64(False) + |> should.equal("/3/+/A") + + [0, 0, 0] + |> list_to_binary() + |> base.encode64(True) + |> should.equal("AAAA") + + [] + |> list_to_binary() + |> base.encode64(True) + |> should.equal("") + } + + pub fn decode64_test() { + "/3/+/A==" + |> base.decode64() + |> should.equal(Ok(list_to_binary([255, 127, 254, 252]))) + + "/3/+/A" + |> base.decode64() + |> should.equal(Ok(list_to_binary([255, 127, 254, 252]))) + + "AAAA" + |> base.decode64() + |> should.equal(Ok(list_to_binary([0, 0, 0]))) + + "" + |> base.decode64() + |> should.equal(Ok(list_to_binary([]))) + + ")!" + |> base.decode64() + |> should.equal(Error(Nil)) + } + + pub fn url_encode64_test() { + [255, 127, 254, 252] + |> list_to_binary() + |> base.url_encode64(True) + |> should.equal("_3_-_A==") + + [255, 127, 254, 252] + |> list_to_binary() + |> base.url_encode64(False) + |> should.equal("_3_-_A") + + [0, 0, 0] + |> list_to_binary() + |> base.url_encode64(True) + |> should.equal("AAAA") + + [] + |> list_to_binary() + |> base.url_encode64(True) + |> should.equal("") + } + + pub fn url_decode64_test() { + "_3_-_A==" + |> base.url_decode64() + |> should.equal(Ok(list_to_binary([255, 127, 254, 252]))) + + "_3_-_A" + |> base.url_decode64() + |> should.equal(Ok(list_to_binary([255, 127, 254, 252]))) + + "AAAA" + |> base.url_decode64() + |> should.equal(Ok(list_to_binary([0, 0, 0]))) + + "" + |> base.url_decode64() + |> should.equal(Ok(list_to_binary([]))) + + ")!" + |> base.url_decode64() + |> should.equal(Error(Nil)) + } } diff --git a/test/gleam/bit_builder_test.gleam b/test/gleam/bit_builder_test.gleam index e1ec57b..fac962f 100644 --- a/test/gleam/bit_builder_test.gleam +++ b/test/gleam/bit_builder_test.gleam @@ -1,75 +1,77 @@ -import gleam/should -import gleam/bit_builder +if erlang { + import gleam/should + import gleam/bit_builder -pub fn builder_test() { - let data = - bit_builder.from_bit_string(<<1>>) - |> bit_builder.append(<<2>>) - |> bit_builder.append(<<3>>) - |> bit_builder.prepend(<<0>>) + pub fn builder_test() { + let data = + bit_builder.from_bit_string(<<1>>) + |> bit_builder.append(<<2>>) + |> bit_builder.append(<<3>>) + |> bit_builder.prepend(<<0>>) - data - |> bit_builder.to_bit_string - |> should.equal(<<0, 1, 2, 3>>) + data + |> bit_builder.to_bit_string + |> should.equal(<<0, 1, 2, 3>>) - data - |> bit_builder.byte_size - |> should.equal(4) -} + data + |> bit_builder.byte_size + |> should.equal(4) + } -pub fn builder_with_strings_test() { - let data = - bit_builder.from_bit_string(<<1>>) - |> bit_builder.append_string("2") - |> bit_builder.append_string("3") - |> bit_builder.prepend_string("0") + pub fn builder_with_strings_test() { + let data = + bit_builder.from_bit_string(<<1>>) + |> bit_builder.append_string("2") + |> bit_builder.append_string("3") + |> bit_builder.prepend_string("0") - data - |> bit_builder.to_bit_string - |> should.equal(<<"0":utf8, 1, "2":utf8, "3":utf8>>) + data + |> bit_builder.to_bit_string + |> should.equal(<<"0":utf8, 1, "2":utf8, "3":utf8>>) - data - |> bit_builder.byte_size - |> should.equal(4) -} + data + |> bit_builder.byte_size + |> should.equal(4) + } -pub fn builder_with_builders_test() { - let data = - bit_builder.from_bit_string(<<1>>) - |> bit_builder.append_builder(bit_builder.from_bit_string(<<2>>)) - |> bit_builder.append_builder(bit_builder.from_bit_string(<<3>>)) - |> bit_builder.prepend_builder(bit_builder.from_bit_string(<<0>>)) + pub fn builder_with_builders_test() { + let data = + bit_builder.from_bit_string(<<1>>) + |> bit_builder.append_builder(bit_builder.from_bit_string(<<2>>)) + |> bit_builder.append_builder(bit_builder.from_bit_string(<<3>>)) + |> bit_builder.prepend_builder(bit_builder.from_bit_string(<<0>>)) - data - |> bit_builder.to_bit_string - |> should.equal(<<0, 1, 2, 3>>) + data + |> bit_builder.to_bit_string + |> should.equal(<<0, 1, 2, 3>>) - data - |> bit_builder.byte_size - |> should.equal(4) -} + data + |> bit_builder.byte_size + |> should.equal(4) + } -pub fn concat_test() { - [ - bit_builder.from_bit_string(<<1, 2>>), - bit_builder.from_bit_string(<<3, 4>>), - bit_builder.from_bit_string(<<5, 6>>), - ] - |> bit_builder.concat - |> bit_builder.to_bit_string - |> should.equal(<<1, 2, 3, 4, 5, 6>>) -} + pub fn concat_test() { + [ + bit_builder.from_bit_string(<<1, 2>>), + bit_builder.from_bit_string(<<3, 4>>), + bit_builder.from_bit_string(<<5, 6>>), + ] + |> bit_builder.concat + |> bit_builder.to_bit_string + |> should.equal(<<1, 2, 3, 4, 5, 6>>) + } -pub fn from_bit_string_test() { - // Regression test: no additional modification of the builder - bit_builder.from_bit_string(<<>>) - |> bit_builder.to_bit_string - |> should.equal(<<>>) -} + pub fn from_bit_string_test() { + // Regression test: no additional modification of the builder + bit_builder.from_bit_string(<<>>) + |> bit_builder.to_bit_string + |> should.equal(<<>>) + } -pub fn from_string_test() { - // Regression test: no additional modification of the builder - bit_builder.from_string("") - |> bit_builder.to_bit_string - |> should.equal(<<>>) + pub fn from_string_test() { + // Regression test: no additional modification of the builder + bit_builder.from_string("") + |> bit_builder.to_bit_string + |> should.equal(<<>>) + } } diff --git a/test/gleam/bit_string_test.gleam b/test/gleam/bit_string_test.gleam index ea577fd..a69bc90 100644 --- a/test/gleam/bit_string_test.gleam +++ b/test/gleam/bit_string_test.gleam @@ -1,93 +1,98 @@ -import gleam/bit_string -import gleam/should - -pub fn length_test() { - bit_string.byte_size(bit_string.from_string("hello")) - |> should.equal(5) - - bit_string.byte_size(bit_string.from_string("")) - |> should.equal(0) -} - -pub fn append_test() { - bit_string.from_string("Test") - |> bit_string.append(bit_string.from_string(" Me")) - |> should.equal(bit_string.from_string("Test Me")) - - let Ok(zero_32bit) = bit_string.int_to_u32(0) - zero_32bit - |> bit_string.append(bit_string.from_string("")) - |> should.equal(zero_32bit) -} - -pub fn part_test() { - bit_string.from_string("hello") - |> bit_string.part(0, 5) - |> should.equal(Ok(bit_string.from_string("hello"))) - - bit_string.from_string("hello") - |> bit_string.part(0, 0) - |> should.equal(Ok(bit_string.from_string(""))) - - bit_string.from_string("hello") - |> bit_string.part(2, 2) - |> should.equal(Ok(bit_string.from_string("ll"))) - - bit_string.from_string("hello") - |> bit_string.part(5, -2) - |> should.equal(Ok(bit_string.from_string("lo"))) - - bit_string.from_string("") - |> bit_string.part(0, 0) - |> should.equal(Ok(bit_string.from_string(""))) - - bit_string.from_string("hello") - |> bit_string.part(6, 0) - |> should.equal(Error(Nil)) - - bit_string.from_string("hello") - |> bit_string.part(-1, 1) - |> should.equal(Error(Nil)) - - bit_string.from_string("hello") - |> bit_string.part(1, 6) - |> should.equal(Error(Nil)) -} - -pub fn u32_test() { - let Ok(bin) = bit_string.int_to_u32(0) - should.equal(4, bit_string.byte_size(bin)) - should.equal(Ok(0), bit_string.int_from_u32(bin)) - - let Ok(bin) = bit_string.int_to_u32(4294967295) - should.equal(4, bit_string.byte_size(bin)) - should.equal(Ok(4294967295), bit_string.int_from_u32(bin)) - - should.equal(Error(Nil), bit_string.int_from_u32(bit_string.from_string(""))) - should.equal( - Error(Nil), - bit_string.int_from_u32(bit_string.from_string("12345")), - ) -} - -pub fn to_string_test() { - <<>> - |> bit_string.to_string - |> should.equal(Ok("")) - - <<"":utf8>> - |> bit_string.to_string - |> should.equal(Ok("")) - - <<"Hello":utf8>> - |> bit_string.to_string - |> should.equal(Ok("Hello")) - - <<"ø":utf8>> - |> bit_string.to_string - |> should.equal(Ok("ø")) - - <<65535:16>> - |> bit_string.to_string - |> should.equal(Error(Nil)) +if erlang { + import gleam/bit_string + import gleam/should + + pub fn length_test() { + bit_string.byte_size(bit_string.from_string("hello")) + |> should.equal(5) + + bit_string.byte_size(bit_string.from_string("")) + |> should.equal(0) + } + + pub fn append_test() { + bit_string.from_string("Test") + |> bit_string.append(bit_string.from_string(" Me")) + |> should.equal(bit_string.from_string("Test Me")) + + let Ok(zero_32bit) = bit_string.int_to_u32(0) + zero_32bit + |> bit_string.append(bit_string.from_string("")) + |> should.equal(zero_32bit) + } + + pub fn part_test() { + bit_string.from_string("hello") + |> bit_string.part(0, 5) + |> should.equal(Ok(bit_string.from_string("hello"))) + + bit_string.from_string("hello") + |> bit_string.part(0, 0) + |> should.equal(Ok(bit_string.from_string(""))) + + bit_string.from_string("hello") + |> bit_string.part(2, 2) + |> should.equal(Ok(bit_string.from_string("ll"))) + + bit_string.from_string("hello") + |> bit_string.part(5, -2) + |> should.equal(Ok(bit_string.from_string("lo"))) + + bit_string.from_string("") + |> bit_string.part(0, 0) + |> should.equal(Ok(bit_string.from_string(""))) + + bit_string.from_string("hello") + |> bit_string.part(6, 0) + |> should.equal(Error(Nil)) + + bit_string.from_string("hello") + |> bit_string.part(-1, 1) + |> should.equal(Error(Nil)) + + bit_string.from_string("hello") + |> bit_string.part(1, 6) + |> should.equal(Error(Nil)) + } + + pub fn u32_test() { + let Ok(bin) = bit_string.int_to_u32(0) + should.equal(4, bit_string.byte_size(bin)) + should.equal(Ok(0), bit_string.int_from_u32(bin)) + + let Ok(bin) = bit_string.int_to_u32(4294967295) + should.equal(4, bit_string.byte_size(bin)) + should.equal(Ok(4294967295), bit_string.int_from_u32(bin)) + + should.equal( + Error(Nil), + bit_string.int_from_u32(bit_string.from_string("")), + ) + should.equal( + Error(Nil), + bit_string.int_from_u32(bit_string.from_string("12345")), + ) + } + + pub fn to_string_test() { + <<>> + |> bit_string.to_string + |> should.equal(Ok("")) + + <<"":utf8>> + |> bit_string.to_string + |> should.equal(Ok("")) + + <<"Hello":utf8>> + |> bit_string.to_string + |> should.equal(Ok("Hello")) + + <<"ø":utf8>> + |> bit_string.to_string + |> should.equal(Ok("ø")) + + <<65535:16>> + |> bit_string.to_string + |> should.equal(Error(Nil)) + } } diff --git a/test/gleam/bool_test.gleam b/test/gleam/bool_test.gleam index 3edea25..9658377 100644 --- a/test/gleam/bool_test.gleam +++ b/test/gleam/bool_test.gleam @@ -1,117 +1,119 @@ -import gleam/bool -import gleam/order -import gleam/should +if erlang { + import gleam/bool + import gleam/order + import gleam/should -pub fn negate_test() { - bool.negate(True) - |> should.be_false + pub fn negate_test() { + bool.negate(True) + |> should.be_false - bool.negate(False) - |> should.be_true -} + bool.negate(False) + |> should.be_true + } -pub fn nor_test() { - bool.nor(False, False) - |> should.be_true + pub fn nor_test() { + bool.nor(False, False) + |> should.be_true - bool.nor(False, True) - |> should.be_false + bool.nor(False, True) + |> should.be_false - bool.nor(True, False) - |> should.be_false + bool.nor(True, False) + |> should.be_false - bool.nor(True, True) - |> should.be_false -} + bool.nor(True, True) + |> should.be_false + } -pub fn nand_test() { - bool.nand(False, False) - |> should.be_true + pub fn nand_test() { + bool.nand(False, False) + |> should.be_true - bool.nand(False, True) - |> should.be_true + bool.nand(False, True) + |> should.be_true - bool.nand(True, False) - |> should.be_true + bool.nand(True, False) + |> should.be_true - bool.nand(True, True) - |> should.be_false -} + bool.nand(True, True) + |> should.be_false + } -pub fn exclusive_or_test() { - bool.exclusive_or(True, True) - |> should.be_false + pub fn exclusive_or_test() { + bool.exclusive_or(True, True) + |> should.be_false - bool.exclusive_or(False, False) - |> should.be_false + bool.exclusive_or(False, False) + |> should.be_false - bool.exclusive_or(True, False) - |> should.be_true + bool.exclusive_or(True, False) + |> should.be_true - bool.exclusive_or(False, True) - |> should.be_true -} + bool.exclusive_or(False, True) + |> should.be_true + } -pub fn exclusive_nor_test() { - bool.exclusive_nor(False, False) - |> should.be_true + pub fn exclusive_nor_test() { + bool.exclusive_nor(False, False) + |> should.be_true - bool.exclusive_nor(False, True) - |> should.be_false + bool.exclusive_nor(False, True) + |> should.be_false - bool.exclusive_nor(True, False) - |> should.be_false + bool.exclusive_nor(True, False) + |> should.be_false - bool.exclusive_nor(True, True) - |> should.be_true -} + bool.exclusive_nor(True, True) + |> should.be_true + } -pub fn compare_test() { - bool.compare(True, True) - |> should.equal(order.Eq) + pub fn compare_test() { + bool.compare(True, True) + |> should.equal(order.Eq) - bool.compare(True, False) - |> should.equal(order.Gt) + bool.compare(True, False) + |> should.equal(order.Gt) - bool.compare(False, False) - |> should.equal(order.Eq) + bool.compare(False, False) + |> should.equal(order.Eq) - bool.compare(False, True) - |> should.equal(order.Lt) -} + bool.compare(False, True) + |> should.equal(order.Lt) + } -pub fn max_test() { - bool.max(True, True) - |> should.equal(True) + pub fn max_test() { + bool.max(True, True) + |> should.equal(True) - bool.max(True, False) - |> should.equal(True) + bool.max(True, False) + |> should.equal(True) - bool.max(False, False) - |> should.equal(False) + bool.max(False, False) + |> should.equal(False) - bool.max(False, True) - |> should.equal(True) -} + bool.max(False, True) + |> should.equal(True) + } -pub fn min_test() { - bool.min(True, True) - |> should.equal(True) + pub fn min_test() { + bool.min(True, True) + |> should.equal(True) - bool.min(True, False) - |> should.equal(False) + bool.min(True, False) + |> should.equal(False) - bool.min(False, False) - |> should.equal(False) + bool.min(False, False) + |> should.equal(False) - bool.min(False, True) - |> should.equal(False) -} + bool.min(False, True) + |> should.equal(False) + } -pub fn to_int_test() { - bool.to_int(True) - |> should.equal(1) + pub fn to_int_test() { + bool.to_int(True) + |> should.equal(1) - bool.to_int(False) - |> should.equal(0) + bool.to_int(False) + |> should.equal(0) + } } diff --git a/test/gleam/dynamic_test.gleam b/test/gleam/dynamic_test.gleam index 2cc82b1..ca82802 100644 --- a/test/gleam/dynamic_test.gleam +++ b/test/gleam/dynamic_test.gleam @@ -1,744 +1,746 @@ -import gleam/bit_string -import gleam/dynamic -import gleam/atom -import gleam/list -import gleam/should -import gleam/result -import gleam/map -import gleam/option.{None, Some} - -pub fn bit_string_test() { - "" - |> dynamic.from - |> dynamic.bit_string - |> should.equal(Ok(<<"":utf8>>)) - - "Hello" - |> dynamic.from - |> dynamic.bit_string - |> should.equal(Ok(<<"Hello":utf8>>)) - - <<65535:16>> - |> dynamic.from - |> dynamic.bit_string - |> should.equal(Ok(<<65535:16>>)) - - 1 - |> dynamic.from - |> dynamic.bit_string - |> should.equal(Error("Expected a bit_string, got an int")) - - [] - |> dynamic.from - |> dynamic.bit_string - |> should.equal(Error("Expected a bit_string, got a list")) -} - -pub fn string_test() { - "" - |> dynamic.from - |> dynamic.string - |> should.equal(Ok("")) - - "Hello" - |> dynamic.from - |> dynamic.string - |> should.equal(Ok("Hello")) - - <<65535:16>> - |> dynamic.from - |> dynamic.string - |> should.equal(Error("Expected a string, got a bit_string")) - - 1 - |> dynamic.from - |> dynamic.string - |> should.equal(Error("Expected a bit_string, got an int")) - - [] - |> dynamic.from - |> dynamic.string - |> should.equal(Error("Expected a bit_string, got a list")) -} - -pub fn int_test() { - 1 - |> dynamic.from - |> dynamic.int - |> should.equal(Ok(1)) - - 2 - |> dynamic.from - |> dynamic.int - |> should.equal(Ok(2)) - - 1.0 - |> dynamic.from - |> dynamic.int - |> should.equal(Error("Expected an int, got a float")) - - [] - |> dynamic.from - |> dynamic.int - |> should.equal(Error("Expected an int, got a list")) -} - -pub fn float_test() { - 1.0 - |> dynamic.from - |> dynamic.float - |> should.equal(Ok(1.0)) - - 2.2 - |> dynamic.from - |> dynamic.float - |> should.equal(Ok(2.2)) - - 1 - |> dynamic.from - |> dynamic.float - |> should.equal(Error("Expected a float, got an int")) - - [] - |> dynamic.from - |> dynamic.float - |> should.equal(Error("Expected a float, got a list")) -} - -pub fn thunk_test() { - fn() { 1 } - |> dynamic.from - |> dynamic.thunk - |> should.be_ok - - fn() { 1 } - |> dynamic.from - |> dynamic.thunk - |> result.map(fn(f) { f() }) - |> should.equal(Ok(dynamic.from(1))) - - fn(x) { x } - |> dynamic.from - |> dynamic.thunk - |> should.be_error - - 1 - |> dynamic.from - |> dynamic.thunk - |> should.be_error - - [] - |> dynamic.from - |> dynamic.thunk - |> should.be_error -} - -pub fn bool_test() { - True - |> dynamic.from - |> dynamic.bool - |> should.equal(Ok(True)) - - False - |> dynamic.from - |> dynamic.bool - |> should.equal(Ok(False)) - - 1 - |> dynamic.from - |> dynamic.bool - |> should.equal(Error("Expected a bool, got an int")) - - [] - |> dynamic.from - |> dynamic.bool - |> should.equal(Error("Expected a bool, got a list")) -} - -pub fn atom_test() { - "" - |> atom.create_from_string - |> dynamic.from - |> dynamic.atom - |> should.equal(Ok(atom.create_from_string(""))) - - "ok" - |> atom.create_from_string - |> dynamic.from - |> dynamic.atom - |> should.equal(Ok(atom.create_from_string("ok"))) - - 1 - |> dynamic.from - |> dynamic.atom - |> should.be_error - - [] - |> dynamic.from - |> dynamic.atom - |> should.be_error -} - -pub fn typed_list_test() { - [] - |> dynamic.from - |> dynamic.typed_list(dynamic.string) - |> should.equal(Ok([])) - - [] - |> dynamic.from - |> dynamic.typed_list(dynamic.int) - |> should.equal(Ok([])) - - [1, 2, 3] - |> dynamic.from - |> dynamic.typed_list(dynamic.int) - |> should.equal(Ok([1, 2, 3])) - - [[1], [2], [3]] - |> dynamic.from - |> dynamic.typed_list(dynamic.typed_list(_, dynamic.int)) - |> should.equal(Ok([[1], [2], [3]])) - - 1 - |> dynamic.from - |> dynamic.typed_list(dynamic.string) - |> should.be_error - - 1.0 - |> dynamic.from - |> dynamic.typed_list(dynamic.int) - |> should.be_error - - [""] - |> dynamic.from - |> dynamic.typed_list(dynamic.int) - |> should.be_error - - [dynamic.from(1), dynamic.from("not an int")] - |> dynamic.from - |> dynamic.typed_list(dynamic.int) - |> should.be_error -} - -pub fn option_test() { - let Ok(null) = atom.from_string("null") - - 1 - |> dynamic.from - |> dynamic.option(dynamic.int) - |> should.equal(Ok(Some(1))) - null - |> dynamic.from - |> dynamic.option(dynamic.int) - |> should.equal(Ok(None)) - 1 - |> dynamic.from - |> dynamic.option(dynamic.string) - |> should.be_error -} - -pub fn field_test() { - let Ok(ok_atom) = atom.from_string("ok") - let Ok(error_atom) = atom.from_string("error") - - map.new() - |> map.insert(ok_atom, 1) - |> dynamic.from - |> dynamic.field(ok_atom) - |> should.equal(Ok(dynamic.from(1))) - - map.new() - |> map.insert(ok_atom, 3) - |> map.insert(error_atom, 1) - |> dynamic.from - |> dynamic.field(ok_atom) - |> should.equal(Ok(dynamic.from(3))) - - map.new() - |> dynamic.from - |> dynamic.field(ok_atom) - |> should.be_error - - 1 - |> dynamic.from - |> dynamic.field(ok_atom) - |> should.be_error - - [] - |> dynamic.from - |> dynamic.field([]) - |> should.be_error -} - -pub fn element_test() { - let Ok(ok_atom) = atom.from_string("ok") - let ok_one_tuple = #(ok_atom, 1) - - ok_one_tuple - |> dynamic.from - |> dynamic.element(0) - |> should.equal(Ok(dynamic.from(ok_atom))) - - ok_one_tuple - |> dynamic.from - |> dynamic.element(1) - |> should.equal(Ok(dynamic.from(1))) - - ok_one_tuple - |> dynamic.from - |> dynamic.element(2) - |> should.be_error - - ok_one_tuple - |> dynamic.from - |> dynamic.element(-1) - |> should.be_error - - 1 - |> dynamic.from - |> dynamic.element(0) - |> should.be_error - - map.new() - |> map.insert(1, ok_atom) - |> dynamic.from - |> dynamic.element(0) - |> should.be_error -} - -pub fn tuple2_test() { - #(1, 2) - |> dynamic.from - |> dynamic.tuple2 - |> should.equal(Ok(#(dynamic.from(1), dynamic.from(2)))) - - #(1, "") - |> dynamic.from - |> dynamic.tuple2 - |> should.equal(Ok(#(dynamic.from(1), dynamic.from("")))) - - #(1, 2, 3) - |> dynamic.from - |> dynamic.tuple2 - |> should.equal(Error("Expected a 2 element tuple, got a 3 element tuple")) - - 1 - |> dynamic.from - |> dynamic.tuple2 - |> should.equal(Error("Expected a 2 element tuple, got an int")) -} - -pub fn typed_tuple2_test() { - #(1, 2) - |> dynamic.from - |> dynamic.typed_tuple2(dynamic.int, dynamic.int) - |> should.equal(Ok(#(1, 2))) - - #(1, "") - |> dynamic.from - |> dynamic.typed_tuple2(dynamic.int, dynamic.string) - |> should.equal(Ok(#(1, ""))) - - #(1, "") - |> dynamic.from - |> dynamic.typed_tuple2(dynamic.int, dynamic.int) - |> should.equal(Error("Expected an int, got a binary")) - - #(1, 2, 3) - |> dynamic.from - |> dynamic.typed_tuple2(dynamic.int, dynamic.int) - |> should.equal(Error("Expected a 2 element tuple, got a 3 element tuple")) - - 1 - |> dynamic.from - |> dynamic.typed_tuple2(dynamic.int, dynamic.int) - |> should.equal(Error("Expected a 2 element tuple, got an int")) -} - -pub fn tuple3_test() { - #(1, 2, 3) - |> dynamic.from - |> dynamic.tuple3 - |> should.equal(Ok(#(dynamic.from(1), dynamic.from(2), dynamic.from(3)))) - - #(1, "", 3.0) - |> dynamic.from - |> dynamic.tuple3 - |> should.equal(Ok(#(dynamic.from(1), dynamic.from(""), dynamic.from(3.0)))) - - #(1, 2) - |> dynamic.from - |> dynamic.tuple3 - |> should.equal(Error("Expected a 3 element tuple, got a 2 element tuple")) - - 1 - |> dynamic.from - |> dynamic.tuple3 - |> should.equal(Error("Expected a 3 element tuple, got an int")) -} - -pub fn typed_tuple3_test() { - #(1, 2, 3) - |> dynamic.from - |> dynamic.typed_tuple3(dynamic.int, dynamic.int, dynamic.int) - |> should.equal(Ok(#(1, 2, 3))) - - #(1, "", 3.0) - |> dynamic.from - |> dynamic.typed_tuple3(dynamic.int, dynamic.string, dynamic.float) - |> should.equal(Ok(#(1, "", 3.0))) - - #(1, 2, "") - |> dynamic.from - |> dynamic.typed_tuple3(dynamic.int, dynamic.int, dynamic.int) - |> should.equal(Error("Expected an int, got a binary")) - - #(1, 2) - |> dynamic.from - |> dynamic.typed_tuple3(dynamic.int, dynamic.int, dynamic.int) - |> should.equal(Error("Expected a 3 element tuple, got a 2 element tuple")) - - 1 - |> dynamic.from - |> dynamic.typed_tuple3(dynamic.int, dynamic.int, dynamic.int) - |> should.equal(Error("Expected a 3 element tuple, got an int")) -} - -pub fn tuple4_test() { - #(1, 2, 3, 4) - |> dynamic.from - |> dynamic.tuple4 - |> should.equal(Ok(#( - dynamic.from(1), - dynamic.from(2), - dynamic.from(3), - dynamic.from(4), - ))) - - #(1, "", 3.0, 4) - |> dynamic.from - |> dynamic.tuple4 - |> should.equal(Ok(#( - dynamic.from(1), - dynamic.from(""), - dynamic.from(3.0), - dynamic.from(4), - ))) - - #(1, 2) - |> dynamic.from - |> dynamic.tuple4 - |> should.equal(Error("Expected a 4 element tuple, got a 2 element tuple")) - - 1 - |> dynamic.from - |> dynamic.tuple4 - |> should.equal(Error("Expected a 4 element tuple, got an int")) -} - -pub fn typed_tuple4_test() { - #(1, 2, 3, 4) - |> dynamic.from - |> dynamic.typed_tuple4(dynamic.int, dynamic.int, dynamic.int, dynamic.int) - |> should.equal(Ok(#(1, 2, 3, 4))) - - #(1, "", 3.0, 4) - |> dynamic.from - |> dynamic.typed_tuple4( - dynamic.int, - dynamic.string, - dynamic.float, - dynamic.int, - ) - |> should.equal(Ok(#(1, "", 3.0, 4))) - - #(1, 2, 3, "") - |> dynamic.from - |> dynamic.typed_tuple4(dynamic.int, dynamic.int, dynamic.int, dynamic.int) - |> should.equal(Error("Expected an int, got a binary")) - - #(1, 2) - |> dynamic.from - |> dynamic.typed_tuple4(dynamic.int, dynamic.int, dynamic.int, dynamic.int) - |> should.equal(Error("Expected a 4 element tuple, got a 2 element tuple")) - - 1 - |> dynamic.from - |> dynamic.typed_tuple4(dynamic.int, dynamic.int, dynamic.int, dynamic.int) - |> should.equal(Error("Expected a 4 element tuple, got an int")) -} - -pub fn tuple5_test() { - #(1, 2, 3, 4, 5) - |> dynamic.from - |> dynamic.tuple5 - |> should.equal(Ok(#( - dynamic.from(1), - dynamic.from(2), - dynamic.from(3), - dynamic.from(4), - dynamic.from(5), - ))) - - #(1, "", 3.0, 4, 5) - |> dynamic.from - |> dynamic.tuple5 - |> should.equal(Ok(#( - dynamic.from(1), - dynamic.from(""), - dynamic.from(3.0), - dynamic.from(4), - dynamic.from(5), - ))) - - #(1, 2) - |> dynamic.from - |> dynamic.tuple5 - |> should.equal(Error("Expected a 5 element tuple, got a 2 element tuple")) - - 1 - |> dynamic.from - |> dynamic.tuple5 - |> should.equal(Error("Expected a 5 element tuple, got an int")) -} - -pub fn typed_tuple5_test() { - #(1, 2, 3, 4, 5) - |> dynamic.from - |> dynamic.typed_tuple5( - dynamic.int, - dynamic.int, - dynamic.int, - dynamic.int, - dynamic.int, - ) - |> should.equal(Ok(#(1, 2, 3, 4, 5))) - - #(1, "", 3.0, 4, 5) - |> dynamic.from - |> dynamic.typed_tuple5( - dynamic.int, - dynamic.string, - dynamic.float, - dynamic.int, - dynamic.int, - ) - |> should.equal(Ok(#(1, "", 3.0, 4, 5))) - - #(1, 2, 3, 4, "") - |> dynamic.from - |> dynamic.typed_tuple5( - dynamic.int, - dynamic.int, - dynamic.int, - dynamic.int, - dynamic.int, - ) - |> should.equal(Error("Expected an int, got a binary")) - - #(1, 2) - |> dynamic.from - |> dynamic.typed_tuple5( - dynamic.int, - dynamic.int, - dynamic.int, - dynamic.int, - dynamic.int, - ) - |> should.equal(Error("Expected a 5 element tuple, got a 2 element tuple")) - - 1 - |> dynamic.from - |> dynamic.typed_tuple5( - dynamic.int, - dynamic.int, - dynamic.int, - dynamic.int, - dynamic.int, - ) - |> should.equal(Error("Expected a 5 element tuple, got an int")) -} - -pub fn tuple6_test() { - #(1, 2, 3, 4, 5, 6) - |> dynamic.from - |> dynamic.tuple6 - |> should.equal(Ok(#( - dynamic.from(1), - dynamic.from(2), - dynamic.from(3), - dynamic.from(4), - dynamic.from(5), - dynamic.from(6), - ))) - - #(1, "", 3.0, 4, 5, 6) - |> dynamic.from - |> dynamic.tuple6 - |> should.equal(Ok(#( - dynamic.from(1), - dynamic.from(""), - dynamic.from(3.0), - dynamic.from(4), - dynamic.from(5), - dynamic.from(6), - ))) - - #(1, 2) - |> dynamic.from - |> dynamic.tuple6 - |> should.equal(Error("Expected a 6 element tuple, got a 2 element tuple")) - - 1 - |> dynamic.from - |> dynamic.tuple6 - |> should.equal(Error("Expected a 6 element tuple, got an int")) -} - -pub fn typed_tuple6_test() { - #(1, 2, 3, 4, 5, 6) - |> dynamic.from - |> dynamic.typed_tuple6( - dynamic.int, - dynamic.int, - dynamic.int, - dynamic.int, - dynamic.int, - dynamic.int, - ) - |> should.equal(Ok(#(1, 2, 3, 4, 5, 6))) - - #(1, "", 3.0, 4, 5, 6) - |> dynamic.from - |> dynamic.typed_tuple6( - dynamic.int, - dynamic.string, - dynamic.float, - dynamic.int, - dynamic.int, - dynamic.int, - ) - |> should.equal(Ok(#(1, "", 3.0, 4, 5, 6))) - - #(1, 2, 3, 4, 5, "") - |> dynamic.from - |> dynamic.typed_tuple6( - dynamic.int, - dynamic.int, - dynamic.int, - dynamic.int, - dynamic.int, - dynamic.int, - ) - |> should.equal(Error("Expected an int, got a binary")) - - #(1, 2) - |> dynamic.from - |> dynamic.typed_tuple6( - dynamic.int, - dynamic.int, - dynamic.int, - dynamic.int, - dynamic.int, - dynamic.int, - ) - |> should.equal(Error("Expected a 6 element tuple, got a 2 element tuple")) - - 1 - |> dynamic.from - |> dynamic.typed_tuple6( - dynamic.int, - dynamic.int, - dynamic.int, - dynamic.int, - dynamic.int, - dynamic.int, - ) - |> should.equal(Error("Expected a 6 element tuple, got an int")) -} - -pub fn map_test() { - map.new() - |> dynamic.from - |> dynamic.map - |> should.equal(Ok(map.new())) - - 1 - |> dynamic.from - |> dynamic.map - |> should.equal(Error("Expected a map, got an int")) -} - -pub fn list_test() { - [] - |> dynamic.from - |> dynamic.list - |> should.equal(Ok([])) - - [1, 2] - |> dynamic.from - |> dynamic.list - |> should.equal(Ok([dynamic.from(1), dynamic.from(2)])) - - [dynamic.from(1), dynamic.from(2.0)] - |> dynamic.from - |> dynamic.list - |> should.equal(Ok([dynamic.from(1), dynamic.from(2.0)])) - - 1 - |> dynamic.from - |> dynamic.list - |> should.equal(Error("Expected a list, got an int")) -} - -pub fn result_test() { - Ok(1) - |> dynamic.from - |> dynamic.result - |> should.equal(Ok(Ok(dynamic.from(1)))) - - Error("error") - |> dynamic.from - |> dynamic.result - |> should.equal(Ok(Error(dynamic.from("error")))) - - 1 - |> dynamic.from - |> dynamic.result - |> should.equal(Error("Expected a 2 element tuple, got an int")) - - let tag = atom.create_from_string("bad") - - #(tag, "value") - |> dynamic.from - |> dynamic.result - |> should.equal(Error("Expected a tag of \"ok\" or \"error\", got \"bad\"")) -} - -pub fn typed_result_test() { - Ok(1) - |> dynamic.from - |> dynamic.typed_result(ok: dynamic.int, error: dynamic.string) - |> should.equal(Ok(Ok(1))) - - Error("error") - |> dynamic.from - |> dynamic.typed_result(ok: dynamic.int, error: dynamic.string) - |> should.equal(Ok(Error("error"))) - - Ok("1") - |> dynamic.from - |> dynamic.typed_result(ok: dynamic.int, error: dynamic.string) - |> should.equal(Error("Expected an int, got a binary")) - - Error(1) - |> dynamic.from - |> dynamic.typed_result(ok: dynamic.int, error: dynamic.string) - |> should.equal(Error("Expected a bit_string, got an int")) - - 1 - |> dynamic.from - |> dynamic.typed_result(ok: dynamic.int, error: dynamic.string) - |> should.equal(Error("Expected a 2 element tuple, got an int")) +if erlang { + import gleam/bit_string + import gleam/dynamic + import gleam/atom + import gleam/list + import gleam/should + import gleam/result + import gleam/map + import gleam/option.{None, Some} + + pub fn bit_string_test() { + "" + |> dynamic.from + |> dynamic.bit_string + |> should.equal(Ok(<<"":utf8>>)) + + "Hello" + |> dynamic.from + |> dynamic.bit_string + |> should.equal(Ok(<<"Hello":utf8>>)) + + <<65535:16>> + |> dynamic.from + |> dynamic.bit_string + |> should.equal(Ok(<<65535:16>>)) + + 1 + |> dynamic.from + |> dynamic.bit_string + |> should.equal(Error("Expected a bit_string, got an int")) + + [] + |> dynamic.from + |> dynamic.bit_string + |> should.equal(Error("Expected a bit_string, got a list")) + } + + pub fn string_test() { + "" + |> dynamic.from + |> dynamic.string + |> should.equal(Ok("")) + + "Hello" + |> dynamic.from + |> dynamic.string + |> should.equal(Ok("Hello")) + + <<65535:16>> + |> dynamic.from + |> dynamic.string + |> should.equal(Error("Expected a string, got a bit_string")) + + 1 + |> dynamic.from + |> dynamic.string + |> should.equal(Error("Expected a bit_string, got an int")) + + [] + |> dynamic.from + |> dynamic.string + |> should.equal(Error("Expected a bit_string, got a list")) + } + + pub fn int_test() { + 1 + |> dynamic.from + |> dynamic.int + |> should.equal(Ok(1)) + + 2 + |> dynamic.from + |> dynamic.int + |> should.equal(Ok(2)) + + 1.0 + |> dynamic.from + |> dynamic.int + |> should.equal(Error("Expected an int, got a float")) + + [] + |> dynamic.from + |> dynamic.int + |> should.equal(Error("Expected an int, got a list")) + } + + pub fn float_test() { + 1.0 + |> dynamic.from + |> dynamic.float + |> should.equal(Ok(1.0)) + + 2.2 + |> dynamic.from + |> dynamic.float + |> should.equal(Ok(2.2)) + + 1 + |> dynamic.from + |> dynamic.float + |> should.equal(Error("Expected a float, got an int")) + + [] + |> dynamic.from + |> dynamic.float + |> should.equal(Error("Expected a float, got a list")) + } + + pub fn thunk_test() { + fn() { 1 } + |> dynamic.from + |> dynamic.thunk + |> should.be_ok + + fn() { 1 } + |> dynamic.from + |> dynamic.thunk + |> result.map(fn(f) { f() }) + |> should.equal(Ok(dynamic.from(1))) + + fn(x) { x } + |> dynamic.from + |> dynamic.thunk + |> should.be_error + + 1 + |> dynamic.from + |> dynamic.thunk + |> should.be_error + + [] + |> dynamic.from + |> dynamic.thunk + |> should.be_error + } + + pub fn bool_test() { + True + |> dynamic.from + |> dynamic.bool + |> should.equal(Ok(True)) + + False + |> dynamic.from + |> dynamic.bool + |> should.equal(Ok(False)) + + 1 + |> dynamic.from + |> dynamic.bool + |> should.equal(Error("Expected a bool, got an int")) + + [] + |> dynamic.from + |> dynamic.bool + |> should.equal(Error("Expected a bool, got a list")) + } + + pub fn atom_test() { + "" + |> atom.create_from_string + |> dynamic.from + |> dynamic.atom + |> should.equal(Ok(atom.create_from_string(""))) + + "ok" + |> atom.create_from_string + |> dynamic.from + |> dynamic.atom + |> should.equal(Ok(atom.create_from_string("ok"))) + + 1 + |> dynamic.from + |> dynamic.atom + |> should.be_error + + [] + |> dynamic.from + |> dynamic.atom + |> should.be_error + } + + pub fn typed_list_test() { + [] + |> dynamic.from + |> dynamic.typed_list(dynamic.string) + |> should.equal(Ok([])) + + [] + |> dynamic.from + |> dynamic.typed_list(dynamic.int) + |> should.equal(Ok([])) + + [1, 2, 3] + |> dynamic.from + |> dynamic.typed_list(dynamic.int) + |> should.equal(Ok([1, 2, 3])) + + [[1], [2], [3]] + |> dynamic.from + |> dynamic.typed_list(dynamic.typed_list(_, dynamic.int)) + |> should.equal(Ok([[1], [2], [3]])) + + 1 + |> dynamic.from + |> dynamic.typed_list(dynamic.string) + |> should.be_error + + 1.0 + |> dynamic.from + |> dynamic.typed_list(dynamic.int) + |> should.be_error + + [""] + |> dynamic.from + |> dynamic.typed_list(dynamic.int) + |> should.be_error + + [dynamic.from(1), dynamic.from("not an int")] + |> dynamic.from + |> dynamic.typed_list(dynamic.int) + |> should.be_error + } + + pub fn option_test() { + let Ok(null) = atom.from_string("null") + + 1 + |> dynamic.from + |> dynamic.option(dynamic.int) + |> should.equal(Ok(Some(1))) + null + |> dynamic.from + |> dynamic.option(dynamic.int) + |> should.equal(Ok(None)) + 1 + |> dynamic.from + |> dynamic.option(dynamic.string) + |> should.be_error + } + + pub fn field_test() { + let Ok(ok_atom) = atom.from_string("ok") + let Ok(error_atom) = atom.from_string("error") + + map.new() + |> map.insert(ok_atom, 1) + |> dynamic.from + |> dynamic.field(ok_atom) + |> should.equal(Ok(dynamic.from(1))) + + map.new() + |> map.insert(ok_atom, 3) + |> map.insert(error_atom, 1) + |> dynamic.from + |> dynamic.field(ok_atom) + |> should.equal(Ok(dynamic.from(3))) + + map.new() + |> dynamic.from + |> dynamic.field(ok_atom) + |> should.be_error + + 1 + |> dynamic.from + |> dynamic.field(ok_atom) + |> should.be_error + + [] + |> dynamic.from + |> dynamic.field([]) + |> should.be_error + } + + pub fn element_test() { + let Ok(ok_atom) = atom.from_string("ok") + let ok_one_tuple = #(ok_atom, 1) + + ok_one_tuple + |> dynamic.from + |> dynamic.element(0) + |> should.equal(Ok(dynamic.from(ok_atom))) + + ok_one_tuple + |> dynamic.from + |> dynamic.element(1) + |> should.equal(Ok(dynamic.from(1))) + + ok_one_tuple + |> dynamic.from + |> dynamic.element(2) + |> should.be_error + + ok_one_tuple + |> dynamic.from + |> dynamic.element(-1) + |> should.be_error + + 1 + |> dynamic.from + |> dynamic.element(0) + |> should.be_error + + map.new() + |> map.insert(1, ok_atom) + |> dynamic.from + |> dynamic.element(0) + |> should.be_error + } + + pub fn tuple2_test() { + #(1, 2) + |> dynamic.from + |> dynamic.tuple2 + |> should.equal(Ok(#(dynamic.from(1), dynamic.from(2)))) + + #(1, "") + |> dynamic.from + |> dynamic.tuple2 + |> should.equal(Ok(#(dynamic.from(1), dynamic.from("")))) + + #(1, 2, 3) + |> dynamic.from + |> dynamic.tuple2 + |> should.equal(Error("Expected a 2 element tuple, got a 3 element tuple")) + + 1 + |> dynamic.from + |> dynamic.tuple2 + |> should.equal(Error("Expected a 2 element tuple, got an int")) + } + + pub fn typed_tuple2_test() { + #(1, 2) + |> dynamic.from + |> dynamic.typed_tuple2(dynamic.int, dynamic.int) + |> should.equal(Ok(#(1, 2))) + + #(1, "") + |> dynamic.from + |> dynamic.typed_tuple2(dynamic.int, dynamic.string) + |> should.equal(Ok(#(1, ""))) + + #(1, "") + |> dynamic.from + |> dynamic.typed_tuple2(dynamic.int, dynamic.int) + |> should.equal(Error("Expected an int, got a binary")) + + #(1, 2, 3) + |> dynamic.from + |> dynamic.typed_tuple2(dynamic.int, dynamic.int) + |> should.equal(Error("Expected a 2 element tuple, got a 3 element tuple")) + + 1 + |> dynamic.from + |> dynamic.typed_tuple2(dynamic.int, dynamic.int) + |> should.equal(Error("Expected a 2 element tuple, got an int")) + } + + pub fn tuple3_test() { + #(1, 2, 3) + |> dynamic.from + |> dynamic.tuple3 + |> should.equal(Ok(#(dynamic.from(1), dynamic.from(2), dynamic.from(3)))) + + #(1, "", 3.0) + |> dynamic.from + |> dynamic.tuple3 + |> should.equal(Ok(#(dynamic.from(1), dynamic.from(""), dynamic.from(3.0)))) + + #(1, 2) + |> dynamic.from + |> dynamic.tuple3 + |> should.equal(Error("Expected a 3 element tuple, got a 2 element tuple")) + + 1 + |> dynamic.from + |> dynamic.tuple3 + |> should.equal(Error("Expected a 3 element tuple, got an int")) + } + + pub fn typed_tuple3_test() { + #(1, 2, 3) + |> dynamic.from + |> dynamic.typed_tuple3(dynamic.int, dynamic.int, dynamic.int) + |> should.equal(Ok(#(1, 2, 3))) + + #(1, "", 3.0) + |> dynamic.from + |> dynamic.typed_tuple3(dynamic.int, dynamic.string, dynamic.float) + |> should.equal(Ok(#(1, "", 3.0))) + + #(1, 2, "") + |> dynamic.from + |> dynamic.typed_tuple3(dynamic.int, dynamic.int, dynamic.int) + |> should.equal(Error("Expected an int, got a binary")) + + #(1, 2) + |> dynamic.from + |> dynamic.typed_tuple3(dynamic.int, dynamic.int, dynamic.int) + |> should.equal(Error("Expected a 3 element tuple, got a 2 element tuple")) + + 1 + |> dynamic.from + |> dynamic.typed_tuple3(dynamic.int, dynamic.int, dynamic.int) + |> should.equal(Error("Expected a 3 element tuple, got an int")) + } + + pub fn tuple4_test() { + #(1, 2, 3, 4) + |> dynamic.from + |> dynamic.tuple4 + |> should.equal(Ok(#( + dynamic.from(1), + dynamic.from(2), + dynamic.from(3), + dynamic.from(4), + ))) + + #(1, "", 3.0, 4) + |> dynamic.from + |> dynamic.tuple4 + |> should.equal(Ok(#( + dynamic.from(1), + dynamic.from(""), + dynamic.from(3.0), + dynamic.from(4), + ))) + + #(1, 2) + |> dynamic.from + |> dynamic.tuple4 + |> should.equal(Error("Expected a 4 element tuple, got a 2 element tuple")) + + 1 + |> dynamic.from + |> dynamic.tuple4 + |> should.equal(Error("Expected a 4 element tuple, got an int")) + } + + pub fn typed_tuple4_test() { + #(1, 2, 3, 4) + |> dynamic.from + |> dynamic.typed_tuple4(dynamic.int, dynamic.int, dynamic.int, dynamic.int) + |> should.equal(Ok(#(1, 2, 3, 4))) + + #(1, "", 3.0, 4) + |> dynamic.from + |> dynamic.typed_tuple4( + dynamic.int, + dynamic.string, + dynamic.float, + dynamic.int, + ) + |> should.equal(Ok(#(1, "", 3.0, 4))) + + #(1, 2, 3, "") + |> dynamic.from + |> dynamic.typed_tuple4(dynamic.int, dynamic.int, dynamic.int, dynamic.int) + |> should.equal(Error("Expected an int, got a binary")) + + #(1, 2) + |> dynamic.from + |> dynamic.typed_tuple4(dynamic.int, dynamic.int, dynamic.int, dynamic.int) + |> should.equal(Error("Expected a 4 element tuple, got a 2 element tuple")) + + 1 + |> dynamic.from + |> dynamic.typed_tuple4(dynamic.int, dynamic.int, dynamic.int, dynamic.int) + |> should.equal(Error("Expected a 4 element tuple, got an int")) + } + + pub fn tuple5_test() { + #(1, 2, 3, 4, 5) + |> dynamic.from + |> dynamic.tuple5 + |> should.equal(Ok(#( + dynamic.from(1), + dynamic.from(2), + dynamic.from(3), + dynamic.from(4), + dynamic.from(5), + ))) + + #(1, "", 3.0, 4, 5) + |> dynamic.from + |> dynamic.tuple5 + |> should.equal(Ok(#( + dynamic.from(1), + dynamic.from(""), + dynamic.from(3.0), + dynamic.from(4), + dynamic.from(5), + ))) + + #(1, 2) + |> dynamic.from + |> dynamic.tuple5 + |> should.equal(Error("Expected a 5 element tuple, got a 2 element tuple")) + + 1 + |> dynamic.from + |> dynamic.tuple5 + |> should.equal(Error("Expected a 5 element tuple, got an int")) + } + + pub fn typed_tuple5_test() { + #(1, 2, 3, 4, 5) + |> dynamic.from + |> dynamic.typed_tuple5( + dynamic.int, + dynamic.int, + dynamic.int, + dynamic.int, + dynamic.int, + ) + |> should.equal(Ok(#(1, 2, 3, 4, 5))) + + #(1, "", 3.0, 4, 5) + |> dynamic.from + |> dynamic.typed_tuple5( + dynamic.int, + dynamic.string, + dynamic.float, + dynamic.int, + dynamic.int, + ) + |> should.equal(Ok(#(1, "", 3.0, 4, 5))) + + #(1, 2, 3, 4, "") + |> dynamic.from + |> dynamic.typed_tuple5( + dynamic.int, + dynamic.int, + dynamic.int, + dynamic.int, + dynamic.int, + ) + |> should.equal(Error("Expected an int, got a binary")) + + #(1, 2) + |> dynamic.from + |> dynamic.typed_tuple5( + dynamic.int, + dynamic.int, + dynamic.int, + dynamic.int, + dynamic.int, + ) + |> should.equal(Error("Expected a 5 element tuple, got a 2 element tuple")) + + 1 + |> dynamic.from + |> dynamic.typed_tuple5( + dynamic.int, + dynamic.int, + dynamic.int, + dynamic.int, + dynamic.int, + ) + |> should.equal(Error("Expected a 5 element tuple, got an int")) + } + + pub fn tuple6_test() { + #(1, 2, 3, 4, 5, 6) + |> dynamic.from + |> dynamic.tuple6 + |> should.equal(Ok(#( + dynamic.from(1), + dynamic.from(2), + dynamic.from(3), + dynamic.from(4), + dynamic.from(5), + dynamic.from(6), + ))) + + #(1, "", 3.0, 4, 5, 6) + |> dynamic.from + |> dynamic.tuple6 + |> should.equal(Ok(#( + dynamic.from(1), + dynamic.from(""), + dynamic.from(3.0), + dynamic.from(4), + dynamic.from(5), + dynamic.from(6), + ))) + + #(1, 2) + |> dynamic.from + |> dynamic.tuple6 + |> should.equal(Error("Expected a 6 element tuple, got a 2 element tuple")) + + 1 + |> dynamic.from + |> dynamic.tuple6 + |> should.equal(Error("Expected a 6 element tuple, got an int")) + } + + pub fn typed_tuple6_test() { + #(1, 2, 3, 4, 5, 6) + |> dynamic.from + |> dynamic.typed_tuple6( + dynamic.int, + dynamic.int, + dynamic.int, + dynamic.int, + dynamic.int, + dynamic.int, + ) + |> should.equal(Ok(#(1, 2, 3, 4, 5, 6))) + + #(1, "", 3.0, 4, 5, 6) + |> dynamic.from + |> dynamic.typed_tuple6( + dynamic.int, + dynamic.string, + dynamic.float, + dynamic.int, + dynamic.int, + dynamic.int, + ) + |> should.equal(Ok(#(1, "", 3.0, 4, 5, 6))) + + #(1, 2, 3, 4, 5, "") + |> dynamic.from + |> dynamic.typed_tuple6( + dynamic.int, + dynamic.int, + dynamic.int, + dynamic.int, + dynamic.int, + dynamic.int, + ) + |> should.equal(Error("Expected an int, got a binary")) + + #(1, 2) + |> dynamic.from + |> dynamic.typed_tuple6( + dynamic.int, + dynamic.int, + dynamic.int, + dynamic.int, + dynamic.int, + dynamic.int, + ) + |> should.equal(Error("Expected a 6 element tuple, got a 2 element tuple")) + + 1 + |> dynamic.from + |> dynamic.typed_tuple6( + dynamic.int, + dynamic.int, + dynamic.int, + dynamic.int, + dynamic.int, + dynamic.int, + ) + |> should.equal(Error("Expected a 6 element tuple, got an int")) + } + + pub fn map_test() { + map.new() + |> dynamic.from + |> dynamic.map + |> should.equal(Ok(map.new())) + + 1 + |> dynamic.from + |> dynamic.map + |> should.equal(Error("Expected a map, got an int")) + } + + pub fn list_test() { + [] + |> dynamic.from + |> dynamic.list + |> should.equal(Ok([])) + + [1, 2] + |> dynamic.from + |> dynamic.list + |> should.equal(Ok([dynamic.from(1), dynamic.from(2)])) + + [dynamic.from(1), dynamic.from(2.0)] + |> dynamic.from + |> dynamic.list + |> should.equal(Ok([dynamic.from(1), dynamic.from(2.0)])) + + 1 + |> dynamic.from + |> dynamic.list + |> should.equal(Error("Expected a list, got an int")) + } + + pub fn result_test() { + Ok(1) + |> dynamic.from + |> dynamic.result + |> should.equal(Ok(Ok(dynamic.from(1)))) + + Error("error") + |> dynamic.from + |> dynamic.result + |> should.equal(Ok(Error(dynamic.from("error")))) + + 1 + |> dynamic.from + |> dynamic.result + |> should.equal(Error("Expected a 2 element tuple, got an int")) + + let tag = atom.create_from_string("bad") + + #(tag, "value") + |> dynamic.from + |> dynamic.result + |> should.equal(Error("Expected a tag of \"ok\" or \"error\", got \"bad\"")) + } + + pub fn typed_result_test() { + Ok(1) + |> dynamic.from + |> dynamic.typed_result(ok: dynamic.int, error: dynamic.string) + |> should.equal(Ok(Ok(1))) + + Error("error") + |> dynamic.from + |> dynamic.typed_result(ok: dynamic.int, error: dynamic.string) + |> should.equal(Ok(Error("error"))) + + Ok("1") + |> dynamic.from + |> dynamic.typed_result(ok: dynamic.int, error: dynamic.string) + |> should.equal(Error("Expected an int, got a binary")) + + Error(1) + |> dynamic.from + |> dynamic.typed_result(ok: dynamic.int, error: dynamic.string) + |> should.equal(Error("Expected a bit_string, got an int")) + + 1 + |> dynamic.from + |> dynamic.typed_result(ok: dynamic.int, error: dynamic.string) + |> should.equal(Error("Expected a 2 element tuple, got an int")) + } } diff --git a/test/gleam/float_test.gleam b/test/gleam/float_test.gleam index 3e0e6f6..79dba56 100644 --- a/test/gleam/float_test.gleam +++ b/test/gleam/float_test.gleam @@ -1,274 +1,276 @@ -import gleam/should -import gleam/float -import gleam/order - -pub fn parse_test() { - "1.23" - |> float.parse - |> should.equal(Ok(1.23)) - - "5.0" - |> float.parse - |> should.equal(Ok(5.0)) - - "0.123456789" - |> float.parse - |> should.equal(Ok(0.123456789)) - - "" - |> float.parse - |> should.equal(Error(Nil)) - - "what" - |> float.parse - |> should.equal(Error(Nil)) - - "1" - |> float.parse - |> should.equal(Error(Nil)) -} +if erlang { + import gleam/should + import gleam/float + import gleam/order + + pub fn parse_test() { + "1.23" + |> float.parse + |> should.equal(Ok(1.23)) + + "5.0" + |> float.parse + |> should.equal(Ok(5.0)) + + "0.123456789" + |> float.parse + |> should.equal(Ok(0.123456789)) + + "" + |> float.parse + |> should.equal(Error(Nil)) + + "what" + |> float.parse + |> should.equal(Error(Nil)) + + "1" + |> float.parse + |> should.equal(Error(Nil)) + } + + pub fn to_string_test() { + 123.0 + |> float.to_string + |> should.equal("123.0") + + -8.1 + |> float.to_string + |> should.equal("-8.1") + } + + pub fn clamp_test() { + float.clamp(1.4, min: 1.3, max: 1.5) + |> should.equal(1.4) + + float.clamp(1.2, min: 1.3, max: 1.5) + |> should.equal(1.3) + + float.clamp(1.6, min: 1.3, max: 1.5) + |> should.equal(1.5) + } + + pub fn compare_test() { + float.compare(0., 0.) + |> should.equal(order.Eq) + + float.compare(0.1, 0.1) + |> should.equal(order.Eq) + + float.compare(0., 0.1) + |> should.equal(order.Lt) + + float.compare(-2., -1.9) + |> should.equal(order.Lt) + + float.compare(2., 1.9) + |> should.equal(order.Gt) + + float.compare(-1.9, -2.) + |> should.equal(order.Gt) + } + + pub fn ceiling_test() { + 8.1 + |> float.ceiling + |> should.equal(9.0) + + -8.1 + |> float.ceiling + |> should.equal(-8.0) + + -8.0 + |> float.ceiling + |> should.equal(-8.0) + } -pub fn to_string_test() { - 123.0 - |> float.to_string - |> should.equal("123.0") + pub fn floor_test() { + 8.1 + |> float.floor + |> should.equal(8.0) - -8.1 - |> float.to_string - |> should.equal("-8.1") -} + -8.1 + |> float.floor + |> should.equal(-9.0) -pub fn clamp_test() { - float.clamp(1.4, min: 1.3, max: 1.5) - |> should.equal(1.4) + -8.0 + |> float.floor + |> should.equal(-8.0) + } - float.clamp(1.2, min: 1.3, max: 1.5) - |> should.equal(1.3) + pub fn round_test() { + 8.1 + |> float.round + |> should.equal(8) - float.clamp(1.6, min: 1.3, max: 1.5) - |> should.equal(1.5) -} + 8.4 + |> float.round + |> should.equal(8) -pub fn compare_test() { - float.compare(0., 0.) - |> should.equal(order.Eq) + 8.499 + |> float.round + |> should.equal(8) - float.compare(0.1, 0.1) - |> should.equal(order.Eq) + 8.5 + |> float.round + |> should.equal(9) - float.compare(0., 0.1) - |> should.equal(order.Lt) + -8.1 + |> float.round + |> should.equal(-8) - float.compare(-2., -1.9) - |> should.equal(order.Lt) + -7.5 + |> float.round + |> should.equal(-8) + } - float.compare(2., 1.9) - |> should.equal(order.Gt) + pub fn truncate_test() { + 8.1 + |> float.truncate + |> should.equal(8) - float.compare(-1.9, -2.) - |> should.equal(order.Gt) -} + 8.4 + |> float.truncate + |> should.equal(8) -pub fn ceiling_test() { - 8.1 - |> float.ceiling - |> should.equal(9.0) + 8.499 + |> float.truncate + |> should.equal(8) - -8.1 - |> float.ceiling - |> should.equal(-8.0) + 8.5 + |> float.truncate + |> should.equal(8) - -8.0 - |> float.ceiling - |> should.equal(-8.0) -} + -8.1 + |> float.truncate + |> should.equal(-8) -pub fn floor_test() { - 8.1 - |> float.floor - |> should.equal(8.0) + -7.5 + |> float.truncate + |> should.equal(-7) + } - -8.1 - |> float.floor - |> should.equal(-9.0) + pub fn min_test() { + float.min(0., 0.) + |> should.equal(0.) - -8.0 - |> float.floor - |> should.equal(-8.0) -} + float.min(0.3, 1.5) + |> should.equal(0.3) -pub fn round_test() { - 8.1 - |> float.round - |> should.equal(8) + float.min(1., 0.) + |> should.equal(0.) - 8.4 - |> float.round - |> should.equal(8) + float.min(-1.7, 2.5) + |> should.equal(-1.7) - 8.499 - |> float.round - |> should.equal(8) + float.min(-2.2, -2.2) + |> should.equal(-2.2) - 8.5 - |> float.round - |> should.equal(9) + float.min(-1., -1.) + |> should.equal(-1.) - -8.1 - |> float.round - |> should.equal(-8) + float.min(-1.1, -1.) + |> should.equal(-1.1) + } - -7.5 - |> float.round - |> should.equal(-8) -} + pub fn max_test() { + float.max(0., 0.) + |> should.equal(0.) -pub fn truncate_test() { - 8.1 - |> float.truncate - |> should.equal(8) + float.max(0.3, 1.5) + |> should.equal(1.5) - 8.4 - |> float.truncate - |> should.equal(8) + float.max(1., 0.) + |> should.equal(1.) - 8.499 - |> float.truncate - |> should.equal(8) + float.max(-1.7, 2.5) + |> should.equal(2.5) - 8.5 - |> float.truncate - |> should.equal(8) + float.max(-2.2, -2.2) + |> should.equal(-2.2) - -8.1 - |> float.truncate - |> should.equal(-8) + float.max(-1., -1.) + |> should.equal(-1.) - -7.5 - |> float.truncate - |> should.equal(-7) -} + float.max(-1.1, -1.) + |> should.equal(-1.) + } -pub fn min_test() { - float.min(0., 0.) - |> should.equal(0.) + pub fn absolute_value_test() { + float.absolute_value(-1.0) + |> should.equal(1.0) - float.min(0.3, 1.5) - |> should.equal(0.3) + float.absolute_value(-20.6) + |> should.equal(20.6) - float.min(1., 0.) - |> should.equal(0.) + float.absolute_value(0.0) + |> should.equal(0.0) - float.min(-1.7, 2.5) - |> should.equal(-1.7) + float.absolute_value(1.0) + |> should.equal(1.0) - float.min(-2.2, -2.2) - |> should.equal(-2.2) + float.absolute_value(25.2) + |> should.equal(25.2) + } - float.min(-1., -1.) - |> should.equal(-1.) + pub fn power_test() { + float.power(2.0, 2.0) + |> should.equal(4.0) - float.min(-1.1, -1.) - |> should.equal(-1.1) -} - -pub fn max_test() { - float.max(0., 0.) - |> should.equal(0.) + float.power(-5.0, 3.0) + |> should.equal(-125.0) - float.max(0.3, 1.5) - |> should.equal(1.5) + float.power(10.5, 0.0) + |> should.equal(1.0) - float.max(1., 0.) - |> should.equal(1.) + float.power(16.0, 0.5) + |> should.equal(4.0) - float.max(-1.7, 2.5) - |> should.equal(2.5) + float.power(2.0, -1.0) + |> should.equal(0.5) + } - float.max(-2.2, -2.2) - |> should.equal(-2.2) + pub fn square_root_test() { + float.square_root(4.0) + |> should.equal(Ok(2.0)) - float.max(-1., -1.) - |> should.equal(-1.) - - float.max(-1.1, -1.) - |> should.equal(-1.) -} + float.square_root(16.0) + |> should.equal(Ok(4.0)) -pub fn absolute_value_test() { - float.absolute_value(-1.0) - |> should.equal(1.0) + float.square_root(0.0) + |> should.equal(Ok(0.0)) - float.absolute_value(-20.6) - |> should.equal(20.6) + float.square_root(-4.0) + |> should.equal(Error(Nil)) + } - float.absolute_value(0.0) - |> should.equal(0.0) - - float.absolute_value(1.0) - |> should.equal(1.0) - - float.absolute_value(25.2) - |> should.equal(25.2) -} + pub fn negate_test() { + float.negate(-1.) + |> should.equal(1.) -pub fn power_test() { - float.power(2.0, 2.0) - |> should.equal(4.0) + float.negate(2.) + |> should.equal(-2.) - float.power(-5.0, 3.0) - |> should.equal(-125.0) + float.negate(0.) + |> should.equal(0.) + } - float.power(10.5, 0.0) - |> should.equal(1.0) + pub fn sum_test() { + float.sum([]) + |> should.equal(0.0) - float.power(16.0, 0.5) - |> should.equal(4.0) - - float.power(2.0, -1.0) - |> should.equal(0.5) -} - -pub fn square_root_test() { - float.square_root(4.0) - |> should.equal(Ok(2.0)) - - float.square_root(16.0) - |> should.equal(Ok(4.0)) - - float.square_root(0.0) - |> should.equal(Ok(0.0)) - - float.square_root(-4.0) - |> should.equal(Error(Nil)) -} - -pub fn negate_test() { - float.negate(-1.) - |> should.equal(1.) - - float.negate(2.) - |> should.equal(-2.) - - float.negate(0.) - |> should.equal(0.) -} - -pub fn sum_test() { - float.sum([]) - |> should.equal(0.0) - - float.sum([1.0, 2.2, 3.3]) - |> should.equal(6.5) -} + float.sum([1.0, 2.2, 3.3]) + |> should.equal(6.5) + } -pub fn product_test() { - float.product([]) - |> should.equal(0.) + pub fn product_test() { + float.product([]) + |> should.equal(0.) - float.product([4.]) - |> should.equal(4.) + float.product([4.]) + |> should.equal(4.) - float.product([2.5, 3.2, 4.2]) - |> should.equal(33.6) + float.product([2.5, 3.2, 4.2]) + |> should.equal(33.6) + } } diff --git a/test/gleam/function_test.gleam b/test/gleam/function_test.gleam index 721d10d..ec65f63 100644 --- a/test/gleam/function_test.gleam +++ b/test/gleam/function_test.gleam @@ -1,126 +1,128 @@ -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") -} +if erlang { + 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) + pub fn curry2_test() { + let fun = fn(a, b) { a + b } + let curried = function.curry2(fun) - curried(1)(2) - |> should.equal(3) -} + curried(1)(2) + |> should.equal(3) + } -pub fn curry3_test() { - let fun = fn(a, b, c) { a + b + c } - let curried = function.curry3(fun) + pub fn curry3_test() { + let fun = fn(a, b, c) { a + b + c } + let curried = function.curry3(fun) - curried(1)(2)(4) - |> should.equal(7) -} - -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'") -pub fn identity_test() { - 1 - |> function.identity - |> should.equal(1) + flipped_fun(2, "Alice") + |> should.equal("String: 'Alice', Int: '2'") + } - "" - |> function.identity - |> should.equal("") + pub fn identity_test() { + 1 + |> function.identity + |> should.equal(1) - [] - |> function.identity - |> should.equal([]) + "" + |> function.identity + |> should.equal("") - #(1, 2.0) - |> function.identity - |> should.equal(#(1, 2.0)) -} + [] + |> function.identity + |> should.equal([]) + + #(1, 2.0) + |> function.identity + |> should.equal(#(1, 2.0)) + } -external fn throw(a) -> Nil = - "erlang" "throw" + external fn throw(a) -> Nil = + "erlang" "throw" -external fn raise_error(a) -> Nil = - "erlang" "error" + external fn raise_error(a) -> Nil = + "erlang" "error" -pub fn rescue_test() { - function.rescue(fn() { 1 }) - |> should.equal(Ok(1)) + pub fn rescue_test() { + function.rescue(fn() { 1 }) + |> should.equal(Ok(1)) - function.rescue(fn() { throw(1) }) - |> should.equal(Error(function.Thrown(dynamic.from(1)))) + function.rescue(fn() { throw(1) }) + |> should.equal(Error(function.Thrown(dynamic.from(1)))) - function.rescue(fn() { raise_error("") }) - |> should.equal(Error(function.Errored(dynamic.from("")))) + function.rescue(fn() { raise_error("") }) + |> should.equal(Error(function.Errored(dynamic.from("")))) + } } diff --git a/test/gleam/int_test.gleam b/test/gleam/int_test.gleam index bec9624..31d06de 100644 --- a/test/gleam/int_test.gleam +++ b/test/gleam/int_test.gleam @@ -1,228 +1,230 @@ -import gleam/should -import gleam/int -import gleam/order - -pub fn absolute_value_test() { - 123 - |> int.absolute_value - |> should.equal(123) - - -123 - |> int.absolute_value - |> should.equal(123) -} +if erlang { + import gleam/should + import gleam/int + import gleam/order + + pub fn absolute_value_test() { + 123 + |> int.absolute_value + |> should.equal(123) -pub fn clamp_test() { - int.clamp(40, min: 30, max: 50) - |> should.equal(40) + -123 + |> int.absolute_value + |> should.equal(123) + } - int.clamp(20, min: 30, max: 50) - |> should.equal(30) + pub fn clamp_test() { + int.clamp(40, min: 30, max: 50) + |> should.equal(40) - int.clamp(60, min: 30, max: 50) - |> should.equal(50) + int.clamp(20, min: 30, max: 50) + |> should.equal(30) - // If the bounds are reversed we return the min - int.clamp(100, min: 50, max: 30) - |> should.equal(50) -} + int.clamp(60, min: 30, max: 50) + |> should.equal(50) -pub fn to_string_test() { - 123 - |> int.to_string - |> should.equal("123") + // If the bounds are reversed we return the min + int.clamp(100, min: 50, max: 30) + |> should.equal(50) + } - -123 - |> int.to_string - |> should.equal("-123") + pub fn to_string_test() { + 123 + |> int.to_string + |> should.equal("123") - 123 - |> int.to_string - |> should.equal("123") -} + -123 + |> int.to_string + |> should.equal("-123") -pub fn parse_test() { - "123" - |> int.parse - |> should.equal(Ok(123)) + 123 + |> int.to_string + |> should.equal("123") + } - "-123" - |> int.parse - |> should.equal(Ok(-123)) + pub fn parse_test() { + "123" + |> int.parse + |> should.equal(Ok(123)) - "0123" - |> int.parse - |> should.equal(Ok(123)) + "-123" + |> int.parse + |> should.equal(Ok(-123)) - "" - |> int.parse - |> should.equal(Error(Nil)) + "0123" + |> int.parse + |> should.equal(Ok(123)) - "what" - |> int.parse - |> should.equal(Error(Nil)) + "" + |> int.parse + |> should.equal(Error(Nil)) - "1.23" - |> int.parse - |> should.equal(Error(Nil)) -} + "what" + |> int.parse + |> should.equal(Error(Nil)) -pub fn to_base_string_test() { - 100 - |> int.to_base_string(16) - |> should.equal("64") + "1.23" + |> int.parse + |> should.equal(Error(Nil)) + } - -100 - |> int.to_base_string(16) - |> should.equal("-64") -} + pub fn to_base_string_test() { + 100 + |> int.to_base_string(16) + |> should.equal("64") -pub fn to_float_test() { - int.to_float(1) - |> should.equal(1.) + -100 + |> int.to_base_string(16) + |> should.equal("-64") + } - int.to_float(5) - |> should.equal(5.) + pub fn to_float_test() { + int.to_float(1) + |> should.equal(1.) - int.to_float(0) - |> should.equal(0.) + int.to_float(5) + |> should.equal(5.) - int.to_float(-5) - |> should.equal(-5.) -} + int.to_float(0) + |> should.equal(0.) -pub fn compare_test() { - int.compare(0, 0) - |> should.equal(order.Eq) + int.to_float(-5) + |> should.equal(-5.) + } - int.compare(1, 1) - |> should.equal(order.Eq) + pub fn compare_test() { + int.compare(0, 0) + |> should.equal(order.Eq) - int.compare(0, 1) - |> should.equal(order.Lt) + int.compare(1, 1) + |> should.equal(order.Eq) - int.compare(-2, -1) - |> should.equal(order.Lt) + int.compare(0, 1) + |> should.equal(order.Lt) - int.compare(2, 1) - |> should.equal(order.Gt) + int.compare(-2, -1) + |> should.equal(order.Lt) - int.compare(-1, -2) - |> should.equal(order.Gt) -} + int.compare(2, 1) + |> should.equal(order.Gt) -pub fn min_test() { - int.min(0, 0) - |> should.equal(0) + int.compare(-1, -2) + |> should.equal(order.Gt) + } - int.min(0, 1) - |> should.equal(0) + pub fn min_test() { + int.min(0, 0) + |> should.equal(0) - int.min(1, 0) - |> should.equal(0) + int.min(0, 1) + |> should.equal(0) - int.min(-1, 2) - |> should.equal(-1) + int.min(1, 0) + |> should.equal(0) - int.min(2, -2) - |> should.equal(-2) + int.min(-1, 2) + |> should.equal(-1) - int.min(-1, -1) - |> should.equal(-1) -} + int.min(2, -2) + |> should.equal(-2) -pub fn max_test() { - int.max(0, 0) - |> should.equal(0) + int.min(-1, -1) + |> should.equal(-1) + } - int.max(0, 1) - |> should.equal(1) + pub fn max_test() { + int.max(0, 0) + |> should.equal(0) - int.max(1, 0) - |> should.equal(1) + int.max(0, 1) + |> should.equal(1) - int.max(-1, 2) - |> should.equal(2) + int.max(1, 0) + |> should.equal(1) - int.max(2, -2) - |> should.equal(2) + int.max(-1, 2) + |> should.equal(2) - int.max(-1, -1) - |> should.equal(-1) -} + int.max(2, -2) + |> should.equal(2) -pub fn is_even_test() { - int.is_even(0) - |> should.be_true + int.max(-1, -1) + |> should.equal(-1) + } - int.is_even(2) - |> should.be_true + pub fn is_even_test() { + int.is_even(0) + |> should.be_true - int.is_even(-2) - |> should.be_true + int.is_even(2) + |> should.be_true - int.is_even(10006) - |> should.be_true + int.is_even(-2) + |> should.be_true - int.is_even(1) - |> should.be_false + int.is_even(10006) + |> should.be_true - int.is_even(-3) - |> should.be_false + int.is_even(1) + |> should.be_false - int.is_even(10005) - |> should.be_false -} + int.is_even(-3) + |> should.be_false -pub fn is_odd_test() { - int.is_odd(0) - |> should.be_false + int.is_even(10005) + |> should.be_false + } - int.is_odd(2) - |> should.be_false + pub fn is_odd_test() { + int.is_odd(0) + |> should.be_false - int.is_odd(-2) - |> should.be_false + int.is_odd(2) + |> should.be_false - int.is_odd(10006) - |> should.be_false + int.is_odd(-2) + |> should.be_false - int.is_odd(1) - |> should.be_true + int.is_odd(10006) + |> should.be_false - int.is_odd(-3) - |> should.be_true + int.is_odd(1) + |> should.be_true - int.is_odd(10005) - |> should.be_true -} + int.is_odd(-3) + |> should.be_true -pub fn negate_test() { - int.negate(-1) - |> should.equal(1) + int.is_odd(10005) + |> should.be_true + } - int.negate(2) - |> should.equal(-2) + pub fn negate_test() { + int.negate(-1) + |> should.equal(1) - int.negate(0) - |> should.equal(0) -} + int.negate(2) + |> should.equal(-2) -pub fn sum_test() { - int.sum([]) - |> should.equal(0) + int.negate(0) + |> should.equal(0) + } - int.sum([1, 2, 3]) - |> should.equal(6) -} + pub fn sum_test() { + int.sum([]) + |> should.equal(0) + + int.sum([1, 2, 3]) + |> should.equal(6) + } -pub fn product_test() { - int.product([]) - |> should.equal(0) + pub fn product_test() { + int.product([]) + |> should.equal(0) - int.product([4]) - |> should.equal(4) + int.product([4]) + |> should.equal(4) - int.product([1, 2, 3]) - |> should.equal(6) + int.product([1, 2, 3]) + |> should.equal(6) + } } diff --git a/test/gleam/iterator_test.gleam b/test/gleam/iterator_test.gleam index 0fb7143..0c0176f 100644 --- a/test/gleam/iterator_test.gleam +++ b/test/gleam/iterator_test.gleam @@ -1,421 +1,423 @@ -import gleam/should -import gleam/iterator.{Done, Next} -import gleam/list -import gleam/map - -// a |> from_list |> to_list == a -pub fn to_from_list_test() { - let test = fn(subject) { - subject - |> iterator.from_list - |> iterator.to_list - |> should.equal(subject) +if erlang { + import gleam/should + import gleam/iterator.{Done, Next} + import gleam/list + import gleam/map + + // a |> from_list |> to_list == a + pub fn to_from_list_test() { + let test = fn(subject) { + subject + |> iterator.from_list + |> iterator.to_list + |> should.equal(subject) + } + + test([]) + test([1]) + test([1, 2]) + test([1, 2, 4, 8]) } - test([]) - test([1]) - test([1, 2]) - test([1, 2, 4, 8]) -} + pub fn step_test() { + let test = fn(subject) { + let step = + subject + |> iterator.from_list + |> iterator.step + + case subject { + [] -> + step + |> should.equal(Done) + + [h, ..t] -> + step + |> should.equal(Next(h, iterator.from_list(t))) + } + } -pub fn step_test() { - let test = fn(subject) { - let step = + test([]) + test([1]) + test([1, 2]) + test([1, 2, 3]) + } + + // a |> from_list |> take(n) == a |> list.take(_, n) + pub fn take_test() { + let test = fn(n, subject) { subject |> iterator.from_list - |> iterator.step + |> iterator.take(n) + |> iterator.to_list + |> should.equal(list.take(subject, n)) + } - case subject { - [] -> - step - |> should.equal(Done) + test(0, []) + test(1, []) + test(-1, []) + test(0, [0]) + test(1, [0]) + test(-1, [0]) + test(0, [0, 1, 2, 3, 4]) + test(1, [0, 1, 2, 3, 4]) + test(2, [0, 1, 2, 3, 4]) + test(22, [0, 1, 2, 3, 4]) + } - [h, ..t] -> - step - |> should.equal(Next(h, iterator.from_list(t))) + // a |> from_list |> fold(a, f) == a |> list.fold(_, a, f) + pub fn fold_test() { + let test = fn(subject, acc, f) { + subject + |> iterator.from_list + |> iterator.fold(acc, f) + |> should.equal(list.fold(subject, acc, f)) } - } - test([]) - test([1]) - test([1, 2]) - test([1, 2, 3]) -} + let f = fn(e, acc) { [e, ..acc] } + test([], [], f) + test([1], [], f) + test([1, 2, 3], [], f) + test([1, 2, 3, 4, 5, 6, 7, 8], [], f) + } -// a |> from_list |> take(n) == a |> list.take(_, n) -pub fn take_test() { - let test = fn(n, subject) { - subject - |> iterator.from_list - |> iterator.take(n) - |> iterator.to_list - |> should.equal(list.take(subject, n)) - } - - test(0, []) - test(1, []) - test(-1, []) - test(0, [0]) - test(1, [0]) - test(-1, [0]) - test(0, [0, 1, 2, 3, 4]) - test(1, [0, 1, 2, 3, 4]) - test(2, [0, 1, 2, 3, 4]) - test(22, [0, 1, 2, 3, 4]) -} + // a |> from_list |> map(f) |> to_list == a |> list.map(_, f) + pub fn map_test() { + let test = fn(subject, f) { + subject + |> iterator.from_list + |> iterator.map(f) + |> iterator.to_list + |> should.equal(list.map(subject, f)) + } -// a |> from_list |> fold(a, f) == a |> list.fold(_, a, f) -pub fn fold_test() { - let test = fn(subject, acc, f) { - subject - |> iterator.from_list - |> iterator.fold(acc, f) - |> should.equal(list.fold(subject, acc, f)) + let f = fn(e) { e * 2 } + test([], f) + test([1], f) + test([1, 2, 3], f) + test([1, 2, 3, 4, 5, 6, 7, 8], f) } - let f = fn(e, acc) { [e, ..acc] } - test([], [], f) - test([1], [], f) - test([1, 2, 3], [], f) - test([1, 2, 3, 4, 5, 6, 7, 8], [], f) -} + // a |> from_list |> flat_map(f) |> to_list == + // a |> list.map(f) |> list.map(to_list) |> list.flatten + pub fn flat_map_test() { + let test = fn(subject, f) { + subject + |> iterator.from_list + |> iterator.flat_map(f) + |> iterator.to_list + |> should.equal( + subject + |> list.map(f) + |> list.map(iterator.to_list) + |> list.flatten, + ) + } -// a |> from_list |> map(f) |> to_list == a |> list.map(_, f) -pub fn map_test() { - let test = fn(subject, f) { - subject - |> iterator.from_list - |> iterator.map(f) - |> iterator.to_list - |> should.equal(list.map(subject, f)) + let f = fn(i) { iterator.range(i, i + 2) } + + test([], f) + test([1], f) + test([1, 2], f) } - let f = fn(e) { e * 2 } - test([], f) - test([1], f) - test([1, 2, 3], f) - test([1, 2, 3, 4, 5, 6, 7, 8], f) -} + // a |> from_list |> append(from_list(b)) |> to_list == list.flatten([a, b]) + pub fn append_test() { + let test = fn(left, right) { + left + |> iterator.from_list + |> iterator.append(iterator.from_list(right)) + |> iterator.to_list + |> should.equal(list.flatten([left, right])) + } -// a |> from_list |> flat_map(f) |> to_list == -// a |> list.map(f) |> list.map(to_list) |> list.flatten -pub fn flat_map_test() { - let test = fn(subject, f) { - subject - |> iterator.from_list - |> iterator.flat_map(f) - |> iterator.to_list - |> should.equal( - subject - |> list.map(f) - |> list.map(iterator.to_list) - |> list.flatten, - ) + test([], []) + test([1], [2]) + test([1, 2], [3, 4]) } - let f = fn(i) { iterator.range(i, i + 2) } - - test([], f) - test([1], f) - test([1, 2], f) -} + // a |> list.map(from_list) |> flatten |> to_list == list.flatten(a) + pub fn flatten_test() { + let test = fn(lists) { + lists + |> list.map(iterator.from_list) + |> iterator.from_list + |> iterator.flatten + |> iterator.to_list + |> should.equal(list.flatten(lists)) + } -// a |> from_list |> append(from_list(b)) |> to_list == list.flatten([a, b]) -pub fn append_test() { - let test = fn(left, right) { - left - |> iterator.from_list - |> iterator.append(iterator.from_list(right)) - |> iterator.to_list - |> should.equal(list.flatten([left, right])) + test([[], []]) + test([[1], [2]]) + test([[1, 2], [3, 4]]) } - test([], []) - test([1], [2]) - test([1, 2], [3, 4]) -} + // a |> from_list |> filter(f) |> to_list == a |> list.filter(_, f) + pub fn filter_test() { + let test = fn(subject, f) { + subject + |> iterator.from_list + |> iterator.filter(f) + |> iterator.to_list + |> should.equal(list.filter(subject, f)) + } -// a |> list.map(from_list) |> flatten |> to_list == list.flatten(a) -pub fn flatten_test() { - let test = fn(lists) { - lists - |> list.map(iterator.from_list) - |> iterator.from_list - |> iterator.flatten - |> iterator.to_list - |> should.equal(list.flatten(lists)) + let even = fn(x) { x % 2 == 0 } + test([], even) + test([1], even) + test([1, 2], even) + test([1, 2, 3], even) + test([1, 2, 3, 4], even) + test([1, 2, 3, 4, 5], even) + test([1, 2, 3, 4, 5, 6], even) } - test([[], []]) - test([[1], [2]]) - test([[1, 2], [3, 4]]) -} + pub fn repeat_test() { + 1 + |> iterator.repeat + |> iterator.take(5) + |> iterator.to_list + |> should.equal([1, 1, 1, 1, 1]) + } -// a |> from_list |> filter(f) |> to_list == a |> list.filter(_, f) -pub fn filter_test() { - let test = fn(subject, f) { - subject + pub fn cycle_test() { + [1, 2, 3] |> iterator.from_list - |> iterator.filter(f) + |> iterator.cycle + |> iterator.take(9) |> iterator.to_list - |> should.equal(list.filter(subject, f)) + |> should.equal([1, 2, 3, 1, 2, 3, 1, 2, 3]) } - let even = fn(x) { x % 2 == 0 } - test([], even) - test([1], even) - test([1, 2], even) - test([1, 2, 3], even) - test([1, 2, 3, 4], even) - test([1, 2, 3, 4, 5], even) - test([1, 2, 3, 4, 5, 6], even) -} + pub fn unfold_test() { + iterator.unfold(2, fn(acc) { iterator.Next(acc, acc * 2) }) + |> iterator.take(5) + |> iterator.to_list + |> should.equal([2, 4, 8, 16, 32]) -pub fn repeat_test() { - 1 - |> iterator.repeat - |> iterator.take(5) - |> iterator.to_list - |> should.equal([1, 1, 1, 1, 1]) -} + iterator.unfold(2, fn(_) { iterator.Done }) + |> iterator.take(5) + |> iterator.to_list + |> should.equal([]) -pub fn cycle_test() { - [1, 2, 3] - |> iterator.from_list - |> iterator.cycle - |> iterator.take(9) - |> iterator.to_list - |> should.equal([1, 2, 3, 1, 2, 3, 1, 2, 3]) -} + fn(n) { + case n { + 0 -> iterator.Done + n -> iterator.Next(element: n, accumulator: n - 1) + } + } + |> iterator.unfold(from: 5) + |> iterator.to_list + |> should.equal([5, 4, 3, 2, 1]) + } -pub fn unfold_test() { - iterator.unfold(2, fn(acc) { iterator.Next(acc, acc * 2) }) - |> iterator.take(5) - |> iterator.to_list - |> should.equal([2, 4, 8, 16, 32]) - - iterator.unfold(2, fn(_) { iterator.Done }) - |> iterator.take(5) - |> iterator.to_list - |> should.equal([]) - - fn(n) { - case n { - 0 -> iterator.Done - n -> iterator.Next(element: n, accumulator: n - 1) + pub fn range_test() { + let test = fn(a, b, expected) { + iterator.range(a, b) + |> iterator.to_list + |> should.equal(expected) } + + test(0, 0, []) + test(1, 1, []) + test(-1, -1, []) + test(0, 1, [0]) + test(0, 5, [0, 1, 2, 3, 4]) + test(1, -5, [1, 0, -1, -2, -3, -4]) } - |> iterator.unfold(from: 5) - |> iterator.to_list - |> should.equal([5, 4, 3, 2, 1]) -} -pub fn range_test() { - let test = fn(a, b, expected) { - iterator.range(a, b) + pub fn drop_test() { + iterator.range(0, 10) + |> iterator.drop(5) |> iterator.to_list - |> should.equal(expected) + |> should.equal([5, 6, 7, 8, 9]) } - test(0, 0, []) - test(1, 1, []) - test(-1, -1, []) - test(0, 1, [0]) - test(0, 5, [0, 1, 2, 3, 4]) - test(1, -5, [1, 0, -1, -2, -3, -4]) -} + type Cat { + Cat(id: Int) + } -pub fn drop_test() { - iterator.range(0, 10) - |> iterator.drop(5) - |> iterator.to_list - |> should.equal([5, 6, 7, 8, 9]) -} + pub fn find_test() { + iterator.range(0, 10) + |> iterator.find(fn(e) { e == 5 }) + |> should.equal(Ok(5)) -type Cat { - Cat(id: Int) -} + iterator.range(0, 10) + |> iterator.find(fn(e) { e > 10 }) + |> should.equal(Error(Nil)) -pub fn find_test() { - iterator.range(0, 10) - |> iterator.find(fn(e) { e == 5 }) - |> should.equal(Ok(5)) - - iterator.range(0, 10) - |> iterator.find(fn(e) { e > 10 }) - |> should.equal(Error(Nil)) - - iterator.empty() - |> iterator.find(fn(_x) { True }) - |> should.equal(Error(Nil)) - - iterator.unfold( - Cat(id: 1), - fn(cat: Cat) { iterator.Next(cat, Cat(id: cat.id + 1)) }, - ) - |> iterator.find(fn(cat: Cat) { cat.id == 10 }) - |> should.equal(Ok(Cat(id: 10))) -} + iterator.empty() + |> iterator.find(fn(_x) { True }) + |> should.equal(Error(Nil)) -pub fn index_test() { - iterator.from_list(["a", "b", "c"]) - |> iterator.index - |> iterator.to_list - |> should.equal([#(0, "a"), #(1, "b"), #(2, "c")]) -} + iterator.unfold( + Cat(id: 1), + fn(cat: Cat) { iterator.Next(cat, Cat(id: cat.id + 1)) }, + ) + |> iterator.find(fn(cat: Cat) { cat.id == 10 }) + |> should.equal(Ok(Cat(id: 10))) + } -pub fn iterate_test() { - fn(x) { x * 3 } - |> iterator.iterate(from: 1) - |> iterator.take(5) - |> iterator.to_list - |> should.equal([1, 3, 9, 27, 81]) -} + pub fn index_test() { + iterator.from_list(["a", "b", "c"]) + |> iterator.index + |> iterator.to_list + |> should.equal([#(0, "a"), #(1, "b"), #(2, "c")]) + } -pub fn take_while_test() { - iterator.from_list([1, 2, 3, 2, 4]) - |> iterator.take_while(satisfying: fn(x) { x < 3 }) - |> iterator.to_list - |> should.equal([1, 2]) -} + pub fn iterate_test() { + fn(x) { x * 3 } + |> iterator.iterate(from: 1) + |> iterator.take(5) + |> iterator.to_list + |> should.equal([1, 3, 9, 27, 81]) + } -pub fn drop_while_test() { - iterator.from_list([1, 2, 3, 4, 2, 5]) - |> iterator.drop_while(satisfying: fn(x) { x < 4 }) - |> iterator.to_list - |> should.equal([4, 2, 5]) -} + pub fn take_while_test() { + iterator.from_list([1, 2, 3, 2, 4]) + |> iterator.take_while(satisfying: fn(x) { x < 3 }) + |> iterator.to_list + |> should.equal([1, 2]) + } -pub fn scan_test() { - iterator.from_list([1, 2, 3, 4, 5]) - |> iterator.scan(from: 0, with: fn(el, acc) { acc + el }) - |> iterator.to_list - |> should.equal([1, 3, 6, 10, 15]) -} + pub fn drop_while_test() { + iterator.from_list([1, 2, 3, 4, 2, 5]) + |> iterator.drop_while(satisfying: fn(x) { x < 4 }) + |> iterator.to_list + |> should.equal([4, 2, 5]) + } -pub fn zip_test() { - iterator.from_list(["a", "b", "c"]) - |> iterator.zip(iterator.range(20, 30)) - |> iterator.to_list - |> should.equal([#("a", 20), #("b", 21), #("c", 22)]) -} + pub fn scan_test() { + iterator.from_list([1, 2, 3, 4, 5]) + |> iterator.scan(from: 0, with: fn(el, acc) { acc + el }) + |> iterator.to_list + |> should.equal([1, 3, 6, 10, 15]) + } -pub fn chunk_test() { - iterator.from_list([1, 2, 2, 3, 4, 4, 6, 7, 7]) - |> iterator.chunk(by: fn(n) { n % 2 }) - |> iterator.to_list - |> should.equal([[1], [2, 2], [3], [4, 4, 6], [7, 7]]) -} + pub fn zip_test() { + iterator.from_list(["a", "b", "c"]) + |> iterator.zip(iterator.range(20, 30)) + |> iterator.to_list + |> should.equal([#("a", 20), #("b", 21), #("c", 22)]) + } -pub fn sized_chunk_test() { - iterator.from_list([1, 2, 3, 4, 5, 6]) - |> iterator.sized_chunk(into: 2) - |> iterator.to_list - |> should.equal([[1, 2], [3, 4], [5, 6]]) + pub fn chunk_test() { + iterator.from_list([1, 2, 2, 3, 4, 4, 6, 7, 7]) + |> iterator.chunk(by: fn(n) { n % 2 }) + |> iterator.to_list + |> should.equal([[1], [2, 2], [3], [4, 4, 6], [7, 7]]) + } - iterator.from_list([1, 2, 3, 4, 5, 6, 7, 8]) - |> iterator.sized_chunk(into: 3) - |> iterator.to_list - |> should.equal([[1, 2, 3], [4, 5, 6], [7, 8]]) -} + pub fn sized_chunk_test() { + iterator.from_list([1, 2, 3, 4, 5, 6]) + |> iterator.sized_chunk(into: 2) + |> iterator.to_list + |> should.equal([[1, 2], [3, 4], [5, 6]]) -pub fn intersperse_test() { - iterator.empty() - |> iterator.intersperse(with: 0) - |> iterator.to_list - |> should.equal([]) - - iterator.from_list([1]) - |> iterator.intersperse(with: 0) - |> iterator.to_list - |> should.equal([1]) - - iterator.from_list([1, 2, 3, 4, 5]) - |> iterator.intersperse(with: 0) - |> iterator.to_list - |> should.equal([1, 0, 2, 0, 3, 0, 4, 0, 5]) -} + iterator.from_list([1, 2, 3, 4, 5, 6, 7, 8]) + |> iterator.sized_chunk(into: 3) + |> iterator.to_list + |> should.equal([[1, 2, 3], [4, 5, 6], [7, 8]]) + } -pub fn any_test() { - iterator.empty() - |> iterator.any(satisfying: fn(n) { n % 2 == 0 }) - |> should.be_false + pub fn intersperse_test() { + iterator.empty() + |> iterator.intersperse(with: 0) + |> iterator.to_list + |> should.equal([]) - iterator.from_list([1, 2, 5, 7, 9]) - |> iterator.any(satisfying: fn(n) { n % 2 == 0 }) - |> should.be_true + iterator.from_list([1]) + |> iterator.intersperse(with: 0) + |> iterator.to_list + |> should.equal([1]) - iterator.from_list([1, 3, 5, 7, 9]) - |> iterator.any(satisfying: fn(n) { n % 2 == 0 }) - |> should.be_false -} + iterator.from_list([1, 2, 3, 4, 5]) + |> iterator.intersperse(with: 0) + |> iterator.to_list + |> should.equal([1, 0, 2, 0, 3, 0, 4, 0, 5]) + } -pub fn all_test() { - iterator.empty() - |> iterator.all(satisfying: fn(n) { n % 2 == 0 }) - |> should.be_true + pub fn any_test() { + iterator.empty() + |> iterator.any(satisfying: fn(n) { n % 2 == 0 }) + |> should.be_false - iterator.from_list([2, 4, 6, 8]) - |> iterator.all(satisfying: fn(n) { n % 2 == 0 }) - |> should.be_true + iterator.from_list([1, 2, 5, 7, 9]) + |> iterator.any(satisfying: fn(n) { n % 2 == 0 }) + |> should.be_true - iterator.from_list([2, 4, 5, 8]) - |> iterator.all(satisfying: fn(n) { n % 2 == 0 }) - |> should.be_false -} + iterator.from_list([1, 3, 5, 7, 9]) + |> iterator.any(satisfying: fn(n) { n % 2 == 0 }) + |> should.be_false + } -pub fn group_test() { - iterator.from_list([1, 2, 3, 4, 5, 6]) - |> iterator.group(by: fn(n) { n % 3 }) - |> should.equal(map.from_list([#(0, [3, 6]), #(1, [1, 4]), #(2, [2, 5])])) -} + pub fn all_test() { + iterator.empty() + |> iterator.all(satisfying: fn(n) { n % 2 == 0 }) + |> should.be_true -pub fn reduce_test() { - iterator.empty() - |> iterator.reduce(with: fn(x, y) { x + y }) - |> should.equal(Error(Nil)) + iterator.from_list([2, 4, 6, 8]) + |> iterator.all(satisfying: fn(n) { n % 2 == 0 }) + |> should.be_true - iterator.from_list([1, 2, 3, 4, 5]) - |> iterator.reduce(with: fn(x, y) { x + y }) - |> should.equal(Ok(15)) -} + iterator.from_list([2, 4, 5, 8]) + |> iterator.all(satisfying: fn(n) { n % 2 == 0 }) + |> should.be_false + } -pub fn last_test() { - iterator.empty() - |> iterator.last - |> should.equal(Error(Nil)) + pub fn group_test() { + iterator.from_list([1, 2, 3, 4, 5, 6]) + |> iterator.group(by: fn(n) { n % 3 }) + |> should.equal(map.from_list([#(0, [3, 6]), #(1, [1, 4]), #(2, [2, 5])])) + } - iterator.range(1, 10) - |> iterator.last - |> should.equal(Ok(9)) -} + pub fn reduce_test() { + iterator.empty() + |> iterator.reduce(with: fn(x, y) { x + y }) + |> should.equal(Error(Nil)) -pub fn empty_test() { - iterator.empty() - |> iterator.to_list - |> should.equal([]) -} + iterator.from_list([1, 2, 3, 4, 5]) + |> iterator.reduce(with: fn(x, y) { x + y }) + |> should.equal(Ok(15)) + } -pub fn once_test() { - iterator.once(fn() { 1 }) - |> iterator.to_list - |> should.equal([1]) -} + pub fn last_test() { + iterator.empty() + |> iterator.last + |> should.equal(Error(Nil)) -pub fn single_test() { - iterator.single(1) - |> iterator.to_list - |> should.equal([1]) -} + iterator.range(1, 10) + |> iterator.last + |> should.equal(Ok(9)) + } + + pub fn empty_test() { + iterator.empty() + |> iterator.to_list + |> should.equal([]) + } -pub fn interleave_test() { - iterator.from_list([1, 2, 3, 4]) - |> iterator.interleave(with: iterator.from_list([11, 12, 13, 14])) - |> iterator.to_list - |> should.equal([1, 11, 2, 12, 3, 13, 4, 14]) + pub fn once_test() { + iterator.once(fn() { 1 }) + |> iterator.to_list + |> should.equal([1]) + } - iterator.from_list([1, 2, 3, 4]) - |> iterator.interleave(with: iterator.from_list([100])) - |> iterator.to_list - |> should.equal([1, 100, 2, 3, 4]) + pub fn single_test() { + iterator.single(1) + |> iterator.to_list + |> should.equal([1]) + } + + pub fn interleave_test() { + iterator.from_list([1, 2, 3, 4]) + |> iterator.interleave(with: iterator.from_list([11, 12, 13, 14])) + |> iterator.to_list + |> should.equal([1, 11, 2, 12, 3, 13, 4, 14]) + + iterator.from_list([1, 2, 3, 4]) + |> iterator.interleave(with: iterator.from_list([100])) + |> iterator.to_list + |> should.equal([1, 100, 2, 3, 4]) + } } diff --git a/test/gleam/list_test.gleam b/test/gleam/list_test.gleam index c4f5ea3..bfe2c0b 100644 --- a/test/gleam/list_test.gleam +++ b/test/gleam/list_test.gleam @@ -1,724 +1,726 @@ -import gleam/should -import gleam/list -import gleam/int -import gleam/float -import gleam/string -import gleam/pair - -pub fn length_test() { - list.length([]) - |> should.equal(0) - - list.length([1]) - |> should.equal(1) +if erlang { + import gleam/should + import gleam/list + import gleam/int + import gleam/float + import gleam/string + import gleam/pair + + pub fn length_test() { + list.length([]) + |> should.equal(0) + + list.length([1]) + |> should.equal(1) + + list.length([1, 1]) + |> should.equal(2) + + list.length([1, 1, 1]) + |> should.equal(3) + } - list.length([1, 1]) - |> should.equal(2) + pub fn reverse_test() { + list.reverse([]) + |> should.equal([]) + list.reverse([1, 2, 3, 4, 5]) + |> should.equal([5, 4, 3, 2, 1]) + } - list.length([1, 1, 1]) - |> should.equal(3) -} + pub fn is_empty_test() { + list.is_empty([]) + |> should.be_true + list.is_empty([1]) + |> should.be_false + } -pub fn reverse_test() { - list.reverse([]) - |> should.equal([]) - list.reverse([1, 2, 3, 4, 5]) - |> should.equal([5, 4, 3, 2, 1]) -} + pub fn contains_test() { + list.contains([0, 4, 5, 1], 1) + |> should.be_true + list.contains([0, 4, 5, 7], 1) + |> should.be_false + list.contains([], 1) + |> should.be_false + } -pub fn is_empty_test() { - list.is_empty([]) - |> should.be_true - list.is_empty([1]) - |> should.be_false -} + pub fn head_test() { + list.head([0, 4, 5, 7]) + |> should.equal(Ok(0)) -pub fn contains_test() { - list.contains([0, 4, 5, 1], 1) - |> should.be_true - list.contains([0, 4, 5, 7], 1) - |> should.be_false - list.contains([], 1) - |> should.be_false -} + list.head([]) + |> should.equal(Error(Nil)) + } -pub fn head_test() { - list.head([0, 4, 5, 7]) - |> should.equal(Ok(0)) + pub fn tail_test() { + list.tail([0, 4, 5, 7]) + |> should.equal(Ok([4, 5, 7])) - list.head([]) - |> should.equal(Error(Nil)) -} + list.tail([0]) + |> should.equal(Ok([])) -pub fn tail_test() { - list.tail([0, 4, 5, 7]) - |> should.equal(Ok([4, 5, 7])) + list.tail([]) + |> should.equal(Error(Nil)) + } - list.tail([0]) - |> should.equal(Ok([])) + pub fn filter_test() { + [] + |> list.filter(fn(_) { True }) + |> should.equal([]) - list.tail([]) - |> should.equal(Error(Nil)) -} + [0, 4, 5, 7, 3] + |> list.filter(fn(_) { True }) + |> should.equal([0, 4, 5, 7, 3]) -pub fn filter_test() { - [] - |> list.filter(fn(_) { True }) - |> should.equal([]) + [0, 4, 5, 7, 3] + |> list.filter(fn(x) { x > 4 }) + |> should.equal([5, 7]) - [0, 4, 5, 7, 3] - |> list.filter(fn(_) { True }) - |> should.equal([0, 4, 5, 7, 3]) + [0, 4, 5, 7, 3] + |> list.filter(fn(x) { x < 4 }) + |> should.equal([0, 3]) + } - [0, 4, 5, 7, 3] - |> list.filter(fn(x) { x > 4 }) - |> should.equal([5, 7]) + pub fn filter_map_test() { + [2, 4, 6, 1] + |> list.filter_map(fn(x) { Ok(x + 1) }) + |> should.equal([3, 5, 7, 2]) - [0, 4, 5, 7, 3] - |> list.filter(fn(x) { x < 4 }) - |> should.equal([0, 3]) -} + [2, 4, 6, 1] + |> list.filter_map(Error) + |> should.equal([]) + } -pub fn filter_map_test() { - [2, 4, 6, 1] - |> list.filter_map(fn(x) { Ok(x + 1) }) - |> should.equal([3, 5, 7, 2]) + pub fn map_test() { + [] + |> list.map(fn(x) { x * 2 }) + |> should.equal([]) - [2, 4, 6, 1] - |> list.filter_map(Error) - |> should.equal([]) -} + [0, 4, 5, 7, 3] + |> list.map(fn(x) { x * 2 }) + |> should.equal([0, 8, 10, 14, 6]) + } -pub fn map_test() { - [] - |> list.map(fn(x) { x * 2 }) - |> should.equal([]) + pub fn map_fold_test() { + [1, 2, 3, 4] + |> list.map_fold(from: 0, with: fn(i, acc) { #(i * 2, acc + i) }) + |> should.equal(#([2, 4, 6, 8], 10)) + } - [0, 4, 5, 7, 3] - |> list.map(fn(x) { x * 2 }) - |> should.equal([0, 8, 10, 14, 6]) -} + pub fn try_map_test() { + let fun = fn(x) { + case x == 6 || x == 5 || x == 4 { + True -> Ok(x * 2) + False -> Error(x) + } + } -pub fn map_fold_test() { - [1, 2, 3, 4] - |> list.map_fold(from: 0, with: fn(i, acc) { #(i * 2, acc + i) }) - |> should.equal(#([2, 4, 6, 8], 10)) -} + [5, 6, 5, 6] + |> list.try_map(fun) + |> should.equal(Ok([10, 12, 10, 12])) -pub fn try_map_test() { - let fun = fn(x) { - case x == 6 || x == 5 || x == 4 { - True -> Ok(x * 2) - False -> Error(x) - } + [4, 6, 5, 7, 3] + |> list.try_map(fun) + |> should.equal(Error(7)) } - [5, 6, 5, 6] - |> list.try_map(fun) - |> should.equal(Ok([10, 12, 10, 12])) - - [4, 6, 5, 7, 3] - |> list.try_map(fun) - |> should.equal(Error(7)) -} + pub fn drop_test() { + [] + |> list.drop(5) + |> should.equal([]) -pub fn drop_test() { - [] - |> list.drop(5) - |> should.equal([]) + [1, 2, 3, 4, 5, 6, 7, 8] + |> list.drop(5) + |> should.equal([6, 7, 8]) + } - [1, 2, 3, 4, 5, 6, 7, 8] - |> list.drop(5) - |> should.equal([6, 7, 8]) -} + pub fn take_test() { + [] + |> list.take(5) + |> should.equal([]) + [1, 2, 3, 4, 5, 6, 7, 8] + |> list.take(5) + |> should.equal([1, 2, 3, 4, 5]) + } -pub fn take_test() { - [] - |> list.take(5) - |> should.equal([]) - [1, 2, 3, 4, 5, 6, 7, 8] - |> list.take(5) - |> should.equal([1, 2, 3, 4, 5]) -} + pub fn new_test() { + list.new() + |> should.equal([]) + } -pub fn new_test() { - list.new() - |> should.equal([]) -} + pub fn append_test() { + list.append([1], [2, 3]) + |> should.equal([1, 2, 3]) + } -pub fn append_test() { - list.append([1], [2, 3]) - |> should.equal([1, 2, 3]) -} + pub fn flatten_test() { + list.flatten([]) + |> should.equal([]) -pub fn flatten_test() { - list.flatten([]) - |> should.equal([]) + list.flatten([[]]) + |> should.equal([]) - list.flatten([[]]) - |> should.equal([]) + list.flatten([[], [], []]) + |> should.equal([]) - list.flatten([[], [], []]) - |> should.equal([]) + list.flatten([[1, 2], [], [3, 4]]) + |> should.equal([1, 2, 3, 4]) + } - list.flatten([[1, 2], [], [3, 4]]) - |> should.equal([1, 2, 3, 4]) -} + pub fn flat_map_test() { + list.flat_map([1, 10, 20], fn(x) { [x, x + 1] }) + |> should.equal([1, 2, 10, 11, 20, 21]) + } -pub fn flat_map_test() { - list.flat_map([1, 10, 20], fn(x) { [x, x + 1] }) - |> should.equal([1, 2, 10, 11, 20, 21]) -} + pub fn fold_test() { + [1, 2, 3] + |> list.fold([], fn(x, acc) { [x, ..acc] }) + |> should.equal([3, 2, 1]) + } -pub fn fold_test() { - [1, 2, 3] - |> list.fold([], fn(x, acc) { [x, ..acc] }) - |> should.equal([3, 2, 1]) -} + pub fn fold_right_test() { + [1, 2, 3] + |> list.fold_right(from: [], with: fn(x, acc) { [x, ..acc] }) + |> should.equal([1, 2, 3]) + } -pub fn fold_right_test() { - [1, 2, 3] - |> list.fold_right(from: [], with: fn(x, acc) { [x, ..acc] }) - |> should.equal([1, 2, 3]) -} + pub fn index_fold_test() { + ["a", "b", "c"] + |> list.index_fold([], fn(ix, i, acc) { [#(ix, i), ..acc] }) + |> should.equal([#(2, "c"), #(1, "b"), #(0, "a")]) + } -pub fn index_fold_test() { - ["a", "b", "c"] - |> list.index_fold([], fn(ix, i, acc) { [#(ix, i), ..acc] }) - |> should.equal([#(2, "c"), #(1, "b"), #(0, "a")]) -} + pub fn fold_until_test() { + [1, 2, 3, 4] + |> list.fold_until( + from: 0, + with: fn(n, acc) { + case n < 4 { + True -> list.Continue(acc + n) + False -> list.Stop(acc) + } + }, + ) + |> should.equal(6) + } -pub fn fold_until_test() { - [1, 2, 3, 4] - |> list.fold_until( - from: 0, - with: fn(n, acc) { - case n < 4 { - True -> list.Continue(acc + n) - False -> list.Stop(acc) - } - }, - ) - |> should.equal(6) -} + pub fn try_fold_test() { + [1, 2, 3] + |> list.try_fold( + 0, + fn(i, acc) { + case i < 4 { + True -> Ok(acc + i) + False -> Error(Nil) + } + }, + ) + |> should.equal(Ok(6)) + + [1, 2, 3] + |> list.try_fold( + 0, + fn(i, acc) { + case i < 3 { + True -> Ok(acc + i) + False -> Error(Nil) + } + }, + ) + |> should.equal(Error(Nil)) + } -pub fn try_fold_test() { - [1, 2, 3] - |> list.try_fold( - 0, - fn(i, acc) { - case i < 4 { - True -> Ok(acc + i) - False -> Error(Nil) - } - }, - ) - |> should.equal(Ok(6)) - - [1, 2, 3] - |> list.try_fold( - 0, - fn(i, acc) { - case i < 3 { - True -> Ok(acc + i) - False -> Error(Nil) + pub fn find_map_test() { + let f = fn(x) { + case x { + 2 -> Ok(4) + _ -> Error(Nil) } - }, - ) - |> should.equal(Error(Nil)) -} - -pub fn find_map_test() { - let f = fn(x) { - case x { - 2 -> Ok(4) - _ -> Error(Nil) } - } - [1, 2, 3] - |> list.find_map(with: f) - |> should.equal(Ok(4)) + [1, 2, 3] + |> list.find_map(with: f) + |> should.equal(Ok(4)) - [1, 3, 2] - |> list.find_map(with: f) - |> should.equal(Ok(4)) + [1, 3, 2] + |> list.find_map(with: f) + |> should.equal(Ok(4)) - [1, 3] - |> list.find_map(with: f) - |> should.equal(Error(Nil)) -} + [1, 3] + |> list.find_map(with: f) + |> should.equal(Error(Nil)) + } -pub fn find_test() { - let is_two = fn(x) { x == 2 } + pub fn find_test() { + let is_two = fn(x) { x == 2 } - [1, 2, 3] - |> list.find(one_that: is_two) - |> should.equal(Ok(2)) + [1, 2, 3] + |> list.find(one_that: is_two) + |> should.equal(Ok(2)) - [1, 3, 2] - |> list.find(one_that: is_two) - |> should.equal(Ok(2)) + [1, 3, 2] + |> list.find(one_that: is_two) + |> should.equal(Ok(2)) - [1, 3] - |> list.find(one_that: is_two) - |> should.equal(Error(Nil)) -} + [1, 3] + |> list.find(one_that: is_two) + |> should.equal(Error(Nil)) + } -pub fn all_test() { - list.all([1, 2, 3, 4, 5], fn(x) { x > 0 }) - |> should.equal(True) + pub fn all_test() { + list.all([1, 2, 3, 4, 5], fn(x) { x > 0 }) + |> should.equal(True) - list.all([1, 2, 3, 4, 5], fn(x) { x < 0 }) - |> should.equal(False) + list.all([1, 2, 3, 4, 5], fn(x) { x < 0 }) + |> should.equal(False) - list.all([], fn(_) { False }) - |> should.equal(True) -} + list.all([], fn(_) { False }) + |> should.equal(True) + } -pub fn any_test() { - list.any([1, 2, 3, 4, 5], fn(x) { x == 2 }) - |> should.equal(True) + pub fn any_test() { + list.any([1, 2, 3, 4, 5], fn(x) { x == 2 }) + |> should.equal(True) - list.any([1, 2, 3, 4, 5], fn(x) { x < 0 }) - |> should.equal(False) + list.any([1, 2, 3, 4, 5], fn(x) { x < 0 }) + |> should.equal(False) - list.any([], fn(_) { False }) - |> should.equal(False) -} + list.any([], fn(_) { False }) + |> should.equal(False) + } -pub fn zip_test() { - list.zip([], [1, 2, 3]) - |> should.equal([]) + pub fn zip_test() { + list.zip([], [1, 2, 3]) + |> should.equal([]) - list.zip([1, 2], []) - |> should.equal([]) + list.zip([1, 2], []) + |> should.equal([]) - list.zip([1, 2, 3], [4, 5, 6]) - |> should.equal([#(1, 4), #(2, 5), #(3, 6)]) + list.zip([1, 2, 3], [4, 5, 6]) + |> should.equal([#(1, 4), #(2, 5), #(3, 6)]) - list.zip([5, 6], [1, 2, 3]) - |> should.equal([#(5, 1), #(6, 2)]) + list.zip([5, 6], [1, 2, 3]) + |> should.equal([#(5, 1), #(6, 2)]) - list.zip([5, 6, 7], [1, 2]) - |> should.equal([#(5, 1), #(6, 2)]) -} + list.zip([5, 6, 7], [1, 2]) + |> should.equal([#(5, 1), #(6, 2)]) + } -pub fn strict_zip_test() { - list.strict_zip([], [1, 2, 3]) - |> should.equal(Error(list.LengthMismatch)) + pub fn strict_zip_test() { + list.strict_zip([], [1, 2, 3]) + |> should.equal(Error(list.LengthMismatch)) - list.strict_zip([1, 2], []) - |> should.equal(Error(list.LengthMismatch)) + list.strict_zip([1, 2], []) + |> should.equal(Error(list.LengthMismatch)) - list.strict_zip([1, 2, 3], [4, 5, 6]) - |> should.equal(Ok([#(1, 4), #(2, 5), #(3, 6)])) + list.strict_zip([1, 2, 3], [4, 5, 6]) + |> should.equal(Ok([#(1, 4), #(2, 5), #(3, 6)])) - list.strict_zip([5, 6], [1, 2, 3]) - |> should.equal(Error(list.LengthMismatch)) + list.strict_zip([5, 6], [1, 2, 3]) + |> should.equal(Error(list.LengthMismatch)) - list.strict_zip([5, 6, 7], [1, 2]) - |> should.equal(Error(list.LengthMismatch)) -} + list.strict_zip([5, 6, 7], [1, 2]) + |> should.equal(Error(list.LengthMismatch)) + } -pub fn unzip_test() { - list.unzip([#(1, 2), #(3, 4)]) - |> should.equal(#([1, 3], [2, 4])) + pub fn unzip_test() { + list.unzip([#(1, 2), #(3, 4)]) + |> should.equal(#([1, 3], [2, 4])) - list.unzip([]) - |> should.equal(#([], [])) -} + list.unzip([]) + |> should.equal(#([], [])) + } -pub fn intersperse_test() { - list.intersperse([1, 2, 3], 4) - |> should.equal([1, 4, 2, 4, 3]) + pub fn intersperse_test() { + list.intersperse([1, 2, 3], 4) + |> should.equal([1, 4, 2, 4, 3]) - list.intersperse([], 2) - |> should.equal([]) -} + list.intersperse([], 2) + |> should.equal([]) + } -pub fn at_test() { - list.at([1, 2, 3], 2) - |> should.equal(Ok(3)) + pub fn at_test() { + list.at([1, 2, 3], 2) + |> should.equal(Ok(3)) - list.at([1, 2, 3], 5) - |> should.equal(Error(Nil)) + list.at([1, 2, 3], 5) + |> should.equal(Error(Nil)) - list.at([], 0) - |> should.equal(Error(Nil)) + list.at([], 0) + |> should.equal(Error(Nil)) - list.at([1, 2, 3, 4, 5, 6], -1) - |> should.equal(Error(Nil)) -} + list.at([1, 2, 3, 4, 5, 6], -1) + |> should.equal(Error(Nil)) + } -pub fn unique_test() { - list.unique([1, 1, 2, 3, 4, 4, 4, 5, 6]) - |> should.equal([1, 2, 3, 4, 5, 6]) + pub fn unique_test() { + list.unique([1, 1, 2, 3, 4, 4, 4, 5, 6]) + |> should.equal([1, 2, 3, 4, 5, 6]) - list.unique([7, 1, 45, 6, 2, 47, 2, 7, 5]) - |> should.equal([7, 1, 45, 6, 2, 47, 5]) + list.unique([7, 1, 45, 6, 2, 47, 2, 7, 5]) + |> should.equal([7, 1, 45, 6, 2, 47, 5]) - list.unique([3, 4, 5]) - |> should.equal([3, 4, 5]) + list.unique([3, 4, 5]) + |> should.equal([3, 4, 5]) - list.unique([]) - |> should.equal([]) -} + list.unique([]) + |> should.equal([]) + } -pub fn sort_test() { - [4, 3, 6, 5, 4] - |> list.sort(int.compare) - |> should.equal([3, 4, 4, 5, 6]) + pub fn sort_test() { + [4, 3, 6, 5, 4] + |> list.sort(int.compare) + |> should.equal([3, 4, 4, 5, 6]) - [4, 3, 6, 5, 4, 1] - |> list.sort(int.compare) - |> should.equal([1, 3, 4, 4, 5, 6]) + [4, 3, 6, 5, 4, 1] + |> list.sort(int.compare) + |> should.equal([1, 3, 4, 4, 5, 6]) - [4.1, 3.1, 6.1, 5.1, 4.1] - |> list.sort(float.compare) - |> should.equal([3.1, 4.1, 4.1, 5.1, 6.1]) + [4.1, 3.1, 6.1, 5.1, 4.1] + |> list.sort(float.compare) + |> should.equal([3.1, 4.1, 4.1, 5.1, 6.1]) - [] - |> list.sort(int.compare) - |> should.equal([]) -} + [] + |> list.sort(int.compare) + |> should.equal([]) + } -pub fn index_map_test() { - list.index_map([3, 4, 5], fn(i, x) { #(i, x) }) - |> should.equal([#(0, 3), #(1, 4), #(2, 5)]) + pub fn index_map_test() { + list.index_map([3, 4, 5], fn(i, x) { #(i, x) }) + |> should.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) - |> should.equal(["a0", "b1", "c2"]) -} + let f = fn(i, x) { string.append(x, int.to_string(i)) } + list.index_map(["a", "b", "c"], f) + |> should.equal(["a0", "b1", "c2"]) + } -pub fn range_test() { - list.range(0, 0) - |> should.equal([]) + pub fn range_test() { + list.range(0, 0) + |> should.equal([]) - list.range(1, 1) - |> should.equal([]) + list.range(1, 1) + |> should.equal([]) - list.range(-1, -1) - |> should.equal([]) + list.range(-1, -1) + |> should.equal([]) - list.range(0, 1) - |> should.equal([0]) + list.range(0, 1) + |> should.equal([0]) - list.range(0, 5) - |> should.equal([0, 1, 2, 3, 4]) + list.range(0, 5) + |> should.equal([0, 1, 2, 3, 4]) - list.range(1, -5) - |> should.equal([1, 0, -1, -2, -3, -4]) -} + list.range(1, -5) + |> should.equal([1, 0, -1, -2, -3, -4]) + } -pub fn repeat_test() { - list.repeat(1, -10) - |> should.equal([]) + pub fn repeat_test() { + list.repeat(1, -10) + |> should.equal([]) - list.repeat(1, 0) - |> should.equal([]) + list.repeat(1, 0) + |> should.equal([]) - list.repeat(2, 3) - |> should.equal([2, 2, 2]) + list.repeat(2, 3) + |> should.equal([2, 2, 2]) - list.repeat("x", 5) - |> should.equal(["x", "x", "x", "x", "x"]) -} + list.repeat("x", 5) + |> should.equal(["x", "x", "x", "x", "x"]) + } -pub fn split_test() { - [] - |> list.split(0) - |> should.equal(#([], [])) + pub fn split_test() { + [] + |> list.split(0) + |> should.equal(#([], [])) - [0, 1, 2, 3, 4] - |> list.split(0) - |> should.equal(#([], [0, 1, 2, 3, 4])) + [0, 1, 2, 3, 4] + |> list.split(0) + |> should.equal(#([], [0, 1, 2, 3, 4])) - [0, 1, 2, 3, 4] - |> list.split(-2) - |> should.equal(#([], [0, 1, 2, 3, 4])) + [0, 1, 2, 3, 4] + |> list.split(-2) + |> should.equal(#([], [0, 1, 2, 3, 4])) - [0, 1, 2, 3, 4] - |> list.split(1) - |> should.equal(#([0], [1, 2, 3, 4])) + [0, 1, 2, 3, 4] + |> list.split(1) + |> should.equal(#([0], [1, 2, 3, 4])) - [0, 1, 2, 3, 4] - |> list.split(3) - |> should.equal(#([0, 1, 2], [3, 4])) + [0, 1, 2, 3, 4] + |> list.split(3) + |> should.equal(#([0, 1, 2], [3, 4])) - [0, 1, 2, 3, 4] - |> list.split(9) - |> should.equal(#([0, 1, 2, 3, 4], [])) -} + [0, 1, 2, 3, 4] + |> list.split(9) + |> should.equal(#([0, 1, 2, 3, 4], [])) + } -pub fn split_while_test() { - [] - |> list.split_while(fn(x) { x <= 5 }) - |> should.equal(#([], [])) + pub fn split_while_test() { + [] + |> list.split_while(fn(x) { x <= 5 }) + |> should.equal(#([], [])) - [1, 2, 3, 4, 5] - |> list.split_while(fn(x) { x <= 5 }) - |> should.equal(#([1, 2, 3, 4, 5], [])) + [1, 2, 3, 4, 5] + |> list.split_while(fn(x) { x <= 5 }) + |> should.equal(#([1, 2, 3, 4, 5], [])) - [1, 2, 3, 4, 5] - |> list.split_while(fn(x) { x == 2 }) - |> should.equal(#([], [1, 2, 3, 4, 5])) + [1, 2, 3, 4, 5] + |> list.split_while(fn(x) { x == 2 }) + |> should.equal(#([], [1, 2, 3, 4, 5])) - [1, 2, 3, 4, 5] - |> list.split_while(fn(x) { x <= 3 }) - |> should.equal(#([1, 2, 3], [4, 5])) + [1, 2, 3, 4, 5] + |> list.split_while(fn(x) { x <= 3 }) + |> should.equal(#([1, 2, 3], [4, 5])) - [1, 2, 3, 4, 5] - |> list.split_while(fn(x) { x <= -3 }) - |> should.equal(#([], [1, 2, 3, 4, 5])) -} + [1, 2, 3, 4, 5] + |> list.split_while(fn(x) { x <= -3 }) + |> should.equal(#([], [1, 2, 3, 4, 5])) + } -pub fn key_find_test() { - let proplist = [#(0, "1"), #(1, "2")] + pub fn key_find_test() { + let proplist = [#(0, "1"), #(1, "2")] - proplist - |> list.key_find(0) - |> should.equal(Ok("1")) + proplist + |> list.key_find(0) + |> should.equal(Ok("1")) - proplist - |> list.key_find(1) - |> should.equal(Ok("2")) + proplist + |> list.key_find(1) + |> should.equal(Ok("2")) - proplist - |> list.key_find(2) - |> should.equal(Error(Nil)) -} + proplist + |> list.key_find(2) + |> should.equal(Error(Nil)) + } -pub fn pop_test() { - list.pop([1, 2, 3], fn(x) { x > 2 }) - |> should.equal(Ok(#(3, [1, 2]))) + pub fn pop_test() { + list.pop([1, 2, 3], fn(x) { x > 2 }) + |> should.equal(Ok(#(3, [1, 2]))) - list.pop([1, 2, 3], fn(x) { x > 4 }) - |> should.equal(Error(Nil)) + list.pop([1, 2, 3], fn(x) { x > 4 }) + |> should.equal(Error(Nil)) - list.pop([], fn(_x) { True }) - |> should.equal(Error(Nil)) -} + list.pop([], fn(_x) { True }) + |> should.equal(Error(Nil)) + } -pub fn pop_map_test() { - list.pop_map(["foo", "2", "3"], int.parse) - |> should.equal(Ok(#(2, ["foo", "3"]))) + pub fn pop_map_test() { + list.pop_map(["foo", "2", "3"], int.parse) + |> should.equal(Ok(#(2, ["foo", "3"]))) - list.pop_map(["foo", "bar"], int.parse) - |> should.equal(Error(Nil)) + list.pop_map(["foo", "bar"], int.parse) + |> should.equal(Error(Nil)) - list.pop_map([], int.parse) - |> should.equal(Error(Nil)) -} + list.pop_map([], int.parse) + |> should.equal(Error(Nil)) + } -pub fn key_pop_test() { - list.key_pop([#("a", 0), #("b", 1)], "a") - |> should.equal(Ok(#(0, [#("b", 1)]))) + pub fn key_pop_test() { + list.key_pop([#("a", 0), #("b", 1)], "a") + |> should.equal(Ok(#(0, [#("b", 1)]))) - list.key_pop([#("a", 0), #("b", 1)], "b") - |> should.equal(Ok(#(1, [#("a", 0)]))) + list.key_pop([#("a", 0), #("b", 1)], "b") + |> should.equal(Ok(#(1, [#("a", 0)]))) - list.key_pop([#("a", 0), #("b", 1)], "c") - |> should.equal(Error(Nil)) -} + list.key_pop([#("a", 0), #("b", 1)], "c") + |> should.equal(Error(Nil)) + } -pub fn key_set_test() { - [#(5, 0), #(4, 1)] - |> list.key_set(4, 100) - |> should.equal([#(5, 0), #(4, 100)]) + pub fn key_set_test() { + [#(5, 0), #(4, 1)] + |> list.key_set(4, 100) + |> should.equal([#(5, 0), #(4, 100)]) - [#(5, 0), #(4, 1)] - |> list.key_set(1, 100) - |> should.equal([#(5, 0), #(4, 1), #(1, 100)]) -} + [#(5, 0), #(4, 1)] + |> list.key_set(1, 100) + |> should.equal([#(5, 0), #(4, 1), #(1, 100)]) + } -pub fn partition_test() { - [1, 2, 3, 4, 5, 6, 7] - |> list.partition(int.is_odd) - |> should.equal(#([1, 3, 5, 7], [2, 4, 6])) -} + pub fn partition_test() { + [1, 2, 3, 4, 5, 6, 7] + |> list.partition(int.is_odd) + |> should.equal(#([1, 3, 5, 7], [2, 4, 6])) + } -pub fn permutations_test() { - [1, 2] - |> list.permutations - |> should.equal([[1, 2], [2, 1]]) - - let expected = [ - [1, 2, 3], - [1, 3, 2], - [2, 1, 3], - [2, 3, 1], - [3, 1, 2], - [3, 2, 1], - ] - - [1, 2, 3] - |> list.permutations - |> should.equal(expected) - - ["a", "b"] - |> list.permutations - |> should.equal([["a", "b"], ["b", "a"]]) -} + pub fn permutations_test() { + [1, 2] + |> list.permutations + |> should.equal([[1, 2], [2, 1]]) + + let expected = [ + [1, 2, 3], + [1, 3, 2], + [2, 1, 3], + [2, 3, 1], + [3, 1, 2], + [3, 2, 1], + ] + + [1, 2, 3] + |> list.permutations + |> should.equal(expected) + + ["a", "b"] + |> list.permutations + |> should.equal([["a", "b"], ["b", "a"]]) + } -pub fn window_test() { - [1, 2, 3] - |> list.window(by: 2) - |> should.equal([[1, 2], [2, 3]]) + pub fn window_test() { + [1, 2, 3] + |> list.window(by: 2) + |> should.equal([[1, 2], [2, 3]]) - [1, 2, 3] - |> list.window(3) - |> should.equal([[1, 2, 3]]) + [1, 2, 3] + |> list.window(3) + |> should.equal([[1, 2, 3]]) - [1, 2, 3] - |> list.window(4) - |> should.equal([]) + [1, 2, 3] + |> list.window(4) + |> should.equal([]) - [1, 2, 3, 4, 5] - |> list.window(3) - |> should.equal([[1, 2, 3], [2, 3, 4], [3, 4, 5]]) -} + [1, 2, 3, 4, 5] + |> list.window(3) + |> should.equal([[1, 2, 3], [2, 3, 4], [3, 4, 5]]) + } -pub fn window_by_2_test() { - [1, 2, 3, 4] - |> list.window_by_2 - |> should.equal([#(1, 2), #(2, 3), #(3, 4)]) + pub fn window_by_2_test() { + [1, 2, 3, 4] + |> list.window_by_2 + |> should.equal([#(1, 2), #(2, 3), #(3, 4)]) - [1] - |> list.window_by_2 - |> should.equal([]) -} + [1] + |> list.window_by_2 + |> should.equal([]) + } -pub fn drop_while_test() { - [1, 2, 3, 4] - |> list.drop_while(fn(x) { x < 3 }) - |> should.equal([3, 4]) -} + pub fn drop_while_test() { + [1, 2, 3, 4] + |> list.drop_while(fn(x) { x < 3 }) + |> should.equal([3, 4]) + } -pub fn take_while_test() { - [1, 2, 3, 2, 4] - |> list.take_while(fn(x) { x < 3 }) - |> should.equal([1, 2]) -} + pub fn take_while_test() { + [1, 2, 3, 2, 4] + |> list.take_while(fn(x) { x < 3 }) + |> should.equal([1, 2]) + } -pub fn chunk_test() { - [1, 2, 2, 3, 4, 4, 6, 7, 7] - |> list.chunk(by: fn(n) { n % 2 }) - |> should.equal([[1], [2, 2], [3], [4, 4, 6], [7, 7]]) -} + pub fn chunk_test() { + [1, 2, 2, 3, 4, 4, 6, 7, 7] + |> list.chunk(by: fn(n) { n % 2 }) + |> should.equal([[1], [2, 2], [3], [4, 4, 6], [7, 7]]) + } -pub fn sized_chunk_test() { - [1, 2, 3, 4, 5, 6] - |> list.sized_chunk(into: 2) - |> should.equal([[1, 2], [3, 4], [5, 6]]) + pub fn sized_chunk_test() { + [1, 2, 3, 4, 5, 6] + |> list.sized_chunk(into: 2) + |> should.equal([[1, 2], [3, 4], [5, 6]]) - [1, 2, 3, 4, 5, 6, 7, 8] - |> list.sized_chunk(into: 3) - |> should.equal([[1, 2, 3], [4, 5, 6], [7, 8]]) -} + [1, 2, 3, 4, 5, 6, 7, 8] + |> list.sized_chunk(into: 3) + |> should.equal([[1, 2, 3], [4, 5, 6], [7, 8]]) + } -pub fn reduce_test() { - [] - |> list.reduce(with: fn(x, y) { x + y }) - |> should.equal(Error(Nil)) + pub fn reduce_test() { + [] + |> list.reduce(with: fn(x, y) { x + y }) + |> should.equal(Error(Nil)) - [1, 2, 3, 4, 5] - |> list.reduce(with: fn(x, y) { x + y }) - |> should.equal(Ok(15)) -} + [1, 2, 3, 4, 5] + |> list.reduce(with: fn(x, y) { x + y }) + |> should.equal(Ok(15)) + } -pub fn scan_test() { - [] - |> list.scan(from: 0, with: fn(i, acc) { i + acc }) - |> should.equal([]) - - [1, 2, 3, 4] - |> list.scan(from: 0, with: fn(i, acc) { 2 * i + acc }) - |> should.equal([2, 6, 12, 20]) - - [1, 2, 3, 4] - |> list.scan( - from: "", - with: fn(i, acc) { - case int.is_even(i) { - True -> "Even" - False -> "Odd" - } - |> string.append(acc, _) - }, - ) - |> should.equal(["Odd", "OddEven", "OddEvenOdd", "OddEvenOddEven"]) -} + pub fn scan_test() { + [] + |> list.scan(from: 0, with: fn(i, acc) { i + acc }) + |> should.equal([]) + + [1, 2, 3, 4] + |> list.scan(from: 0, with: fn(i, acc) { 2 * i + acc }) + |> should.equal([2, 6, 12, 20]) + + [1, 2, 3, 4] + |> list.scan( + from: "", + with: fn(i, acc) { + case int.is_even(i) { + True -> "Even" + False -> "Odd" + } + |> string.append(acc, _) + }, + ) + |> should.equal(["Odd", "OddEven", "OddEvenOdd", "OddEvenOddEven"]) + } -pub fn last_test() { - list.last([]) - |> should.equal(Error(Nil)) + pub fn last_test() { + list.last([]) + |> should.equal(Error(Nil)) - list.last([1, 2, 3, 4, 5]) - |> should.equal(Ok(5)) -} + list.last([1, 2, 3, 4, 5]) + |> should.equal(Ok(5)) + } -pub fn combinations_test() { - list.combinations([1, 2, 3], by: 0) - |> should.equal([[]]) + pub fn combinations_test() { + list.combinations([1, 2, 3], by: 0) + |> should.equal([[]]) - list.combinations([1, 2, 3], by: 1) - |> should.equal([[1], [2], [3]]) + list.combinations([1, 2, 3], by: 1) + |> should.equal([[1], [2], [3]]) - list.combinations([1, 2, 3], by: 2) - |> should.equal([[1, 2], [1, 3], [2, 3]]) + list.combinations([1, 2, 3], by: 2) + |> should.equal([[1, 2], [1, 3], [2, 3]]) - list.combinations([1, 2, 3], by: 3) - |> should.equal([[1, 2, 3]]) + list.combinations([1, 2, 3], by: 3) + |> should.equal([[1, 2, 3]]) - list.combinations([1, 2, 3], by: 4) - |> should.equal([]) + list.combinations([1, 2, 3], by: 4) + |> should.equal([]) - list.combinations([1, 2, 3, 4], 3) - |> should.equal([[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]) -} + list.combinations([1, 2, 3, 4], 3) + |> should.equal([[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]) + } -pub fn combination_pairs_test() { - list.combination_pairs([1]) - |> should.equal([]) + pub fn combination_pairs_test() { + list.combination_pairs([1]) + |> should.equal([]) - list.combination_pairs([1, 2]) - |> should.equal([#(1, 2)]) + list.combination_pairs([1, 2]) + |> should.equal([#(1, 2)]) - list.combination_pairs([1, 2, 3]) - |> should.equal([#(1, 2), #(1, 3), #(2, 3)]) + list.combination_pairs([1, 2, 3]) + |> should.equal([#(1, 2), #(1, 3), #(2, 3)]) - list.combination_pairs([1, 2, 3, 4]) - |> should.equal([#(1, 2), #(1, 3), #(1, 4), #(2, 3), #(2, 4), #(3, 4)]) -} + list.combination_pairs([1, 2, 3, 4]) + |> should.equal([#(1, 2), #(1, 3), #(1, 4), #(2, 3), #(2, 4), #(3, 4)]) + } -pub fn interleave_test() { - list.interleave([[1, 2], [101, 102]]) - |> should.equal([1, 101, 2, 102]) + pub fn interleave_test() { + list.interleave([[1, 2], [101, 102]]) + |> should.equal([1, 101, 2, 102]) - list.interleave([[1, 2], [101, 102], [201, 202]]) - |> should.equal([1, 101, 201, 2, 102, 202]) + list.interleave([[1, 2], [101, 102], [201, 202]]) + |> should.equal([1, 101, 201, 2, 102, 202]) - // Left over elements are added at the end - list.interleave([[1, 2, 3], [101, 102]]) - |> should.equal([1, 101, 2, 102, 3]) + // Left over elements are added at the end + list.interleave([[1, 2, 3], [101, 102]]) + |> should.equal([1, 101, 2, 102, 3]) - list.interleave([[1, 2], [101, 102, 103]]) - |> should.equal([1, 101, 2, 102, 103]) -} + list.interleave([[1, 2], [101, 102, 103]]) + |> should.equal([1, 101, 2, 102, 103]) + } -pub fn transpose_test() { - list.transpose([[1, 2, 3], [101, 102, 103]]) - |> should.equal([[1, 101], [2, 102], [3, 103]]) + pub fn transpose_test() { + list.transpose([[1, 2, 3], [101, 102, 103]]) + |> should.equal([[1, 101], [2, 102], [3, 103]]) - list.transpose([[1, 2, 3], [101, 102, 103], [201, 202, 203]]) - |> should.equal([[1, 101, 201], [2, 102, 202], [3, 103, 203]]) + list.transpose([[1, 2, 3], [101, 102, 103], [201, 202, 203]]) + |> should.equal([[1, 101, 201], [2, 102, 202], [3, 103, 203]]) - // Left over elements are still returned - list.transpose([[1, 2], [101, 102, 103]]) - |> should.equal([[1, 101], [2, 102], [103]]) + // Left over elements are still returned + list.transpose([[1, 2], [101, 102, 103]]) + |> should.equal([[1, 101], [2, 102], [103]]) - list.transpose([[1, 2, 3], [101, 102], [201, 202, 203]]) - |> should.equal([[1, 101, 201], [2, 102, 202], [3, 203]]) + list.transpose([[1, 2, 3], [101, 102], [201, 202, 203]]) + |> should.equal([[1, 101, 201], [2, 102, 202], [3, 203]]) + } } diff --git a/test/gleam/map_test.gleam b/test/gleam/map_test.gleam index e393a7f..6f32c8f 100644 --- a/test/gleam/map_test.gleam +++ b/test/gleam/map_test.gleam @@ -1,169 +1,171 @@ -import gleam/string -import gleam/should -import gleam/map - -pub fn from_list_test() { - [#(4, 0), #(1, 0)] - |> map.from_list - |> map.size - |> should.equal(2) - - [#(1, 0), #(1, 1)] - |> map.from_list - |> should.equal(map.from_list([#(1, 1)])) -} +if erlang { + import gleam/string + import gleam/should + import gleam/map + + pub fn from_list_test() { + [#(4, 0), #(1, 0)] + |> map.from_list + |> map.size + |> should.equal(2) + + [#(1, 0), #(1, 1)] + |> map.from_list + |> should.equal(map.from_list([#(1, 1)])) + } -pub fn has_key_test() { - [] - |> map.from_list - |> map.has_key(1) - |> should.be_false - - [#(1, 0)] - |> map.from_list - |> map.has_key(1) - |> should.be_true - - [#(4, 0), #(1, 0)] - |> map.from_list - |> map.has_key(1) - |> should.be_true - - [#(4, 0), #(1, 0)] - |> map.from_list - |> map.has_key(0) - |> should.be_false -} + pub fn has_key_test() { + [] + |> map.from_list + |> map.has_key(1) + |> should.be_false + + [#(1, 0)] + |> map.from_list + |> map.has_key(1) + |> should.be_true + + [#(4, 0), #(1, 0)] + |> map.from_list + |> map.has_key(1) + |> should.be_true + + [#(4, 0), #(1, 0)] + |> map.from_list + |> map.has_key(0) + |> should.be_false + } -pub fn new_test() { - map.new() - |> map.size - |> should.equal(0) + pub fn new_test() { + map.new() + |> map.size + |> should.equal(0) - map.new() - |> map.to_list - |> should.equal([]) -} + map.new() + |> map.to_list + |> should.equal([]) + } -pub fn get_test() { - let proplist = [#(4, 0), #(1, 1)] - let m = map.from_list(proplist) + pub fn get_test() { + let proplist = [#(4, 0), #(1, 1)] + let m = map.from_list(proplist) - m - |> map.get(4) - |> should.equal(Ok(0)) + m + |> map.get(4) + |> should.equal(Ok(0)) - m - |> map.get(1) - |> should.equal(Ok(1)) + m + |> map.get(1) + |> should.equal(Ok(1)) - m - |> map.get(2) - |> should.equal(Error(Nil)) -} + m + |> map.get(2) + |> should.equal(Error(Nil)) + } -pub fn insert_test() { - map.new() - |> map.insert("a", 0) - |> map.insert("b", 1) - |> map.insert("c", 2) - |> should.equal(map.from_list([#("a", 0), #("b", 1), #("c", 2)])) -} + pub fn insert_test() { + map.new() + |> map.insert("a", 0) + |> map.insert("b", 1) + |> map.insert("c", 2) + |> should.equal(map.from_list([#("a", 0), #("b", 1), #("c", 2)])) + } -pub fn map_values_test() { - [#(1, 0), #(2, 1), #(3, 2)] - |> map.from_list - |> map.map_values(fn(k, v) { k + v }) - |> should.equal(map.from_list([#(1, 1), #(2, 3), #(3, 5)])) -} + pub fn map_values_test() { + [#(1, 0), #(2, 1), #(3, 2)] + |> map.from_list + |> map.map_values(fn(k, v) { k + v }) + |> should.equal(map.from_list([#(1, 1), #(2, 3), #(3, 5)])) + } -pub fn keys_test() { - [#("a", 0), #("b", 1), #("c", 2)] - |> map.from_list - |> map.keys - |> should.equal(["a", "b", "c"]) -} + pub fn keys_test() { + [#("a", 0), #("b", 1), #("c", 2)] + |> map.from_list + |> map.keys + |> should.equal(["a", "b", "c"]) + } -pub fn values_test() { - [#("a", 0), #("b", 1), #("c", 2)] - |> map.from_list - |> map.values - |> should.equal([0, 1, 2]) -} + pub fn values_test() { + [#("a", 0), #("b", 1), #("c", 2)] + |> map.from_list + |> map.values + |> should.equal([0, 1, 2]) + } -pub fn take_test() { - [#("a", 0), #("b", 1), #("c", 2)] - |> map.from_list - |> map.take(["a", "b", "d"]) - |> should.equal(map.from_list([#("a", 0), #("b", 1)])) -} + pub fn take_test() { + [#("a", 0), #("b", 1), #("c", 2)] + |> map.from_list + |> map.take(["a", "b", "d"]) + |> should.equal(map.from_list([#("a", 0), #("b", 1)])) + } -pub fn drop_test() { - [#("a", 0), #("b", 1), #("c", 2)] - |> map.from_list - |> map.drop(["a", "b", "d"]) - |> should.equal(map.from_list([#("c", 2)])) -} + pub fn drop_test() { + [#("a", 0), #("b", 1), #("c", 2)] + |> map.from_list + |> map.drop(["a", "b", "d"]) + |> should.equal(map.from_list([#("c", 2)])) + } -pub fn merge_test() { - let a = map.from_list([#("a", 2), #("c", 4), #("d", 3)]) + pub fn merge_test() { + let a = map.from_list([#("a", 2), #("c", 4), #("d", 3)]) - let b = map.from_list([#("a", 0), #("b", 1), #("c", 2)]) + let b = map.from_list([#("a", 0), #("b", 1), #("c", 2)]) - map.merge(a, b) - |> should.equal(map.from_list([#("a", 0), #("b", 1), #("c", 2), #("d", 3)])) + map.merge(a, b) + |> should.equal(map.from_list([#("a", 0), #("b", 1), #("c", 2), #("d", 3)])) - map.merge(b, a) - |> should.equal(map.from_list([#("a", 2), #("b", 1), #("c", 4), #("d", 3)])) -} + map.merge(b, a) + |> should.equal(map.from_list([#("a", 2), #("b", 1), #("c", 4), #("d", 3)])) + } -pub fn delete_test() { - [#("a", 0), #("b", 1), #("c", 2)] - |> map.from_list - |> map.delete("a") - |> map.delete("d") - |> should.equal(map.from_list([#("b", 1), #("c", 2)])) -} + pub fn delete_test() { + [#("a", 0), #("b", 1), #("c", 2)] + |> map.from_list + |> map.delete("a") + |> map.delete("d") + |> should.equal(map.from_list([#("b", 1), #("c", 2)])) + } -pub fn update_test() { - let dict = map.from_list([#("a", 0), #("b", 1), #("c", 2)]) + pub fn update_test() { + let dict = map.from_list([#("a", 0), #("b", 1), #("c", 2)]) - let inc_or_zero = fn(x) { - case x { - Ok(i) -> i + 1 - Error(_) -> 0 + let inc_or_zero = fn(x) { + case x { + Ok(i) -> i + 1 + Error(_) -> 0 + } } - } - dict - |> map.update("a", inc_or_zero) - |> should.equal(map.from_list([#("a", 1), #("b", 1), #("c", 2)])) + dict + |> map.update("a", inc_or_zero) + |> should.equal(map.from_list([#("a", 1), #("b", 1), #("c", 2)])) - dict - |> map.update("b", inc_or_zero) - |> should.equal(map.from_list([#("a", 0), #("b", 2), #("c", 2)])) + dict + |> map.update("b", inc_or_zero) + |> should.equal(map.from_list([#("a", 0), #("b", 2), #("c", 2)])) - dict - |> map.update("z", inc_or_zero) - |> should.equal(map.from_list([#("a", 0), #("b", 1), #("c", 2), #("z", 0)])) -} + dict + |> map.update("z", inc_or_zero) + |> should.equal(map.from_list([#("a", 0), #("b", 1), #("c", 2), #("z", 0)])) + } -pub fn fold_test() { - let dict = map.from_list([#("a", 0), #("b", 1), #("c", 2), #("d", 3)]) + pub fn fold_test() { + let dict = map.from_list([#("a", 0), #("b", 1), #("c", 2), #("d", 3)]) - let add = fn(_, v, acc) { v + acc } + let add = fn(_, v, acc) { v + acc } - dict - |> map.fold(0, add) - |> should.equal(6) + dict + |> map.fold(0, add) + |> should.equal(6) - let concat = fn(k, _, acc) { string.append(acc, k) } + let concat = fn(k, _, acc) { string.append(acc, k) } - dict - |> map.fold("", concat) - |> should.equal("abcd") + dict + |> map.fold("", concat) + |> should.equal("abcd") - map.from_list([]) - |> map.fold(0, add) - |> should.equal(0) + map.from_list([]) + |> map.fold(0, add) + |> should.equal(0) + } } diff --git a/test/gleam/option_test.gleam b/test/gleam/option_test.gleam index 35c9eb3..32b1df6 100644 --- a/test/gleam/option_test.gleam +++ b/test/gleam/option_test.gleam @@ -1,115 +1,117 @@ -import gleam/should -import gleam/option.{None, Some} - -pub fn all_test() { - option.all([Some(1), Some(2), Some(3)]) - |> should.equal(Some([1, 2, 3])) - - option.all([Some(1), None, Some(3)]) - |> should.equal(None) -} - -pub fn is_some_test() { - option.is_some(Some(1)) - |> should.be_true - - option.is_some(None) - |> should.be_false -} - -pub fn is_none_test() { - option.is_none(Some(1)) - |> should.be_false - - option.is_none(None) - |> should.be_true -} - -pub fn to_result_test() { - option.to_result(Some(1), "possible_error") - |> should.equal(Ok(1)) - - option.to_result(None, "possible_error") - |> should.equal(Error("possible_error")) -} - -pub fn from_result_test() { - option.from_result(Ok(1)) - |> should.equal(Some(1)) - - option.from_result(Error("some_error")) - |> should.equal(None) -} - -pub fn unwrap_option_test() { - option.unwrap(Some(1), 0) - |> should.equal(1) - - option.unwrap(None, 0) - |> should.equal(0) -} - -pub fn map_option_test() { - Some(1) - |> option.map(fn(x) { x + 1 }) - |> should.equal(Some(2)) - - Some(1) - |> option.map(fn(_) { "2" }) - |> should.equal(Some("2")) - - None - |> option.map(fn(x) { x + 1 }) - |> should.equal(None) -} - -pub fn flatten_option_test() { - Some(Some(1)) - |> option.flatten() - |> should.equal(Some(1)) - - Some(None) - |> option.flatten() - |> should.equal(None) - - None - |> option.flatten() - |> should.equal(None) -} - -pub fn then_option_test() { - Some(1) - |> option.then(fn(x) { Some(x + 1) }) - |> should.equal(Some(2)) - - Some(1) - |> option.then(fn(_) { Some("2") }) - |> should.equal(Some("2")) - - None - |> option.then(fn(x) { Some(x + 1) }) - |> should.equal(None) -} - -pub fn or_option_test() { - Some(1) - |> option.or(Some(2)) - |> should.equal(Some(1)) - - Some(1) - |> option.or(None) - |> should.equal(Some(1)) - - None - |> option.or(Some(2)) - |> should.equal(Some(2)) - - None - |> option.or(None) - |> should.equal(None) -} - -pub fn values_test() { - option.values([Some(1), None, Some(3)]) - |> should.equal([1, 3]) +if erlang { + import gleam/should + import gleam/option.{None, Some} + + pub fn all_test() { + option.all([Some(1), Some(2), Some(3)]) + |> should.equal(Some([1, 2, 3])) + + option.all([Some(1), None, Some(3)]) + |> should.equal(None) + } + + pub fn is_some_test() { + option.is_some(Some(1)) + |> should.be_true + + option.is_some(None) + |> should.be_false + } + + pub fn is_none_test() { + option.is_none(Some(1)) + |> should.be_false + + option.is_none(None) + |> should.be_true + } + + pub fn to_result_test() { + option.to_result(Some(1), "possible_error") + |> should.equal(Ok(1)) + + option.to_result(None, "possible_error") + |> should.equal(Error("possible_error")) + } + + pub fn from_result_test() { + option.from_result(Ok(1)) + |> should.equal(Some(1)) + + option.from_result(Error("some_error")) + |> should.equal(None) + } + + pub fn unwrap_option_test() { + option.unwrap(Some(1), 0) + |> should.equal(1) + + option.unwrap(None, 0) + |> should.equal(0) + } + + pub fn map_option_test() { + Some(1) + |> option.map(fn(x) { x + 1 }) + |> should.equal(Some(2)) + + Some(1) + |> option.map(fn(_) { "2" }) + |> should.equal(Some("2")) + + None + |> option.map(fn(x) { x + 1 }) + |> should.equal(None) + } + + pub fn flatten_option_test() { + Some(Some(1)) + |> option.flatten() + |> should.equal(Some(1)) + + Some(None) + |> option.flatten() + |> should.equal(None) + + None + |> option.flatten() + |> should.equal(None) + } + + pub fn then_option_test() { + Some(1) + |> option.then(fn(x) { Some(x + 1) }) + |> should.equal(Some(2)) + + Some(1) + |> option.then(fn(_) { Some("2") }) + |> should.equal(Some("2")) + + None + |> option.then(fn(x) { Some(x + 1) }) + |> should.equal(None) + } + + pub fn or_option_test() { + Some(1) + |> option.or(Some(2)) + |> should.equal(Some(1)) + + Some(1) + |> option.or(None) + |> should.equal(Some(1)) + + None + |> option.or(Some(2)) + |> should.equal(Some(2)) + + None + |> option.or(None) + |> should.equal(None) + } + + pub fn values_test() { + option.values([Some(1), None, Some(3)]) + |> should.equal([1, 3]) + } } diff --git a/test/gleam/order_test.gleam b/test/gleam/order_test.gleam index 2517f9c..95cd6e8 100644 --- a/test/gleam/order_test.gleam +++ b/test/gleam/order_test.gleam @@ -1,111 +1,113 @@ -import gleam/should -import gleam/order.{Eq, Gt, Lt} +if erlang { + import gleam/should + import gleam/order.{Eq, Gt, Lt} -pub fn reverse_test() { - order.reverse(Lt) - |> should.equal(Gt) + pub fn reverse_test() { + order.reverse(Lt) + |> should.equal(Gt) - order.reverse(Eq) - |> should.equal(Eq) + order.reverse(Eq) + |> should.equal(Eq) - order.reverse(Gt) - |> should.equal(Lt) -} + order.reverse(Gt) + |> should.equal(Lt) + } -pub fn to_int_test() { - order.to_int(Lt) - |> should.equal(-1) + pub fn to_int_test() { + order.to_int(Lt) + |> should.equal(-1) - order.to_int(Eq) - |> should.equal(0) + order.to_int(Eq) + |> should.equal(0) - order.to_int(Gt) - |> should.equal(1) -} + order.to_int(Gt) + |> should.equal(1) + } -pub fn compare_test() { - order.compare(Lt, Lt) - |> should.equal(Eq) + pub fn compare_test() { + order.compare(Lt, Lt) + |> should.equal(Eq) - order.compare(Lt, Eq) - |> should.equal(Lt) + order.compare(Lt, Eq) + |> should.equal(Lt) - order.compare(Lt, Gt) - |> should.equal(Lt) + order.compare(Lt, Gt) + |> should.equal(Lt) - order.compare(Eq, Lt) - |> should.equal(Gt) + order.compare(Eq, Lt) + |> should.equal(Gt) - order.compare(Eq, Eq) - |> should.equal(Eq) + order.compare(Eq, Eq) + |> should.equal(Eq) - order.compare(Eq, Gt) - |> should.equal(Lt) + order.compare(Eq, Gt) + |> should.equal(Lt) - order.compare(Gt, Lt) - |> should.equal(Gt) + order.compare(Gt, Lt) + |> should.equal(Gt) - order.compare(Gt, Eq) - |> should.equal(Gt) + order.compare(Gt, Eq) + |> should.equal(Gt) - order.compare(Gt, Gt) - |> should.equal(Eq) -} + order.compare(Gt, Gt) + |> should.equal(Eq) + } -pub fn max_test() { - order.max(Lt, Lt) - |> should.equal(Lt) + pub fn max_test() { + order.max(Lt, Lt) + |> should.equal(Lt) - order.max(Lt, Eq) - |> should.equal(Eq) + order.max(Lt, Eq) + |> should.equal(Eq) - order.max(Lt, Gt) - |> should.equal(Gt) + order.max(Lt, Gt) + |> should.equal(Gt) - order.max(Eq, Lt) - |> should.equal(Eq) + order.max(Eq, Lt) + |> should.equal(Eq) - order.max(Eq, Eq) - |> should.equal(Eq) + order.max(Eq, Eq) + |> should.equal(Eq) - order.max(Eq, Gt) - |> should.equal(Gt) + order.max(Eq, Gt) + |> should.equal(Gt) - order.max(Gt, Lt) - |> should.equal(Gt) + order.max(Gt, Lt) + |> should.equal(Gt) - order.max(Gt, Eq) - |> should.equal(Gt) + order.max(Gt, Eq) + |> should.equal(Gt) - order.max(Gt, Gt) - |> should.equal(Gt) -} + order.max(Gt, Gt) + |> should.equal(Gt) + } -pub fn min_test() { - order.min(Lt, Lt) - |> should.equal(Lt) + pub fn min_test() { + order.min(Lt, Lt) + |> should.equal(Lt) - order.min(Lt, Eq) - |> should.equal(Lt) + order.min(Lt, Eq) + |> should.equal(Lt) - order.min(Lt, Gt) - |> should.equal(Lt) + order.min(Lt, Gt) + |> should.equal(Lt) - order.min(Eq, Lt) - |> should.equal(Lt) + order.min(Eq, Lt) + |> should.equal(Lt) - order.min(Eq, Eq) - |> should.equal(Eq) + order.min(Eq, Eq) + |> should.equal(Eq) - order.min(Eq, Gt) - |> should.equal(Eq) + order.min(Eq, Gt) + |> should.equal(Eq) - order.min(Gt, Lt) - |> should.equal(Lt) + order.min(Gt, Lt) + |> should.equal(Lt) - order.min(Gt, Eq) - |> should.equal(Eq) + order.min(Gt, Eq) + |> should.equal(Eq) - order.min(Gt, Gt) - |> should.equal(Gt) + order.min(Gt, Gt) + |> should.equal(Gt) + } } diff --git a/test/gleam/os_test.gleam b/test/gleam/os_test.gleam index 3442a90..b3d799d 100644 --- a/test/gleam/os_test.gleam +++ b/test/gleam/os_test.gleam @@ -1,47 +1,49 @@ -import gleam/map -import gleam/os -import gleam/io -import gleam/should +if erlang { + import gleam/map + import gleam/os + import gleam/io + import gleam/should -pub fn env_test() { - os.insert_env("GLEAM_TEST", "hello") - os.get_env() - |> map.get("GLEAM_TEST") - |> should.equal(Ok("hello")) + pub fn env_test() { + os.insert_env("GLEAM_TEST", "hello") + os.get_env() + |> map.get("GLEAM_TEST") + |> should.equal(Ok("hello")) - os.delete_env("GLEAM_TEST") - os.get_env() - |> map.get("GLEAM_TEST") - |> should.equal(Error(Nil)) -} + os.delete_env("GLEAM_TEST") + os.get_env() + |> map.get("GLEAM_TEST") + |> should.equal(Error(Nil)) + } -pub fn unicode_test() { - os.insert_env("GLEAM_UNICODE_TEST", "Iñtërnâtiônà£ißætiøn☃💩") - os.get_env() - |> map.get("GLEAM_UNICODE_TEST") - |> should.equal(Ok("Iñtërnâtiônà£ißætiøn☃💩")) -} + pub fn unicode_test() { + os.insert_env("GLEAM_UNICODE_TEST", "Iñtërnâtiônà£ißætiøn☃💩") + os.get_env() + |> map.get("GLEAM_UNICODE_TEST") + |> should.equal(Ok("Iñtërnâtiônà£ißætiøn☃💩")) + } -pub fn system_time_test() { - let june_12_2020 = 1591966971 - { os.system_time(os.Second) > june_12_2020 } - |> should.equal(True) - { os.system_time(os.Second) < june_12_2020 * 1000 } - |> should.equal(True) - { os.system_time(os.Millisecond) > june_12_2020 * 1000 } - |> should.equal(True) - { os.system_time(os.Millisecond) < june_12_2020 * 1000000 } - |> should.equal(True) -} + pub fn system_time_test() { + let june_12_2020 = 1591966971 + { os.system_time(os.Second) > june_12_2020 } + |> should.equal(True) + { os.system_time(os.Second) < june_12_2020 * 1000 } + |> should.equal(True) + { os.system_time(os.Millisecond) > june_12_2020 * 1000 } + |> should.equal(True) + { os.system_time(os.Millisecond) < june_12_2020 * 1000000 } + |> should.equal(True) + } -pub fn erlang_timestamp_test() { - // in microseconds - let june_12_2020 = 1591966971000000 - let #(mega_seconds, seconds, micro_seconds) = os.erlang_timestamp() + pub fn erlang_timestamp_test() { + // in microseconds + let june_12_2020 = 1591966971000000 + let #(mega_seconds, seconds, micro_seconds) = os.erlang_timestamp() - let stamp_as_micro = - { mega_seconds * 1_000_000 + seconds } * 1_000_000 + micro_seconds + let stamp_as_micro = + { mega_seconds * 1_000_000 + seconds } * 1_000_000 + micro_seconds - { stamp_as_micro > june_12_2020 } - |> should.be_true() + { stamp_as_micro > june_12_2020 } + |> should.be_true() + } } diff --git a/test/gleam/pair_test.gleam b/test/gleam/pair_test.gleam index 1950929..891ce4a 100644 --- a/test/gleam/pair_test.gleam +++ b/test/gleam/pair_test.gleam @@ -1,58 +1,60 @@ -import gleam/should -import gleam/pair - -pub fn first_test() { - #(1, 2) - |> pair.first - |> should.equal(1) - - #("abc", []) - |> pair.first - |> should.equal("abc") -} - -pub fn second_test() { - #(1, 2) - |> pair.second - |> should.equal(2) - - #("abc", []) - |> pair.second - |> should.equal([]) -} - -pub fn swap_test() { - #(1, "2") - |> pair.swap - |> should.equal(#("2", 1)) -} - -pub fn map_first_test() { - let inc = fn(a) { a + 1 } - pair.map_first(#(1, 2), inc) - |> should.equal(#(2, 2)) - - pair.map_first(#(8, 2), inc) - |> should.equal(#(9, 2)) - - pair.map_first(#(0, -2), inc) - |> should.equal(#(1, -2)) - - pair.map_first(#(-10, 20), inc) - |> should.equal(#(-9, 20)) -} - -pub fn map_second_test() { - let dec = fn(a) { a - 1 } - pair.map_second(#(1, 2), dec) - |> should.equal(#(1, 1)) - - pair.map_second(#(8, 2), dec) - |> should.equal(#(8, 1)) - - pair.map_second(#(0, -2), dec) - |> should.equal(#(0, -3)) - - pair.map_second(#(-10, 20), dec) - |> should.equal(#(-10, 19)) +if erlang { + import gleam/should + import gleam/pair + + pub fn first_test() { + #(1, 2) + |> pair.first + |> should.equal(1) + + #("abc", []) + |> pair.first + |> should.equal("abc") + } + + pub fn second_test() { + #(1, 2) + |> pair.second + |> should.equal(2) + + #("abc", []) + |> pair.second + |> should.equal([]) + } + + pub fn swap_test() { + #(1, "2") + |> pair.swap + |> should.equal(#("2", 1)) + } + + pub fn map_first_test() { + let inc = fn(a) { a + 1 } + pair.map_first(#(1, 2), inc) + |> should.equal(#(2, 2)) + + pair.map_first(#(8, 2), inc) + |> should.equal(#(9, 2)) + + pair.map_first(#(0, -2), inc) + |> should.equal(#(1, -2)) + + pair.map_first(#(-10, 20), inc) + |> should.equal(#(-9, 20)) + } + + pub fn map_second_test() { + let dec = fn(a) { a - 1 } + pair.map_second(#(1, 2), dec) + |> should.equal(#(1, 1)) + + pair.map_second(#(8, 2), dec) + |> should.equal(#(8, 1)) + + pair.map_second(#(0, -2), dec) + |> should.equal(#(0, -3)) + + pair.map_second(#(-10, 20), dec) + |> should.equal(#(-10, 19)) + } } diff --git a/test/gleam/queue_test.gleam b/test/gleam/queue_test.gleam index ae5dfeb..24c70b1 100644 --- a/test/gleam/queue_test.gleam +++ b/test/gleam/queue_test.gleam @@ -1,332 +1,334 @@ -import gleam/queue -import gleam/int -import gleam/list -import gleam/should -import gleam/pair - -pub fn from_and_to_list_test() { - queue.from_list([]) - |> should.equal(queue.new()) - - [1, 2, 3] - |> queue.from_list - |> queue.to_list - |> should.equal([1, 2, 3]) -} - -pub fn is_empty_test() { - queue.new() - |> queue.is_empty - |> should.be_true +if erlang { + import gleam/queue + import gleam/int + import gleam/list + import gleam/should + import gleam/pair - queue.from_list([""]) - |> queue.is_empty - |> should.be_false -} + pub fn from_and_to_list_test() { + queue.from_list([]) + |> should.equal(queue.new()) -pub fn length_test() { - let test = fn(input) { - queue.from_list(input) - |> queue.length - |> should.equal(list.length(input)) + [1, 2, 3] + |> queue.from_list + |> queue.to_list + |> should.equal([1, 2, 3]) } - test([]) - test([1]) - test([1, 2]) - test([1, 2, 1]) - test([1, 2, 1, 5, 2, 7, 2, 7, 8, 4, 545]) -} - -pub fn push_back_test() { - [1, 2] - |> queue.from_list - |> queue.push_back(3) - |> queue.to_list - |> should.equal([1, 2, 3]) - - queue.new() - |> queue.push_back(1) - |> queue.push_back(2) - |> queue.push_back(3) - |> queue.to_list - |> should.equal([1, 2, 3]) -} + pub fn is_empty_test() { + queue.new() + |> queue.is_empty + |> should.be_true -pub fn push_front_test() { - [2, 3] - |> queue.from_list - |> queue.push_front(1) - |> queue.push_front(0) - |> queue.to_list - |> should.equal([0, 1, 2, 3]) -} + queue.from_list([""]) + |> queue.is_empty + |> should.be_false + } -pub fn push_test() { - queue.new() - |> queue.push_front("b") - |> queue.push_back("x") - |> queue.push_front("a") - |> queue.push_back("y") - |> queue.to_list - |> should.equal(["a", "b", "x", "y"]) -} + pub fn length_test() { + let test = fn(input) { + queue.from_list(input) + |> queue.length + |> should.equal(list.length(input)) + } + + test([]) + test([1]) + test([1, 2]) + test([1, 2, 1]) + test([1, 2, 1, 5, 2, 7, 2, 7, 8, 4, 545]) + } -pub fn pop_back_test() { - assert Ok(tup) = - [1, 2, 3] + pub fn push_back_test() { + [1, 2] |> queue.from_list - |> queue.pop_back - - tup - |> pair.first - |> should.equal(3) - - tup - |> pair.second - |> queue.is_equal(queue.from_list([1, 2])) - |> should.be_true -} + |> queue.push_back(3) + |> queue.to_list + |> should.equal([1, 2, 3]) -pub fn pop_back_after_push_back_test() { - assert Ok(tup) = queue.new() |> queue.push_back(1) |> queue.push_back(2) |> queue.push_back(3) - |> queue.pop_back + |> queue.to_list + |> should.equal([1, 2, 3]) + } - tup - |> pair.first - |> should.equal(3) -} + pub fn push_front_test() { + [2, 3] + |> queue.from_list + |> queue.push_front(1) + |> queue.push_front(0) + |> queue.to_list + |> should.equal([0, 1, 2, 3]) + } -pub fn pop_back_after_push_test() { - assert Ok(tup) = + pub fn push_test() { queue.new() |> queue.push_front("b") |> queue.push_back("x") |> queue.push_front("a") |> queue.push_back("y") - |> queue.pop_back + |> queue.to_list + |> should.equal(["a", "b", "x", "y"]) + } - tup - |> pair.first - |> should.equal("y") -} + pub fn pop_back_test() { + assert Ok(tup) = + [1, 2, 3] + |> queue.from_list + |> queue.pop_back -pub fn pop_back_empty_test() { - queue.from_list([]) - |> queue.pop_back - |> should.equal(Error(Nil)) -} + tup + |> pair.first + |> should.equal(3) -pub fn pop_front_test() { - assert Ok(tup) = - [1, 2, 3] - |> queue.from_list - |> queue.pop_front + tup + |> pair.second + |> queue.is_equal(queue.from_list([1, 2])) + |> should.be_true + } - tup - |> pair.first - |> should.equal(1) + pub fn pop_back_after_push_back_test() { + assert Ok(tup) = + queue.new() + |> queue.push_back(1) + |> queue.push_back(2) + |> queue.push_back(3) + |> queue.pop_back + + tup + |> pair.first + |> should.equal(3) + } - tup - |> pair.second - |> queue.is_equal(queue.from_list([2, 3])) - |> should.be_true -} + pub fn pop_back_after_push_test() { + assert Ok(tup) = + queue.new() + |> queue.push_front("b") + |> queue.push_back("x") + |> queue.push_front("a") + |> queue.push_back("y") + |> queue.pop_back + + tup + |> pair.first + |> should.equal("y") + } -pub fn pop_front_after_push_front_test() { - assert Ok(tup) = - queue.new() - |> queue.push_front(3) - |> queue.push_front(2) - |> queue.push_front(1) - |> queue.pop_front + pub fn pop_back_empty_test() { + queue.from_list([]) + |> queue.pop_back + |> should.equal(Error(Nil)) + } - tup - |> pair.first - |> should.equal(1) -} + pub fn pop_front_test() { + assert Ok(tup) = + [1, 2, 3] + |> queue.from_list + |> queue.pop_front -pub fn pop_front_after_push_test() { - assert Ok(tup) = - queue.new() - |> queue.push_front("b") - |> queue.push_back("x") - |> queue.push_front("a") - |> queue.pop_front + tup + |> pair.first + |> should.equal(1) - tup - |> pair.first - |> should.equal("a") -} - -pub fn pop_front_empty_test() { - queue.from_list([]) - |> queue.pop_front - |> should.equal(Error(Nil)) -} + tup + |> pair.second + |> queue.is_equal(queue.from_list([2, 3])) + |> should.be_true + } -pub fn reverse_test() { - queue.from_list([1, 2, 3]) - |> queue.reverse - |> queue.to_list - |> should.equal([3, 2, 1]) - - queue.new() - |> queue.push_back(1) - |> queue.push_back(2) - |> queue.push_back(3) - |> queue.reverse - |> queue.to_list - |> should.equal([3, 2, 1]) - - queue.new() - |> queue.push_front(1) - |> queue.push_front(2) - |> queue.push_front(3) - |> queue.reverse - |> queue.to_list - |> should.equal([1, 2, 3]) - - queue.new() - |> queue.push_front(1) - |> queue.push_front(2) - |> queue.push_back(3) - |> queue.push_back(4) - |> queue.reverse - |> queue.to_list - |> should.equal([4, 3, 1, 2]) -} + pub fn pop_front_after_push_front_test() { + assert Ok(tup) = + queue.new() + |> queue.push_front(3) + |> queue.push_front(2) + |> queue.push_front(1) + |> queue.pop_front + + tup + |> pair.first + |> should.equal(1) + } -pub fn is_equal_test() { - let should_equal = fn(a, b) { - a - |> queue.is_equal(to: b) - |> should.be_true + pub fn pop_front_after_push_test() { + assert Ok(tup) = + queue.new() + |> queue.push_front("b") + |> queue.push_back("x") + |> queue.push_front("a") + |> queue.pop_front + + tup + |> pair.first + |> should.equal("a") } - let should_not_equal = fn(a, b) { - a - |> queue.is_equal(to: b) - |> should.be_false + pub fn pop_front_empty_test() { + queue.from_list([]) + |> queue.pop_front + |> should.equal(Error(Nil)) } - should_equal(queue.new(), queue.new()) + pub fn reverse_test() { + queue.from_list([1, 2, 3]) + |> queue.reverse + |> queue.to_list + |> should.equal([3, 2, 1]) - queue.new() - |> queue.push_front(1) - |> should_equal( queue.new() - |> queue.push_back(1), - ) + |> queue.push_back(1) + |> queue.push_back(2) + |> queue.push_back(3) + |> queue.reverse + |> queue.to_list + |> should.equal([3, 2, 1]) - queue.new() - |> queue.push_front(1) - |> should_equal( queue.new() - |> queue.push_front(1), - ) + |> queue.push_front(1) + |> queue.push_front(2) + |> queue.push_front(3) + |> queue.reverse + |> queue.to_list + |> should.equal([1, 2, 3]) - queue.new() - |> queue.push_back(1) - |> queue.push_back(2) - |> should_equal( queue.new() + |> queue.push_front(1) |> queue.push_front(2) - |> queue.push_front(1), - ) + |> queue.push_back(3) + |> queue.push_back(4) + |> queue.reverse + |> queue.to_list + |> should.equal([4, 3, 1, 2]) + } + + pub fn is_equal_test() { + let should_equal = fn(a, b) { + a + |> queue.is_equal(to: b) + |> should.be_true + } + + let should_not_equal = fn(a, b) { + a + |> queue.is_equal(to: b) + |> should.be_false + } + + should_equal(queue.new(), queue.new()) - queue.new() - |> queue.push_back(1) - |> should_not_equal( queue.new() - |> queue.push_front(2) - |> queue.push_front(1), - ) + |> queue.push_front(1) + |> should_equal( + queue.new() + |> queue.push_back(1), + ) - queue.new() - |> queue.push_back(2) - |> queue.push_back(1) - |> should_not_equal( queue.new() - |> queue.push_front(2) - |> queue.push_front(1), - ) -} + |> queue.push_front(1) + |> should_equal( + queue.new() + |> queue.push_front(1), + ) -pub fn is_logically_equal_test() { - let both_even_or_odd = fn(a, b) { int.is_even(a) == int.is_even(b) } + queue.new() + |> queue.push_back(1) + |> queue.push_back(2) + |> should_equal( + queue.new() + |> queue.push_front(2) + |> queue.push_front(1), + ) - let should_equal = fn(a, b) { - a - |> queue.is_logically_equal(to: b, checking: both_even_or_odd) - |> should.be_true - } + queue.new() + |> queue.push_back(1) + |> should_not_equal( + queue.new() + |> queue.push_front(2) + |> queue.push_front(1), + ) - let should_not_equal = fn(a, b) { - a - |> queue.is_logically_equal(to: b, checking: both_even_or_odd) - |> should.be_false + queue.new() + |> queue.push_back(2) + |> queue.push_back(1) + |> should_not_equal( + queue.new() + |> queue.push_front(2) + |> queue.push_front(1), + ) } - should_equal(queue.new(), queue.new()) + pub fn is_logically_equal_test() { + let both_even_or_odd = fn(a, b) { int.is_even(a) == int.is_even(b) } + + let should_equal = fn(a, b) { + a + |> queue.is_logically_equal(to: b, checking: both_even_or_odd) + |> should.be_true + } + + let should_not_equal = fn(a, b) { + a + |> queue.is_logically_equal(to: b, checking: both_even_or_odd) + |> should.be_false + } + + should_equal(queue.new(), queue.new()) - queue.new() - |> queue.push_front(3) - |> should_equal( queue.new() - |> queue.push_back(1), - ) + |> queue.push_front(3) + |> should_equal( + queue.new() + |> queue.push_back(1), + ) - queue.new() - |> queue.push_front(4) - |> should_equal( queue.new() - |> queue.push_back(2), - ) + |> queue.push_front(4) + |> should_equal( + queue.new() + |> queue.push_back(2), + ) - queue.new() - |> queue.push_front(3) - |> should_equal( queue.new() - |> queue.push_front(1), - ) + |> queue.push_front(3) + |> should_equal( + queue.new() + |> queue.push_front(1), + ) - queue.new() - |> queue.push_back(3) - |> queue.push_back(4) - |> should_equal( queue.new() - |> queue.push_front(2) - |> queue.push_front(1), - ) + |> queue.push_back(3) + |> queue.push_back(4) + |> should_equal( + queue.new() + |> queue.push_front(2) + |> queue.push_front(1), + ) - queue.new() - |> queue.push_back(1) - |> should_not_equal( queue.new() - |> queue.push_front(2) - |> queue.push_front(1), - ) + |> queue.push_back(1) + |> should_not_equal( + queue.new() + |> queue.push_front(2) + |> queue.push_front(1), + ) - queue.new() - |> queue.push_back(2) - |> queue.push_back(1) - |> should_not_equal( queue.new() - |> queue.push_front(2) - |> queue.push_front(1), - ) + |> queue.push_back(2) + |> queue.push_back(1) + |> should_not_equal( + queue.new() + |> queue.push_front(2) + |> queue.push_front(1), + ) - queue.new() - |> queue.push_back(4) - |> queue.push_back(3) - |> should_not_equal( queue.new() - |> queue.push_front(2) - |> queue.push_front(1), - ) + |> queue.push_back(4) + |> queue.push_back(3) + |> should_not_equal( + queue.new() + |> queue.push_front(2) + |> queue.push_front(1), + ) + } } diff --git a/test/gleam/regex_test.gleam b/test/gleam/regex_test.gleam index 1b45c2d..f6410e4 100644 --- a/test/gleam/regex_test.gleam +++ b/test/gleam/regex_test.gleam @@ -1,78 +1,84 @@ -import gleam/io -import gleam/option.{None, Some} -import gleam/regex.{CompileError, Match, Options} -import gleam/should +if erlang { + import gleam/io + import gleam/option.{None, Some} + import gleam/regex.{CompileError, Match, Options} + import gleam/should -pub fn from_string_test() { - assert Ok(re) = regex.from_string("[0-9]") + pub fn from_string_test() { + assert Ok(re) = regex.from_string("[0-9]") - regex.check(re, "abc123") - |> should.equal(True) + regex.check(re, "abc123") + |> should.equal(True) - regex.check(re, "abcxyz") - |> should.equal(False) + regex.check(re, "abcxyz") + |> should.equal(False) - assert Error(from_string_err) = regex.from_string("[0-9") + assert Error(from_string_err) = regex.from_string("[0-9") - from_string_err - |> should.equal(CompileError( - error: "missing terminating ] for character class", - byte_index: 4, - )) -} + from_string_err + |> should.equal(CompileError( + error: "missing terminating ] for character class", + byte_index: 4, + )) + } -pub fn compile_test() { - let options = Options(case_insensitive: True, multi_line: False) - assert Ok(re) = regex.compile("[A-B]", options) + pub fn compile_test() { + let options = Options(case_insensitive: True, multi_line: False) + assert Ok(re) = regex.compile("[A-B]", options) - regex.check(re, "abc123") - |> should.equal(True) + regex.check(re, "abc123") + |> should.equal(True) - let options = Options(case_insensitive: False, multi_line: True) - assert Ok(re) = regex.compile("^[0-9]", options) + let options = Options(case_insensitive: False, multi_line: True) + assert Ok(re) = regex.compile("^[0-9]", options) - regex.check(re, "abc\n123") - |> should.equal(True) -} + regex.check(re, "abc\n123") + |> should.equal(True) + } -pub fn check_test() { - assert Ok(re) = regex.from_string("^f.o.?") + pub fn check_test() { + assert Ok(re) = regex.from_string("^f.o.?") - regex.check(re, "foo") - |> should.equal(True) + regex.check(re, "foo") + |> should.equal(True) - regex.check(re, "boo") - |> should.equal(False) -} + regex.check(re, "boo") + |> should.equal(False) + } -pub fn split_test() { - assert Ok(re) = regex.from_string(" *, *") + pub fn split_test() { + assert Ok(re) = regex.from_string(" *, *") - regex.split(re, "foo,32, 4, 9 ,0") - |> should.equal(["foo", "32", "4", "9", "0"]) -} + regex.split(re, "foo,32, 4, 9 ,0") + |> should.equal(["foo", "32", "4", "9", "0"]) + } -pub fn scan_test() { - assert Ok(re) = regex.from_string("Gl\\w+") + pub fn scan_test() { + assert Ok(re) = regex.from_string("Gl\\w+") - regex.scan(re, "!Gleam") - |> should.equal([Match(content: "Gleam", byte_index: 1, submatches: [])]) + regex.scan(re, "!Gleam") + |> should.equal([Match(content: "Gleam", byte_index: 1, submatches: [])]) - regex.scan(re, "हGleam") - |> should.equal([Match(content: "Gleam", byte_index: 3, submatches: [])]) + regex.scan(re, "हGleam") + |> should.equal([Match(content: "Gleam", byte_index: 3, submatches: [])]) - regex.scan(re, "𐍈Gleam") - |> should.equal([Match(content: "Gleam", byte_index: 4, submatches: [])]) + regex.scan(re, "𐍈Gleam") + |> should.equal([Match(content: "Gleam", byte_index: 4, submatches: [])]) - assert Ok(re) = regex.from_string("[oi]n a(.?) (\\w+)") + assert Ok(re) = regex.from_string("[oi]n a(.?) (\\w+)") - regex.scan(re, "I am on a boat in a lake.") - |> should.equal([ - Match(content: "on a boat", byte_index: 5, submatches: [None, Some("boat")]), - Match( - content: "in a lake", - byte_index: 15, - submatches: [None, Some("lake")], - ), - ]) + regex.scan(re, "I am on a boat in a lake.") + |> should.equal([ + Match( + content: "on a boat", + byte_index: 5, + submatches: [None, Some("boat")], + ), + Match( + content: "in a lake", + byte_index: 15, + submatches: [None, Some("lake")], + ), + ]) + } } diff --git a/test/gleam/result_test.gleam b/test/gleam/result_test.gleam index a2bd6ff..464aa2b 100644 --- a/test/gleam/result_test.gleam +++ b/test/gleam/result_test.gleam @@ -1,169 +1,171 @@ -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]) +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]) + } } diff --git a/test/gleam/set_test.gleam b/test/gleam/set_test.gleam index 871c0b6..593727c 100644 --- a/test/gleam/set_test.gleam +++ b/test/gleam/set_test.gleam @@ -1,93 +1,95 @@ -import gleam/should -import gleam/set -import gleam/list -import gleam/int +if erlang { + import gleam/should + import gleam/set + import gleam/list + import gleam/int -pub fn size_test() { - set.new() - |> set.size - |> should.equal(0) + pub fn size_test() { + set.new() + |> set.size + |> should.equal(0) - set.new() - |> set.insert(1) - |> set.insert(2) - |> set.size - |> should.equal(2) + set.new() + |> set.insert(1) + |> set.insert(2) + |> set.size + |> should.equal(2) - set.new() - |> set.insert(1) - |> set.insert(1) - |> set.insert(2) - |> set.size - |> should.equal(2) -} + set.new() + |> set.insert(1) + |> set.insert(1) + |> set.insert(2) + |> set.size + |> should.equal(2) + } -pub fn contains_test() { - set.new() - |> set.insert(1) - |> set.contains(this: 1) - |> should.be_true + pub fn contains_test() { + set.new() + |> set.insert(1) + |> set.contains(this: 1) + |> should.be_true - set.new() - |> set.contains(this: 1) - |> should.be_false -} + set.new() + |> set.contains(this: 1) + |> should.be_false + } -pub fn delete_test() { - set.new() - |> set.insert(1) - |> set.delete(1) - |> set.contains(1) - |> should.be_false -} + pub fn delete_test() { + set.new() + |> set.insert(1) + |> set.delete(1) + |> set.contains(1) + |> should.be_false + } -pub fn to_list_test() { - set.new() - |> set.insert(2) - |> set.insert(3) - |> set.insert(4) - |> set.to_list - |> list.sort(by: int.compare) - |> should.equal([2, 3, 4]) -} + pub fn to_list_test() { + set.new() + |> set.insert(2) + |> set.insert(3) + |> set.insert(4) + |> set.to_list + |> list.sort(by: int.compare) + |> should.equal([2, 3, 4]) + } -pub fn from_list_test() { - [1, 1, 2, 4, 3, 2] - |> set.from_list - |> set.to_list - |> list.sort(by: int.compare) - |> should.equal([1, 2, 3, 4]) -} + pub fn from_list_test() { + [1, 1, 2, 4, 3, 2] + |> set.from_list + |> set.to_list + |> list.sort(by: int.compare) + |> should.equal([1, 2, 3, 4]) + } -pub fn fold_test() { - [1, 3, 9] - |> set.from_list - |> set.fold(from: 0, with: fn(m, a) { m + a }) -} + pub fn fold_test() { + [1, 3, 9] + |> set.from_list + |> set.fold(from: 0, with: fn(m, a) { m + a }) + } -pub fn filter_test() { - [1, 4, 6, 3, 675, 44, 67] - |> set.from_list() - |> set.filter(for: int.is_even) - |> set.to_list - |> should.equal([4, 6, 44]) -} + pub fn filter_test() { + [1, 4, 6, 3, 675, 44, 67] + |> set.from_list() + |> set.filter(for: int.is_even) + |> set.to_list + |> should.equal([4, 6, 44]) + } -pub fn take_test() { - [1, 2, 3] - |> set.from_list - |> set.take([1, 3, 5]) - |> should.equal(set.from_list([1, 3])) -} + pub fn take_test() { + [1, 2, 3] + |> set.from_list + |> set.take([1, 3, 5]) + |> should.equal(set.from_list([1, 3])) + } -pub fn union_test() { - set.union(set.from_list([1, 2]), set.from_list([2, 3])) - |> set.to_list - |> should.equal([1, 2, 3]) -} + pub fn union_test() { + set.union(set.from_list([1, 2]), set.from_list([2, 3])) + |> set.to_list + |> should.equal([1, 2, 3]) + } -pub fn intersection_test() { - set.intersection(set.from_list([1, 2]), set.from_list([2, 3])) - |> set.to_list - |> should.equal([2]) + pub fn intersection_test() { + set.intersection(set.from_list([1, 2]), set.from_list([2, 3])) + |> set.to_list + |> should.equal([2]) + } } diff --git a/test/gleam/string_builder_test.gleam b/test/gleam/string_builder_test.gleam index fdbd048..663d3b9 100644 --- a/test/gleam/string_builder_test.gleam +++ b/test/gleam/string_builder_test.gleam @@ -1,102 +1,104 @@ -import gleam/should -import gleam/string_builder - -pub fn string_builder_test() { - let data = - string_builder.from_string("ello") - |> string_builder.append(",") - |> string_builder.append(" world!") - |> string_builder.prepend("H") - - data - |> string_builder.to_string - |> should.equal("Hello, world!") - - data - |> string_builder.byte_size - |> should.equal(13) - - let data = - string_builder.from_string("ello") - |> string_builder.append_builder(string_builder.from_string(",")) - |> string_builder.append_builder(string_builder.concat([ - string_builder.from_string(" wo"), - string_builder.from_string("rld!"), - ])) - |> string_builder.prepend_builder(string_builder.from_string("H")) - - data - |> string_builder.to_string - |> should.equal("Hello, world!") - - data - |> string_builder.byte_size - |> should.equal(13) -} - -pub fn lowercase_test() { - ["Gleam", "Gleam"] - |> string_builder.from_strings - |> string_builder.lowercase - |> string_builder.to_string - |> should.equal("gleamgleam") -} - -pub fn uppercase_test() { - ["Gleam", "Gleam"] - |> string_builder.from_strings - |> string_builder.uppercase - |> string_builder.to_string - |> should.equal("GLEAMGLEAM") -} - -pub fn split_test() { - "Gleam,Erlang,Elixir" - |> string_builder.from_string - |> string_builder.split(",") - |> should.equal([ - string_builder.from_string("Gleam"), - string_builder.from_string("Erlang"), - string_builder.from_string("Elixir"), - ]) - - ["Gleam, Erl", "ang,Elixir"] - |> string_builder.from_strings - |> string_builder.split(", ") - |> should.equal([ - string_builder.from_string("Gleam"), - string_builder.from_strings(["Erl", "ang,Elixir"]), - ]) -} - -pub fn is_equal_test() { - string_builder.from_string("12") - |> string_builder.is_equal(string_builder.from_strings(["1", "2"])) - |> should.be_true - - string_builder.from_string("12") - |> string_builder.is_equal(string_builder.from_string("12")) - |> should.be_true - - string_builder.from_string("12") - |> string_builder.is_equal(string_builder.from_string("2")) - |> should.be_false -} - -pub fn is_empty_test() { - string_builder.from_string("") - |> string_builder.is_empty - |> should.be_true - - string_builder.from_string("12") - |> string_builder.is_empty - |> should.be_false - - string_builder.from_strings([]) - |> string_builder.is_empty - |> should.be_true - - string_builder.from_strings(["", ""]) - |> string_builder.is_empty - |> should.be_true +if erlang { + import gleam/should + import gleam/string_builder + + pub fn string_builder_test() { + let data = + string_builder.from_string("ello") + |> string_builder.append(",") + |> string_builder.append(" world!") + |> string_builder.prepend("H") + + data + |> string_builder.to_string + |> should.equal("Hello, world!") + + data + |> string_builder.byte_size + |> should.equal(13) + + let data = + string_builder.from_string("ello") + |> string_builder.append_builder(string_builder.from_string(",")) + |> string_builder.append_builder(string_builder.concat([ + string_builder.from_string(" wo"), + string_builder.from_string("rld!"), + ])) + |> string_builder.prepend_builder(string_builder.from_string("H")) + + data + |> string_builder.to_string + |> should.equal("Hello, world!") + + data + |> string_builder.byte_size + |> should.equal(13) + } + + pub fn lowercase_test() { + ["Gleam", "Gleam"] + |> string_builder.from_strings + |> string_builder.lowercase + |> string_builder.to_string + |> should.equal("gleamgleam") + } + + pub fn uppercase_test() { + ["Gleam", "Gleam"] + |> string_builder.from_strings + |> string_builder.uppercase + |> string_builder.to_string + |> should.equal("GLEAMGLEAM") + } + + pub fn split_test() { + "Gleam,Erlang,Elixir" + |> string_builder.from_string + |> string_builder.split(",") + |> should.equal([ + string_builder.from_string("Gleam"), + string_builder.from_string("Erlang"), + string_builder.from_string("Elixir"), + ]) + + ["Gleam, Erl", "ang,Elixir"] + |> string_builder.from_strings + |> string_builder.split(", ") + |> should.equal([ + string_builder.from_string("Gleam"), + string_builder.from_strings(["Erl", "ang,Elixir"]), + ]) + } + + pub fn is_equal_test() { + string_builder.from_string("12") + |> string_builder.is_equal(string_builder.from_strings(["1", "2"])) + |> should.be_true + + string_builder.from_string("12") + |> string_builder.is_equal(string_builder.from_string("12")) + |> should.be_true + + string_builder.from_string("12") + |> string_builder.is_equal(string_builder.from_string("2")) + |> should.be_false + } + + pub fn is_empty_test() { + string_builder.from_string("") + |> string_builder.is_empty + |> should.be_true + + string_builder.from_string("12") + |> string_builder.is_empty + |> should.be_false + + string_builder.from_strings([]) + |> string_builder.is_empty + |> should.be_true + + string_builder.from_strings(["", ""]) + |> string_builder.is_empty + |> should.be_true + } } diff --git a/test/gleam/string_test.gleam b/test/gleam/string_test.gleam index 805f3ae..085e2f9 100644 --- a/test/gleam/string_test.gleam +++ b/test/gleam/string_test.gleam @@ -1,332 +1,334 @@ -import gleam/string -import gleam/should -import gleam/order - -pub fn length_test() { - string.length("ß↑e̊") - |> should.equal(3) - - string.length("Gleam") - |> should.equal(5) - - string.length("") - |> should.equal(0) -} - -pub fn lowercase_test() { - string.lowercase("Gleam") - |> should.equal("gleam") -} - -pub fn uppercase_test() { - string.uppercase("Gleam") - |> should.equal("GLEAM") -} - -pub fn reverse_test() { - string.reverse("Gleam") - |> should.equal("maelG") -} - -pub fn split_test() { - "Gleam,Erlang,Elixir" - |> string.split(",") - |> should.equal(["Gleam", "Erlang", "Elixir"]) - - "Gleam, Erlang,Elixir" - |> string.split(", ") - |> should.equal(["Gleam", "Erlang,Elixir"]) -} - -pub fn split_once_test() { - "Gleam,Erlang,Elixir" - |> string.split_once(",") - |> should.equal(Ok(#("Gleam", "Erlang,Elixir"))) - - "Gleam" - |> string.split_once(",") - |> should.equal(Error(Nil)) - - "" - |> string.split_once(",") - |> should.equal(Error(Nil)) -} - -pub fn replace_test() { - "Gleam,Erlang,Elixir" - |> string.replace(",", "++") - |> should.equal("Gleam++Erlang++Elixir") -} - -pub fn append_test() { - "Test" - |> string.append(" Me") - |> should.equal("Test Me") -} - -pub fn compare_test() { - string.compare("", "") - |> should.equal(order.Eq) - - string.compare("a", "") - |> should.equal(order.Gt) - - string.compare("a", "A") - |> should.equal(order.Gt) - - string.compare("A", "B") - |> should.equal(order.Lt) - - string.compare("t", "ABC") - |> should.equal(order.Gt) -} - -pub fn contains_test() { - "gleam" - |> string.contains("ea") - |> should.equal(True) - - "gleam" - |> string.contains("x") - |> should.equal(False) - - string.contains(does: "bellwether", contain: "bell") - |> should.equal(True) -} - -pub fn concat_test() { - ["Hello", ", ", "world!"] - |> string.concat - |> should.equal("Hello, world!") -} - -pub fn repeat_test() { - "hi" - |> string.repeat(times: 3) - |> should.equal("hihihi") - - "hi" - |> string.repeat(0) - |> should.equal("") - - "hi" - |> string.repeat(-1) - |> should.equal("") -} - -pub fn join_test() { - ["Hello", "world!"] - |> string.join(with: ", ") - |> should.equal("Hello, world!") - - ["Hello", "world!"] - |> string.join(with: "-") - |> should.equal("Hello-world!") -} - -pub fn trim_test() { - " hats \n" - |> string.trim() - |> should.equal("hats") -} - -pub fn trim_left_test() { - " hats \n" - |> string.trim_left() - |> should.equal("hats \n") -} - -pub fn trim_right_test() { - " hats \n" - |> string.trim_right() - |> should.equal(" hats") -} - -pub fn starts_with_test() { - "theory" - |> string.starts_with("") - |> should.equal(True) - - "theory" - |> string.starts_with("the") - |> should.equal(True) - - "theory" - |> string.starts_with("ory") - |> should.equal(False) - - "theory" - |> string.starts_with("theory2") - |> should.equal(False) -} - -pub fn ends_with_test() { - "theory" - |> string.ends_with("") - |> should.equal(True) - - "theory" - |> string.ends_with("ory") - |> should.equal(True) - - "theory" - |> string.ends_with("the") - |> should.equal(False) - - "theory" - |> string.ends_with("theory2") - |> should.equal(False) -} - -pub fn slice_test() { - "gleam" - |> string.slice(at_index: 1, length: 2) - |> should.equal("le") - - "gleam" - |> string.slice(at_index: 1, length: 10) - |> should.equal("leam") - - "gleam" - |> string.slice(at_index: 10, length: 3) - |> should.equal("") - - "gleam" - |> string.slice(at_index: -2, length: 2) - |> should.equal("am") - - "gleam" - |> string.slice(at_index: -12, length: 2) - |> should.equal("") - - "gleam" - |> string.slice(at_index: 2, length: -3) - |> should.equal("") -} - -pub fn crop_test() { - "gleam" - |> string.crop("gl") - |> should.equal("gleam") - - "gleam" - |> string.crop("le") - |> should.equal("leam") - - string.crop(from: "gleam", before: "ea") - |> should.equal("eam") - - "gleam" - |> string.crop("") - |> should.equal("gleam") - - "gleam" - |> string.crop("!") - |> should.equal("gleam") -} - -pub fn drop_left_test() { - "gleam" - |> string.drop_left(up_to: 2) - |> should.equal("eam") - - "gleam" - |> string.drop_left(up_to: 6) - |> should.equal("") - - "gleam" - |> string.drop_left(up_to: -2) - |> should.equal("gleam") -} - -pub fn drop_right_test() { - "gleam" - |> string.drop_right(up_to: 2) - |> should.equal("gle") - - "gleam" - |> string.drop_right(up_to: 5) - |> should.equal("") - - "gleam" - |> string.drop_right(up_to: -2) - |> should.equal("gleam") -} - -pub fn pad_left_test() { - "121" - |> string.pad_left(to: 5, with: ".") - |> should.equal("..121") - - "121" - |> string.pad_left(to: 3, with: ".") - |> should.equal("121") - - "121" - |> string.pad_left(to: 2, with: ".") - |> should.equal("121") - - "121" - |> string.pad_left(to: 5, with: "XY") - |> should.equal("XYXY121") -} - -pub fn pad_right_test() { - "121" - |> string.pad_right(to: 5, with: ".") - |> should.equal("121..") - - "121" - |> string.pad_right(to: 3, with: ".") - |> should.equal("121") - - "121" - |> string.pad_right(to: 2, with: ".") - |> should.equal("121") - - "121" - |> string.pad_right(to: 5, with: "XY") - |> should.equal("121XYXY") -} - -pub fn pop_grapheme_test() { - "gleam" - |> string.pop_grapheme() - |> should.equal(Ok(#("g", "leam"))) - - "g" - |> string.pop_grapheme() - |> should.equal(Ok(#("g", ""))) - - "" - |> string.pop_grapheme() - |> should.equal(Error(Nil)) -} - -pub fn to_graphemes_test() { - "abc" - |> string.to_graphemes() - |> should.equal(["a", "b", "c"]) - - "a" - |> string.to_graphemes() - |> should.equal(["a"]) - - "" - |> string.to_graphemes() - |> should.equal([]) -} - -pub fn utf_codepoint_test() { - string.utf_codepoint(1114444) - |> should.be_error - - string.utf_codepoint(65534) - |> should.be_error - - string.utf_codepoint(55296) - |> should.be_error - - assert Ok(snake) = string.utf_codepoint(128013) - should.equal(<<snake:utf8_codepoint>>, <<"🐍":utf8>>) +if erlang { + import gleam/string + import gleam/should + import gleam/order + + pub fn length_test() { + string.length("ß↑e̊") + |> should.equal(3) + + string.length("Gleam") + |> should.equal(5) + + string.length("") + |> should.equal(0) + } + + pub fn lowercase_test() { + string.lowercase("Gleam") + |> should.equal("gleam") + } + + pub fn uppercase_test() { + string.uppercase("Gleam") + |> should.equal("GLEAM") + } + + pub fn reverse_test() { + string.reverse("Gleam") + |> should.equal("maelG") + } + + pub fn split_test() { + "Gleam,Erlang,Elixir" + |> string.split(",") + |> should.equal(["Gleam", "Erlang", "Elixir"]) + + "Gleam, Erlang,Elixir" + |> string.split(", ") + |> should.equal(["Gleam", "Erlang,Elixir"]) + } + + pub fn split_once_test() { + "Gleam,Erlang,Elixir" + |> string.split_once(",") + |> should.equal(Ok(#("Gleam", "Erlang,Elixir"))) + + "Gleam" + |> string.split_once(",") + |> should.equal(Error(Nil)) + + "" + |> string.split_once(",") + |> should.equal(Error(Nil)) + } + + pub fn replace_test() { + "Gleam,Erlang,Elixir" + |> string.replace(",", "++") + |> should.equal("Gleam++Erlang++Elixir") + } + + pub fn append_test() { + "Test" + |> string.append(" Me") + |> should.equal("Test Me") + } + + pub fn compare_test() { + string.compare("", "") + |> should.equal(order.Eq) + + string.compare("a", "") + |> should.equal(order.Gt) + + string.compare("a", "A") + |> should.equal(order.Gt) + + string.compare("A", "B") + |> should.equal(order.Lt) + + string.compare("t", "ABC") + |> should.equal(order.Gt) + } + + pub fn contains_test() { + "gleam" + |> string.contains("ea") + |> should.equal(True) + + "gleam" + |> string.contains("x") + |> should.equal(False) + + string.contains(does: "bellwether", contain: "bell") + |> should.equal(True) + } + + pub fn concat_test() { + ["Hello", ", ", "world!"] + |> string.concat + |> should.equal("Hello, world!") + } + + pub fn repeat_test() { + "hi" + |> string.repeat(times: 3) + |> should.equal("hihihi") + + "hi" + |> string.repeat(0) + |> should.equal("") + + "hi" + |> string.repeat(-1) + |> should.equal("") + } + + pub fn join_test() { + ["Hello", "world!"] + |> string.join(with: ", ") + |> should.equal("Hello, world!") + + ["Hello", "world!"] + |> string.join(with: "-") + |> should.equal("Hello-world!") + } + + pub fn trim_test() { + " hats \n" + |> string.trim() + |> should.equal("hats") + } + + pub fn trim_left_test() { + " hats \n" + |> string.trim_left() + |> should.equal("hats \n") + } + + pub fn trim_right_test() { + " hats \n" + |> string.trim_right() + |> should.equal(" hats") + } + + pub fn starts_with_test() { + "theory" + |> string.starts_with("") + |> should.equal(True) + + "theory" + |> string.starts_with("the") + |> should.equal(True) + + "theory" + |> string.starts_with("ory") + |> should.equal(False) + + "theory" + |> string.starts_with("theory2") + |> should.equal(False) + } + + pub fn ends_with_test() { + "theory" + |> string.ends_with("") + |> should.equal(True) + + "theory" + |> string.ends_with("ory") + |> should.equal(True) + + "theory" + |> string.ends_with("the") + |> should.equal(False) + + "theory" + |> string.ends_with("theory2") + |> should.equal(False) + } + + pub fn slice_test() { + "gleam" + |> string.slice(at_index: 1, length: 2) + |> should.equal("le") + + "gleam" + |> string.slice(at_index: 1, length: 10) + |> should.equal("leam") + + "gleam" + |> string.slice(at_index: 10, length: 3) + |> should.equal("") + + "gleam" + |> string.slice(at_index: -2, length: 2) + |> should.equal("am") + + "gleam" + |> string.slice(at_index: -12, length: 2) + |> should.equal("") + + "gleam" + |> string.slice(at_index: 2, length: -3) + |> should.equal("") + } + + pub fn crop_test() { + "gleam" + |> string.crop("gl") + |> should.equal("gleam") + + "gleam" + |> string.crop("le") + |> should.equal("leam") + + string.crop(from: "gleam", before: "ea") + |> should.equal("eam") + + "gleam" + |> string.crop("") + |> should.equal("gleam") + + "gleam" + |> string.crop("!") + |> should.equal("gleam") + } + + pub fn drop_left_test() { + "gleam" + |> string.drop_left(up_to: 2) + |> should.equal("eam") + + "gleam" + |> string.drop_left(up_to: 6) + |> should.equal("") + + "gleam" + |> string.drop_left(up_to: -2) + |> should.equal("gleam") + } + + pub fn drop_right_test() { + "gleam" + |> string.drop_right(up_to: 2) + |> should.equal("gle") + + "gleam" + |> string.drop_right(up_to: 5) + |> should.equal("") + + "gleam" + |> string.drop_right(up_to: -2) + |> should.equal("gleam") + } + + pub fn pad_left_test() { + "121" + |> string.pad_left(to: 5, with: ".") + |> should.equal("..121") + + "121" + |> string.pad_left(to: 3, with: ".") + |> should.equal("121") + + "121" + |> string.pad_left(to: 2, with: ".") + |> should.equal("121") + + "121" + |> string.pad_left(to: 5, with: "XY") + |> should.equal("XYXY121") + } + + pub fn pad_right_test() { + "121" + |> string.pad_right(to: 5, with: ".") + |> should.equal("121..") + + "121" + |> string.pad_right(to: 3, with: ".") + |> should.equal("121") + + "121" + |> string.pad_right(to: 2, with: ".") + |> should.equal("121") + + "121" + |> string.pad_right(to: 5, with: "XY") + |> should.equal("121XYXY") + } + + pub fn pop_grapheme_test() { + "gleam" + |> string.pop_grapheme() + |> should.equal(Ok(#("g", "leam"))) + + "g" + |> string.pop_grapheme() + |> should.equal(Ok(#("g", ""))) + + "" + |> string.pop_grapheme() + |> should.equal(Error(Nil)) + } + + pub fn to_graphemes_test() { + "abc" + |> string.to_graphemes() + |> should.equal(["a", "b", "c"]) + + "a" + |> string.to_graphemes() + |> should.equal(["a"]) + + "" + |> string.to_graphemes() + |> should.equal([]) + } + + pub fn utf_codepoint_test() { + string.utf_codepoint(1114444) + |> should.be_error + + string.utf_codepoint(65534) + |> should.be_error + + string.utf_codepoint(55296) + |> should.be_error + + assert Ok(snake) = string.utf_codepoint(128013) + should.equal(<<snake:utf8_codepoint>>, <<"🐍":utf8>>) + } } diff --git a/test/gleam/uri_test.gleam b/test/gleam/uri_test.gleam index c0ee595..9c6d2e0 100644 --- a/test/gleam/uri_test.gleam +++ b/test/gleam/uri_test.gleam @@ -1,294 +1,296 @@ -import gleam/uri -import gleam/should -import gleam/string -import gleam/list -import gleam/option.{None, Some} - -pub fn full_parse_test() { - assert Ok(parsed) = - uri.parse("https://foo:bar@example.com:1234/path?query=true#fragment") - should.equal(parsed.scheme, Some("https")) - should.equal(parsed.userinfo, Some("foo:bar")) - should.equal(parsed.host, Some("example.com")) - should.equal(parsed.port, Some(1234)) - should.equal(parsed.path, "/path") - should.equal(parsed.query, Some("query=true")) - should.equal(parsed.fragment, Some("fragment")) -} - -pub fn parse_only_path_test() { - assert Ok(parsed) = uri.parse("") - should.equal(parsed.scheme, None) - should.equal(parsed.userinfo, None) - should.equal(parsed.host, None) - should.equal(parsed.port, None) - should.equal(parsed.path, "") - should.equal(parsed.query, None) - should.equal(parsed.fragment, None) -} - -pub fn parse_only_host_test() { - assert Ok(parsed) = uri.parse("//") - should.equal(parsed.scheme, None) - should.equal(parsed.userinfo, None) - should.equal(parsed.host, Some("")) - should.equal(parsed.port, None) - should.equal(parsed.path, "") - should.equal(parsed.query, None) - should.equal(parsed.fragment, None) -} - -pub fn error_parsing_uri_test() { - should.equal(uri.parse("::"), Error(Nil)) -} - -pub fn full_uri_to_string_test() { - let test_uri = - uri.Uri( - Some("https"), - Some("foo:bar"), - Some("example.com"), - Some(1234), - "/path", - Some("query=true"), - Some("fragment"), +if erlang { + import gleam/uri + import gleam/should + import gleam/string + import gleam/list + import gleam/option.{None, Some} + + pub fn full_parse_test() { + assert Ok(parsed) = + uri.parse("https://foo:bar@example.com:1234/path?query=true#fragment") + should.equal(parsed.scheme, Some("https")) + should.equal(parsed.userinfo, Some("foo:bar")) + should.equal(parsed.host, Some("example.com")) + should.equal(parsed.port, Some(1234)) + should.equal(parsed.path, "/path") + should.equal(parsed.query, Some("query=true")) + should.equal(parsed.fragment, Some("fragment")) + } + + pub fn parse_only_path_test() { + assert Ok(parsed) = uri.parse("") + should.equal(parsed.scheme, None) + should.equal(parsed.userinfo, None) + should.equal(parsed.host, None) + should.equal(parsed.port, None) + should.equal(parsed.path, "") + should.equal(parsed.query, None) + should.equal(parsed.fragment, None) + } + + pub fn parse_only_host_test() { + assert Ok(parsed) = uri.parse("//") + should.equal(parsed.scheme, None) + should.equal(parsed.userinfo, None) + should.equal(parsed.host, Some("")) + should.equal(parsed.port, None) + should.equal(parsed.path, "") + should.equal(parsed.query, None) + should.equal(parsed.fragment, None) + } + + pub fn error_parsing_uri_test() { + should.equal(uri.parse("::"), Error(Nil)) + } + + pub fn full_uri_to_string_test() { + let test_uri = + uri.Uri( + Some("https"), + Some("foo:bar"), + Some("example.com"), + Some(1234), + "/path", + Some("query=true"), + Some("fragment"), + ) + should.equal( + uri.to_string(test_uri), + "https://foo:bar@example.com:1234/path?query=true#fragment", ) - should.equal( - uri.to_string(test_uri), - "https://foo:bar@example.com:1234/path?query=true#fragment", - ) -} - -pub fn path_only_uri_to_string_test() { - let test_uri = uri.Uri(None, None, None, None, "/", None, None) - should.equal(uri.to_string(test_uri), "/") -} - -pub fn parse_query_string_test() { - assert Ok(parsed) = uri.parse_query("foo+bar=1&city=%C3%B6rebro") - should.equal(parsed, [#("foo bar", "1"), #("city", "örebro")]) - - // Duplicates keys not overridden - assert Ok(parsed) = uri.parse_query("a[]=1&a[]=2") - - parsed - |> should.equal([#("a[]", "1"), #("a[]", "2")]) -} - -pub fn parse_empty_query_string_test() { - assert Ok(parsed) = uri.parse_query("") - should.equal(parsed, []) -} - -pub fn parse_query_string_with_empty_test() { - uri.parse_query("present") - |> should.equal(Ok([#("present", "")])) -} - -pub fn error_parsing_query_test() { - should.equal(uri.parse_query("%C2"), Error(Nil)) -} - -pub fn query_to_string_test() { - let query_string = - uri.query_to_string([#("foo bar", "1"), #("city", "örebro")]) - should.equal(query_string, "foo+bar=1&city=%C3%B6rebro") -} - -pub fn empty_query_to_string_test() { - let query_string = uri.query_to_string([]) - should.equal(query_string, "") -} - -fn percent_codec_fixtures() { - [ - #(" ", "+"), - #(",", "%2C"), - #(";", "%3B"), - #(":", "%3A"), - #("!", "%21"), - #("?", "%3F"), - #("'", "%27"), - #("(", "%28"), - #(")", "%29"), - #("[", "%5B"), - #("@", "%40"), - #("/", "%2F"), - #("\\", "%5C"), - #("&", "%26"), - #("#", "%23"), - #("=", "%3D"), - #("~", "%7E"), - #("ñ", "%C3%B1"), - // Allowed chars - #("-", "-"), - #("_", "_"), - #(".", "."), - #("*", "*"), - #("100% great", "100%25+great"), - ] -} - -pub fn percent_encode_test() { - percent_codec_fixtures() - |> list.map(fn(t) { - let #(a, b) = t - uri.percent_encode(a) - |> should.equal(b) - }) -} - -pub fn percent_encode_consistency_test() { - let k = "foo bar[]" - let v = "ñaña (,:*~)" - - let query_string = uri.query_to_string([#(k, v)]) - - let encoded_key = uri.percent_encode(k) - let encoded_value = uri.percent_encode(v) - let manual_query_string = string.concat([encoded_key, "=", encoded_value]) - - should.equal(query_string, manual_query_string) -} - -pub fn percent_decode_test() { - percent_codec_fixtures() - |> list.map(fn(t) { - let #(a, b) = t - uri.percent_decode(b) - |> should.equal(Ok(a)) - }) -} - -pub fn percent_decode_consistency_test() { - let k = "foo+bar[]" - let v = "%C3%B6rebro" - let query = string.concat([k, "=", v]) - assert Ok(parsed) = uri.parse_query(query) - - assert Ok(decoded_key) = uri.percent_decode(k) - assert Ok(decoded_value) = uri.percent_decode(v) - - should.equal(parsed, [#(decoded_key, decoded_value)]) -} - -pub fn parse_segments_test() { - should.equal(uri.path_segments("/"), []) - should.equal(uri.path_segments("/foo/bar"), ["foo", "bar"]) - should.equal(uri.path_segments("////"), []) - should.equal(uri.path_segments("/foo//bar"), ["foo", "bar"]) - - should.equal(uri.path_segments("/."), []) - should.equal(uri.path_segments("/.foo"), [".foo"]) - - should.equal(uri.path_segments("/../bar"), ["bar"]) - should.equal(uri.path_segments("../bar"), ["bar"]) - should.equal(uri.path_segments("/foo/../bar"), ["bar"]) -} - -pub fn origin_test() { - assert Ok(parsed) = uri.parse("http://example.test/path?foo#bar") - uri.origin(parsed) - |> should.equal(Ok("http://example.test")) - - assert Ok(parsed) = uri.parse("http://example.test:8080") - uri.origin(parsed) - |> should.equal(Ok("http://example.test:8080")) - - assert Ok(parsed) = uri.parse("https://example.test") - uri.origin(parsed) - |> should.equal(Ok("https://example.test")) - - assert Ok(parsed) = uri.parse("http:///path") - uri.origin(parsed) - |> should.equal(Ok("http://")) - - assert Ok(parsed) = uri.parse("http://") - uri.origin(parsed) - |> should.equal(Ok("http://")) - - assert Ok(parsed) = uri.parse("/path") - uri.origin(parsed) - |> should.equal(Error(Nil)) - - assert Ok(parsed) = uri.parse("file:///dev/null") - uri.origin(parsed) - |> should.equal(Error(Nil)) -} - -pub fn merge_test() { - assert Ok(a) = uri.parse("/relative") - assert Ok(b) = uri.parse("") - uri.merge(a, b) - |> should.equal(Error(Nil)) - - assert Ok(a) = uri.parse("http://google.com/foo") - assert Ok(b) = uri.parse("http://example.com/baz") - uri.merge(a, b) - |> should.equal(uri.parse("http://example.com/baz")) - - assert Ok(a) = uri.parse("http://google.com/foo") - assert Ok(b) = uri.parse("http://example.com/.././bar/../../baz") - uri.merge(a, b) - |> should.equal(uri.parse("http://example.com/baz")) - - assert Ok(a) = uri.parse("http://google.com/foo") - assert Ok(b) = uri.parse("//example.com/baz") - uri.merge(a, b) - |> should.equal(uri.parse("http://example.com/baz")) - - assert Ok(a) = uri.parse("http://google.com/foo") - assert Ok(b) = uri.parse("//example.com/.././bar/../../../baz") - uri.merge(a, b) - |> should.equal(uri.parse("http://example.com/baz")) - - assert Ok(a) = uri.parse("http://example.com/foo/bar") - assert Ok(b) = uri.parse("/baz") - uri.merge(a, b) - |> should.equal(uri.parse("http://example.com/baz")) - - assert Ok(a) = uri.parse("http://example.com/foo/bar") - assert Ok(b) = uri.parse("baz") - uri.merge(a, b) - |> should.equal(uri.parse("http://example.com/foo/baz")) - - assert Ok(a) = uri.parse("http://example.com/foo/") - assert Ok(b) = uri.parse("baz") - uri.merge(a, b) - |> should.equal(uri.parse("http://example.com/foo/baz")) - - assert Ok(a) = uri.parse("http://example.com") - assert Ok(b) = uri.parse("baz") - uri.merge(a, b) - |> should.equal(uri.parse("http://example.com/baz")) - - assert Ok(a) = uri.parse("http://example.com") - assert Ok(b) = uri.parse("/.././bar/../../../baz") - uri.merge(a, b) - |> should.equal(uri.parse("http://example.com/baz")) - - assert Ok(a) = uri.parse("http://example.com/foo/bar") - assert Ok(b) = uri.parse("") - uri.merge(a, b) - |> should.equal(uri.parse("http://example.com/foo/bar")) - - assert Ok(a) = uri.parse("http://example.com/foo/bar") - assert Ok(b) = uri.parse("#fragment") - uri.merge(a, b) - |> should.equal(uri.parse("http://example.com/foo/bar#fragment")) - - assert Ok(a) = uri.parse("http://example.com/foo/bar") - assert Ok(b) = uri.parse("?query") - uri.merge(a, b) - |> should.equal(uri.parse("http://example.com/foo/bar?query")) - - assert Ok(a) = uri.parse("http://example.com/foo/bar?query1") - assert Ok(b) = uri.parse("?query2") - uri.merge(a, b) - |> should.equal(uri.parse("http://example.com/foo/bar?query2")) - - assert Ok(a) = uri.parse("http://example.com/foo/bar?query") - assert Ok(b) = uri.parse("") - uri.merge(a, b) - |> should.equal(uri.parse("http://example.com/foo/bar?query")) + } + + pub fn path_only_uri_to_string_test() { + let test_uri = uri.Uri(None, None, None, None, "/", None, None) + should.equal(uri.to_string(test_uri), "/") + } + + pub fn parse_query_string_test() { + assert Ok(parsed) = uri.parse_query("foo+bar=1&city=%C3%B6rebro") + should.equal(parsed, [#("foo bar", "1"), #("city", "örebro")]) + + // Duplicates keys not overridden + assert Ok(parsed) = uri.parse_query("a[]=1&a[]=2") + + parsed + |> should.equal([#("a[]", "1"), #("a[]", "2")]) + } + + pub fn parse_empty_query_string_test() { + assert Ok(parsed) = uri.parse_query("") + should.equal(parsed, []) + } + + pub fn parse_query_string_with_empty_test() { + uri.parse_query("present") + |> should.equal(Ok([#("present", "")])) + } + + pub fn error_parsing_query_test() { + should.equal(uri.parse_query("%C2"), Error(Nil)) + } + + pub fn query_to_string_test() { + let query_string = + uri.query_to_string([#("foo bar", "1"), #("city", "örebro")]) + should.equal(query_string, "foo+bar=1&city=%C3%B6rebro") + } + + pub fn empty_query_to_string_test() { + let query_string = uri.query_to_string([]) + should.equal(query_string, "") + } + + fn percent_codec_fixtures() { + [ + #(" ", "+"), + #(",", "%2C"), + #(";", "%3B"), + #(":", "%3A"), + #("!", "%21"), + #("?", "%3F"), + #("'", "%27"), + #("(", "%28"), + #(")", "%29"), + #("[", "%5B"), + #("@", "%40"), + #("/", "%2F"), + #("\\", "%5C"), + #("&", "%26"), + #("#", "%23"), + #("=", "%3D"), + #("~", "%7E"), + #("ñ", "%C3%B1"), + // Allowed chars + #("-", "-"), + #("_", "_"), + #(".", "."), + #("*", "*"), + #("100% great", "100%25+great"), + ] + } + + pub fn percent_encode_test() { + percent_codec_fixtures() + |> list.map(fn(t) { + let #(a, b) = t + uri.percent_encode(a) + |> should.equal(b) + }) + } + + pub fn percent_encode_consistency_test() { + let k = "foo bar[]" + let v = "ñaña (,:*~)" + + let query_string = uri.query_to_string([#(k, v)]) + + let encoded_key = uri.percent_encode(k) + let encoded_value = uri.percent_encode(v) + let manual_query_string = string.concat([encoded_key, "=", encoded_value]) + + should.equal(query_string, manual_query_string) + } + + pub fn percent_decode_test() { + percent_codec_fixtures() + |> list.map(fn(t) { + let #(a, b) = t + uri.percent_decode(b) + |> should.equal(Ok(a)) + }) + } + + pub fn percent_decode_consistency_test() { + let k = "foo+bar[]" + let v = "%C3%B6rebro" + let query = string.concat([k, "=", v]) + assert Ok(parsed) = uri.parse_query(query) + + assert Ok(decoded_key) = uri.percent_decode(k) + assert Ok(decoded_value) = uri.percent_decode(v) + + should.equal(parsed, [#(decoded_key, decoded_value)]) + } + + pub fn parse_segments_test() { + should.equal(uri.path_segments("/"), []) + should.equal(uri.path_segments("/foo/bar"), ["foo", "bar"]) + should.equal(uri.path_segments("////"), []) + should.equal(uri.path_segments("/foo//bar"), ["foo", "bar"]) + + should.equal(uri.path_segments("/."), []) + should.equal(uri.path_segments("/.foo"), [".foo"]) + + should.equal(uri.path_segments("/../bar"), ["bar"]) + should.equal(uri.path_segments("../bar"), ["bar"]) + should.equal(uri.path_segments("/foo/../bar"), ["bar"]) + } + + pub fn origin_test() { + assert Ok(parsed) = uri.parse("http://example.test/path?foo#bar") + uri.origin(parsed) + |> should.equal(Ok("http://example.test")) + + assert Ok(parsed) = uri.parse("http://example.test:8080") + uri.origin(parsed) + |> should.equal(Ok("http://example.test:8080")) + + assert Ok(parsed) = uri.parse("https://example.test") + uri.origin(parsed) + |> should.equal(Ok("https://example.test")) + + assert Ok(parsed) = uri.parse("http:///path") + uri.origin(parsed) + |> should.equal(Ok("http://")) + + assert Ok(parsed) = uri.parse("http://") + uri.origin(parsed) + |> should.equal(Ok("http://")) + + assert Ok(parsed) = uri.parse("/path") + uri.origin(parsed) + |> should.equal(Error(Nil)) + + assert Ok(parsed) = uri.parse("file:///dev/null") + uri.origin(parsed) + |> should.equal(Error(Nil)) + } + + pub fn merge_test() { + assert Ok(a) = uri.parse("/relative") + assert Ok(b) = uri.parse("") + uri.merge(a, b) + |> should.equal(Error(Nil)) + + assert Ok(a) = uri.parse("http://google.com/foo") + assert Ok(b) = uri.parse("http://example.com/baz") + uri.merge(a, b) + |> should.equal(uri.parse("http://example.com/baz")) + + assert Ok(a) = uri.parse("http://google.com/foo") + assert Ok(b) = uri.parse("http://example.com/.././bar/../../baz") + uri.merge(a, b) + |> should.equal(uri.parse("http://example.com/baz")) + + assert Ok(a) = uri.parse("http://google.com/foo") + assert Ok(b) = uri.parse("//example.com/baz") + uri.merge(a, b) + |> should.equal(uri.parse("http://example.com/baz")) + + assert Ok(a) = uri.parse("http://google.com/foo") + assert Ok(b) = uri.parse("//example.com/.././bar/../../../baz") + uri.merge(a, b) + |> should.equal(uri.parse("http://example.com/baz")) + + assert Ok(a) = uri.parse("http://example.com/foo/bar") + assert Ok(b) = uri.parse("/baz") + uri.merge(a, b) + |> should.equal(uri.parse("http://example.com/baz")) + + assert Ok(a) = uri.parse("http://example.com/foo/bar") + assert Ok(b) = uri.parse("baz") + uri.merge(a, b) + |> should.equal(uri.parse("http://example.com/foo/baz")) + + assert Ok(a) = uri.parse("http://example.com/foo/") + assert Ok(b) = uri.parse("baz") + uri.merge(a, b) + |> should.equal(uri.parse("http://example.com/foo/baz")) + + assert Ok(a) = uri.parse("http://example.com") + assert Ok(b) = uri.parse("baz") + uri.merge(a, b) + |> should.equal(uri.parse("http://example.com/baz")) + + assert Ok(a) = uri.parse("http://example.com") + assert Ok(b) = uri.parse("/.././bar/../../../baz") + uri.merge(a, b) + |> should.equal(uri.parse("http://example.com/baz")) + + assert Ok(a) = uri.parse("http://example.com/foo/bar") + assert Ok(b) = uri.parse("") + uri.merge(a, b) + |> should.equal(uri.parse("http://example.com/foo/bar")) + + assert Ok(a) = uri.parse("http://example.com/foo/bar") + assert Ok(b) = uri.parse("#fragment") + uri.merge(a, b) + |> should.equal(uri.parse("http://example.com/foo/bar#fragment")) + + assert Ok(a) = uri.parse("http://example.com/foo/bar") + assert Ok(b) = uri.parse("?query") + uri.merge(a, b) + |> should.equal(uri.parse("http://example.com/foo/bar?query")) + + assert Ok(a) = uri.parse("http://example.com/foo/bar?query1") + assert Ok(b) = uri.parse("?query2") + uri.merge(a, b) + |> should.equal(uri.parse("http://example.com/foo/bar?query2")) + + assert Ok(a) = uri.parse("http://example.com/foo/bar?query") + assert Ok(b) = uri.parse("") + uri.merge(a, b) + |> should.equal(uri.parse("http://example.com/foo/bar?query")) + } } |