@@ -20,6 +20,7 @@ return function()
2020 describe (" tracker" , function ()
2121 before (function ()
2222 mock_fs .mock ()
23+ mock .mock (sys )
2324 tracker = require " googleanalytics.tracker"
2425 queue = require " googleanalytics.internal.queue"
2526
@@ -33,6 +34,7 @@ return function()
3334 after (function ()
3435 mock_fs .unmock ()
3536 mock .unmock (queue )
37+ mock .unmock (sys )
3638 package.loaded [" googleanalytics.tracker" ] = nil
3739 package.loaded [" googleanalytics.internal.queue" ] = nil
3840 end )
@@ -140,18 +142,82 @@ return function()
140142 assert (queue_params [2 ] == t .base_params .. " &t=timing&utc=category2&utv=variable2&utt=20&utl=label2" )
141143 end )
142144
143- it (" should be able to enable automatic crash reporting" , function ()
145+ it (" should be able to enable automatic hard crash reporting" , function ()
144146 if not crash then
145147 return
146148 end
147149 local t = tracker .create (" UA-87977671-1" )
148150 crash .write_dump ()
149151 t .enable_crash_reporting (true )
150-
152+
153+ assert (# queue_params == 1 )
154+ local params = split_params (queue_params [1 ])
155+ assert (params .exf == " 1" ) -- fatal
156+ assert (params .exd )
157+ end )
158+
159+ it (" should be able to enable automatic soft crash reporting" , function ()
160+ if not crash then
161+ return
162+ end
163+ local soft_crash
164+ sys .set_error_handler .replace (function (handler )
165+ soft_crash = handler
166+ end )
167+
168+ local t = tracker .create (" UA-87977671-1" )
169+ t .enable_crash_reporting (true )
170+ soft_crash (" lua" , " message" , " traceback" )
171+
172+ assert (# queue_params == 1 )
173+ local params = split_params (queue_params [1 ])
174+ assert (params .exf == " 0" ) -- non-fatal
175+ assert (params .exd == " message" )
176+ assert (params .exd )
177+ end )
178+
179+ it (" should be able to forward hard crashes when automatic crash reporting is enabled" , function ()
180+ if not crash then
181+ return
182+ end
183+
184+ local on_hard_crash_invoked = false
185+ local function on_hard_crash () on_hard_crash_invoked = true end
186+
187+ local t = tracker .create (" UA-87977671-1" )
188+ crash .write_dump ()
189+ t .enable_crash_reporting (true , nil , on_hard_crash )
190+
151191 assert (# queue_params == 1 )
152192 local params = split_params (queue_params [1 ])
153193 assert (params .exf == " 1" )
154194 assert (params .exd )
195+ assert (on_hard_crash_invoked )
196+ end )
197+
198+ it (" should be able to forward soft crashes when automatic crash reporting is enabled" , function ()
199+ if not crash then
200+ return
201+ end
202+
203+ local on_soft_crash_invoked = false
204+ local function on_soft_crash () on_soft_crash_invoked = true end
205+
206+ local soft_crash
207+ sys .set_error_handler .replace (function (handler )
208+ soft_crash = handler
209+ end )
210+
211+ local t = tracker .create (" UA-87977671-1" )
212+ t .enable_crash_reporting (true , on_soft_crash , nil )
213+ soft_crash (" lua" , " message" , " traceback" )
214+
215+ assert (# queue_params == 1 )
216+ local params = split_params (queue_params [1 ])
217+ assert (params .exf == " 0" ) -- non-fatal
218+ assert (params .exd == " message" )
219+ assert (params .exd )
220+ assert (on_soft_crash_invoked )
155221 end )
156222 end )
157223end
0 commit comments