using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.Text; using NUnit.Framework; namespace Developpez.Dotnet.Tests { [TestFixture] public class ExceptionExtensionsTests { [Test] public void IsCausedBy_ReturnsTrueForExceptionOfSpecifiedType() { var ex = new FooException("Foo"); Assert.IsTrue(ex.IsCausedBy()); } [Test] public void IsCausedBy_ReturnsFalseForExceptionNotOfSpecifiedTypeWithNoInnerException() { var ex = new FooException("Foo"); Assert.IsFalse(ex.IsCausedBy()); } [Test] public void IsCausedBy_ReturnsTrueForExceptionNotOfSpecifiedTypeWithInnerExceptionOfSpecifiedType() { var ex = new FooException("Foo", new BarException("Bar")); Assert.IsTrue(ex.IsCausedBy()); Assert.IsTrue(ex.IsCausedBy()); } [Test] public void IsCausedBy_ReturnsTrueForExceptionNotOfSpecifiedTypeWithDeepInnerExceptionOfSpecifiedType() { var ex = new FooException("Foo", new BarException("Bar", new BazException("Baz"))); Assert.IsTrue(ex.IsCausedBy()); Assert.IsTrue(ex.IsCausedBy()); Assert.IsTrue(ex.IsCausedBy()); } [Serializable] class FooException : Exception { public FooException() { } public FooException(string message) : base(message) { } public FooException(string message, Exception inner) : base(message, inner) { } protected FooException( SerializationInfo info, StreamingContext context) : base(info, context) { } } [Serializable] class BarException : Exception { public BarException() { } public BarException(string message) : base(message) { } public BarException(string message, Exception inner) : base(message, inner) { } protected BarException( SerializationInfo info, StreamingContext context) : base(info, context) { } } [Serializable] class BazException : Exception { public BazException() { } public BazException(string message) : base(message) { } public BazException(string message, Exception inner) : base(message, inner) { } protected BazException( SerializationInfo info, StreamingContext context) : base(info, context) { } } } }