Skip to content

Commit

Permalink
Ensure aliases can be overriden
Browse files Browse the repository at this point in the history
  • Loading branch information
José Valim committed May 31, 2012
1 parent 9947f6a commit c926b7f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 29 deletions.
22 changes: 8 additions & 14 deletions src/elixir_translator.erl
Expand Up @@ -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{
Expand All @@ -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
Expand Down
29 changes: 29 additions & 0 deletions test/elixir/kernel/alias_test.exs
@@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit c926b7f

Please sign in to comment.