|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace Microsoft.Extensions.ApiDescription.Client |
| 7 | +{ |
| 8 | + public class CSharpIdentifierTest |
| 9 | + { |
| 10 | + [Theory] |
| 11 | + [InlineData('a')] |
| 12 | + [InlineData('Q')] |
| 13 | + [InlineData('\u2164')] // UnicodeCategory.LetterNumber (roman numeral five) |
| 14 | + [InlineData('_')] |
| 15 | + [InlineData('9')] |
| 16 | + [InlineData('\u0303')] // UnicodeCategory.NonSpacingMark (combining tilde) |
| 17 | + [InlineData('\u09CB')] // UnicodeCategory.SpacingCombiningMark (Bengali vowel sign O) |
| 18 | + [InlineData('\uFE4F')] // UnicodeCategory.ConnectorPunctuation (wavy low line) |
| 19 | + [InlineData('\u2062')] // UnicodeCategory.Format (invisible times) |
| 20 | + public void IsIdentifierPart_ReturnsTrue_WhenItShould(char character) |
| 21 | + { |
| 22 | + // Arrange and Act |
| 23 | + var result = CSharpIdentifier.IsIdentifierPart(character); |
| 24 | + |
| 25 | + // Assert |
| 26 | + Assert.True(result); |
| 27 | + } |
| 28 | + |
| 29 | + [Theory] |
| 30 | + [InlineData('/')] |
| 31 | + [InlineData('-')] |
| 32 | + [InlineData('\u20DF')] // UnicodeCategory.EnclosingMark (combining enclosing diamond) |
| 33 | + [InlineData('\u2005')] // UnicodeCategory.SpaceSeparator (four-per-em space) |
| 34 | + [InlineData('\u0096')] // UnicodeCategory.Control (start of guarded area) |
| 35 | + [InlineData('\uFF1C')] // UnicodeCategory.MathSymbol (fullwidth less-than sign) |
| 36 | + public void IsIdentifierPart_ReturnsFalse_WhenItShould(char character) |
| 37 | + { |
| 38 | + // Arrange and Act |
| 39 | + var result = CSharpIdentifier.IsIdentifierPart(character); |
| 40 | + |
| 41 | + // Assert |
| 42 | + Assert.False(result); |
| 43 | + } |
| 44 | + |
| 45 | + // Output length is one longer than input in these cases. |
| 46 | + [Theory] |
| 47 | + [InlineData("9", "_9")] |
| 48 | + [InlineData("\u0303", "_\u0303")] // UnicodeCategory.NonSpacingMark (combining tilde) |
| 49 | + [InlineData("\u09CB", "_\u09CB")] // UnicodeCategory.SpacingCombiningMark (Bengali vowel sign O) |
| 50 | + [InlineData("\uFE4F", "_\uFE4F")] // UnicodeCategory.ConnectorPunctuation (wavy low line) |
| 51 | + [InlineData("\u2062", "_\u2062")] // UnicodeCategory.Format (invisible times) |
| 52 | + public void SanitizeIdentifier_AddsUnderscore_WhenItShould(string input, string expectdOutput) |
| 53 | + { |
| 54 | + // Arrange and Act |
| 55 | + var output = CSharpIdentifier.SanitizeIdentifier(input); |
| 56 | + |
| 57 | + // Assert |
| 58 | + Assert.Equal(expectdOutput, output); |
| 59 | + } |
| 60 | + |
| 61 | + [Theory] |
| 62 | + [InlineData("a", "a")] |
| 63 | + [InlineData("Q", "Q")] |
| 64 | + [InlineData("\u2164", "\u2164")] |
| 65 | + [InlineData("_", "_")] |
| 66 | + public void SanitizeIdentifier_DoesNotAddUnderscore_WhenValidStartCharacter(string input, string expectdOutput) |
| 67 | + { |
| 68 | + // Arrange and Act |
| 69 | + var output = CSharpIdentifier.SanitizeIdentifier(input); |
| 70 | + |
| 71 | + // Assert |
| 72 | + Assert.Equal(expectdOutput, output); |
| 73 | + } |
| 74 | + |
| 75 | + [Theory] |
| 76 | + [InlineData("/", "_")] |
| 77 | + [InlineData("-", "_")] |
| 78 | + [InlineData("\u20DF", "_")] // UnicodeCategory.EnclosingMark (combining enclosing diamond) |
| 79 | + [InlineData("\u2005", "_")] // UnicodeCategory.SpaceSeparator (four-per-em space) |
| 80 | + [InlineData("\u0096", "_")] // UnicodeCategory.Control (start of guarded area) |
| 81 | + [InlineData("\uFF1C", "_")] // UnicodeCategory.MathSymbol (fullwidth less-than sign) |
| 82 | + public void SanitizeIdentifier_DoesNotAddUnderscore_WhenInvalidCharacter(string input, string expectdOutput) |
| 83 | + { |
| 84 | + // Arrange and Act |
| 85 | + var output = CSharpIdentifier.SanitizeIdentifier(input); |
| 86 | + |
| 87 | + // Assert |
| 88 | + Assert.Equal(expectdOutput, output); |
| 89 | + } |
| 90 | + |
| 91 | + [Theory] |
| 92 | + [InlineData("a/", "a_")] |
| 93 | + [InlineData("aa-bb", "aa_bb")] |
| 94 | + [InlineData("aa\u20DF\u20DF", "aa__")] // UnicodeCategory.EnclosingMark (combining enclosing diamond) |
| 95 | + [InlineData("aa\u2005bb\u2005cc", "aa_bb_cc")] // UnicodeCategory.SpaceSeparator (four-per-em space) |
| 96 | + [InlineData("aa\u0096\u0096bb", "aa__bb")] // UnicodeCategory.Control (start of guarded area) |
| 97 | + [InlineData("aa\uFF1C\uFF1C\uFF1Cbb", "aa___bb")] // UnicodeCategory.MathSymbol (fullwidth less-than sign) |
| 98 | + public void SanitizeIdentifier_ReplacesInvalidCharacters_WhenNotFirst(string input, string expectdOutput) |
| 99 | + { |
| 100 | + // Arrange and Act |
| 101 | + var output = CSharpIdentifier.SanitizeIdentifier(input); |
| 102 | + |
| 103 | + // Assert |
| 104 | + Assert.Equal(expectdOutput, output); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments