Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ namespace ktsu.Semantics.Strings;
using System;

/// <summary>
/// Validates that the string has both the specified prefix and suffix
/// Validates that the string has both the specified prefix and suffix, without the two overlapping.
/// An empty string fails, as it does for <see cref="StartsWithAttribute"/> and <see cref="EndsWithAttribute"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public sealed class PrefixAndSuffixAttribute(string prefix, string suffix, StringComparison comparison = StringComparison.Ordinal) : NativeSemanticStringValidationAttribute
Expand Down Expand Up @@ -55,7 +56,13 @@ protected override ValidationResult ValidateValue(string value)
{
if (string.IsNullOrEmpty(value))
{
return ValidationResult.Success();
return ValidationResult.Failure($"The value must start with '{_prefix}' and end with '{_suffix}'.");
}

// The prefix and suffix must each have their own characters, so one '"' cannot be both quotes
if (value.Length < _prefix.Length + _suffix.Length)
{
return ValidationResult.Failure($"The value must start with '{_prefix}' and end with '{_suffix}' without the two overlapping.");
}

// Check prefix
Expand Down
42 changes: 42 additions & 0 deletions Semantics.Test/TextValidationAttributesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,48 @@ public void PrefixAndSuffixAttribute_MissingBoth_ShouldThrow()
Assert.ThrowsExactly<ArgumentException>(() => SemanticString<PrefixSuffixTestString>.Create<PrefixSuffixTestString>("middle"));
}

[PrefixAndSuffix("\"", "\"")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1812:Avoid uninstantiated internal classes", Justification = "Used via generic type references")]
private sealed partial record QuotedTestString : SemanticString<QuotedTestString> { }

[PrefixAndSuffix("ab", "ba")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1812:Avoid uninstantiated internal classes", Justification = "Used via generic type references")]
private sealed partial record AbBaTestString : SemanticString<AbBaTestString> { }

[TestMethod]
public void PrefixAndSuffixAttribute_LoneDelimiterServingAsBoth_ShouldThrow()
{
Assert.ThrowsExactly<ArgumentException>(() => SemanticString<QuotedTestString>.Create<QuotedTestString>("\""));
}

[TestMethod]
public void PrefixAndSuffixAttribute_OverlappingPrefixAndSuffix_ShouldThrow()
{
Assert.ThrowsExactly<ArgumentException>(() => SemanticString<AbBaTestString>.Create<AbBaTestString>("aba"));
}

[TestMethod]
public void PrefixAndSuffixAttribute_EmptyString_ShouldThrow()
{
Assert.ThrowsExactly<ArgumentException>(() => SemanticString<QuotedTestString>.Create<QuotedTestString>(string.Empty));
}

[TestMethod]
[DataRow("\"\"")]
[DataRow("\"quoted\"")]
public void PrefixAndSuffixAttribute_DelimitersEachPresent_ShouldPass(string value)
{
QuotedTestString quoted = SemanticString<QuotedTestString>.Create<QuotedTestString>(value);
Assert.AreEqual(value, quoted.WeakString);
}

[TestMethod]
public void PrefixAndSuffixAttribute_AdjacentPrefixAndSuffix_ShouldPass()
{
AbBaTestString value = SemanticString<AbBaTestString>.Create<AbBaTestString>("abba");
Assert.AreEqual("abba", value.WeakString);
}

// Test multiple validation attributes on empty strings
[TestMethod]
public void ValidationAttributes_EmptyString_ShouldHandleCorrectly()
Expand Down
Loading