|
1 | 1 | Code.require_file "../test_helper.exs", __DIR__
|
2 | 2 |
|
3 |
| -defmodule IEx.Helpers.Test do |
| 3 | +defmodule IEx.HelpersTest do |
4 | 4 | use IEx.Case
|
5 | 5 |
|
| 6 | + @doc """ |
| 7 | + Test function 1 |
| 8 | + """ |
| 9 | + def test_fun_1 |
| 10 | + |
| 11 | + @doc """ |
| 12 | + Test function 2 |
| 13 | + """ |
| 14 | + def test_fun_1(arg) |
| 15 | + |
6 | 16 | test "h helper" do
|
7 |
| - assert "# IEx.Helpers\n\nWelcome to Interactive Elixir" <> _ = capture_iex("h") |
| 17 | + assert "# IEx.Helpers\n\nWelcome to Interactive Elixir" <> _ |
| 18 | + = capture_iex("h") |
8 | 19 | end
|
9 | 20 |
|
10 | 21 | test "h helper module" do
|
11 |
| - assert "# Enumerable\n\nThis is the protocol used by the `Enum` module" <> _ = capture_iex("h Enumerable") |
| 22 | + assert "# IEx.Helpers\n\nWelcome to Interactive Elixir" <> _ |
| 23 | + = capture_iex("h IEx.Helpers") |
12 | 24 | assert capture_iex("h :whatever") == "Could not load module :whatever: nofile\n:ok"
|
13 | 25 | end
|
14 | 26 |
|
15 | 27 | test "h helper function" do
|
16 |
| - expand_1_re = %r/\* def expand\(path\)\n\nConverts the path to an absolute one and expands/ |
17 |
| - expand_2_re = %r/\* def expand\(path, relative_to\)\n\nExpands the path relative to the path given as the second argument/ |
| 28 | + doc_1 = "* def test_fun_1()\n\nTest function 1\n:ok" |
| 29 | + doc_2 = "* def test_fun_1(arg)\n\nTest function 2\n:ok" |
18 | 30 |
|
19 |
| - assert capture_iex("h Path.expand/1") =~ expand_1_re |
20 |
| - assert capture_iex("h Path.expand/2") =~ expand_2_re |
| 31 | + assert capture_iex("h IEx.HelpersTest.test_fun_1/0") == doc_1 |
| 32 | + assert capture_iex("h IEx.HelpersTest.test_fun_1/1") == doc_2 |
21 | 33 |
|
22 |
| - output = capture_iex("h Path.expand") |
23 |
| - assert output =~ expand_1_re |
24 |
| - assert output =~ expand_2_re |
| 34 | + output = capture_iex("h IEx.HelpersTest.test_fun_1") |
| 35 | + assert :binary.match(output, doc_1) |
| 36 | + assert :binary.match(output, doc_2) |
25 | 37 |
|
26 | 38 | assert capture_iex("h pwd") == "* def pwd()\n\nPrints the current working directory.\n\n:ok"
|
27 | 39 | end
|
| 40 | + |
| 41 | + test "t helper" do |
| 42 | + assert "** (UndefinedFunctionError) undefined function: IEx.Helpers.t/0" <> _ |
| 43 | + = capture_iex("t") |
| 44 | + |
| 45 | + assert capture_iex("t ExUnit") == "No types for ExUnit have been found\n:ok" |
| 46 | + |
| 47 | + # Test that it shows at least two types |
| 48 | + assert Enum.count(capture_iex("t Enum") |> String.split("\n"), fn line -> |
| 49 | + String.starts_with? line, "@type" |
| 50 | + end) >= 2 |
| 51 | + |
| 52 | + assert "@type t() :: " <> _ |
| 53 | + = capture_iex("t Enum.t") |
| 54 | + assert capture_iex("t Enum.t") == capture_iex("t Enum.t/0") |
| 55 | + end |
| 56 | + |
| 57 | + test "s helper" do |
| 58 | + assert "** (UndefinedFunctionError) undefined function: IEx.Helpers.s/0" <> _ |
| 59 | + = capture_iex("s") |
| 60 | + |
| 61 | + assert capture_iex("s ExUnit") == "No specs for ExUnit have been found\n:ok" |
| 62 | + |
| 63 | + # Test that it shows at least two specs |
| 64 | + assert Enum.count(capture_iex("s Enum") |> String.split("\n"), fn line -> |
| 65 | + String.starts_with? line, "@spec" |
| 66 | + end) >= 2 |
| 67 | + |
| 68 | + assert Enum.count(capture_iex("s Enum.all?") |> String.split("\n"), fn line -> |
| 69 | + String.starts_with? line, "@spec" |
| 70 | + end) >= 2 |
| 71 | + |
| 72 | + assert capture_iex("s Enum.all?/1") == "@spec all?(t()) :: boolean()\n:ok" |
| 73 | + assert capture_iex("s list_to_binary") == "@spec list_to_binary(iolist()) :: binary()\n:ok" |
| 74 | + end |
| 75 | + |
| 76 | + test "v helper" do |
| 77 | + assert capture_iex("v") == ":ok" |
| 78 | + assert capture_iex("1\n2\nv") == String.rstrip """ |
| 79 | + 1 |
| 80 | + 2 |
| 81 | + 1: 1 |
| 82 | + #=> 1 |
| 83 | +
|
| 84 | + 2: 2 |
| 85 | + #=> 2 |
| 86 | +
|
| 87 | + :ok |
| 88 | + """ |
| 89 | + |
| 90 | + assert "** (RuntimeError) Out of bounds" <> _ |
| 91 | + = capture_iex("v(0)") |
| 92 | + assert capture_iex("1\n2\nv(2)") == "1\n2\n2" |
| 93 | + assert capture_iex("1\n2\nv(2)") == capture_iex("1\n2\nv(-1)") |
| 94 | + end |
| 95 | + |
| 96 | + test "flush helper" do |
| 97 | + assert capture_iex("self() <- :hello\nflush") == ":hello\n:hello\n:ok" |
| 98 | + end |
| 99 | + |
| 100 | + test "pwd helper" do |
| 101 | + assert capture_iex("pwd") =~ %r"lib[\\/]iex\n:ok$" |
| 102 | + end |
| 103 | + |
| 104 | + test "ls helper" do |
| 105 | + assert [":ok", "ebin", "lib", "mix.exs", "test"] |
| 106 | + = capture_iex("ls") |
| 107 | + |> String.split |
| 108 | + |> Enum.map(String.strip(&1)) |
| 109 | + |> Enum.sort |
| 110 | + assert capture_iex("ls \"~\"") == capture_iex("ls System.user_home") |
| 111 | + end |
| 112 | + |
| 113 | + test "import_file helper" do |
| 114 | + File.write! "dot-iex", "variable = :hello\nimport IO" |
| 115 | + |
| 116 | + assert "** (UndefinedFunctionError) undefined function: IEx.Helpers.variable/0" <> _ |
| 117 | + = capture_iex("variable") |
| 118 | + assert "** (UndefinedFunctionError) undefined function: IEx.Helpers.puts/1" <> _ |
| 119 | + = capture_iex("puts \"hi\"") |
| 120 | + |
| 121 | + assert capture_iex("import_file \"dot-iex\"\nvariable\nputs \"hi\"") |
| 122 | + == "nil\n:hello\nhi\n:ok" |
| 123 | + after |
| 124 | + File.rm! "dot-iex" |
| 125 | + end |
| 126 | + |
| 127 | + test "import_file nested" do |
| 128 | + File.write! "dot-iex-1", "variable = :hello\nimport IO" |
| 129 | + File.write! "dot-iex", "parent = true\nimport_file \"dot-iex-1\"" |
| 130 | + |
| 131 | + assert "** (UndefinedFunctionError) undefined function: IEx.Helpers.parent/0" <> _ |
| 132 | + = capture_iex("parent") |
| 133 | + assert "** (UndefinedFunctionError) undefined function: IEx.Helpers.variable/0" <> _ |
| 134 | + = capture_iex("variable") |
| 135 | + assert "** (UndefinedFunctionError) undefined function: IEx.Helpers.puts/1" <> _ |
| 136 | + = capture_iex("puts \"hi\"") |
| 137 | + |
| 138 | + assert capture_iex("import_file \"dot-iex\"\nvariable\nputs \"hi\"\nparent") |
| 139 | + == "nil\n:hello\nhi\n:ok\ntrue" |
| 140 | + after |
| 141 | + File.rm "dot-iex-1" |
| 142 | + File.rm! "dot-iex" |
| 143 | + end |
| 144 | + |
| 145 | + test "m helper" do |
| 146 | + regexes = [ |
| 147 | + %r/^:application\s+.+application\.beam$/, |
| 148 | + %r/^:code\s+.+code\.beam$/, |
| 149 | + %r/^Kernel\s+.+Elixir\.Kernel\.beam$/, |
| 150 | + ] |
| 151 | + |
| 152 | + assert Enum.count(capture_iex("m") |> String.split("\n"), fn line -> |
| 153 | + Enum.any? regexes, fn re -> |
| 154 | + Regex.match? re, line |
| 155 | + end |
| 156 | + end) >= 2 |
| 157 | + end |
| 158 | + |
| 159 | + test "c helper" do |
| 160 | + assert "** (UndefinedFunctionError) undefined function: Helpers_test_module.run/0" <> _ |
| 161 | + = capture_iex("Helpers_test_module.run") |
| 162 | + |
| 163 | + File.write! "test-module-code.ex", test_module_code |
| 164 | + assert capture_iex("c \"test-module-code.ex\"\nHelpers_test_module.run") == "[Helpers_test_module]\nrun!\n:ok" |
| 165 | + after |
| 166 | + File.rm "test-module-code.ex" |
| 167 | + File.rm! "Elixir.Helpers_test_module.beam" |
| 168 | + true = :code.delete Helpers_test_module |
| 169 | + :code.purge Helpers_test_module |
| 170 | + end |
| 171 | + |
| 172 | + test "c helper multiple modules" do |
| 173 | + File.write! "test-module-code.ex", test_module_code <> "\n" <> another_test_module |
| 174 | + assert capture_iex("c(\"test-module-code.ex\") |> Enum.sort\nHelpers_test_module.run\nAnother_test_module.hello") |
| 175 | + == "[Another_test_module,Helpers_test_module]\nrun!\n:ok\nworld\n:ok" |
| 176 | + after |
| 177 | + File.rm "test-module-code.ex" |
| 178 | + File.rm "Elixir.Helpers_test_module.beam" |
| 179 | + true = :code.delete Helpers_test_module |
| 180 | + :code.purge Helpers_test_module |
| 181 | + |
| 182 | + File.rm! "Elixir.Another_test_module.beam" |
| 183 | + true = :code.delete Another_test_module |
| 184 | + :code.purge Another_test_module |
| 185 | + end |
| 186 | + |
| 187 | + test "c helper list" do |
| 188 | + File.write! "test-module-code-1.ex", test_module_code |
| 189 | + File.write! "test-module-code-2.ex", another_test_module |
| 190 | + assert capture_iex("c([\"test-module-code-1.ex\", \"test-module-code-2.ex\"]) |> Enum.sort\nHelpers_test_module.run\nAnother_test_module.hello") |
| 191 | + == "[Another_test_module,Helpers_test_module]\nrun!\n:ok\nworld\n:ok" |
| 192 | + after |
| 193 | + File.rm "test-module-code-1.ex" |
| 194 | + File.rm "test-module-code-2.ex" |
| 195 | + |
| 196 | + File.rm "Elixir.Helpers_test_module.beam" |
| 197 | + true = :code.delete Helpers_test_module |
| 198 | + :code.purge Helpers_test_module |
| 199 | + |
| 200 | + File.rm! "Elixir.Another_test_module.beam" |
| 201 | + true = :code.delete Another_test_module |
| 202 | + :code.purge Another_test_module |
| 203 | + end |
| 204 | + |
| 205 | + test "l helper" do |
| 206 | + File.write! "test-module-code.ex", test_module_code |
| 207 | + input = """ |
| 208 | + c "test-module-code.ex" |
| 209 | + File.write! "test-module-code.ex", "defmodule Helpers_test_module do end" |
| 210 | + l Helpers_test_module |
| 211 | + Helpers_test_module.run |
| 212 | + """ |
| 213 | + assert capture_iex(input) == "[Helpers_test_module]\n:ok\n{:module,Helpers_test_module}\nrun!\n:ok" |
| 214 | + |
| 215 | + assert capture_iex("l :non_existent_module") == "{:error,:nofile}" |
| 216 | + after |
| 217 | + File.rm "test-module-code.ex" |
| 218 | + File.rm! "Elixir.Helpers_test_module.beam" |
| 219 | + |
| 220 | + # FIXME: This errors out with "Module 'Elixir.Helpers_test_module' must be purged before loading" |
| 221 | + #true = :code.delete Helpers_test_module |
| 222 | + |
| 223 | + :code.purge Helpers_test_module |
| 224 | + end |
| 225 | + |
| 226 | + test "r helper" do |
| 227 | + assert capture_iex("r") == "[]" |
| 228 | + assert capture_iex("r Kernel") == ":nosource" |
| 229 | + assert "** (UndefinedFunctionError) undefined function: :non_existent_module.module_info/1" <> _ |
| 230 | + = capture_iex("r :non_existent_module") |
| 231 | + |
| 232 | + File.write! "test-module-code.ex", test_module_code |
| 233 | + # FIXME: `r Helpers_test_module` returns :nosource |
| 234 | + assert capture_iex("c \"test-module-code.ex\"\nr Helpers_test_module") == "[Helpers_test_module]\n[Helpers_test_module]" |
| 235 | + |
| 236 | + assert capture_iex("r") == "[Helpers_test_module]" |
| 237 | + after |
| 238 | + File.rm "test-module-code.ex" |
| 239 | + File.rm! "Elixir.Helpers_test_module.beam" |
| 240 | + |
| 241 | + # FIXME: This errors out with "Module 'Elixir.Helpers_test_module' must be purged before loading" |
| 242 | + #true = :code.delete Helpers_test_module |
| 243 | + |
| 244 | + :code.purge Helpers_test_module |
| 245 | + end |
| 246 | + |
| 247 | + defp test_module_code do |
| 248 | + """ |
| 249 | + defmodule Helpers_test_module do |
| 250 | + def run do |
| 251 | + IO.puts "run!" |
| 252 | + end |
| 253 | + end |
| 254 | + """ |
| 255 | + end |
| 256 | + |
| 257 | + defp another_test_module do |
| 258 | + """ |
| 259 | + defmodule Another_test_module do |
| 260 | + def hello do |
| 261 | + IO.puts "world" |
| 262 | + end |
| 263 | + end |
| 264 | + """ |
| 265 | + end |
28 | 266 | end
|
0 commit comments