nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: msz/hammox
Pull request #26 (2020-02-19 15:54:06)

Fix printing struct name mismatch error
Hammox uses the `struct_name_type_mismatch` error when it detects an incorrect struct being used compared to what was specified. As I understand, it is raised in two cases:

* when a map is passed where a struct is expected,
* when struct A is passed where struct B is expected.

The second case has an error associated, but there was no function formatting this error into a human-readable message.

This PR adds a missing test case and fixes the problem by formatting the message correctly.


Patch:
diff --git a/lib/hammox/type_match_error.ex b/lib/hammox/type_match_error.ex
index e91aebe..213c58f 100644
--- a/lib/hammox/type_match_error.ex
+++ b/lib/hammox/type_match_error.ex
@@ -94,6 +94,12 @@ defmodule Hammox.TypeMatchError do
     "Expected the value to be #{Utils.module_to_string(expected_struct_name)} struct."
   end
 
+  defp human_reason({:struct_name_type_mismatch, actual_struct_name, expected_struct_name}) do
+    "Expected the value to be a #{Utils.module_to_string(expected_struct_name)}, got a #{
+      Utils.module_to_string(actual_struct_name)
+    }"
+  end
+
   defp human_reason({:module_fetch_failure, module_name}) do
     "Could not load module #{Utils.module_to_string(module_name)}."
   end


Test patch:
diff --git a/test/hammox_test.exs b/test/hammox_test.exs
index 306f9fa..0076fac 100644
--- a/test/hammox_test.exs
+++ b/test/hammox_test.exs
@@ -579,6 +579,10 @@ defmodule HammoxTest do
       assert_fail(:foo_struct_literal, %{foo: :bar})
     end
 
+    test "fail different struct" do
+      assert_fail(:foo_struct_literal, %Hammox.Test.OtherStruct{})
+    end
+
     test "pass default struct" do
       assert_pass(:foo_struct_literal, %Hammox.Test.Struct{})
     end
diff --git a/test/support/other_struct.ex b/test/support/other_struct.ex
new file mode 100644
index 0000000..285bf0d
--- /dev/null
+++ b/test/support/other_struct.ex
@@ -0,0 +1,3 @@
+defmodule Hammox.Test.OtherStruct do
+  defstruct [:foo]
+end
Repository: msz/hammox
Pull request #54 (2020-10-07 19:45:06)

Decorate all functions inside behaviour-implementation module
:wave:
Relates to https://github.com/msz/hammox/issues/8


Patch:
diff --git a/lib/hammox.ex b/lib/hammox.ex
index f9eb09d..4c11463 100644
--- a/lib/hammox.ex
+++ b/lib/hammox.ex
@@ -87,6 +87,34 @@ defmodule Hammox do
   """
   defdelegate verify_on_exit!(context \\ %{}), to: Mox
 
+  @doc since: "0.1.0"
+  @doc """
+  Same as `protect/3`, but decorate all public functions inside the module.
+
+  Example:
+
+  ```elixir
+  defmodule Calculator do
+    @callback add(integer(), integer()) :: integer()
+    def add(a, b), do: a + b
+    @callback add(integer(), integer(), integer()) :: integer()
+    def add(a, b, c), do: a + b + c
+    @callback multiply(integer(), integer()) :: integer()
+    def multiply(a, b), do: a * b
+  end
+
+  %{
+    add_2: add_2,
+    add_3: add_3,
+    multiply_2: multiply_2
+  } = Hammox.protect(Calculator)
+  """
+  @spec protect(module_name :: module()) :: map()
+  def protect(module_name) when is_atom(module_name) do
+    funs = module_name.__info__(:functions)
+    protect(module_name, module_name, funs)
+  end
+
   @doc since: "0.1.0"
   @doc """
   Takes the function provided by a module, function, arity tuple and


Test patch:
diff --git a/test/hammox_test.exs b/test/hammox_test.exs
index 84319ad..5ee8cf0 100644
--- a/test/hammox_test.exs
+++ b/test/hammox_test.exs
@@ -5,6 +5,13 @@ defmodule HammoxTest do
 
   defmock(TestMock, for: Hammox.Test.Behaviour)
 
+  describe "protect/1" do
+    test "decorate all functions inside the module" do
+      assert %{other_foo_0: _, other_foo_1: _, foo_0: _} =
+        Hammox.protect(Hammox.Test.BehaviourImplementation)
+    end
+  end
+
   describe "protect/2" do
     test "returns function protected from contract errors" do
       fun = Hammox.protect({Hammox.Test.SmallImplementation, :foo, 0}, Hammox.Test.SmallBehaviour)
Repository: msz/hammox
Pull request #75 (2021-03-06 16:49:14)

raise if Hammox.Protect used on module with no callbacks
Using Hammox.Protect on a module without callbacks results in a no-op. This is
most likely due to a mistake, and we should raise in this scenario.

`Hammox.protect/3` and friends can still be used on a module without callbacks
and will return an empty map.



Patch:
diff --git a/lib/hammox/protect.ex b/lib/hammox/protect.ex
index a1af764..027605f 100644
--- a/lib/hammox/protect.ex
+++ b/lib/hammox/protect.ex
@@ -89,7 +89,15 @@ defmodule Hammox.Protect do
         """
     end
 
-    funs = Keyword.get_lazy(opts, :funs, fn -> get_funs!(behaviour || module) end)
+    module_with_callbacks = behaviour || module
+
+    funs = Keyword.get_lazy(opts, :funs, fn -> get_funs!(module_with_callbacks) end)
+
+    if funs == [] do
+      raise ArgumentError,
+        message:
+          "The module #{inspect(module_with_callbacks)} does not contain any callbacks. Please use a behaviour with at least one callback."
+    end
 
     {module, behaviour, funs}
   end


Test patch:
diff --git a/test/hammox/protect_test.exs b/test/hammox/protect_test.exs
index 34a8b2d..383e4ae 100644
--- a/test/hammox/protect_test.exs
+++ b/test/hammox/protect_test.exs
@@ -22,6 +22,34 @@ defmodule Hammox.ProtectTest do
     end
   end
 
+  test "using Protect on a module without callbacks throws an exception" do
+    module_string = """
+    defmodule ProtectNoCallbacks do
+      use Hammox.Protect, module: Hammox.Test.Protect.Implementation
+    end
+    """
+
+    assert_raise ArgumentError,
+                 ~r/The module Hammox.Test.Protect.Implementation does not contain any callbacks./,
+                 fn ->
+                   Code.compile_string(module_string)
+                 end
+  end
+
+  test "using Protect on a behaviour without callbacks throws an exception" do
+    module_string = """
+    defmodule ProtectNoCallbacks do
+      use Hammox.Protect, module: Hammox.Test.Protect.Implementation, behaviour: Hammox.Test.Protect.EmptyBehaviour
+    end
+    """
+
+    assert_raise ArgumentError,
+                 ~r/The module Hammox.Test.Protect.EmptyBehaviour does not contain any callbacks./,
+                 fn ->
+                   Code.compile_string(module_string)
+                 end
+  end
+
   test "using Protect creates protected versions of functions from given behaviour-implementation module" do
     assert_raise Hammox.TypeMatchError, fn -> behaviour_implementation_wrong_typespec() end
   end
diff --git a/test/support/protect/empty_behaviour.ex b/test/support/protect/empty_behaviour.ex
new file mode 100644
index 0000000..c5f8d52
--- /dev/null
+++ b/test/support/protect/empty_behaviour.ex
@@ -0,0 +1,3 @@
+defmodule Hammox.Test.Protect.EmptyBehaviour do
+  @moduledoc false
+end