mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-23 03:04:29 +08:00
- 将所有小写的命名空间导入更正为首字母大写格式 - 统一 GFramework 框架的命名空间引用规范 - 修复 core、ecs、godot 等模块的命名空间导入错误 - 标准化文档示例代码中的 using 语句格式 - 确保所有文档中的命名空间引用保持一致性 - 更新 global using 语句以匹配正确的命名空间格式
117 lines
3.8 KiB
C#
117 lines
3.8 KiB
C#
using GFramework.Core.Abstractions.Logging;
|
|
using GFramework.Core.Logging.Formatters;
|
|
using NUnit.Framework;
|
|
|
|
namespace GFramework.Core.Tests.Logging;
|
|
|
|
/// <summary>
|
|
/// 测试 DefaultLogFormatter 的功能和行为
|
|
/// </summary>
|
|
[TestFixture]
|
|
public class DefaultLogFormatterTests
|
|
{
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
_formatter = new DefaultLogFormatter();
|
|
}
|
|
|
|
private DefaultLogFormatter _formatter = null!;
|
|
|
|
[Test]
|
|
public void Format_WithBasicEntry_ShouldFormatCorrectly()
|
|
{
|
|
var timestamp = new DateTime(2026, 2, 26, 10, 30, 45, 123);
|
|
var entry = new LogEntry(timestamp, LogLevel.Info, "TestLogger", "Test message", null, null);
|
|
|
|
var result = _formatter.Format(entry);
|
|
|
|
Assert.That(result, Does.Contain("[2026-02-26 10:30:45.123]"));
|
|
Assert.That(result, Does.Contain("INFO"));
|
|
Assert.That(result, Does.Contain("[TestLogger]"));
|
|
Assert.That(result, Does.Contain("Test message"));
|
|
}
|
|
|
|
[Test]
|
|
public void Format_WithException_ShouldIncludeException()
|
|
{
|
|
var exception = new InvalidOperationException("Test exception");
|
|
var entry = new LogEntry(DateTime.UtcNow, LogLevel.Error, "TestLogger", "Error occurred", exception, null);
|
|
|
|
var result = _formatter.Format(entry);
|
|
|
|
Assert.That(result, Does.Contain("Error occurred"));
|
|
Assert.That(result, Does.Contain("InvalidOperationException"));
|
|
Assert.That(result, Does.Contain("Test exception"));
|
|
}
|
|
|
|
[Test]
|
|
public void Format_WithProperties_ShouldIncludeProperties()
|
|
{
|
|
var properties = new Dictionary<string, object?>
|
|
{
|
|
["UserId"] = 12345,
|
|
["UserName"] = "TestUser"
|
|
};
|
|
var entry = new LogEntry(DateTime.UtcNow, LogLevel.Info, "TestLogger", "User action", null, properties);
|
|
|
|
var result = _formatter.Format(entry);
|
|
|
|
Assert.That(result, Does.Contain("User action"));
|
|
Assert.That(result, Does.Contain("|"));
|
|
Assert.That(result, Does.Contain("UserId=12345"));
|
|
Assert.That(result, Does.Contain("UserName=TestUser"));
|
|
}
|
|
|
|
[Test]
|
|
public void Format_WithNullProperty_ShouldHandleNull()
|
|
{
|
|
var properties = new Dictionary<string, object?>
|
|
{
|
|
["Key1"] = null
|
|
};
|
|
var entry = new LogEntry(DateTime.UtcNow, LogLevel.Info, "TestLogger", "Test message", null, properties);
|
|
|
|
var result = _formatter.Format(entry);
|
|
|
|
Assert.That(result, Does.Contain("Key1="));
|
|
}
|
|
|
|
[Test]
|
|
public void Format_WithAllLogLevels_ShouldFormatCorrectly()
|
|
{
|
|
var levels = new[]
|
|
{ LogLevel.Trace, LogLevel.Debug, LogLevel.Info, LogLevel.Warning, LogLevel.Error, LogLevel.Fatal };
|
|
var expectedStrings = new[] { "TRACE", "DEBUG", "INFO", "WARNING", "ERROR", "FATAL" };
|
|
|
|
for (int i = 0; i < levels.Length; i++)
|
|
{
|
|
var entry = new LogEntry(DateTime.UtcNow, levels[i], "TestLogger", "Test", null, null);
|
|
var result = _formatter.Format(entry);
|
|
|
|
Assert.That(result, Does.Contain(expectedStrings[i]));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void Format_WithLongMessage_ShouldNotTruncate()
|
|
{
|
|
var longMessage = new string('A', 1000);
|
|
var entry = new LogEntry(DateTime.UtcNow, LogLevel.Info, "TestLogger", longMessage, null, null);
|
|
|
|
var result = _formatter.Format(entry);
|
|
|
|
Assert.That(result, Does.Contain(longMessage));
|
|
}
|
|
|
|
[Test]
|
|
public void Format_WithSpecialCharacters_ShouldPreserveCharacters()
|
|
{
|
|
var message = "Test\nNew\tLine\r\nSpecial: <>&\"'";
|
|
var entry = new LogEntry(DateTime.UtcNow, LogLevel.Info, "TestLogger", message, null, null);
|
|
|
|
var result = _formatter.Format(entry);
|
|
|
|
Assert.That(result, Does.Contain(message));
|
|
}
|
|
} |