Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reset/1 with the option to reset specific sequences at once #331

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions lib/ex_machina/sequence.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ defmodule ExMachina.Sequence do

You can use list as well

ExMachina.Sequence.next(["A", "B"]) # "A"
ExMachina.Sequence.next(["A", "B"]) # "B"
ExMachina.Sequence.next("alphabet_sequence", ["A", "B"]) # "A"
ExMachina.Sequence.next("alphabet_sequence", ["A", "B"]) # "B"

Sequence.reset

ExMachina.Sequence.next(["A", "B"]) # resets so the return value is "A"
ExMachina.Sequence.next("alphabet_sequence"["A", "B"]) # resets so the return value is "A"

If you want to reset sequences at the beginning of every test, put it in a
`setup` block in your test.
Expand All @@ -45,6 +45,45 @@ defmodule ExMachina.Sequence do
Agent.update(__MODULE__, fn _ -> Map.new() end)
end

@doc """
If a list is parsed, reset all sequences in the list to starts from 0 and remains other sequences
with the current index, otherwise will reset
a single sequence.

## Example

Sequence.next(:alphabet, ["A", "B", "C"]) # "A"
Sequence.next(:alphabet, ["A", "B", "C"]) # "B"
Sequence.next(:numeric, [1, 2, 3]) # 1
Sequence.next(:numeric, [1, 2, 3]) # 2
Sequence.next("joe") # "joe0"
Sequence.next("joe") # "joe1"

ExMachina.Sequence.reset(["joe", :numeric])

ExMachina.Sequence.next(:numeric, [1, 2, 3]) # 1
ExMachina.Sequence.next("joe") # "joe0"
ExMachina.Sequence.next(:alphabet, ["A", "B", "C"]) # "C"

ExMachina.Sequence.reset(:alphabet)

ExMachina.Sequence.next(:alphabet, ["A", "B", "C"]) # "A"
"""

@spec reset(list()) :: :ok
def reset(sequence_names) when is_list(sequence_names) do
Agent.update(__MODULE__, fn sequences ->
Enum.reduce(sequence_names, sequences, &(Map.put(&2, &1, 0)))
end)
end

@spec reset(any()) :: :ok
def reset(sequence_name) do
Agent.update(__MODULE__, fn sequences ->
Map.put(sequences, sequence_name, 0)
end)
end

@doc false
def next(sequence_name) when is_binary(sequence_name) do
next(sequence_name, &(sequence_name <> to_string(&1)))
Expand Down
20 changes: 20 additions & 0 deletions test/ex_machina/sequence_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,24 @@ defmodule ExMachina.SequenceTest do

assert "joe0" == Sequence.next("joe")
end

test "can reset specific sequences" do
Sequence.next(:alphabet, ["A", "B", "C"])
Sequence.next(:alphabet, ["A", "B", "C"])
Sequence.next(:numeric, [1, 2, 3])
Sequence.next(:numeric, [1, 2, 3])
Sequence.next("joe")
Sequence.next("joe")

Sequence.reset(["joe", :numeric])

assert 1 == Sequence.next(:numeric, [1, 2, 3])
assert "joe0" == Sequence.next("joe")
assert "C" == Sequence.next(:alphabet, ["A", "B", "C"])

Sequence.reset(:alphabet)

assert "A" == Sequence.next(:alphabet, ["A", "B", "C"])
assert 2 == Sequence.next(:numeric, [1, 2, 3])
end
end