Skip to content

Commit

Permalink
Improve tests and docs in STDLIB.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Mar 12, 2011
1 parent 2c7b898 commit d15459e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/file.ex
Expand Up @@ -16,7 +16,7 @@ object File
end

def join(list)
Erlang.filename.join list.map(_.to_bin)
String.new Erlang.filename.join list.map(_.to_bin)
end

def join(a, b)
Expand Down
16 changes: 16 additions & 0 deletions lib/list.ex
Expand Up @@ -37,10 +37,26 @@ object List
Erlang.lists.keyfind(key, n + 1, self)
end
% "Zips" two lists of equal length into one list of two-tuples, where the
% first element of each tuple is taken from the first list and the second
% element is taken from corresponding element in the second list.
%
% Raises an error is list sizes does not match.
%
% ## Examples
%
% ['foo, 'bar].zip [1,2] % => [{'foo,1}, {'bar,2}]
%
def zip(list)
Erlang.lists.zip(self, list)
end
% Does the opposite of `zip`.
%
% ## Examples
%
% {['foo, 'bar], [1,2]} = [{'foo, 1}, {'bar, 2}].unzip
%
def unzip
Erlang.lists.unzip(self)
end
Expand Down
6 changes: 6 additions & 0 deletions lib/ordered_dict.ex
Expand Up @@ -4,6 +4,12 @@ object OrderedDict
% Implement OrderedDict as a record. This is done mainly
% to have improved performance for the equality operator.
module Mixin
% Generates a new OrderedDict from a list of tuples
%
% ## Examples
%
% { 'a: 1, 'b: 2 } = OrderedDict.from_list(['a/1, 'b/2])
%
def from_list(list)
{ 'elixir_orddict__, Erlang.orddict.from_list(list) }
end
Expand Down
11 changes: 11 additions & 0 deletions test/elixir/file_test.ex
Expand Up @@ -18,5 +18,16 @@ object FileTest
full = File.expand_path("foo/bar")
full = File.expand_path("bar/../bar", "foo")
end

def join_test
"foo/bar" = File.join("foo", "bar")
"foo/bar/baz" = File.join(["foo", "bar", "baz"])
end

def split_test
["foo"] = File.split("foo")
["foo", "bar"] = File.split("foo/bar")
["foo", "bar", "baz"] = File.split("foo/bar/baz")
end
end

17 changes: 17 additions & 0 deletions test/elixir/list_test.ex
Expand Up @@ -87,6 +87,23 @@ object ListTest
true = [2,4,6].all? -> (i) i rem 2 == 0
end

def keyfind_test
{'foo, 1} = ['foo/1, 'bar/2].keyfind('foo, 0)
{'bar, 2} = ['foo/1, 'bar/2].keyfind('bar, 0)
false = ['foo/1, 'bar/2].keyfind('foo, 1)
false = ['foo/1, 'bar/2].keyfind('baz, 0)
end

def zip_test
[{'foo, 1}, {'bar, 2}] = ['foo, 'bar].zip([1, 2])
self.assert_error 'function_clause, -> ['foo].zip([1, 2])
end

def unzip_test
{['foo, 'bar], [1,2]} = [{'foo, 1}, {'bar, 2}].unzip
self.assert_error 'function_clause, -> [{'foo, 0}, 'bar].unzip
end
def head_test
1 = [1,2,3].head
1 = [1].head
Expand Down
4 changes: 4 additions & 0 deletions test/elixir/ordered_dict_test.ex
Expand Up @@ -12,6 +12,10 @@ object OrderedDictTest
{ 1: 11, 2: 44 } == a_dict.map -> (key, value) key * value
end

def from_list_test
{ 'a: 1, 'b: 2 } = OrderedDict.from_list(['a/1, 'b/2])
end

def match_test
{ 1: 2, 2: 4 } = { 2: 4, 1: 2 }

Expand Down

0 comments on commit d15459e

Please sign in to comment.