Skip to content

Commit 316e75a

Browse files
Unit tests
1 parent 69d0d07 commit 316e75a

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# if IOS
2+
using ObjCRuntime;
3+
using Sentry.Cocoa;
4+
5+
namespace Sentry.Tests.Platforms.iOS;
6+
7+
public class RuntimeMarshalManagedExceptionIntegrationTests
8+
{
9+
private class Fixture
10+
{
11+
public IHub Hub { get; } = Substitute.For<IHub, IDisposable>();
12+
public IRuntime Runtime { get; } = Substitute.For<IRuntime>();
13+
14+
public RuntimeMarshalManagedExceptionIntegration GetSut() => new(Runtime);
15+
}
16+
17+
private readonly Fixture _fixture = new();
18+
private SentryOptions SentryOptions { get; } = new();
19+
20+
[Fact]
21+
public void Handle_WithException_CaptureEvent()
22+
{
23+
var sut = _fixture.GetSut();
24+
sut.Register(_fixture.Hub, SentryOptions);
25+
26+
sut.Handle(this, new MarshalManagedExceptionEventArgs{Exception = new Exception()});
27+
28+
_fixture.Hub.Received(1).CaptureEvent(Arg.Any<SentryEvent>());
29+
}
30+
31+
[Fact]
32+
public void Handle_WithException_IsHandledFalse()
33+
{
34+
var sut = _fixture.GetSut();
35+
sut.Register(_fixture.Hub, SentryOptions);
36+
37+
var exception = new Exception();
38+
sut.Handle(this, new MarshalManagedExceptionEventArgs{Exception = exception});
39+
Assert.Equal(false, exception.Data[Mechanism.HandledKey]);
40+
Assert.True(exception.Data.Contains(Mechanism.MechanismKey));
41+
42+
var stackTraceFactory = Substitute.For<ISentryStackTraceFactory>();
43+
var exceptionProcessor = new MainExceptionProcessor(SentryOptions, () => stackTraceFactory);
44+
var @event = new SentryEvent(exception);
45+
46+
exceptionProcessor.Process(exception, @event);
47+
Assert.NotNull(@event.SentryExceptions?.ToList().Single(p => p.Mechanism?.Handled == false));
48+
}
49+
50+
[Fact]
51+
public void Handle_NoException_NoCaptureEvent()
52+
{
53+
var sut = _fixture.GetSut();
54+
sut.Register(_fixture.Hub, SentryOptions);
55+
56+
sut.Handle(this, new MarshalManagedExceptionEventArgs());
57+
58+
_fixture.Hub.DidNotReceive().CaptureEvent(Arg.Any<SentryEvent>());
59+
}
60+
61+
[Fact]
62+
public void Register_UnhandledException_Subscribes()
63+
{
64+
var sut = _fixture.GetSut();
65+
sut.Register(_fixture.Hub, SentryOptions);
66+
67+
_fixture.Runtime.Received().MarshalManagedException += sut.Handle;
68+
}
69+
}
70+
#endif

test/Sentry.Tests/SentryOptionsTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
namespace Sentry.Tests;
22
#if NETFRAMEWORK
33
using Sentry.PlatformAbstractions;
4+
#elif IOS || MACCATALYST
5+
using Sentry.Cocoa;
46
#endif
57
public partial class SentryOptionsTests
68
{
@@ -285,6 +287,16 @@ public void DisableNetFxInstallationsEventProcessor_RemovesDisableNetFxInstallat
285287
}
286288
#endif
287289

290+
#if IOS || MACCATALYST
291+
[Fact]
292+
public void DisableRuntimeMarshalManagedExceptionCapture_RemovesRuntimeMarshalManagedExceptionIntegration()
293+
{
294+
var sut = new SentryOptions();
295+
sut.DisableRuntimeMarshalManagedExceptionCapture();
296+
Assert.DoesNotContain(sut.Integrations,
297+
p => p is RuntimeMarshalManagedExceptionIntegration);
298+
}
299+
#else
288300
[Fact]
289301
public void DisableAppDomainUnhandledExceptionCapture_RemovesAppDomainUnhandledExceptionIntegration()
290302
{
@@ -293,6 +305,7 @@ public void DisableAppDomainUnhandledExceptionCapture_RemovesAppDomainUnhandledE
293305
Assert.DoesNotContain(sut.Integrations,
294306
p => p is AppDomainUnhandledExceptionIntegration);
295307
}
308+
#endif
296309

297310
[Fact]
298311
public void DisableTaskUnobservedTaskExceptionCapture_UnobservedTaskExceptionIntegration()
@@ -581,12 +594,21 @@ public void GetAllEventProcessors_NoAdding_FirstReturned_DuplicateDetectionProce
581594
_ = Assert.IsType<DuplicateEventDetectionEventProcessor>(sut.GetAllEventProcessors().First());
582595
}
583596

597+
#if IOS || MACCATALYST
598+
[Fact]
599+
public void Integrations_Includes_RuntimeMarshalManagedExceptionIntegration()
600+
{
601+
var sut = new SentryOptions();
602+
Assert.Contains(sut.Integrations, i => i.GetType() == typeof(RuntimeMarshalManagedExceptionIntegration));
603+
}
604+
#else
584605
[Fact]
585606
public void Integrations_Includes_AppDomainUnhandledExceptionIntegration()
586607
{
587608
var sut = new SentryOptions();
588609
Assert.Contains(sut.Integrations, i => i.GetType() == typeof(AppDomainUnhandledExceptionIntegration));
589610
}
611+
#endif
590612

591613
[Fact]
592614
public void Integrations_Includes_AppDomainProcessExitIntegration()

0 commit comments

Comments
 (0)