build-flow.api/tests/BuildFlow.Workflow.Tests/Condition/ValueComparatorRegistryTests.cs

96 lines
2.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Workflow.Application.Expressions;
using Workflow.Domain.Expressions.Comparators;
namespace BuildFlow.Workflow.Tests.Condition;
using FluentAssertions;
using Xunit;
public class ValueComparatorRegistryTests
{
private readonly ValueComparatorRegistry _registry;
public ValueComparatorRegistryTests()
{
IValueComparator[] comparators =
[
new NumericComparator(),
new DateTimeComparator(),
new BooleanComparator(),
new CollectionComparator(),
new StringComparator(),
];
_registry = new ValueComparatorRegistry(comparators);
}
[Fact]
public void RegisteredOperators_ContainsAllExpected()
{
_registry.RegisteredOperators.Should().Contain(
"==", "!=", ">", "<", ">=", "<=",
"contains", "startsWith", "endsWith", "isEmpty", "in");
}
[Fact]
public void TryCompare_NumericEquals_ReturnsTrue()
{
_registry.TryCompare(10, "==", 10).Should().BeTrue();
}
[Fact]
public void TryCompare_StringEquals_ReturnsTrue()
{
_registry.TryCompare("hello", "==", "hello").Should().BeTrue();
}
[Fact]
public void TryCompare_NumericGreaterThan_ReturnsTrue()
{
_registry.TryCompare(10, ">", 5).Should().BeTrue();
}
[Fact]
public void TryCompare_StringContains_ReturnsTrue()
{
_registry.TryCompare("hello world", "contains", "world").Should().BeTrue();
}
[Fact]
public void TryCompare_UnregisteredOperator_ReturnsFalse()
{
_registry.TryCompare(10, "unknown_op", 5).Should().BeFalse();
}
[Fact]
public void TryCompare_NumericFallsThroughToString()
{
// "hello" 不是数值,NumericComparator 返回 false,StringComparator 兜底
_registry.TryCompare("hello", "==", "hello").Should().BeTrue();
}
[Fact]
public void TryCompare_BoolEquals_ReturnsTrue()
{
_registry.TryCompare(true, "==", "true").Should().BeTrue();
}
[Fact]
public void Constructor_EmptyComparators_NoOperators()
{
var registry = new ValueComparatorRegistry([]);
registry.RegisteredOperators.Should().BeEmpty();
}
[Fact]
public void TryCompare_DateTimeGreaterThan_ReturnsTrue()
{
_registry.TryCompare("2024-06-01", ">", "2024-01-01").Should().BeTrue();
}
[Fact]
public void TryCompare_NumericNotEquals_ReturnsTrue()
{
_registry.TryCompare(5, "!=", 10).Should().BeTrue();
}
}