-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add assertion testing if value passes eventually (#470)
- Loading branch information
Showing
2 changed files
with
44 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
defmodule Supavisor.Asserts do | ||
@doc """ | ||
Asserts that `function` will eventually success. Fails otherwise. | ||
It performs `repeats` checks with `delay` milliseconds between each check. | ||
""" | ||
def assert_eventually(repeats \\ 5, delay \\ 1000, function) | ||
|
||
def assert_eventually(0, _, _) do | ||
raise ExUnit.AssertionError, message: "Expected function to return truthy value" | ||
end | ||
|
||
def assert_eventually(n, delay, func) do | ||
if func.() do | ||
:ok | ||
else | ||
Process.sleep(delay) | ||
assert_eventually(n - 1, delay, func) | ||
end | ||
end | ||
|
||
@doc """ | ||
Asserts that `function` will eventually fail. Fails otherwise. | ||
It performs `repeats` checks with `delay` milliseconds between each check. | ||
""" | ||
def refute_eventually(repeats \\ 5, delay \\ 1000, function) | ||
|
||
def refute_eventually(0, _, _) do | ||
raise ExUnit.AssertionError, message: "Expected function to return falsey value" | ||
end | ||
|
||
def refute_eventually(n, delay, func) do | ||
if func.() do | ||
Process.sleep(delay) | ||
refute_eventually(n - 1, delay, func) | ||
else | ||
:ok | ||
end | ||
end | ||
end |