|
| 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 |
0 commit comments