From be759dbefc1eae1113ea12fa2d6510481b5a3cfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 19 Feb 2011 11:16:15 +0100 Subject: [PATCH] A bunch of tests. --- lib/binary.ex | 4 ++-- lib/list.ex | 30 +++++++++++++++++++++++++++++- lib/ordered_dict.ex | 2 +- lib/string.ex | 4 ++-- lib/tuple.ex | 36 ++++++++++++++++++++++++++++++++++++ src/elixir_object.erl | 7 ++++++- test/elixir/list_test.ex | 26 ++++++++++++++++++++++++++ test/elixir/test_helper.ex | 3 ++- test/elixir/tuple_test.ex | 30 ++++++++++++++++++++++++++++++ 9 files changed, 134 insertions(+), 8 deletions(-) create mode 100644 test/elixir/tuple_test.ex diff --git a/lib/binary.ex b/lib/binary.ex index aaeb9c0e6a..52c4e72af0 100644 --- a/lib/binary.ex +++ b/lib/binary.ex @@ -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 \ No newline at end of file diff --git a/lib/list.ex b/lib/list.ex index e77a884e7b..f6b0d56484 100644 --- a/lib/list.ex +++ b/lib/list.ex @@ -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 \ No newline at end of file diff --git a/lib/ordered_dict.ex b/lib/ordered_dict.ex index 13512dbdb7..41f1527232 100644 --- a/lib/ordered_dict.ex +++ b/lib/ordered_dict.ex @@ -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. diff --git a/lib/string.ex b/lib/string.ex index e7db29c328..2d3d48987b 100644 --- a/lib/string.ex +++ b/lib/string.ex @@ -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. diff --git a/lib/tuple.ex b/lib/tuple.ex index b11ddd9812..081d91a83e 100644 --- a/lib/tuple.ex +++ b/lib/tuple.ex @@ -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 diff --git a/src/elixir_object.erl b/src/elixir_object.erl index 15cceea455..d1439533e2 100644 --- a/src/elixir_object.erl +++ b/src/elixir_object.erl @@ -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)}; diff --git a/test/elixir/list_test.ex b/test/elixir/list_test.ex index 03383b7300..73d232e02b 100644 --- a/test/elixir/list_test.ex +++ b/test/elixir/list_test.ex @@ -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] diff --git a/test/elixir/test_helper.ex b/test/elixir/test_helper.ex index 0a3dad7879..ecffe9508e 100644 --- a/test/elixir/test_helper.ex +++ b/test/elixir/test_helper.ex @@ -1,4 +1,5 @@ Code.require "ex_unit" Code.require "list_test" +Code.require "tuple_test" -ExUnit.run([ListTest]) \ No newline at end of file +ExUnit.run([ListTest, TupleTest]) \ No newline at end of file diff --git a/test/elixir/tuple_test.ex b/test/elixir/tuple_test.ex new file mode 100644 index 0000000000..3c914d20b1 --- /dev/null +++ b/test/elixir/tuple_test.ex @@ -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 \ No newline at end of file