Skip to content
Browse files

Fix is_record tests to check tuple_size before accessing the first el…

…ement
  • Loading branch information...
1 parent c6ff84e commit fee5c5d762111d00301ac929a54be6264f0b1626 @devinus committed Mar 26, 2013
Showing with 24 additions and 9 deletions.
  1. +8 −4 lib/elixir/lib/kernel.ex
  2. +8 −2 lib/elixir/test/elixir/exception_test.exs
  3. +8 −3 lib/elixir/test/elixir/record_test.exs
View
12 lib/elixir/lib/kernel.ex
@@ -1558,12 +1558,14 @@ defmodule Kernel do
case __CALLER__.in_guard? do
true ->
quote do
- is_tuple(unquote(thing)) and :erlang.element(1, unquote(thing)) == unquote(kind)
+ is_tuple(unquote(thing)) and tuple_size(unquote(thing)) > 0
+ and :erlang.element(1, unquote(thing)) == unquote(kind)
end
false ->
quote do
result = unquote(thing)
- is_tuple(result) and :erlang.element(1, result) == unquote(kind)
+ is_tuple(result) and tuple_size(unquote(thing)) > 0
+ and :erlang.element(1, result) == unquote(kind)
end
end
end
@@ -1575,12 +1577,14 @@ defmodule Kernel do
case __CALLER__.in_guard? do
true ->
quote do
- is_tuple(unquote(thing)) and is_atom(:erlang.element(1, unquote(thing)))
+ is_tuple(unquote(thing)) and tuple_size(unquote(thing)) > 0
+ and is_atom(:erlang.element(1, unquote(thing)))
end
false ->
quote do
result = unquote(thing)
- is_tuple(result) and is_atom(:erlang.element(1, result))
+ is_tuple(result) and tuple_size(unquote(thing)) > 0
+ and is_atom(:erlang.element(1, result))
end
end
end
View
10 lib/elixir/test/elixir/exception_test.exs
@@ -5,7 +5,9 @@ defmodule Kernel.ExceptionTest do
test :is_exception do
assert is_exception(RuntimeError.new)
- refute is_exception({ :foo, :bar })
+ refute is_exception(empty_tuple)
+ refute is_exception(a_tuple)
+ refute is_exception(a_list)
end
test :format_entry_with_no_file_or_line do
@@ -74,4 +76,8 @@ defmodule Kernel.ExceptionTest do
test :erlang_error_message do
assert ErlangError.new(original: :sample).message == "erlang error: :sample"
end
-end
+
+ defp empty_tuple, do: {}
+ defp a_tuple, do: { :foo, :bar, :baz }
+ defp a_list, do: [ :foo, :bar, :baz ]
+end
View
11 lib/elixir/test/elixir/record_test.exs
@@ -103,6 +103,11 @@ defmodule RecordTest do
test :is_record do
assert is_record(RecordTest.FileInfo.new, RecordTest.FileInfo)
+ assert is_record(RecordTest.WithNoField.new)
+ refute is_record(empty_tuple)
+ refute is_record(a_list)
+ refute is_record(empty_tuple, RecordTest.FileInfo)
+ refute is_record(a_tuple, RecordTest.FileInfo)
refute is_record(a_list, RecordTest.FileInfo)
refute is_record(RecordTest.FileInfo.new, List)
end
@@ -177,7 +182,7 @@ defmodule RecordTest do
file_info
end
- defp a_list do
- [:a, :b, :c]
- end
+ defp empty_tuple, do: {}
+ defp a_tuple, do: { :foo, :bar, :baz }
+ defp a_list, do: [ :foo, :bar, :baz ]
end

0 comments on commit fee5c5d

Please sign in to comment.
Something went wrong with that request. Please try again.