feat: 初始化NewLife Studio项目,完成基础框架与数据管理模块
何炳宏 authored at 2026-05-26 12:09:09
2.40 KiB
NewLife.Studio
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);
    }
}