|
1 | 1 | Code.require_file "../test_helper.exs", __DIR__ |
2 | 2 |
|
3 | | -defmodule Kernel.ImportAvailable do |
4 | | - defmacro flatten do |
5 | | - [flatten: 1] |
6 | | - end |
7 | | -end |
8 | | - |
9 | | -defmodule Kernel.ImportOnlyTest do |
| 3 | +defmodule Kernel.ImportTest do |
10 | 4 | use ExUnit.Case, async: true |
11 | 5 |
|
12 | | - test :import_with_only do |
13 | | - require Kernel.ImportAvailable |
14 | | - import :lists, only: Kernel.ImportAvailable.flatten |
15 | | - assert flatten([1, [2], 3]) == [1, 2, 3] |
| 6 | + defmodule ImportAvailable do |
| 7 | + defmacro flatten do |
| 8 | + [flatten: 1] |
| 9 | + end |
16 | 10 | end |
17 | 11 |
|
18 | | - test :import_all do |
| 12 | + |
| 13 | + test "import all" do |
19 | 14 | import :lists |
20 | 15 | assert flatten([1, [2], 3]) == [1, 2, 3] |
21 | 16 | end |
22 | 17 |
|
23 | | - test :import_except_none do |
| 18 | + test "import except none" do |
24 | 19 | import :lists, except: [] |
25 | 20 | assert flatten([1, [2], 3]) == [1, 2, 3] |
26 | 21 | end |
27 | 22 |
|
28 | | - test :import_with_except_erlang do |
| 23 | + test "import except one" do |
29 | 24 | import :lists, except: [each: 2] |
30 | 25 | assert flatten([1, [2], 3]) == [1, 2, 3] |
31 | 26 | end |
32 | 27 |
|
| 28 | + test "import only via macro" do |
| 29 | + require ImportAvailable |
| 30 | + import :lists, only: ImportAvailable.flatten |
| 31 | + assert flatten([1, [2], 3]) == [1, 2, 3] |
| 32 | + end |
| 33 | + |
33 | 34 | defmacrop dynamic_opts do |
34 | | - [except: [each: 2]] |
| 35 | + [only: [flatten: 1]] |
35 | 36 | end |
36 | 37 |
|
37 | | - test :import_with_dynamic_opts do |
| 38 | + test "import with options via macro" do |
38 | 39 | import :lists, dynamic_opts |
39 | 40 | assert flatten([1, [2], 3]) == [1, 2, 3] |
40 | 41 | end |
41 | | -end |
42 | | - |
43 | | -defmodule Kernel.DoubleImportTest do |
44 | | - use ExUnit.Case, async: true |
45 | 42 |
|
46 | | - test :import_double_except do |
47 | | - import :lists, except: [flatten: 1] |
| 43 | + test "import with double except" do |
| 44 | + import :lists, except: [duplicate: 2] |
48 | 45 | import :lists, except: [each: 2] |
49 | 46 | assert append([1], [2, 3]) == [1, 2, 3] |
50 | | - assert flatten([1, [2], 3]) == [1, [2], 3] |
| 47 | + # Buggy local duplicate is untouched |
| 48 | + assert duplicate([1], 2) == [1] |
51 | 49 | end |
52 | 50 |
|
53 | | - def flatten(list), do: list |
54 | | -end |
55 | | - |
56 | | -defmodule Kernel.MessedBitwise do |
57 | | - defmacro bnot(x), do: x |
58 | | - defmacro bor(x, _), do: x |
59 | | -end |
60 | | - |
61 | | -defmodule Kernel.Underscored do |
62 | | - def hello(x), do: x |
63 | | - def __underscore__(x), do: x |
64 | | -end |
65 | | - |
66 | | -defmodule Kernel.ExplicitUnderscored do |
67 | | - def __underscore__(x), do: x * 2 |
68 | | -end |
| 51 | + defmodule Underscored do |
| 52 | + def hello(x), do: x |
| 53 | + def __underscore__(x), do: x |
| 54 | + end |
69 | 55 |
|
70 | | -defmodule Kernel.ImportUnderscoreTest do |
71 | | - use ExUnit.Case, async: true |
| 56 | + defmodule ExplicitUnderscored do |
| 57 | + def __underscore__(x), do: x * 2 |
| 58 | + end |
72 | 59 |
|
73 | | - test :includes_only_underscore do |
74 | | - import Kernel.Underscored, only: [__underscore__: 1] |
| 60 | + test "import only with underscore" do |
| 61 | + import Underscored, only: [__underscore__: 1] |
75 | 62 | assert __underscore__(3) == 3 |
76 | 63 | end |
77 | 64 |
|
78 | | - import :all, Kernel.ExplicitUnderscored |
79 | | - |
80 | | - test :does_not_include_underscored do |
81 | | - import Kernel.Underscored |
| 65 | + test "import all includes underscores" do |
| 66 | + import :all, ExplicitUnderscored |
| 67 | + import Underscored |
82 | 68 | assert __underscore__(2) == 4 |
83 | 69 | end |
84 | 70 |
|
85 | | - test :includes_remaining do |
86 | | - import Kernel.Underscored |
| 71 | + test "import non underscored" do |
| 72 | + import :all, ExplicitUnderscored |
| 73 | + import Underscored |
87 | 74 | assert hello(2) == 2 |
88 | 75 | end |
89 | | -end |
90 | 76 |
|
91 | | -defmodule Kernel.ImportMacrosTest do |
92 | | - use ExUnit.Case, async: true |
| 77 | + defmodule MessedBitwise do |
| 78 | + defmacro bnot(x), do: x |
| 79 | + defmacro bor(x, _), do: x |
| 80 | + end |
93 | 81 |
|
94 | 82 | import :macros, Bitwise |
95 | 83 |
|
96 | | - test :import_true do |
97 | | - assert band(1, 1) == 1 |
98 | | - assert bor(0, 1) == 1 |
99 | | - assert bnot(0) == -1 |
100 | | - end |
101 | | - |
102 | | - test :function_import_with_only do |
| 84 | + test "conflicing imports with only and except" do |
103 | 85 | import :macros, Bitwise, except: [bnot: 1] |
104 | | - import :macros, Kernel.MessedBitwise, only: [bnot: 1] |
| 86 | + import :macros, MessedBitwise, only: [bnot: 1] |
105 | 87 | assert bnot(0) == 0 |
106 | 88 | assert bor(0, 1) == 1 |
107 | 89 | end |
108 | 90 |
|
109 | | - # This test is asserting that the requires done |
110 | | - # inside the function do not affect outer ones. |
111 | | - test :import_true_not_affected do |
| 91 | + # This test is asserting that the imports in the |
| 92 | + # test above do not affect this test. |
| 93 | + test "imports from other functions do not leak" do |
112 | 94 | assert band(1, 1) == 1 |
113 | 95 | assert bor(0, 1) == 1 |
114 | 96 | assert bnot(0) == -1 |
115 | 97 | end |
116 | | -end |
117 | 98 |
|
118 | | -defmodule Kernel.MultipleImportTest do |
119 | | - use ExUnit.Case, async: true |
120 | | - |
121 | | - test :import_ambiguous do |
| 99 | + test "import ambiguous" do |
122 | 100 | # Simply make sure that we can indeed import functions with |
123 | 101 | # the same name and arity from different modules without the |
124 | 102 | # import itself causing any errors. |
125 | 103 | import List |
126 | 104 | import String |
127 | 105 | end |
128 | 106 |
|
129 | | - test :import_many do |
| 107 | + test "import many" do |
130 | 108 | [import(List), import(String)] |
131 | 109 | assert capitalize("foo") == "Foo" |
132 | 110 | assert flatten([1, [2], 3]) == [1, 2, 3] |
133 | 111 | end |
| 112 | + |
| 113 | + test "import lexical on if" do |
| 114 | + if false do |
| 115 | + import :lists |
| 116 | + flatten([1, [2], 3]) |
| 117 | + flunk |
| 118 | + else |
| 119 | + # Buggy local duplicate is untouched |
| 120 | + assert duplicate([1], 2) == [1] |
| 121 | + end |
| 122 | + end |
| 123 | + |
| 124 | + test "import lexical on case" do |
| 125 | + case true do |
| 126 | + false -> |
| 127 | + import :lists |
| 128 | + flatten([1, [2], 3]) |
| 129 | + flunk |
| 130 | + true -> |
| 131 | + # Buggy local duplicate is untouched |
| 132 | + assert duplicate([1], 2) == [1] |
| 133 | + end |
| 134 | + end |
| 135 | + |
| 136 | + test "import lexical on try" do |
| 137 | + try do |
| 138 | + import :lists |
| 139 | + flatten([1, [2], 3]) |
| 140 | + catch |
| 141 | + _ -> flatten([:a, [:b], :c]) |
| 142 | + end |
| 143 | + |
| 144 | + # Buggy local duplicate is untouched |
| 145 | + assert duplicate([1], 2) == [1] |
| 146 | + end |
| 147 | + |
| 148 | + defp duplicate(list, _), do: list |
134 | 149 | end |
0 commit comments