A bunch of tests.

This commit is contained in:
José Valim
2011-02-19 11:16:15 +01:00
parent 3e9a58e8a9
commit be759dbefc
9 changed files with 134 additions and 8 deletions
+2 -2
View File
@@ -4,10 +4,10 @@ object Binary
end
def to_char_list
Erlang.binary_to_list self
Erlang.binary_to_list(self)
end
def to_list
Erlang.binary_to_list self
Erlang.binary_to_list(self)
end
end
+29 -1
View File
@@ -19,8 +19,28 @@ object List
Erlang.lists.foldr(function, acc, self)
end
% Retrieves an item from the list. Negative indexes are allowed
% and they retrieve the element in the reverse order. Out of bound
% indexes raises 'function_clause error.
%
% ## Examples
%
% [1,2,3][0] % => 1
% [1,2,3][1] % => 2
% [1,2,3][2] % => 3
% [1,2,3][3] % => Raises 'function_clause error
%
% [1,2,3][-1] % => 3
% [1,2,3][-2] % => 2
% [1,2,3][-3] % => 1
% [1,2,3][-43] % => Raises 'function_clause error
%
def [](number)
Erlang.lists.nth(number + 1, self)
if number < 0
Erlang.lists.nth(length + number + 1, self)
else
Erlang.lists.nth(number + 1, self)
end
end
% Calls the function once for each element in the list.
@@ -132,7 +152,15 @@ object List
inspect
end
% Returns the list length. Also aliased to size.
%
% ## Examples
%
% [1,2,3].length % => 3
% [].size % => 0
%
def length
Erlang.length(self)
end
alias_local 'length, 'size, 0
end
+1 -1
View File
@@ -5,7 +5,7 @@ object OrderedDict
% Return a new Elixir OrderedDict. Remember that new is
% special cased by the compiler to receive an array as argument.
def new([])
{ 'elixir_orddict__, Erlang.orddict.new }
{ 'elixir_orddict__, Erlang.orddict.new() }
end
% Return a new Elixir OrderedDict given the dictionary.
+2 -2
View File
@@ -45,12 +45,12 @@ object String
% Returns the list representation of this String.
def to_list
Erlang.binary_to_list bin
Erlang.binary_to_list(bin)
end
% Returns the list of chars represantion of this String.
def to_char_list
Erlang.binary_to_list bin
Erlang.binary_to_list(bin)
end
% Returns a string representation of this string.
+36
View File
@@ -1,4 +1,28 @@
object Tuple
% Retrieves an item from the tuple. Negative indexes are allowed
% and they retrieve the element in the reverse order. Out of bound
% indexes raises 'function_clause error.
%
% ## Examples
%
% {1,2,3}[0] % => 1
% {1,2,3}[1] % => 2
% {1,2,3}[2] % => 3
% {1,2,3}[3] % => Raises 'badarg error
%
% {1,2,3}[-1] % => 3
% {1,2,3}[-2] % => 2
% {1,2,3}[-3] % => 1
% {1,2,3}[-4] % => Raises 'badarg error
%
def [](number)
if number < 0
Erlang.element(length + number + 1, self)
else
Erlang.element(number + 1, self)
end
end
def inspect
strings = to_list.map -> (x) x.inspect.to_char_list
"{#{String.new Erlang.string.join(strings, [$,, $\s])}}"
@@ -11,4 +35,16 @@ object Tuple
def to_list
Erlang.tuple_to_list(self)
end
% Returns the tuple length. Also aliased to size.
%
% ## Examples
%
% {1,2,3}.length % => 3
% {}.size % => 0
%
def length
Erlang.size(self)
end
alias_local 'length, 'size, 0
end
+6 -1
View File
@@ -156,9 +156,14 @@ build_erlang_form(Line, Object, {Export, Protected, Functions}) ->
AttrTable = Object#elixir_object__.data,
Transform = fun(X, Acc) -> [transform_attribute(Line, X)|Acc] end,
Base = ets:foldr(Transform, Functions, AttrTable),
[{attribute, Line, module, Name}, {attribute, Line, parent, Parent},
[{attribute, Line, module, Name}, {attribute, Line, parent, Parent}, {attribute, Line, compile, no_auto_import()},
{attribute, Line, export, Export}, {attribute, Line, protected, Protected} | Base].
no_auto_import() ->
{no_auto_import, [
{size, 1}, {length, 1}, {error, 1}, {throw, 1}, {exit, 1}
]}.
transform_attribute(Line, {mixins, List}) ->
{attribute, Line, mixins, lists:delete('Module::Methods', List)};
+26
View File
@@ -1,6 +1,11 @@
object ListTest
proto ExUnit::Case
def length_size_test
3 = [1,2,3].length
0 = [].size
end
def include_and_member_test
list = [1,2,3]
true = list.include?(1)
@@ -65,11 +70,30 @@ object ListTest
[8,6,4,9] = x.delete(6)
end
def brackets_test
1 = [1,2,3][0]
2 = [1,2,3][1]
3 = [1,2,3][2]
self.assert_raise 'function_clause, do
[1,2,3][3]
end
end
def brackets_negative_index_test
1 = [1,2,3][-3]
2 = [1,2,3][-2]
3 = [1,2,3][-1]
self.assert_raise 'function_clause, do
[1,2,3][-4]
end
end
def brackets_syntax_test
2 = [1,2,3][1]
2 = [1,2,3] [1]
2 = ([1] + [2,3])[1]
% Test ambiguity in method calls
2 = a_list()[1]
2 = self.a_list()[1]
2 = a_list[1]
@@ -78,10 +102,12 @@ object ListTest
[1] = self.a_list [1]
2 = 6 - 1 - self.a_list[1] - 1
% Test ambiguity in variables
a_list = [4,5,6]
5 = a_list[1]
[1] = a_list [1]
% Test ambiguity in method calls with punctuation
2 = a_list!()[1]
2 = self.a_list!()[1]
2 = a_list![1]
+2 -1
View File
@@ -1,4 +1,5 @@
Code.require "ex_unit"
Code.require "list_test"
Code.require "tuple_test"
ExUnit.run([ListTest])
ExUnit.run([ListTest, TupleTest])
+30
View File
@@ -0,0 +1,30 @@
object TupleTest
proto ExUnit::Case
def brackets_test
1 = {1,2,3}[0]
2 = {1,2,3}[1]
3 = {1,2,3}[2]
self.assert_raise 'badarg, do
{1,2,3}[3]
end
end
def brackets_negative_index_test
1 = {1,2,3}[-3]
2 = {1,2,3}[-2]
3 = {1,2,3}[-1]
self.assert_raise 'badarg, do
{1,2,3}[-4]
end
end
def to_list_test
[1,2,3] = {1,2,3}.to_list
end
def length_size_test
3 = {1,2,3}.length
0 = {}.size
end
end