Permalink
Browse files

Ensure aliases can be overriden

  • Loading branch information...
1 parent 9947f6a commit c926b7f9430c83670fc7f9b206b948ee1c1bbdf7 @josevalim josevalim committed May 31, 2012
View
@@ -78,29 +78,25 @@ translate_each({refer, Line, Args}, S) ->
elixir_errors:deprecation(Line, S#elixir_scope.filename, "refer is deprecated, please use alias instead"),
translate_each({alias, Line, Args}, S);
-translate_each({alias, Line, [Ref|T]}, S) ->
- KV = case T of
- [NotEmpty] -> NotEmpty;
- [] -> []
- end,
+translate_each({alias, Line, [Ref]}, S) ->
+ translate_each({alias, Line, [Ref,[]]}, S);
+translate_each({alias, Line, [Ref, KV]}, S) ->
{ TRef, SR } = translate_each(Ref, S),
case TRef of
{ atom, _, Old } ->
{ New, SF } = case orddict:find(as, KV) of
{ ok, false } ->
{ Old, SR };
- { ok, true } ->
+ Opt when Opt == { ok, true }; Opt == error ->
{ elixir_aliases:last(Old), SR };
{ ok, Other } ->
{ TOther, SA } = translate_each(Other, SR),
case TOther of
{ atom, _, Atom } -> { Atom, SA };
_ -> syntax_error(Line, S#elixir_scope.filename, "invalid args for alias, expected an atom or alias as argument")
- end;
- error ->
- { elixir_aliases:last(Old), SR }
+ end
end,
{ { nil, Line }, SF#elixir_scope{
@@ -110,12 +106,10 @@ translate_each({alias, Line, [Ref|T]}, S) ->
syntax_error(Line, S#elixir_scope.filename, "invalid args for alias, expected an atom or alias as argument")
end;
-translate_each({require, Line, [Ref|T]}, S) ->
- KV = case T of
- [NotEmpty] -> NotEmpty;
- [] -> []
- end,
+translate_each({require, Line, [Ref]}, S) ->
+ translate_each({require, Line, [Ref, []]}, S);
+translate_each({require, Line, [Ref, KV]}, S) ->
{ TRef, SR } = translate_each(Ref, S),
As = case orddict:find(as, KV) of
@@ -0,0 +1,29 @@
+Code.require_file "../../test_helper", __FILE__
+
+defmodule Kernel.AliasTest.Nested do
+ def value, do: 1
+end
+
+defmodule Kernel.AliasTest do
+ alias Kernel.AliasTest.Nested, as: Nested
+
+ use ExUnit.Case
+
+ test :alias_erlang do
+ alias Erlang.lists, as: MyList
+ assert MyList.flatten([1,[2],3]) == [1,2,3]
+ assert __MAIN__.MyList.Bar == :"__MAIN__.MyList.Bar"
+ assert MyList.Bar == :"__MAIN__.lists.Bar"
+ end
+
+ test :double_alias do
+ alias Kernel.AliasTest.Nested, as: Nested2
+ assert Nested.value == 1
+ assert Nested2.value == 1
+ end
+
+ test :overwriten_alias do
+ alias List, as: Nested
+ assert Nested.flatten([[13]]) == [13]
+ end
+end
@@ -1,11 +1,11 @@
Code.require_file "../../test_helper", __FILE__
-defmodule Kernel.RequireTest.Nested do
+defmodule Kernel.MacrosTest.Nested do
defmacro value, do: 1
end
-defmodule Kernel.RequireTest do
- require Kernel.RequireTest.Nested, as: Nested
+defmodule Kernel.MacrosTest do
+ require Kernel.MacrosTest.Nested, as: Nested
use ExUnit.Case
@@ -21,17 +21,12 @@ defmodule Kernel.RequireTest do
quote do: 1 + unquote(value)
end
- test :require_erlang do
- require Erlang.lists, as: MyList
- assert MyList.flatten([1,[2],3]) == [1,2,3]
- assert __MAIN__.MyList.Bar == :"__MAIN__.MyList.Bar"
- assert MyList.Bar == :"__MAIN__.lists.Bar"
+ test :require do
+ assert Kernel.MacrosTest.Nested.value == 1
end
- test :double_named_require do
- require Kernel.RequireTest.Nested, as: Nested2
+ test :require_with_alias do
assert Nested.value == 1
- assert Nested2.value == 1
end
test :default_required do
@@ -43,19 +38,19 @@ defmodule Kernel.RequireTest do
assert result
end
- test :locals_are_always_required do
+ test :locals_macros do
assert __MODULE__.my_macro == 2
end
- test :locals_and_private_are_always_required do
+ test :local_but_private_macro do
assert my_private_macro == 4
end
- test :locals_with_default_are_always_required do
+ test :local_with_defaults_macro do
assert my_macro_with_default == 6
end
- test :cannot_be_called_dynamically_even_if_required do
+ test :macros_cannot_be_called_dynamically do
x = Nested
assert_raise UndefinedFunctionError, fn -> x.value end
end

0 comments on commit c926b7f

Please sign in to comment.