|
| 1 | +using NpgsqlRest.Common; |
| 2 | + |
| 3 | +namespace NpgsqlRestTests.ParserTests; |
| 4 | + |
| 5 | +// Characterization tests pinning the behavior of the shared comment-parsing string primitives |
| 6 | +// (NpgsqlRest.Common.CommentPrimitives) — extracted from DefaultCommentParser in MCP plan Phase 0. |
| 7 | +// They document behavior, quirks included. |
| 8 | +public class CommentPrimitivesTests |
| 9 | +{ |
| 10 | + // ---- StrEquals: optional leading '@' stripped from str1, then OrdinalIgnoreCase compare ---- |
| 11 | + |
| 12 | + [Theory] |
| 13 | + [InlineData("authorize", "authorize", true)] |
| 14 | + [InlineData("@authorize", "authorize", true)] // '@' prefix on str1 is stripped |
| 15 | + [InlineData("AUTHORIZE", "authorize", true)] // case-insensitive |
| 16 | + [InlineData("authorize", "AUTHORIZE", true)] |
| 17 | + [InlineData("authorize", "auth", false)] |
| 18 | + [InlineData("authorize", "@authorize", false)] // '@' is NOT stripped from str2 |
| 19 | + [InlineData("@", "", true)] // "@" -> "" equals "" |
| 20 | + [InlineData("", "", true)] |
| 21 | + public void StrEquals_MatchesCurrentBehavior(string str1, string str2, bool expected) |
| 22 | + { |
| 23 | + CommentPrimitives.StrEquals(str1, str2).Should().Be(expected); |
| 24 | + } |
| 25 | + |
| 26 | + // ---- StrEqualsToArray: '@' stripped from str, then OrdinalIgnoreCase match against any ---- |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public void StrEqualsToArray_MatchesAnyAlias() |
| 30 | + { |
| 31 | + CommentPrimitives.StrEqualsToArray("cached", "cached", "cache").Should().BeTrue(); |
| 32 | + CommentPrimitives.StrEqualsToArray("@cache", "cached", "cache").Should().BeTrue(); |
| 33 | + CommentPrimitives.StrEqualsToArray("CACHE", "cached", "cache").Should().BeTrue(); |
| 34 | + } |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public void StrEqualsToArray_NoMatch_ReturnsFalse() |
| 38 | + { |
| 39 | + CommentPrimitives.StrEqualsToArray("x", "a", "b").Should().BeFalse(); |
| 40 | + } |
| 41 | + |
| 42 | + [Fact] |
| 43 | + public void StrEqualsToArray_EmptyArray_ReturnsFalse() |
| 44 | + { |
| 45 | + CommentPrimitives.StrEqualsToArray("anything").Should().BeFalse(); |
| 46 | + } |
| 47 | + |
| 48 | + // ---- SplitWords: split on space/comma, RemoveEmptyEntries, trim, preserve case ---- |
| 49 | + |
| 50 | + [Fact] |
| 51 | + public void SplitWords_SplitsOnSpaceAndComma_Trimmed_PreservingCase() |
| 52 | + { |
| 53 | + "a, b, c".SplitWords().Should().Equal("a", "b", "c"); |
| 54 | + "a b".SplitWords().Should().Equal("a", "b"); |
| 55 | + " a , , b ".SplitWords().Should().Equal("a", "b"); // empty entries removed |
| 56 | + "Foo Bar".SplitWords().Should().Equal("Foo", "Bar"); // case preserved |
| 57 | + } |
| 58 | + |
| 59 | + [Fact] |
| 60 | + public void SplitWords_NullOrEmpty_ReturnsEmpty() |
| 61 | + { |
| 62 | + ((string)null!).SplitWords().Should().BeEmpty(); |
| 63 | + "".SplitWords().Should().BeEmpty(); |
| 64 | + } |
| 65 | + |
| 66 | + // ---- SplitWordsLower: same as SplitWords but lowercased ---- |
| 67 | + |
| 68 | + [Fact] |
| 69 | + public void SplitWordsLower_LowercasesEachWord() |
| 70 | + { |
| 71 | + "Foo, BAR".SplitWordsLower().Should().Equal("foo", "bar"); |
| 72 | + "MixedCase Word".SplitWordsLower().Should().Equal("mixedcase", "word"); |
| 73 | + } |
| 74 | + |
| 75 | + [Fact] |
| 76 | + public void SplitWordsLower_NullOrEmpty_ReturnsEmpty() |
| 77 | + { |
| 78 | + ((string)null!).SplitWordsLower().Should().BeEmpty(); |
| 79 | + "".SplitWordsLower().Should().BeEmpty(); |
| 80 | + } |
| 81 | + |
| 82 | + // ---- SplitBySeparatorChar: split on first separator; false if absent or part1 has invalid name char ---- |
| 83 | + // Valid name chars: letter, digit, '-', '_', '@'. Anything else in part1 => not a key/value pair. |
| 84 | + |
| 85 | + [Theory] |
| 86 | + [InlineData("key = value", '=', true, "key", "value")] |
| 87 | + [InlineData("Content-Type: application/json", ':', true, "Content-Type", "application/json")] // '-' allowed |
| 88 | + [InlineData("@timeout = 30s", '=', true, "@timeout", "30s")] // '@' allowed |
| 89 | + [InlineData("no separator here", '=', false, null, null)] // no separator |
| 90 | + [InlineData("a b = value", '=', false, null, null)] // space in part1 -> invalid |
| 91 | + [InlineData("key=", '=', true, "key", "")] // empty value |
| 92 | + [InlineData("=value", '=', true, "", "value")] // empty key |
| 93 | + public void SplitBySeparatorChar_MatchesCurrentBehavior(string input, char sep, bool expected, string? p1, string? p2) |
| 94 | + { |
| 95 | + var result = CommentPrimitives.SplitBySeparatorChar(input, sep, out var part1, out var part2); |
| 96 | + result.Should().Be(expected); |
| 97 | + if (expected) |
| 98 | + { |
| 99 | + part1.Should().Be(p1); |
| 100 | + part2.Should().Be(p2); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments