Skip to content

Commit 32e3794

Browse files
committed
Add more typespec and examples
1 parent f83d35e commit 32e3794

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

README.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,6 @@ the test finishes running.
8787
Multiple concurrent Bypass instances are supported, all will have a different unique port. Concurrent
8888
requests are also supported on the same instance.
8989

90-
In case you need to assign a specific port to a Bypass instance to listen on, you can pass the
91-
`port` option to `Bypass.open()`:
92-
93-
```elixir
94-
bypass = Bypass.open(port: 1234)
95-
```
96-
9790
> Note: `Bypass.open/0` **must not** be called in a `setup_all` blocks due to the way Bypass verifies the expectations at the end of each
9891
> test.
9992

lib/bypass.ex

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,25 @@ defmodule Bypass do
2121
2222
Use the other functions in this module to declare which requests are handled
2323
and set expectations on the calls.
24+
25+
## Options
26+
27+
- `port` - Optional TCP port to listen to requests
28+
29+
## Examples
30+
31+
```elixir
32+
bypass = Bypass.open()
33+
```
34+
35+
Assign a specific port to a Bypass instance to listen on:
36+
37+
```elixir
38+
bypass = Bypass.open(port: 1234)
39+
```
40+
2441
"""
42+
@spec open(Keyword.t()) :: Bypass.t() | DynamicSupervisor.on_start_child()
2543
def open(opts \\ []) do
2644
case DynamicSupervisor.start_child(Bypass.Supervisor, Bypass.Instance.child_spec(opts)) do
2745
{:ok, pid} ->
@@ -51,6 +69,7 @@ defmodule Bypass do
5169
5270
Returns `:ok` on success and raises an error on failure.
5371
"""
72+
@spec verify_expectations!(Bypass.t()) :: :ok | no_return()
5473
def verify_expectations!(bypass) do
5574
verify_expectations!(test_framework(), bypass)
5675
end
@@ -101,13 +120,21 @@ defmodule Bypass do
101120
@doc """
102121
Re-opens the TCP socket on the same port. Blocks until the operation is
103122
complete.
123+
124+
```elixir
125+
Bypass.up(bypass)
126+
```
104127
"""
105128
@spec up(Bypass.t()) :: :ok | {:error, :already_up}
106129
def up(%Bypass{pid: pid}),
107130
do: Bypass.Instance.call(pid, :up)
108131

109132
@doc """
110133
Closes the TCP socket. Blocks until the operation is complete.
134+
135+
```elixir
136+
Bypass.down(bypass)
137+
```
111138
"""
112139
@spec down(Bypass.t()) :: :ok | {:error, :already_down}
113140
def down(%Bypass{pid: pid}),
@@ -157,7 +184,7 @@ defmodule Bypass do
157184
end)
158185
```
159186
"""
160-
@spec expect(Bypass.t(), (Plug.Conn.t() -> Plug.Conn.t())) :: :ok
187+
@spec expect_once(Bypass.t(), (Plug.Conn.t() -> Plug.Conn.t())) :: :ok
161188
def expect_once(%Bypass{pid: pid}, fun),
162189
do: Bypass.Instance.call(pid, {:expect_once, fun})
163190

0 commit comments

Comments
 (0)