应文浩wenhao.ying@xiaobao100.com bce2612b64 feat: 完善具体实现
2026-04-06 15:25:34 +08:00

48 lines
1.5 KiB
C#

using FluentAssertions;
using FreeCode.Tools;
using FreeCode.Tests.Unit.Helpers;
using Xunit;
namespace FreeCode.Tests.Unit.Tools;
public sealed class GrepToolTests : IDisposable
{
private readonly string _tempDirectory = Path.Combine(Path.GetTempPath(), "free-code-tests", Guid.NewGuid().ToString("N"));
private readonly GrepTool _sut = new();
public GrepToolTests()
{
Directory.CreateDirectory(_tempDirectory);
File.WriteAllText(Path.Combine(_tempDirectory, "alpha.txt"), "hello\nworld\nhello again");
File.WriteAllText(Path.Combine(_tempDirectory, "beta.txt"), "nothing here");
}
[Fact]
public async Task ExecuteAsync_FindsMatchingLines()
{
var result = await _sut.ExecuteAsync(new GrepToolInput("hello", _tempDirectory), TestHelper.CreateContext());
result.IsError.Should().BeFalse();
result.Data.TotalMatches.Should().Be(2);
result.Data.Matches.Should().OnlyContain(match => match.Line.Contains("hello", StringComparison.Ordinal));
}
[Fact]
public async Task ExecuteAsync_WithNoMatches_ReturnsEmpty()
{
var result = await _sut.ExecuteAsync(new GrepToolInput("absent", _tempDirectory), TestHelper.CreateContext());
result.IsError.Should().BeFalse();
result.Data.TotalMatches.Should().Be(0);
result.Data.Matches.Should().BeEmpty();
}
public void Dispose()
{
if (Directory.Exists(_tempDirectory))
{
Directory.Delete(_tempDirectory, recursive: true);
}
}
}