Fix bug in prefix's enumerable implementation.
This commit is contained in:
parent
52a04d37b5
commit
5fe6c4e289
2 changed files with 9 additions and 3 deletions
|
@ -43,7 +43,7 @@ defimpl Enumerable, for: IP.Prefix do
|
||||||
iex> ~i(192.0.2.128/29)
|
iex> ~i(192.0.2.128/29)
|
||||||
...> |> Stream.filter(fn a -> rem(IP.Address.to_integer(a), 2) == 0 end)
|
...> |> Stream.filter(fn a -> rem(IP.Address.to_integer(a), 2) == 0 end)
|
||||||
...> |> Enum.map(fn a -> IP.Address.to_string(a) 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()
|
@spec reduce(Prefix.t(), Enumerable.acc(), Enumerable.reducer()) :: Enumerable.result()
|
||||||
def reduce(_, {:halt, acc}, _fun), do: {:halted, acc}
|
def reduce(_, {:halt, acc}, _fun), do: {:halted, acc}
|
||||||
|
@ -75,9 +75,10 @@ defimpl Enumerable, for: IP.Prefix do
|
||||||
{:done, acc}
|
{:done, acc}
|
||||||
|
|
||||||
pos ->
|
pos ->
|
||||||
pos = pos + 1
|
|
||||||
next = Address.from_integer!(pos, version)
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def slice(_), do: {:error, __MODULE__}
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,4 +2,9 @@ defmodule EnumerableIPPrefixTest do
|
||||||
use ExUnit.Case
|
use ExUnit.Case
|
||||||
import IP.Sigil
|
import IP.Sigil
|
||||||
doctest Enumerable.IP.Prefix
|
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
|
end
|
||||||
|
|
Loading…
Reference in a new issue