Skip to content

Commit

Permalink
Add 'use Mockery.Macro'
Browse files Browse the repository at this point in the history
  • Loading branch information
amatalai committed Jul 4, 2024
1 parent be784c1 commit b31c256
Show file tree
Hide file tree
Showing 16 changed files with 56 additions and 43 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.3.3 (2024-07-04)

- Added `use Mockery.Macro` to solve new Elixir warnings

## 2.3.2 (2024-07-03)

- Added missing () to fix warnings
Expand Down
2 changes: 1 addition & 1 deletion MIGRATION_TO_OTP21.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Previous example rewritten to use macros:

```elixir
defmodule MyProject do
import Mockery.Macro
use Mockery.Macro

def bar, do: mockable(Foo).bar()
def baz, do: mockable(Foo).baz()
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ end
```elixir
# lib/my_app/foo.ex
defmodule MyApp.Foo do
import Mockery.Macro
use Mockery.Macro
alias MyApp.Bar

def baz, do: mockable(Bar).function()
Expand All @@ -57,7 +57,7 @@ end
```elixir
defmodule MyApp.Controller do
# ...
import Mockery.Macro
use Mockery.Macro

def all do
mockable(MyApp.UserService).users()
Expand Down Expand Up @@ -111,7 +111,7 @@ end

# prepare tested module
defmodule Other do
import Mockery.Macro
use Mockery.Macro

def parse(value) do
mockable(Foo).bar(value)
Expand All @@ -135,7 +135,7 @@ end
```elixir
# prepare tested module
defmodule Tested do
import Mockery.Macro
use Mockery.Macro

def call(value, opts) do
mockable(Foo).bar(value)
Expand Down Expand Up @@ -223,7 +223,7 @@ end

# prepare tested module
defmodule Other do
import Mockery.Macro
use Mockery.Macro

def bar, do: mockable(Foo, by: FooGlobalMock).bar()
def baz, do: mockable(Foo, by: FooGlobalMock).baz()
Expand Down Expand Up @@ -262,7 +262,7 @@ For advanced usage examples see

## License

Copyright 2017-2019 Tobiasz Małecki [[email protected]](mailto:[email protected])
Copyright 2017-2024 Tobiasz Małecki [[email protected]](mailto:[email protected])

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Expand Down
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
use Mix.Config
import Config

import_config "#{Mix.env()}.exs"
2 changes: 1 addition & 1 deletion config/dev.exs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
use Mix.Config
import Config
2 changes: 1 addition & 1 deletion config/prod.exs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
use Mix.Config
import Config
2 changes: 1 addition & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
use Mix.Config
import Config

config :mockery, :integration_test, {Mockery.Proxy, IntegrationTest.Mocked, nil}
11 changes: 9 additions & 2 deletions lib/mockery/macro.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ defmodule Mockery.Macro do
Alternative macro-based way to prepare module for mocking/asserting.
"""

defmacro __using__(_) do
quote do
@compile {:no_warn_undefined, Mockery.Proxy.MacroProxy}
import unquote(__MODULE__)
end
end

@doc """
Function used to prepare module for mocking/asserting.
Expand All @@ -14,7 +21,7 @@ defmodule Mockery.Macro do
#### Prepare for mocking
defmodule Foo do
import Mockery.Macro
use Mockery.Macro
def foo do
mockable(Bar).bar()
Expand All @@ -30,7 +37,7 @@ defmodule Mockery.Macro do
# lib/foo.ex
defmodule Foo do
import Mockery.Macro
use Mockery.Macro
def foo do
mockable(Bar, by: BarGlobalMock).bar()
Expand Down
4 changes: 2 additions & 2 deletions lib/mockery/proxy/macro_proxy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ defmodule Mockery.Proxy.MacroProxy do
Mockery.Macro.mockable/2 needs to be invoked directly in other function.
You can't use it in module attribute:
import Mockery.Macro
use Mockery.Macro
@foo mockable(Foo)
def bar, do: @foo.foo()
Instead use:
import Mockery.Macro
use Mockery.Macro
def bar, do: mockable(Foo).foo()
"""
Expand Down
2 changes: 1 addition & 1 deletion lib/mockery/utils.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Mockery.Utils do
@moduledoc false

import Mockery.Macro
use Mockery.Macro

alias Mockery.Error

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Mockery.Mixfile do
use Mix.Project

@version "2.3.2"
@version "2.3.3"

def project do
[
Expand Down
42 changes: 22 additions & 20 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
%{
"beam_inspect": {:hex, :beam_inspect, "0.1.1", "4d7af19d9d61a8a4e7b9b5f0e05044aeebcba3f89199584aba026d4acc3bb516", [:mix], [], "hexpm"},
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [], [], "hexpm"},
"certifi": {:hex, :certifi, "1.2.1", "c3904f192bd5284e5b13f20db3ceac9626e14eeacfbb492e19583cf0e37b22be", [:rebar3], []},
"credo": {:hex, :credo, "0.10.2", "03ad3a1eff79a16664ed42fc2975b5e5d0ce243d69318060c626c34720a49512", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"},
"dialyxir": {:hex, :dialyxir, "0.5.1", "b331b091720fd93e878137add264bac4f644e1ddae07a70bf7062c7862c4b952", [:mix], []},
"earmark": {:hex, :earmark, "1.4.3", "364ca2e9710f6bff494117dbbd53880d84bebb692dafc3a78eb50aa3183f2bfd", [:mix], [], "hexpm"},
"ex_doc": {:hex, :ex_doc, "0.21.2", "caca5bc28ed7b3bdc0b662f8afe2bee1eedb5c3cf7b322feeeb7c6ebbde089d6", [:mix], [{:earmark, "~> 1.3.3 or ~> 1.4", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"},
"excoveralls": {:hex, :excoveralls, "0.7.1", "3dd659db19c290692b5e2c4a2365ae6d4488091a1ba58f62dcbdaa0c03da5491", [:mix], [{:exjsx, ">= 3.0.0", [hex: :exjsx, optional: false]}, {:hackney, ">= 0.12.0", [hex: :hackney, optional: false]}]},
"exjsx": {:hex, :exjsx, "4.0.0", "60548841e0212df401e38e63c0078ec57b33e7ea49b032c796ccad8cde794b5c", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, optional: false]}]},
"hackney": {:hex, :hackney, "1.8.6", "21a725db3569b3fb11a6af17d5c5f654052ce9624219f1317e8639183de4a423", [:rebar3], [{:certifi, "1.2.1", [hex: :certifi, optional: false]}, {:idna, "5.0.2", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, optional: false]}]},
"idna": {:hex, :idna, "5.0.2", "ac203208ada855d95dc591a764b6e87259cb0e2a364218f215ad662daa8cd6b4", [:rebar3], [{:unicode_util_compat, "0.2.0", [hex: :unicode_util_compat, optional: false]}]},
"jason": {:hex, :jason, "1.1.1", "d3ccb840dfb06f2f90a6d335b536dd074db748b3e7f5b11ab61d239506585eb2", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"},
"jsx": {:hex, :jsx, "2.8.2", "7acc7d785b5abe8a6e9adbde926a24e481f29956dd8b4df49e3e4e7bcc92a018", [:mix, :rebar3], []},
"makeup": {:hex, :makeup, "1.0.0", "671df94cf5a594b739ce03b0d0316aa64312cee2574b6a44becb83cd90fb05dc", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"},
"makeup_elixir": {:hex, :makeup_elixir, "0.14.0", "cf8b7c66ad1cff4c14679698d532f0b5d45a3968ffbcbfd590339cb57742f1ae", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], []},
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], []},
"nimble_parsec": {:hex, :nimble_parsec, "0.5.3", "def21c10a9ed70ce22754fdeea0810dafd53c2db3219a0cd54cf5526377af1c6", [:mix], [], "hexpm"},
"beam_inspect": {:hex, :beam_inspect, "0.1.1", "4d7af19d9d61a8a4e7b9b5f0e05044aeebcba3f89199584aba026d4acc3bb516", [:mix], [], "hexpm", "8a85192c98c909c8506b5061d4277783e329f0c77c83e1027a58cf445c9d3944"},
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"},
"certifi": {:hex, :certifi, "1.2.1", "c3904f192bd5284e5b13f20db3ceac9626e14eeacfbb492e19583cf0e37b22be", [:rebar3], [], "hexpm", "54afd282e544833fbafbe2e845333132b5f092aee19dfae3a8fd970f67e0f2e6"},
"credo": {:hex, :credo, "0.10.2", "03ad3a1eff79a16664ed42fc2975b5e5d0ce243d69318060c626c34720a49512", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "539596b6774069260d5938aa73042a2f5157e1c0215aa35f5a53d83889546d14"},
"dialyxir": {:hex, :dialyxir, "0.5.1", "b331b091720fd93e878137add264bac4f644e1ddae07a70bf7062c7862c4b952", [:mix], [], "hexpm", "6c32a70ed5d452c6650916555b1f96c79af5fc4bf286997f8b15f213de786f73"},
"earmark": {:hex, :earmark, "1.4.3", "364ca2e9710f6bff494117dbbd53880d84bebb692dafc3a78eb50aa3183f2bfd", [:mix], [], "hexpm", "8cf8a291ebf1c7b9539e3cddb19e9cef066c2441b1640f13c34c1d3cfc825fec"},
"earmark_parser": {:hex, :earmark_parser, "1.4.40", "f3534689f6b58f48aa3a9ac850d4f05832654fe257bf0549c08cc290035f70d5", [:mix], [], "hexpm", "cdb34f35892a45325bad21735fadb88033bcb7c4c296a999bde769783f53e46a"},
"ex_doc": {:hex, :ex_doc, "0.34.1", "9751a0419bc15bc7580c73fde506b17b07f6402a1e5243be9e0f05a68c723368", [:mix], [{:earmark_parser, "~> 1.4.39", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.0", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1 or ~> 1.0", [hex: :makeup_erlang, repo: "hexpm", optional: false]}, {:makeup_html, ">= 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: true]}], "hexpm", "d441f1a86a235f59088978eff870de2e815e290e44a8bd976fe5d64470a4c9d2"},
"excoveralls": {:hex, :excoveralls, "0.7.1", "3dd659db19c290692b5e2c4a2365ae6d4488091a1ba58f62dcbdaa0c03da5491", [:mix], [{:exjsx, ">= 3.0.0", [hex: :exjsx, repo: "hexpm", optional: false]}, {:hackney, ">= 0.12.0", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "d4f74abd75a8cbdf5224f7a20c1f9ebfe639187d1911fafc70dd299f53d9fad0"},
"exjsx": {:hex, :exjsx, "4.0.0", "60548841e0212df401e38e63c0078ec57b33e7ea49b032c796ccad8cde794b5c", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, repo: "hexpm", optional: false]}], "hexpm", "32e95820a97cffea67830e91514a2ad53b888850442d6d395f53a1ac60c82e07"},
"hackney": {:hex, :hackney, "1.8.6", "21a725db3569b3fb11a6af17d5c5f654052ce9624219f1317e8639183de4a423", [:rebar3], [{:certifi, "1.2.1", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "5.0.2", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm", "677281a6eae416f4ca7980c8dfd1ee85c7cf445ed932c5d7f7909946d1cac37a"},
"idna": {:hex, :idna, "5.0.2", "ac203208ada855d95dc591a764b6e87259cb0e2a364218f215ad662daa8cd6b4", [:rebar3], [{:unicode_util_compat, "0.2.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "352278f34e274a2e000b20e41aa74d6bc9a622407dc043b550a3cd3b7ac5457d"},
"jason": {:hex, :jason, "1.1.1", "d3ccb840dfb06f2f90a6d335b536dd074db748b3e7f5b11ab61d239506585eb2", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "639645cfac325e34938167b272bae0791fea3a34cf32c29525abf1d323ed4c18"},
"jsx": {:hex, :jsx, "2.8.2", "7acc7d785b5abe8a6e9adbde926a24e481f29956dd8b4df49e3e4e7bcc92a018", [:mix, :rebar3], [], "hexpm", "b4c5d3230b397c8d95579e4a3d72826bb6463160130ccf4182f5be8579b5f44c"},
"makeup": {:hex, :makeup, "1.1.2", "9ba8837913bdf757787e71c1581c21f9d2455f4dd04cfca785c70bbfff1a76a3", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cce1566b81fbcbd21eca8ffe808f33b221f9eee2cbc7a1706fc3da9ff18e6cac"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.2", "627e84b8e8bf22e60a2579dad15067c755531fea049ae26ef1020cad58fe9578", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "41193978704763f6bbe6cc2758b84909e62984c7752b3784bd3c218bb341706b"},
"makeup_erlang": {:hex, :makeup_erlang, "1.0.0", "6f0eff9c9c489f26b69b61440bf1b238d95badae49adac77973cbacae87e3c2e", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "ea7a9307de9d1548d2a72d299058d1fd2339e3d398560a0e46c27dab4891e4d2"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], [], "hexpm", "7a4c8e1115a2732a67d7624e28cf6c9f30c66711a9e92928e745c255887ba465"},
"nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"},
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], []},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.2.0", "dbbccf6781821b1c0701845eaf966c9b6d83d7c3bfc65ca2b78b88b8678bfa35", [:rebar3], []},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], [], "hexpm", "4f8805eb5c8a939cf2359367cb651a3180b27dfb48444846be2613d79355d65e"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.2.0", "dbbccf6781821b1c0701845eaf966c9b6d83d7c3bfc65ca2b78b88b8678bfa35", [:rebar3], [], "hexpm", "1582b67d11eab7c57228449a422a401d2295f53edc21ce69edfd16d10d59d2ba"},
}
2 changes: 1 addition & 1 deletion test/mockery/macro_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Mockery.MacroTest do
use ExUnit.Case, async: true
import Mockery.Macro, only: [mockable: 1, mockable: 2]
use Mockery.Macro

test "mockable/2 dev env (atom erlang mod)" do
assert mockable(:a, env: :dev) == :a
Expand Down
2 changes: 1 addition & 1 deletion test/mockery/multiprocess_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule Mockery.MultiprocessTest do
end

defmodule B do
import Mockery.Macro
use Mockery.Macro

def run do
mockable(A).run
Expand Down
4 changes: 2 additions & 2 deletions test/mockery/proxy/macro_proxy_test.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
defmodule Mockery.Proxy.MacroProxyTest do
use ExUnit.Case, async: true
use Mockery.Macro
import Mockery
import Mockery.Assertions
import Mockery.Macro

alias Mockery.Proxy.MacroProxy
alias Mockery.Utils
Expand Down Expand Up @@ -149,7 +149,7 @@ defmodule Mockery.Proxy.MacroProxyTest do

############### INVALID USAGE ###############
defmodule Macro.InvalidUsage do
import Mockery.Macro
use Mockery.Macro
@invalid mockable(Dummy)

def invalid, do: @invalid.fun1()
Expand Down
4 changes: 2 additions & 2 deletions test/support/integration_test/tested.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
defmodule IntegrationTest.Tested do
@moduledoc false
if :erlang.system_info(:otp_release) >= '21', do: @compile(:tuple_calls)
if :erlang.system_info(:otp_release) >= ~c"21", do: @compile(:tuple_calls)

import Mockery.Macro
use Mockery.Macro

@mock1 Mockery.of(IntegrationTest.Mocked)
@mock2 Application.get_env(:mockery, :integration_test, IntegrationTest.Mocked)
Expand Down

0 comments on commit b31c256

Please sign in to comment.