diff --git a/lib/elixir/lib/enum.ex b/lib/elixir/lib/enum.ex index 2a9af55ac1..eeb2578776 100644 --- a/lib/elixir/lib/enum.ex +++ b/lib/elixir/lib/enum.ex @@ -780,6 +780,10 @@ defmodule Enum do end end + def count_until(_enumerable, limit) do + raise ArgumentError, "expected limit to be greater than 0, got: #{limit}" + end + @doc """ Counts the elements in the enumerable for which `fun` returns a truthy value, stopping at `limit`. @@ -801,6 +805,10 @@ defmodule Enum do end end + def count_until(_enumerable, _fun, limit) do + raise ArgumentError, "expected limit to be greater than 0, got: #{limit}" + end + @doc """ Enumerates the `enumerable`, returning a list where all consecutive duplicate elements are collapsed to a single element. diff --git a/lib/elixir/test/elixir/enum_test.exs b/lib/elixir/test/elixir/enum_test.exs index 7396f04663..4a53bc47c7 100644 --- a/lib/elixir/test/elixir/enum_test.exs +++ b/lib/elixir/test/elixir/enum_test.exs @@ -225,6 +225,16 @@ defmodule EnumTest do assert count_until_stream.([1, 2], 2) == 2 end + test "count_until/2 with invalid limit" do + assert_raise ArgumentError, "expected limit to be greater than 0, got: 0", fn -> + Enum.count_until([1, 2, 3], 0) + end + + assert_raise ArgumentError, "expected limit to be greater than 0, got: -22", fn -> + Enum.count_until([1, 2, 3], -22) + end + end + test "count_until/3" do assert Enum.count_until([1, 2, 3, 4, 5, 6], fn x -> rem(x, 2) == 0 end, 2) == 2 assert Enum.count_until([1, 2], fn x -> rem(x, 2) == 0 end, 2) == 1 @@ -243,6 +253,16 @@ defmodule EnumTest do assert count_until_stream.([], fn x -> rem(x, 2) == 0 end, 2) == 0 end + test "count_until/3 with invalid limit" do + assert_raise ArgumentError, "expected limit to be greater than 0, got: 0", fn -> + Enum.count_until([1, 2, 3], fn x -> rem(x, 2) == 0 end, 0) + end + + assert_raise ArgumentError, "expected limit to be greater than 0, got: -22", fn -> + Enum.count_until([1, 2, 3], fn x -> rem(x, 2) == 0 end, -22) + end + end + test "dedup/1" do assert Enum.dedup([1, 1, 2, 1, 1, 2, 1]) == [1, 2, 1, 2, 1] assert Enum.dedup([2, 1, 1, 2, 1]) == [2, 1, 2, 1]