feat: 初始化NewLife Studio项目,完成基础框架与数据管理模块
何炳宏 authored at 2026-05-26 12:09:09
2.23 KiB
NewLife.Studio
using Avalonia.Controls;
using NewLife.Studio.Core;
using Xunit;

namespace NewLife.Studio.Modules.DataStudio.Tests;

public class DataStudioModuleTests
{
    private readonly DataStudioModule _module = new();

    [Fact]
    public void Id_Returns_DataStudio()
    {
        Assert.Equal("data-studio", _module.Id);
    }

    [Fact]
    public void DisplayName_Returns_NormalChineseName()
    {
        Assert.Equal("数据管理", _module.DisplayName);
    }

    [Fact]
    public void Icon_Is_NotNull()
    {
        Assert.NotNull(_module.Icon);
    }

    [Fact]
    public void Icon_Is_NonEmpty()
    {
        Assert.NotEmpty(_module.Icon);
    }

    [Fact]
    public void Order_Returns_Zero()
    {
        Assert.Equal(0, _module.Order);
    }

    [Fact]
    public void GetView_Returns_NonNullControl()
    {
        var view = _module.GetView();
        Assert.NotNull(view);
        Assert.IsAssignableFrom<Control>(view);
    }

    [Fact]
    public async Task OnActivateAsync_Completes_WithoutError()
    {
        var exception = await Record.ExceptionAsync(() => _module.OnActivateAsync());
        Assert.Null(exception);
    }

    [Fact]
    public async Task OnActivateAsync_WithCancellationToken_Completes()
    {
        using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(1));
        var exception = await Record.ExceptionAsync(() => _module.OnActivateAsync(cts.Token));
        Assert.Null(exception);
    }

    [Fact]
    public async Task OnDeactivateAsync_Completes_WithoutError()
    {
        var exception = await Record.ExceptionAsync(() => _module.OnDeactivateAsync());
        Assert.Null(exception);
    }

    [Fact]
    public async Task OnDeactivateAsync_WithCancellationToken_Completes()
    {
        using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(1));
        var exception = await Record.ExceptionAsync(() => _module.OnDeactivateAsync(cts.Token));
        Assert.Null(exception);
    }

    [Fact]
    public void Implements_IStudioModule()
    {
        Assert.IsAssignableFrom<IStudioModule>(_module);
    }

    [Fact]
    public void GetView_EachCall_ReturnsNewInstance()
    {
        var view1 = _module.GetView();
        var view2 = _module.GetView();
        Assert.NotSame(view1, view2);
    }
}