|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using Microsoft.Azure.WebJobs.Script.Extensions; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace Microsoft.Azure.WebJobs.Script.Tests.Utils |
| 9 | +{ |
| 10 | + public sealed class TokenExtensionTests |
| 11 | + { |
| 12 | + [Theory] |
| 13 | + [InlineData("FeatureA,FeatureB,FeatureC", "FeatureB", ',', true)] |
| 14 | + [InlineData("FeatureA,FeatureB,FeatureC", "FeatureD", ',', false)] |
| 15 | + [InlineData("FeatureA,FeatureB,FeatureC", "featureb", ',', true)] |
| 16 | + [InlineData("FeatureA|FeatureB|FeatureC", "FeatureC", '|', true)] |
| 17 | + [InlineData("FeatureA,FeatureB,FeatureC", "FeatureA", ',', true)] |
| 18 | + [InlineData("FeatureA,FeatureB,FeatureC", "FeatureC", ',', true)] |
| 19 | + [InlineData(null, "FeatureA", ',', false)] |
| 20 | + [InlineData("", "FeatureA", ',', false)] |
| 21 | + [InlineData("", "", ',', false)] |
| 22 | + [InlineData("FeatureA,,FeatureB", "FeatureA", ',', true)] |
| 23 | + [InlineData("FeatureA,,FeatureB", "FeatureB", ',', true)] |
| 24 | + [InlineData(",FeatureA", "FeatureA", ',', true)] |
| 25 | + [InlineData("FeatureA,", "FeatureA", ',', true)] |
| 26 | + [InlineData("FeatureA,FeatureB,", "FeatureB", ',', true)] |
| 27 | + |
| 28 | + public void ContainsToken_WorksAsExpected(string delimited, string token, char separator, bool expected) |
| 29 | + { |
| 30 | + var result = delimited.ContainsToken(token, separator); |
| 31 | + |
| 32 | + Assert.Equal(expected, result); |
| 33 | + } |
| 34 | + |
| 35 | + [Theory] |
| 36 | + [InlineData("FeatureA,FeatureB,FeatureC", "FeatureB", ',', true)] |
| 37 | + [InlineData("FeatureA,FeatureB,FeatureC", "FeatureD", ',', false)] |
| 38 | + [InlineData("FeatureA,FeatureB,FeatureC", "featureb", ',', true)] |
| 39 | + [InlineData("FeatureA|FeatureB|FeatureC", "FeatureC", '|', true)] |
| 40 | + [InlineData("FeatureA,FeatureB,FeatureC", "FeatureA", ',', true)] |
| 41 | + [InlineData("FeatureA,FeatureB,FeatureC", "FeatureC", ',', true)] |
| 42 | + [InlineData("FeatureA,,FeatureB", "FeatureA", ',', true)] |
| 43 | + [InlineData("FeatureA,,FeatureB", "FeatureB", ',', true)] |
| 44 | + [InlineData(",FeatureA", "FeatureA", ',', true)] |
| 45 | + [InlineData("FeatureA,", "FeatureA", ',', true)] |
| 46 | + [InlineData("FeatureA,FeatureB,", "FeatureB", ',', true)] |
| 47 | + public void ContainsToken_SpanOverload_WorksAsExpected(string delimited, string token, char separator, bool expected) |
| 48 | + { |
| 49 | + var result = delimited.AsSpan().ContainsToken(token.AsSpan(), separator); |
| 50 | + |
| 51 | + Assert.Equal(expected, result); |
| 52 | + } |
| 53 | + |
| 54 | + [Fact] |
| 55 | + public void ContainsToken_ThrowsArgumentException_WhenTokenContainsSeparator_StringOverload() |
| 56 | + { |
| 57 | + var ex = Assert.Throws<ArgumentException>(() => |
| 58 | + { |
| 59 | + string source = "FeatureA,FeatureB"; |
| 60 | + string invalidToken = "FeatureA,FeatureB"; |
| 61 | + char separator = ','; |
| 62 | + |
| 63 | + source.ContainsToken(invalidToken, separator); |
| 64 | + }); |
| 65 | + |
| 66 | + Assert.Contains("must not contain the separator", ex.Message); |
| 67 | + } |
| 68 | + |
| 69 | + [Fact] |
| 70 | + public void ContainsToken_ThrowsArgumentException_WhenTokenContainsSeparator_SpanOverload() |
| 71 | + { |
| 72 | + var ex = Assert.Throws<ArgumentException>(() => |
| 73 | + { |
| 74 | + ReadOnlySpan<char> source = "FeatureA|FeatureB".AsSpan(); |
| 75 | + ReadOnlySpan<char> invalidToken = "FeatureA|FeatureB".AsSpan(); |
| 76 | + source.ContainsToken(invalidToken, '|'); |
| 77 | + }); |
| 78 | + |
| 79 | + Assert.Contains("must not contain the separator", ex.Message); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments