From 5fe6c4e289a2eb708009cf3ef955bdceda80eb35 Mon Sep 17 00:00:00 2001 From: James Harton Date: Tue, 23 Apr 2019 17:09:28 +1200 Subject: [PATCH] Fix bug in prefix's enumerable implementation. --- lib/enumerable/ip/prefix.ex | 7 ++++--- test/enumerable/ip/prefix_test.exs | 5 +++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/enumerable/ip/prefix.ex b/lib/enumerable/ip/prefix.ex index e660823..83a6b0e 100644 --- a/lib/enumerable/ip/prefix.ex +++ b/lib/enumerable/ip/prefix.ex @@ -43,7 +43,7 @@ defimpl Enumerable, for: IP.Prefix do iex> ~i(192.0.2.128/29) ...> |> Stream.filter(fn a -> rem(IP.Address.to_integer(a), 2) == 0 end) ...> |> Enum.map(fn a -> IP.Address.to_string(a) end) - ["192.0.2.130", "192.0.2.132", "192.0.2.134"] + ["192.0.2.128", "192.0.2.130", "192.0.2.132", "192.0.2.134"] """ @spec reduce(Prefix.t(), Enumerable.acc(), Enumerable.reducer()) :: Enumerable.result() def reduce(_, {:halt, acc}, _fun), do: {:halted, acc} @@ -75,9 +75,10 @@ defimpl Enumerable, for: IP.Prefix do {:done, acc} pos -> - pos = pos + 1 next = Address.from_integer!(pos, version) - reduce({prefix, pos, last}, fun.(next, acc), fun) + reduce({prefix, pos + 1, last}, fun.(next, acc), fun) end end + + def slice(_), do: {:error, __MODULE__} end diff --git a/test/enumerable/ip/prefix_test.exs b/test/enumerable/ip/prefix_test.exs index af39111..5cd6a53 100644 --- a/test/enumerable/ip/prefix_test.exs +++ b/test/enumerable/ip/prefix_test.exs @@ -2,4 +2,9 @@ defmodule EnumerableIPPrefixTest do use ExUnit.Case import IP.Sigil doctest Enumerable.IP.Prefix + + # Regression: https://gitlab.com/jimsy/ip/issues/1 + test "correctly returns the first address in the range" do + assert Enum.take(~i(10.10.10.0/24), 1) == [~i(10.10.10.0)] + end end