Skip to content

Commit 75a1373

Browse files
committed
Merge pull request #1137 from alco/919-iex-test-fmwk
Test framework for IEx
2 parents c0547a1 + d26e4b3 commit 75a1373

File tree

12 files changed

+560
-24
lines changed

12 files changed

+560
-24
lines changed

lib/elixir/lib/code.ex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,10 @@ defmodule Code do
190190
defp unpack_ast(line, forms), do: { :__block__, [line: line], forms }
191191

192192
@doc """
193-
Loads the given `file`. Accepts `relative_to` as an argument
194-
to tell where the file is located. If the file was already
195-
required/loaded, loads it again. It returns all the modules
196-
defined in the file.
193+
Loads the given `file`. Accepts `relative_to` as an argument to tell where
194+
the file is located. If the file was already required/loaded, loads it again.
195+
It returns a list of tuples { ModuleName, <<byte_code>> }, one tuple for each
196+
module defined in the file.
197197
198198
Notice that if `load_file` is invoked by different processes
199199
concurrently, the target file will be invoked concurrently
@@ -211,8 +211,8 @@ defmodule Code do
211211

212212
@doc """
213213
Requires the given `file`. Accepts `relative_to` as an argument to tell where
214-
the file is located. It returns all the modules defined in the file. If the
215-
file was already required/loaded, doesn't do anything and returns nil.
214+
the file is located. The return value is the same as that of `load_file`. If
215+
the file was already required/loaded, doesn't do anything and returns nil.
216216
217217
Notice that if `require_file` is invoked by different processes concurrently,
218218
the first process to invoke `require_file` acquires a lock and the remaining

lib/ex_unit/lib/ex_unit/callbacks.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ defmodule ExUnit.Callbacks do
2929
:ok
3030
end
3131
32+
teardown context do
33+
assert context[:hello] == "world"
34+
:ok
35+
end
36+
3237
test "always pass" do
3338
assert true
3439
end

lib/ex_unit/lib/ex_unit/capture_io.ex

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
defmodule ExUnit.CaptureIO do
22
@moduledoc %B"""
33
This module provides functionality to capture IO to test it.
4-
The way to use this module is to import them into your module.
54
65
## Examples
76
@@ -21,16 +20,16 @@ defmodule ExUnit.CaptureIO do
2120

2221
@doc """
2322
Captures IO. Returns nil in case of no output,
24-
otherwise returns the binary which is captured outputs.
23+
otherwise returns the binary which is the captured output.
2524
2625
By default, capture_io replaces the group_leader (`:stdio`)
2726
for the current process. However, the capturing of any other
2827
named device like `:stderr` is also possible globally by
2928
giving the registered device name explicitly as argument.
3029
31-
When capturing of `:stdio` and the `:capture_prompt` option
32-
is not `false`, this function captures a prompt, otherwise
33-
do not.
30+
When capturing `:stdio` and the `:capture_prompt` option is `false`,
31+
prompts (specified as arguments in IO.get* functions) are not
32+
captured.
3433
3534
A developer can set a string as an input. The default
3635
input is `:eof`.

lib/ex_unit/lib/ex_unit/cli_formatter.ex

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,24 +213,28 @@ defmodule ExUnit.CLIFormatter do
213213

214214
# Print styles
215215

216+
defp colorize(escape, string) do
217+
IO.ANSI.escape_fragment("%{#{escape}}") <> string <> IO.ANSI.escape_fragment("%{reset}")
218+
end
219+
216220
defp success(msg) do
217-
IO.ANSI.escape("%{green}" <> msg)
221+
colorize("green", msg)
218222
end
219223

220224
defp invalid(msg) do
221-
IO.ANSI.escape("%{yellow}" <> msg)
225+
colorize("yellow", msg)
222226
end
223227

224228
defp failure(msg) do
225-
IO.ANSI.escape("%{red}" <> msg)
229+
colorize("red", msg)
226230
end
227231

228232
defp error_info(msg) do
229-
IO.ANSI.escape("%{red} " <> msg)
233+
colorize("red", " " <> msg)
230234
end
231235

232236
defp location_info(msg) do
233-
IO.ANSI.escape("%{cyan} " <> msg)
237+
colorize("cyan", " " <> msg)
234238
end
235239

236240
defp stacktrace_info(msg) do

lib/iex/lib/iex.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,11 @@ defmodule IEx do
205205
:application.start(:iex)
206206
end
207207
208-
defp boot_config(opts) do
208+
@doc """
209+
Returns default config used to launch IEx. This config is also used by
210+
IEx.TestFramework.
211+
"""
212+
def boot_config(opts) do
209213
scope = :elixir.scope_for_eval(
210214
file: "iex",
211215
delegate_locals_to: IEx.Helpers

lib/iex/lib/iex/helpers.ex

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ defmodule IEx.Helpers do
233233
in the current IEx session.
234234
"""
235235
def r do
236-
Enum.map iex_reloaded, r(&1)
236+
List.flatten(Enum.map(iex_reloaded, do_r(&1)))
237237
end
238238

239239
@doc """
@@ -243,9 +243,16 @@ defmodule IEx.Helpers do
243243
are recompiled and reloaded.
244244
"""
245245
def r(module) do
246+
case do_r(module) do
247+
mods when is_list(mods) -> { module, mods }
248+
other -> other
249+
end
250+
end
251+
252+
defp do_r(module) do
246253
if source = source(module) do
247254
Process.put(:iex_reloaded, :ordsets.add_element(module, iex_reloaded))
248-
{ module, Code.load_file source }
255+
Enum.map(Code.load_file(source), fn {name, _} -> name end)
249256
else
250257
:nosource
251258
end

lib/iex/lib/iex/introspection.ex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ defmodule IEx.Introspection do
99
{ :module, _ } ->
1010
case module.__info__(:moduledoc) do
1111
{ _, binary } when is_binary(binary) ->
12-
# FIXME: add tests for `binary` containing ANSI escapes
13-
IO.write IEx.color(:info, "# #{inspect module}\n" <> binary)
12+
IO.write IEx.color(:info, "# #{inspect module}\n\n" <> binary)
1413
{ _, _ } ->
1514
IO.puts IEx.color(:error, "No docs for #{inspect module} have been found")
1615
_ ->
@@ -148,7 +147,6 @@ defmodule IEx.Introspection do
148147
defp print_doc({ { fun, _ }, _line, kind, args, doc }) do
149148
args = Enum.map_join(args, ", ", print_doc_arg(&1))
150149
IO.puts IEx.color(:info, "* #{kind} #{fun}(#{args})\n")
151-
# FIXME: add tests for `doc` containing ANSI escapes
152150
if doc, do: IO.write IEx.color(:info, doc)
153151
end
154152

lib/iex/lib/iex/server.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ defmodule IEx.Server do
157157
end
158158

159159
defp io_put(result) do
160-
# FIXME: add tests for `result` containing ANSI escapes
161160
IO.puts IEx.color(:eval_result, inspect(result, IEx.Options.get(:inspect)))
162161
end
163162

0 commit comments

Comments
 (0)