Skip to content

Commit

Permalink
examples
Browse files Browse the repository at this point in the history
  • Loading branch information
JB committed Mar 21, 2023
1 parent df1a0e2 commit 0003077
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
26 changes: 26 additions & 0 deletions test/examples/humanized_weather_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule MyApp.HumanizedWeatherTest do
use ExUnit.Case, async: true

alias MyApp.HumanizedWeather
alias MyApp.WeatherAPI

test "gets and formats temperature" do
protomock =
ProtoMock.new()
|> ProtoMock.expect(&WeatherAPI.temperature/2, fn _lat_long -> {:ok, 30} end)

assert HumanizedWeather.display_temp({50.06, 19.94}, protomock) ==
"Current temperature is 30 degrees"

ProtoMock.verify!(protomock)
end

test "gets and formats humidity" do
protomock =
ProtoMock.new()
|> ProtoMock.stub(&WeatherAPI.humidity/2, fn _lat_long -> {:ok, 60} end)

assert HumanizedWeather.display_humidity({50.06, 19.94}, protomock) ==
"Current humidity is 60%"
end
end
59 changes: 59 additions & 0 deletions test/support/weather_api.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
defprotocol MyApp.WeatherAPI do
@type lat_long :: {float(), float()}
@type api_result :: {:ok, integer()} | {:error, any()}

@spec temperature(t(), lat_long()) :: api_result()
def temperature(weather_api, lat_long)

@spec humidity(t(), lat_long()) :: api_result()
def humidity(weather_api, lat_long)
end

defmodule AcmeWeather.ApiConfig do
defstruct []
end

defmodule AcmeWeather.Client do
def get_temperature(_lat, _long, _config), do: 0
def get_humidity(_lat, _long, _config), do: 0
end

defimpl MyApp.WeatherAPI, for: AcmeWeather.ApiConfig do
def temperature(api_config, {lat, long}) do
AcmeWeather.Client.get_temperature(lat, long, api_config)
end

def humidity(api_config, {lat, long}) do
AcmeWeather.Client.get_humidity(lat, long, api_config)
end
end

defimpl MyApp.WeatherAPI, for: ProtoMock do
def temperature(protomock, lat_long) do
ProtoMock.invoke(protomock, &MyApp.WeatherAPI.temperature/2, [lat_long])
end

def humidity(protomock, lat_long) do
ProtoMock.invoke(protomock, &MyApp.WeatherAPI.humidity/2, [lat_long])
end
end

# ProtoMock.create_impl(MyApp.WeatherAPI)

defmodule MyApp.HumanizedWeather do
alias MyApp.WeatherAPI

def display_temp({lat, long}, weather_api \\ default_weather_api()) do
{:ok, temp} = WeatherAPI.temperature(weather_api, {lat, long})
"Current temperature is #{temp} degrees"
end

def display_humidity({lat, long}, weather_api \\ default_weather_api()) do
{:ok, humidity} = WeatherAPI.humidity(weather_api, {lat, long})
"Current humidity is #{humidity}%"
end

defp default_weather_api() do
Application.get_env(MyApp, :weather_api)
end
end

0 comments on commit 0003077

Please sign in to comment.