|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Peer.Domain; |
| 7 | +using Peer.Domain.Filters; |
| 8 | +using Peer.Parsing; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace Peer.UnitTests.Parsing |
| 12 | +{ |
| 13 | + public class FilterParserTests |
| 14 | + { |
| 15 | + public class ParseFilterOption |
| 16 | + { |
| 17 | + [Fact] |
| 18 | + public void RawStringNull_Throws() |
| 19 | + { |
| 20 | + Assert.Throws<ArgumentNullException>(() => FilterParser.ParseFilterOption(null)); |
| 21 | + } |
| 22 | + |
| 23 | + [Fact] |
| 24 | + public void RawStringEmpty_ReturnsNotEnoughSections() |
| 25 | + { |
| 26 | + var value = FilterParser.ParseFilterOption(string.Empty); |
| 27 | + ResultAsserts.IsError(value, FilterParseError.NotEnoughSections); |
| 28 | + } |
| 29 | + |
| 30 | + [Fact] |
| 31 | + public void RawStringHasNoDivider_ReturnsNotEnoughSections() |
| 32 | + { |
| 33 | + var value = FilterParser.ParseFilterOption("authorInsomniak"); |
| 34 | + ResultAsserts.IsError(value, FilterParseError.NotEnoughSections); |
| 35 | + } |
| 36 | + |
| 37 | + [Fact] |
| 38 | + public void RawStringHasTooManyDividersWithContent_ReturnsTooManySections() |
| 39 | + { |
| 40 | + var value = FilterParser.ParseFilterOption("author:Insomnia:k"); |
| 41 | + ResultAsserts.IsError(value, FilterParseError.TooManySections); |
| 42 | + } |
| 43 | + |
| 44 | + [Fact] |
| 45 | + public void KeyNotFound_ReturnsUnknownKey() |
| 46 | + { |
| 47 | + var value = FilterParser.ParseFilterOption("doot:Insomniak"); |
| 48 | + ResultAsserts.IsError(value, FilterParseError.UnknownFilterKey); |
| 49 | + } |
| 50 | + |
| 51 | + [Fact] |
| 52 | + public void RegexInvalidForStringKey_ReturnsUnknownMatchValue() |
| 53 | + { |
| 54 | + var value = FilterParser.ParseFilterOption("author:Insom**"); |
| 55 | + ResultAsserts.IsError(value, FilterParseError.UnknownMatchValue); |
| 56 | + } |
| 57 | + |
| 58 | + [Fact] |
| 59 | + public void IntInvalidForIntKey_ReturnsUnknownMatchValue() |
| 60 | + { |
| 61 | + var value = FilterParser.ParseFilterOption("id:waka"); |
| 62 | + ResultAsserts.IsError(value, FilterParseError.UnknownMatchValue); |
| 63 | + } |
| 64 | + |
| 65 | + [Fact] |
| 66 | + public void EnumValueInvalidForEnumKey_ReturnsUnknownMatchValue() |
| 67 | + { |
| 68 | + var value = FilterParser.ParseFilterOption("status:theBreadOne"); |
| 69 | + ResultAsserts.IsError(value, FilterParseError.UnknownMatchValue); |
| 70 | + } |
| 71 | + |
| 72 | + [Fact] |
| 73 | + public void RegexValidForStringKey_ReturnsRegexFilter() |
| 74 | + { |
| 75 | + var value = FilterParser.ParseFilterOption("author:waka"); |
| 76 | + ResultAsserts.IsValue(value); |
| 77 | + Assert.IsType<RegexFilter>(value.Value); |
| 78 | + } |
| 79 | + |
| 80 | + [Fact] |
| 81 | + public void RawStringHasTooManyDividersButNoContent_ReturnsRegexFilter() |
| 82 | + { |
| 83 | + var value = FilterParser.ParseFilterOption("author:Insomniak:::::::::"); |
| 84 | + ResultAsserts.IsValue(value); |
| 85 | + Assert.IsType<RegexFilter>(value.Value); |
| 86 | + } |
| 87 | + |
| 88 | + [Fact] |
| 89 | + public void RawStringHasLeadingOrTrailingWhitespaceInSections_ReturnsRegexFilter() |
| 90 | + { |
| 91 | + var value = FilterParser.ParseFilterOption("author :Insomniak\t\t\t"); |
| 92 | + ResultAsserts.IsValue(value); |
| 93 | + Assert.IsType<RegexFilter>(value.Value); |
| 94 | + } |
| 95 | + |
| 96 | + [Fact] |
| 97 | + public void IntValidForIntKey_ReturnsActionFilter() |
| 98 | + { |
| 99 | + var value = FilterParser.ParseFilterOption("id:10"); |
| 100 | + ResultAsserts.IsValue(value); |
| 101 | + Assert.IsType<ActionFilter>(value.Value); |
| 102 | + } |
| 103 | + |
| 104 | + [Fact] |
| 105 | + public void EnumValidForEnumKey_ReturnsEnumFilter() |
| 106 | + { |
| 107 | + var value = FilterParser.ParseFilterOption("status:Stale"); |
| 108 | + ResultAsserts.IsValue(value); |
| 109 | + Assert.IsType<EnumMatchingFilter<PullRequestStatus>>(value.Value); |
| 110 | + } |
| 111 | + |
| 112 | + [Fact] |
| 113 | + public void EnumValidCaseDoesntMatch_ReturnsEnumFilter() |
| 114 | + { |
| 115 | + //CN: Ensuring case insensitive parsing |
| 116 | + var value = FilterParser.ParseFilterOption("status:stale"); |
| 117 | + ResultAsserts.IsValue(value); |
| 118 | + Assert.IsType<EnumMatchingFilter<PullRequestStatus>>(value.Value); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments