Skip to content

Commit a773618

Browse files
committed
Refactor message formatting
1 parent c241b17 commit a773618

File tree

5 files changed

+57
-50
lines changed

5 files changed

+57
-50
lines changed

lib/test_server.ex

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ defmodule TestServer do
8888

8989
active_routes ->
9090
raise """
91-
The test ended before the following #{inspect(Instance)} #{inspect(instance)} route(s) received a request:
91+
#{Instance.format_instance(instance)} did not receive a request for these routes before the test ended:
9292
9393
#{Instance.format_routes(active_routes)}
9494
"""
@@ -105,7 +105,7 @@ defmodule TestServer do
105105

106106
active_websocket_handlers ->
107107
raise """
108-
The test ended before the following #{inspect(Instance)} #{inspect(instance)} websocket handler(s) received a message:
108+
#{Instance.format_instance(instance)} did not receive a frame for these websocket handlers before the test ended:
109109
110110
#{Instance.format_websocket_handlers(active_websocket_handlers)}
111111
"""
@@ -131,7 +131,7 @@ defmodule TestServer do
131131
defp instance_alive!(instance) do
132132
case Process.alive?(instance) do
133133
true -> :ok
134-
false -> raise "The #{inspect(Instance)} #{inspect(instance)} is not running"
134+
false -> raise "#{Instance.format_instance(instance)} is not running"
135135
end
136136
end
137137

@@ -202,26 +202,26 @@ defmodule TestServer do
202202
[_instance | _rest] = instances ->
203203
[{m, f, a, _} | _stacktrace] = get_stacktrace()
204204

205+
message =
206+
case function_accepts_instance_arg do
207+
true ->
208+
"Multiple #{inspect(Instance)}'s running, please pass instance to `#{inspect(m)}.#{f}/#{a}`."
209+
210+
false ->
211+
"Multiple #{inspect(Instance)}'s running."
212+
end
213+
205214
formatted_instances =
206215
instances
207216
|> Enum.map(&{&1, Instance.get_options(&1)})
208217
|> Enum.with_index()
209218
|> Enum.map_join("\n\n", fn {{instance, options}, index} ->
210219
"""
211-
##{index + 1}: #{inspect(instance)}
220+
##{index + 1}: #{Instance.format_instance(instance)}
212221
#{Enum.map_join(options[:stacktrace], "\n ", &Exception.format_stacktrace_entry/1)}")}
213222
"""
214223
end)
215224

216-
message =
217-
case function_accepts_instance_arg do
218-
true ->
219-
"Multiple #{inspect(Instance)}'s running, please pass instance to `#{inspect(m)}.#{f}/#{a}`."
220-
221-
false ->
222-
"Multiple #{inspect(Instance)}'s running."
223-
end
224-
225225
raise """
226226
#{message}
227227
@@ -358,10 +358,10 @@ defmodule TestServer do
358358

359359
cond do
360360
not (options[:scheme] == :https) ->
361-
raise "The #{inspect(Instance)} is not running with `[scheme: :https]` option"
361+
raise "#{Instance.format_instance(instance)} is not running with `[scheme: :https]` option"
362362

363363
not Keyword.has_key?(options, :x509_suite) ->
364-
raise "The #{inspect(Instance)} is running with custom SSL"
364+
raise "#{Instance.format_instance(instance)} is running with custom SSL"
365365

366366
true ->
367367
options[:x509_suite]

lib/test_server/instance.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ defmodule TestServer.Instance do
115115
end)
116116
end
117117

118+
@spec format_instance(pid()) :: binary()
119+
def format_instance(instance) do
120+
"#{inspect(__MODULE__)} #{inspect(instance)}"
121+
end
122+
118123
@spec websocket_handlers(pid()) :: [map()]
119124
def websocket_handlers(instance) do
120125
GenServer.call(instance, :websocket_handlers)

lib/test_server/plug_cowboy/handler.ex

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ defmodule TestServer.Plug.Cowboy.Handler do
3131
end
3232

3333
@impl true
34-
def websocket_handle(frame, {socket, state}) do
34+
def websocket_handle(frame, {{instance, _route_ref} = socket, state}) do
3535
case Instance.dispatch(socket, {:websocket, {:handle, frame}, state}) do
3636
{:ok, result} ->
3737
handle_reply(result, socket)
3838

3939
{:error, :not_found} ->
4040
message =
41-
"Unexpected message received for WebSocket"
41+
"#{Instance.format_instance(instance)} received an unexpected WebSocket frame"
4242
|> append_formatted_frame(frame)
4343
|> append_formatted_websocket_handlers(socket)
4444

@@ -57,7 +57,7 @@ defmodule TestServer.Plug.Cowboy.Handler do
5757

5858
defp append_formatted_frame(message, frame) do
5959
"""
60-
#{message} with frame:
60+
#{message}:
6161
6262
#{inspect(frame)}
6363
"""
@@ -72,29 +72,29 @@ defmodule TestServer.Plug.Cowboy.Handler do
7272
"""
7373
#{message}
7474
75-
#{format_websocket_handlers(websocket_handlers, instance)}
75+
#{format_websocket_handlers(websocket_handlers)}
7676
"""
7777
end
7878

79-
defp format_websocket_handlers({[], suspended_websocket_handlers}, instance) do
80-
message = "No active websocket handlers for #{inspect(Instance)} #{inspect(instance)}."
79+
defp format_websocket_handlers({[], suspended_websocket_handlers}) do
80+
message = "No active websocket handlers."
8181

8282
case suspended_websocket_handlers do
8383
[] ->
8484
message
8585

8686
websocket_handlers ->
8787
"""
88-
#{message} The following websocket handler(s) have been processed:
88+
#{message} The following websocket handlers have been processed:
8989
9090
#{Instance.format_routes(websocket_handlers)}"
9191
"""
9292
end
9393
end
9494

95-
defp format_websocket_handlers({active_websocket_handlers, _}, instance) do
95+
defp format_websocket_handlers({active_websocket_handlers, _}) do
9696
"""
97-
Active websocket handler(s) #{inspect(Instance)} #{inspect(instance)}:
97+
Active websocket handlers:
9898
9999
#{Instance.format_websocket_handlers(active_websocket_handlers)}
100100
"""

lib/test_server/plug_cowboy/plug.ex

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ defmodule TestServer.Plug.Cowboy.Plug do
1818

1919
{:error, {:not_found, conn}} ->
2020
message =
21-
"Unexpected #{conn.method} request received at #{conn.request_path}"
21+
"#{Instance.format_instance(instance)} received an unexpected #{conn.method} request at #{conn.request_path}"
2222
|> append_formatted_params(conn)
2323
|> append_formatted_routes(instance)
2424

@@ -49,29 +49,29 @@ defmodule TestServer.Plug.Cowboy.Plug do
4949
"""
5050
#{message}
5151
52-
#{format_routes(routes, instance)}
52+
#{format_routes(routes)}
5353
"""
5454
end
5555

56-
defp format_routes({[], suspended_routes}, instance) do
57-
message = "No active routes for #{inspect(Instance)} #{inspect(instance)}."
56+
defp format_routes({[], suspended_routes}) do
57+
message = "No active routes."
5858

5959
case suspended_routes do
6060
[] ->
6161
message
6262

6363
suspended_routes ->
6464
"""
65-
#{message} The following route(s) have been processed:
65+
#{message} The following routes have been processed:
6666
6767
#{Instance.format_routes(suspended_routes)}
6868
"""
6969
end
7070
end
7171

72-
defp format_routes({active_routes, _suspended_routes}, instance) do
72+
defp format_routes({active_routes, _suspended_routes}) do
7373
"""
74-
Active route(s) for #{inspect(Instance)} #{inspect(instance)}:
74+
Active routes:
7575
7676
#{Instance.format_routes(active_routes)}
7777
"""

test/test_server_test.exs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ defmodule TestServerTest do
7070
TestServer.stop()
7171
end
7272

73-
assert_raise RuntimeError, ~r/The TestServer.Instance \#PID\<[0-9.]+\> is not running/, fn ->
73+
assert_raise RuntimeError, ~r/TestServer.Instance \#PID\<[0-9.]+\> is not running/, fn ->
7474
{:ok, instance} = TestServer.start()
7575

7676
assert :ok = TestServer.stop()
@@ -131,7 +131,7 @@ defmodule TestServerTest do
131131
TestServer.url()
132132
end
133133

134-
assert_raise RuntimeError, ~r/The TestServer.Instance \#PID\<[0-9.]+\> is not running/, fn ->
134+
assert_raise RuntimeError, ~r/TestServer.Instance \#PID\<[0-9.]+\> is not running/, fn ->
135135
{:ok, instance} = TestServer.start()
136136

137137
assert :ok = TestServer.stop()
@@ -172,7 +172,7 @@ defmodule TestServerTest do
172172

173173
describe "add/3" do
174174
test "when instance not running" do
175-
assert_raise RuntimeError, ~r/The TestServer.Instance \#PID\<[0-9.]+\> is not running/, fn ->
175+
assert_raise RuntimeError, ~r/TestServer.Instance \#PID\<[0-9.]+\> is not running/, fn ->
176176
{:ok, instance} = TestServer.start()
177177

178178
assert :ok = TestServer.stop()
@@ -217,7 +217,7 @@ defmodule TestServerTest do
217217
end
218218
end
219219

220-
assert capture_io(fn -> ExUnit.run() end) =~ "Unexpected GET request received at /path."
220+
assert capture_io(fn -> ExUnit.run() end) =~ "received an unexpected GET request at /path."
221221
end
222222

223223
test "with mismatching method" do
@@ -232,7 +232,7 @@ defmodule TestServerTest do
232232
end
233233
end
234234

235-
assert capture_io(fn -> ExUnit.run() end) =~ "Unexpected GET request received at /."
235+
assert capture_io(fn -> ExUnit.run() end) =~ "received an unexpected GET request at /."
236236
end
237237

238238
test "with too many requests" do
@@ -249,7 +249,7 @@ defmodule TestServerTest do
249249
end
250250

251251
assert captured_io = capture_io(fn -> ExUnit.run() end)
252-
assert captured_io =~ "Unexpected GET request received at / with params:"
252+
assert captured_io =~ "received an unexpected GET request at / with params:"
253253
assert captured_io =~ "query_params: %{\"a\" => \"1\"}"
254254
end
255255

@@ -263,7 +263,7 @@ defmodule TestServerTest do
263263
end
264264

265265
assert capture_io(fn -> ExUnit.run() end) =~
266-
~r/The test ended before the following TestServer\.Instance \#PID\<[0-9.]+\> route\(s\) received a request/
266+
"did not receive a request for these routes before the test ended:"
267267
end
268268

269269
test "with callback plug" do
@@ -404,7 +404,7 @@ defmodule TestServerTest do
404404
TestServer.x509_suite()
405405
end
406406

407-
assert_raise RuntimeError, ~r/The TestServer\.Instance \#PID\<[0-9.]+\> is not running/, fn ->
407+
assert_raise RuntimeError, ~r/TestServer\.Instance \#PID\<[0-9.]+\> is not running/, fn ->
408408
{:ok, instance} = TestServer.start()
409409

410410
assert :ok = TestServer.stop()
@@ -417,7 +417,7 @@ defmodule TestServerTest do
417417
TestServer.start()
418418

419419
assert_raise RuntimeError,
420-
"The TestServer.Instance is not running with `[scheme: :https]` option",
420+
~r/TestServer\.Instance \#PID\<[0-9.]+\> is not running with `\[scheme: :https\]` option/,
421421
fn ->
422422
TestServer.x509_suite()
423423
end
@@ -434,9 +434,11 @@ defmodule TestServerTest do
434434

435435
TestServer.start(scheme: :https, cowboy_options: cowboy_options)
436436

437-
assert_raise RuntimeError, ~r/The TestServer\.Instance is running with custom SSL/, fn ->
438-
TestServer.x509_suite()
439-
end
437+
assert_raise RuntimeError,
438+
~r/TestServer\.Instance \#PID\<[0-9.]+\> is running with custom SSL/,
439+
fn ->
440+
TestServer.x509_suite()
441+
end
440442
end
441443
end
442444

@@ -445,7 +447,7 @@ defmodule TestServerTest do
445447
{:ok, instance} = TestServer.start()
446448
assert :ok = TestServer.stop()
447449

448-
assert_raise RuntimeError, ~r/The TestServer\.Instance \#PID\<[0-9.]+\> is not running/, fn ->
450+
assert_raise RuntimeError, ~r/TestServer\.Instance \#PID\<[0-9.]+\> is not running/, fn ->
449451
TestServer.websocket_init(instance, "/ws")
450452
end
451453
end
@@ -474,7 +476,7 @@ defmodule TestServerTest do
474476
assert {:ok, socket} = TestServer.websocket_init("/ws")
475477
assert :ok = TestServer.stop(instance)
476478

477-
assert_raise RuntimeError, ~r/The TestServer\.Instance \#PID\<[0-9.]+\> is not running/, fn ->
479+
assert_raise RuntimeError, ~r/TestServer\.Instance \#PID\<[0-9.]+\> is not running/, fn ->
478480
TestServer.websocket_handle(socket)
479481
end
480482
end
@@ -501,10 +503,10 @@ defmodule TestServerTest do
501503
end
502504

503505
assert capture_io(fn -> ExUnit.run() end) =~
504-
~r/The test ended before the following TestServer\.Instance \#PID\<[0-9.]+\> websocket handler\(s\) received a message/
506+
"did not receive a frame for these websocket handlers before the test ended:"
505507
end
506508

507-
test "when receiving unexpected message" do
509+
test "when receiving unexpected frame" do
508510
defmodule WebSocketHandleTooManyMessagesTest do
509511
use ExUnit.Case
510512

@@ -516,11 +518,11 @@ defmodule TestServerTest do
516518
assert WebSocketClient.send_message(client, "ping") == :ok
517519

518520
assert WebSocketClient.send_message(client, "ping") =~
519-
"Unexpected message received for WebSocket"
521+
"received an unexpected WebSocket frame"
520522
end
521523
end
522524

523-
assert capture_io(fn -> ExUnit.run() end) =~ "Unexpected message received for WebSocket"
525+
assert capture_io(fn -> ExUnit.run() end) =~ "received an unexpected WebSocket frame"
524526
end
525527

526528
test "with callback function raising exception" do
@@ -593,7 +595,7 @@ defmodule TestServerTest do
593595
assert {:ok, socket} = TestServer.websocket_init("/ws")
594596
assert :ok = TestServer.stop(instance)
595597

596-
assert_raise RuntimeError, ~r/The TestServer\.Instance \#PID\<[0-9.]+\> is not running/, fn ->
598+
assert_raise RuntimeError, ~r/TestServer\.Instance \#PID\<[0-9.]+\> is not running/, fn ->
597599
TestServer.websocket_info(socket)
598600
end
599601
end

0 commit comments

Comments
 (0)