Skip to content

Commit 29bb8c8

Browse files
committed
Add test coverage of raise-on-inconsistent-opts check
1 parent c475e75 commit 29bb8c8

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

test/sqids_test.exs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,91 @@ defmodule SqidsTest do
743743
end
744744
end
745745
end
746+
747+
defmodule UseOfWrongChildSpecFunction do
748+
@moduledoc false
749+
use ExUnit.Case, async: true
750+
751+
test "Raise on inconsistent options (alphabet)" do
752+
assert_raise RuntimeError, ~r/Inconsistent options for /, fn ->
753+
run_test(alphabet: "abc")
754+
end
755+
end
756+
757+
test "Raise on inconsistent options (min_length)" do
758+
assert_raise RuntimeError, ~r/Inconsistent options for /, fn ->
759+
run_test(min_length: 10)
760+
end
761+
end
762+
763+
test "Raise on inconsistent options (blocklist)" do
764+
assert_raise RuntimeError, ~r/Inconsistent options for /, fn ->
765+
run_test(blocklist: ["hmmm"])
766+
end
767+
end
768+
769+
test "Raise on inconsistent options (alphabet + min_length + blocklist)" do
770+
assert_raise RuntimeError, ~r/Inconsistent options for /, fn ->
771+
run_test(
772+
alphabet: "zyx",
773+
min_length: 7,
774+
blocklist: ["hmmm"]
775+
)
776+
end
777+
end
778+
779+
test "Raise on inconsistent options (alphabet vs. alphabet)" do
780+
assert_raise RuntimeError, ~r/Inconsistent options for /, fn ->
781+
run_test([alphabet: "abc"], alphabet: "xyz")
782+
end
783+
end
784+
785+
test "Raise on inconsistent options (min_length vs. min_length)" do
786+
assert_raise RuntimeError, ~r/Inconsistent options for /, fn ->
787+
run_test([min_length: 10], min_length: 12)
788+
end
789+
end
790+
791+
test "Raise on inconsistent options (blocklist vs. blocklist)" do
792+
assert_raise RuntimeError, ~r/Inconsistent options for /, fn ->
793+
run_test([blocklist: ["hmmm"]], blocklist: ["oohhh"])
794+
end
795+
end
796+
797+
test "Log on child_spec/0 not being used" do
798+
assert match?(
799+
{:ok, _pid},
800+
run_test(
801+
[
802+
alphabet: "equivalnt",
803+
min_length: 40
804+
],
805+
min_length: 40,
806+
alphabet: "equivalnt"
807+
)
808+
)
809+
end
810+
811+
defp run_test(module_opts, passed_opts \\ []) do
812+
module_name = String.to_atom("#{__MODULE__}.UsingModule.#{:rand.uniform(Bitwise.<<<(1, 64))}")
813+
814+
module_content =
815+
quote do
816+
use Sqids
817+
818+
@impl true
819+
def child_spec, do: child_spec(unquote(Macro.escape(module_opts)))
820+
end
821+
822+
Module.create(module_name, module_content, Macro.Env.location(__ENV__))
823+
824+
wrong_child_spec = module_name.child_spec(passed_opts)
825+
%{start: {mod, fun, args}} = wrong_child_spec
826+
827+
Process.flag(:trap_exit, true)
828+
apply(mod, fun, args)
829+
end
830+
end
746831
end
747832

748833
# doctest_file was added on Elixir 1.15

0 commit comments

Comments
 (0)