using NewLife.Studio.Core;
using Xunit;
namespace NewLife.Studio.Core.Tests;
public class StudioExceptionTests
{
[Fact]
public void Constructor_With_Message_Sets_Message_Property()
{
var ex = new StudioException("An error occurred");
Assert.Equal("An error occurred", ex.Message);
Assert.Null(ex.InnerException);
}
[Fact]
public void Constructor_With_Message_And_InnerException_Sets_Both()
{
var inner = new InvalidOperationException("inner error");
var ex = new StudioException("outer error", inner);
Assert.Equal("outer error", ex.Message);
Assert.Same(inner, ex.InnerException);
}
[Fact]
public void StudioException_Inherits_From_Exception()
{
var ex = new StudioException("test");
Assert.IsAssignableFrom<Exception>(ex);
}
[Fact]
public void Can_Be_Caught_As_StudioException()
{
var caught = false;
try
{
throw new StudioException("test throw");
}
catch (StudioException)
{
caught = true;
}
Assert.True(caught);
}
[Fact]
public void Can_Be_Caught_As_Exception()
{
var caught = false;
try
{
throw new StudioException("test throw");
}
catch (Exception)
{
caught = true;
}
Assert.True(caught);
}
[Fact]
public void Empty_Message_Is_Allowed()
{
var ex = new StudioException("");
Assert.Equal("", ex.Message);
}
[Fact]
public void Null_InnerException_Is_Allowed()
{
var ex = new StudioException("test", null!);
Assert.Null(ex.InnerException);
}
[Fact]
public void StackTrace_Is_Preserved()
{
StudioException? ex = null;
try
{
throw new StudioException("test");
}
catch (StudioException caught)
{
ex = caught;
}
Assert.NotNull(ex);
Assert.NotNull(ex.StackTrace);
}
[Fact]
public void Nested_Inner_Exception_Chain_Works()
{
var innerMost = new ArgumentException("innermost");
var inner = new StudioException("middle", innerMost);
var outer = new StudioException("outer", inner);
Assert.Same(inner, outer.InnerException);
Assert.Same(innerMost, outer.InnerException?.InnerException);
}
}
|