@@ -21,10 +21,10 @@ defmodule ParameterizedTest.BacktraceTest do
2121
2222 try do
2323 assert not should_fail?
24- rescue
25- e in ExUnit.AssertionError ->
24+ catch
25+ category , reason ->
2626 try do
27- ParameterizedTest.Backtrace . add_test_context ( e , __STACKTRACE__ , context )
27+ ParameterizedTest.Backtrace . add_test_context ( { category , reason } , __STACKTRACE__ , context )
2828 rescue
2929 ExUnit.AssertionError ->
3030 assert [ failing_line , parameter_line | _ ] = __STACKTRACE__
@@ -44,6 +44,20 @@ defmodule ParameterizedTest.BacktraceTest do
4444 end
4545 end
4646
47+ test "translates Erlang errors" do
48+ assert_raise ArithmeticError , fn ->
49+ context = [ file: __ENV__ . file , min_line: __ENV__ . line , raw: "| true |" ]
50+ ParameterizedTest.Backtrace . add_test_context ( { :error , :badarith } , [ ] , context )
51+ end
52+ end
53+
54+ test "turns other errors into RuntimeErrors" do
55+ assert_raise RuntimeError , fn ->
56+ context = [ file: __ENV__ . file , min_line: __ENV__ . line , raw: "| true |" ]
57+ ParameterizedTest.Backtrace . add_test_context ( { :timeout , { GenServer , :call , [ self ( ) , :slow_call , 0 ] } } , [ ] , context )
58+ end
59+ end
60+
4761 @ tag failure_with_backtrace: true
4862 param_test "points to line #{ __ENV__ . line + 4 } when a test fails" ,
4963 """
@@ -149,4 +163,83 @@ defmodule ParameterizedTest.BacktraceTest do
149163 } do
150164 assert not gets_free_shipping?
151165 end
166+
167+ @ tag failure_with_backtrace: true
168+ param_test "handles other exceptions, attribute to line #{ __ENV__ . line + 4 } " ,
169+ """
170+ | should_fail? |
171+ | false |
172+ | true |
173+ """ ,
174+ % { should_fail?: should_fail? } do
175+ if should_fail? do
176+ raise "test failed"
177+ else
178+ assert 1 == 1
179+ end
180+ end
181+
182+ @ tag failure_with_backtrace: true
183+ param_test "handles code errors, attribute to line #{ __ENV__ . line + 4 } " ,
184+ """
185+ | should_fail? |
186+ | false |
187+ | true |
188+ """ ,
189+ % { should_fail?: should_fail? } do
190+ if should_fail? do
191+ assert Code . eval_string ( "nil + 1" ) == 2
192+ else
193+ assert 1 == 1
194+ end
195+ end
196+
197+ defmodule SlowGenServer do
198+ @ moduledoc false
199+ @ behaviour GenServer
200+
201+ def start_link ( opts \\ [ ] ) , do: GenServer . start_link ( __MODULE__ , opts , name: __MODULE__ )
202+
203+ @ impl GenServer
204+ def init ( _ ) , do: { :ok , [ ] }
205+
206+ def slow_call ( pid ) do
207+ GenServer . call ( pid , :slow_call , 0 )
208+ end
209+
210+ @ impl GenServer
211+ def handle_call ( :slow_call , _from , state ) do
212+ :timer . sleep ( 1000 )
213+ { :reply , :ok , state }
214+ end
215+ end
216+
217+ @ tag failure_with_backtrace: true
218+ param_test "handles non-assertion errors too, attribute to line #{ __ENV__ . line + 4 } " ,
219+ """
220+ | should_fail? |
221+ | false |
222+ | true |
223+ """ ,
224+ % { should_fail?: should_fail? } do
225+ if should_fail? do
226+ { :ok , pid } = SlowGenServer . start_link ( )
227+ SlowGenServer . slow_call ( pid )
228+ end
229+ end
230+
231+ @ tag skip: true
232+ @ tag failure_with_backtrace: true
233+ param_feature "handles non-assertion errors in features, attribute to line #{ __ENV__ . line + 4 } " ,
234+ """
235+ | should_fail? |
236+ | false |
237+ | true |
238+ """ ,
239+ % { should_fail?: should_fail? } do
240+ if should_fail? do
241+ { :ok , pid } = SlowGenServer . start_link ( )
242+ SlowGenServer . slow_call ( pid )
243+ end
244+ end
152245end
0 commit comments