Skip to content

Commit a8d2e45

Browse files
committed
Add docs for IEx test framework
1 parent ecd3c19 commit a8d2e45

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

lib/iex/test/iex/interaction_test.exs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ defmodule IEx.InteractionTest do
1414
end
1515

1616
test "exception" do
17-
assert capture_iex("1 + :atom\n:this_is_still_working") =~ %r/^#{iex_format_exception(ArithmeticError,
18-
"bad argument in arithmetic expression")}.+\n:this_is_still_working$/s
17+
exception = Regex.escape("** (ArithmeticError) bad argument in arithmetic expression")
18+
assert capture_iex("1 + :atom\n:this_is_still_working")
19+
=~ %r/^#{exception}.+\n:this_is_still_working$/s
1920
end
2021

2122
test "empty history at the start" do

lib/iex/test/test_helper.exs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,28 @@
22
ExUnit.start []
33

44
defmodule IEx.Case do
5+
@moduledoc false
6+
7+
#
8+
# Provides convenience functions for testing IEx-related functionality.
9+
# Use this module inside your test module like this:
10+
#
11+
# defmodule IEx.InteractionTest do
12+
# use IEx.Case
13+
#
14+
# test :input do
15+
# assert capture_iex("1+2") == "3"
16+
# end
17+
# end
18+
#
19+
# The environment provided by capture_iex is mostly similar to the normal IEx
20+
# session, except colors are disabled by default and .iex files are not
21+
# loaded.
22+
#
23+
# You can provide your own IEx.Options and a path to a .iex file as
24+
# additional arguments to the capture_iex function.
25+
#
26+
527
defmacro __using__(_) do
628
quote do
729
use ExUnit.Case, async: false
@@ -21,28 +43,26 @@ defmodule IEx.Case do
2143
end
2244
end
2345

46+
@doc """
47+
Starts an IEx eval loop, feeds it the provided input and returns produced
48+
output. The output is stripped of the first intro line and of any trailing
49+
whitespace.
50+
51+
Options, if provided, will be set before the eval loop is started.
52+
53+
If you provide the dot_iex_path argument, it will be passed to
54+
IEx.Server.start to be used in the normal .iex loading process.
55+
"""
2456
def capture_iex(input, options // [], dot_iex_path // "") do
2557
Enum.each options, fn { opt, value } ->
2658
IEx.Options.set(opt, value)
2759
end
2860

2961
ExUnit.CaptureIO.capture_io(input, fn ->
30-
IEx.Server.start(iex_config(dot_iex_path: dot_iex_path))
62+
IEx.Server.start(IEx.boot_config(dot_iex_path: dot_iex_path))
3163
end) |> strip_iex
3264
end
3365

34-
def iex_exception(name, message // "") do
35-
%r/#{iex_format_exception(name, message)}/
36-
end
37-
38-
def iex_format_exception(name, message // "") do
39-
"\\*\\* \\(#{Module.to_binary name}\\) (.*?)#{message}"
40-
end
41-
42-
defp iex_config(opts) do
43-
IEx.boot_config(opts)
44-
end
45-
4666
defp strip_iex(string) do
4767
string
4868
|> strip_line # strip the greeting

0 commit comments

Comments
 (0)