/
githubmirror
/
roslyn
Обзор
Документация
Войти
/
githubmirror
/
roslyn
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/EditorFeatures/CSharpTest/Interactive/NavigateTo/InteractiveNavigateToTests.cs
660 строк
25 KB
Cyrus Najmabadi
Fix test failures from regex pattern detection
12 мар 2026, 23:20
12 мар 2026, 23:20
096a68c
Код
Авторство
О чём код?
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. #nullable disable using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.CodeAnalysis.Editor.UnitTests.NavigateTo; using Microsoft.CodeAnalysis.Remote.Testing; using Microsoft.CodeAnalysis.Test.Utilities; using Microsoft.VisualStudio.Language.NavigateTo.Interfaces; using Microsoft.VisualStudio.Text.PatternMatching; using Roslyn.Test.Utilities; using Xunit; namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.NavigateTo; [Trait(Traits.Feature, Traits.Features.NavigateTo)] public sealed class InteractiveNavigateToTests : AbstractNavigateToTests { protected override string Language => "csharp"; protected override EditorTestWorkspace CreateWorkspace(string content, TestComposition composition) => EditorTestWorkspace.CreateCSharp(content, parseOptions: Options.Script, composition: composition); [WpfTheory] [CombinatorialData] public Task NoItemsForEmptyFile(TestHost testHost, Composition composition) => TestAsync(testHost, composition, "", async w => { Assert.Empty(await _aggregator.GetItemsAsync("Hello")); }); [WpfTheory] [CombinatorialData] public Task FindClass(TestHost testHost, Composition composition) => TestAsync(testHost, composition, """ class Goo { } """, async w => { var item = (await _aggregator.GetItemsAsync("Goo")).Single(x => x.Kind != "Method"); VerifyNavigateToResultItem(item, "Goo", "[|Goo|]", PatternMatchKind.Exact, NavigateToItemKind.Class, Glyph.ClassPrivate); }); [WpfTheory] [CombinatorialData] public Task FindNestedClass(TestHost testHost, Composition composition) => TestAsync(testHost, composition, """ class Goo { class Bar { internal class DogBed { } } } """, async w => { var item = (await _aggregator.GetItemsAsync("DogBed")).Single(x => x.Kind != "Method"); VerifyNavigateToResultItem(item, "DogBed", "[|DogBed|]", PatternMatchKind.Exact, NavigateToItemKind.Class, Glyph.ClassInternal); }); [WpfTheory] [CombinatorialData] public Task FindMemberInANestedClass(TestHost testHost, Composition composition) => TestAsync(testHost, composition, """ class Goo { class Bar { class DogBed { public void Method() { } } } } """, async w => { var item = (await _aggregator.GetItemsAsync("Method")).Single(); VerifyNavigateToResultItem(item, "Method", "[|Method|]()", PatternMatchKind.Exact, NavigateToItemKind.Method, Glyph.MethodPublic, string.Format(FeaturesResources.in_0_project_1, "Goo.Bar.DogBed", "Test")); }); [WpfTheory] [CombinatorialData] public Task FindGenericClassWithConstraints(TestHost testHost, Composition composition) => TestAsync(testHost, composition, """ using System.Collections; class Goo<T> where T : IEnumerable { } """, async w => { var item = (await _aggregator.GetItemsAsync("Goo")).Single(x => x.Kind != "Method"); VerifyNavigateToResultItem(item, "Goo", "[|Goo|]<T>", PatternMatchKind.Exact, NavigateToItemKind.Class, Glyph.ClassPrivate); }); [WpfTheory] [CombinatorialData] public Task FindGenericMethodWithConstraints(TestHost testHost, Composition composition) => TestAsync(testHost, composition, """ using System; class Goo<U> { public void Bar<T>(T item) where T : IComparable<T> { } } """, async w => { var item = (await _aggregator.GetItemsAsync("Bar")).Single(); VerifyNavigateToResultItem(item, "Bar", "[|Bar|]<T>(T)", PatternMatchKind.Exact, NavigateToItemKind.Method, Glyph.MethodPublic, string.Format(FeaturesResources.in_0_project_1, "Goo<U>", "Test")); }); [WpfTheory] [CombinatorialData] public Task FindPartialClass(TestHost testHost, Composition composition) => TestAsync(testHost, composition, """ public partial class Goo { int a; } partial class Goo { int b; } """, async w => { var expecteditem1 = new NavigateToItem("Goo", NavigateToItemKind.Class, "csharp", null, null, s_emptyExactPatternMatch, null); var expecteditems = new List<NavigateToItem> { expecteditem1, expecteditem1 }; var items = await _aggregator.GetItemsAsync("Goo"); VerifyNavigateToResultItems(expecteditems, items); }); [WpfTheory] [CombinatorialData] public Task FindTypesInMetadata(TestHost testHost, Composition composition) => TestAsync(testHost, composition, """ using System; Class Program { FileStyleUriParser f; } """, async w => { var items = await _aggregator.GetItemsAsync("FileStyleUriParser"); Assert.Equal(0, items.Count()); }); [WpfTheory] [CombinatorialData] public Task FindClassInNamespace(TestHost testHost, Composition composition) => TestAsync(testHost, composition, """ namespace Bar { class Goo { } } """, async w => { var item = (await _aggregator.GetItemsAsync("Goo")).Single(x => x.Kind != "Method"); VerifyNavigateToResultItem(item, "Goo", "[|Goo|]", PatternMatchKind.Exact, NavigateToItemKind.Class, Glyph.ClassInternal); }); [WpfTheory] [CombinatorialData] public Task FindStruct(TestHost testHost, Composition composition) => TestAsync(testHost, composition, """ struct Bar { } """, async w => { var item = (await _aggregator.GetItemsAsync("B")).Single(x => x.Kind != "Method"); VerifyNavigateToResultItem(item, "Bar", "[|B|]ar", PatternMatchKind.Prefix, NavigateToItemKind.Structure, Glyph.StructurePrivate); }); [WpfTheory] [CombinatorialData] public Task FindEnum(TestHost testHost, Composition composition) => TestAsync(testHost, composition, """ enum Colors { Red, Green, Blue } """, async w => { var item = (await _aggregator.GetItemsAsync("Colors")).Single(x => x.Kind != "Method"); VerifyNavigateToResultItem(item, "Colors", "[|Colors|]", PatternMatchKind.Exact, NavigateToItemKind.Enum, Glyph.EnumPrivate); }); [WpfTheory] [CombinatorialData] public Task FindEnumMember(TestHost testHost, Composition composition) => TestAsync(testHost, composition, """ enum Colors { Red, Green, Blue } """, async w => { var item = (await _aggregator.GetItemsAsync("R")).Single(); VerifyNavigateToResultItem(item, "Red", "[|R|]ed", PatternMatchKind.Prefix, NavigateToItemKind.EnumItem, Glyph.EnumMemberPublic); }); [WpfTheory] [CombinatorialData] public Task FindConstField(TestHost testHost, Composition composition) => TestAsync(testHost, composition, """ class Goo { const int bar = 7; } """, async w => { var item = (await _aggregator.GetItemsAsync("ba")).Single(); VerifyNavigateToResultItem(item, "bar", "[|ba|]r", PatternMatchKind.Prefix, NavigateToItemKind.Constant, Glyph.ConstantPrivate); }); [WpfTheory] [CombinatorialData] public Task FindVerbatimIdentifier(TestHost testHost, Composition composition) => TestAsync(testHost, composition, """ class Goo { string @string; } """, async w => { var item = (await _aggregator.GetItemsAsync("string")).Single(); VerifyNavigateToResultItem(item, "string", "[|string|]", PatternMatchKind.Exact, NavigateToItemKind.Field, Glyph.FieldPrivate, additionalInfo: string.Format(FeaturesResources.in_0_project_1, "Goo", "Test")); }); [WpfTheory] [CombinatorialData] public Task FindIndexer(TestHost testHost, Composition composition) => TestAsync(testHost, composition, @"class Goo { int[] arr; public int this[int i] { get { return arr[i]; } set { arr[i] = value; } } }", async w => { var item = (await _aggregator.GetItemsAsync("this")).Single(); VerifyNavigateToResultItem(item, "this", "[|this|][int]", PatternMatchKind.Exact, NavigateToItemKind.Property, Glyph.PropertyPublic, additionalInfo: string.Format(FeaturesResources.in_0_project_1, "Goo", "Test")); }); [WpfTheory] [CombinatorialData] public Task FindEvent(TestHost testHost, Composition composition) => TestAsync(testHost, composition, "class Goo { public event EventHandler ChangedEventHandler; }", async w => { var item = (await _aggregator.GetItemsAsync("CEH")).Single(); VerifyNavigateToResultItem(item, "ChangedEventHandler", "[|C|]hanged[|E|]vent[|H|]andler", PatternMatchKind.CamelCaseExact, NavigateToItemKind.Event, Glyph.EventPublic, additionalInfo: string.Format(FeaturesResources.in_0_project_1, "Goo", "Test")); }); [WpfTheory] [CombinatorialData] public Task FindAutoProperty(TestHost testHost, Composition composition) => TestAsync(testHost, composition, """ class Goo { int Bar { get; set; } } """, async w => { var item = (await _aggregator.GetItemsAsync("B")).Single(); VerifyNavigateToResultItem(item, "Bar", "[|B|]ar", PatternMatchKind.Prefix, NavigateToItemKind.Property, Glyph.PropertyPrivate, additionalInfo: string.Format(FeaturesResources.in_0_project_1, "Goo", "Test")); }); [WpfTheory] [CombinatorialData] public Task FindMethod(TestHost testHost, Composition composition) => TestAsync(testHost, composition, """ class Goo { void DoSomething(); } """, async w => { var item = (await _aggregator.GetItemsAsync("DS")).Single(); VerifyNavigateToResultItem(item, "DoSomething", "[|D|]o[|S|]omething()", PatternMatchKind.CamelCaseExact, NavigateToItemKind.Method, Glyph.MethodPrivate, string.Format(FeaturesResources.in_0_project_1, "Goo", "Test")); }); [WpfTheory] [CombinatorialData] public Task FindParameterizedMethod(TestHost testHost, Composition composition) => TestAsync(testHost, composition, """ class Goo { void DoSomething(int a, string b) { } } """, async w => { var item = (await _aggregator.GetItemsAsync("DS")).Single(); VerifyNavigateToResultItem(item, "DoSomething", "[|D|]o[|S|]omething(int, string)", PatternMatchKind.CamelCaseExact, NavigateToItemKind.Method, Glyph.MethodPrivate, string.Format(FeaturesResources.in_0_project_1, "Goo", "Test")); }); [WpfTheory] [CombinatorialData] public Task FindConstructor(TestHost testHost, Composition composition) => TestAsync(testHost, composition, """ class Goo { public Goo() { } } """, async w => { var item = (await _aggregator.GetItemsAsync("Goo")).Single(t => t.Kind == NavigateToItemKind.Method); VerifyNavigateToResultItem(item, "Goo", "[|Goo|]()", PatternMatchKind.Exact, NavigateToItemKind.Method, Glyph.MethodPublic, string.Format(FeaturesResources.in_0_project_1, "Goo", "Test")); }); [WpfTheory] [CombinatorialData] public Task FindParameterizedConstructor(TestHost testHost, Composition composition) => TestAsync(testHost, composition, """ class Goo { public Goo(int i) { } } """, async w => { var item = (await _aggregator.GetItemsAsync("Goo")).Single(t => t.Kind == NavigateToItemKind.Method); VerifyNavigateToResultItem(item, "Goo", "[|Goo|](int)", PatternMatchKind.Exact, NavigateToItemKind.Method, Glyph.MethodPublic, string.Format(FeaturesResources.in_0_project_1, "Goo", "Test")); }); [WpfTheory] [CombinatorialData] public Task FindStaticConstructor(TestHost testHost, Composition composition) => TestAsync(testHost, composition, """ class Goo { static Goo() { } } """, async w => { var item = (await _aggregator.GetItemsAsync("Goo")).Single(t => t.Kind == NavigateToItemKind.Method && t.Name != ".ctor"); VerifyNavigateToResultItem(item, "Goo", "[|Goo|].static Goo()", PatternMatchKind.Exact, NavigateToItemKind.Method, Glyph.MethodPrivate, string.Format(FeaturesResources.in_0_project_1, "Goo", "Test")); }); [WpfTheory] [CombinatorialData] public Task FindPartialMethods(TestHost testHost, Composition composition) => TestAsync(testHost, composition, "partial class Goo { partial void Bar(); } partial class Goo { partial void Bar() { Console.Write(\"hello\"); } }", async w => { var expecteditem1 = new NavigateToItem("Bar", NavigateToItemKind.Method, "csharp", null, null, s_emptyExactPatternMatch, null); var expecteditems = new List<NavigateToItem> { expecteditem1, expecteditem1 }; var items = await _aggregator.GetItemsAsync("Bar"); VerifyNavigateToResultItems(expecteditems, items); }); [WpfTheory] [CombinatorialData] public Task FindPartialMethodDefinitionOnly(TestHost testHost, Composition composition) => TestAsync(testHost, composition, """ partial class Goo { partial void Bar(); } """, async w => { var item = (await _aggregator.GetItemsAsync("Bar")).Single(); VerifyNavigateToResultItem(item, "Bar", "[|Bar|]()", PatternMatchKind.Exact, NavigateToItemKind.Method, Glyph.MethodPrivate, string.Format(FeaturesResources.in_0_1_2, "Goo", "test1.cs", "Test")); }); [WpfTheory] [CombinatorialData] public Task FindOverriddenMembers(TestHost testHost, Composition composition) => TestAsync(testHost, composition, "class Goo { public virtual string Name { get; set; } } class DogBed : Goo { public override string Name { get { return base.Name; } set {} } }", async w => { var expecteditem1 = new NavigateToItem("Name", NavigateToItemKind.Property, "csharp", null, null, s_emptyExactPatternMatch, null); var expecteditems = new List<NavigateToItem> { expecteditem1, expecteditem1 }; var items = await _aggregator.GetItemsAsync("Name"); VerifyNavigateToResultItems(expecteditems, items); var item = items.ElementAt(1); var itemDisplay = item.DisplayFactory.CreateItemDisplay(item); var unused = itemDisplay.Glyph; Assert.Equal("Name", itemDisplay.Name); Assert.Equal(string.Format(FeaturesResources.in_0_project_1, "DogBed", "Test"), itemDisplay.AdditionalInformation); item = items.ElementAt(0); itemDisplay = item.DisplayFactory.CreateItemDisplay(item); unused = itemDisplay.Glyph; Assert.Equal("Name", itemDisplay.Name); Assert.Equal(string.Format(FeaturesResources.in_0_project_1, "Goo", "Test"), itemDisplay.AdditionalInformation); }); [WpfTheory] [CombinatorialData] public Task FindInterface(TestHost testHost, Composition composition) => TestAsync(testHost, composition, """ public interface IGoo { } """, async w => { var item = (await _aggregator.GetItemsAsync("IG")).Single(); VerifyNavigateToResultItem(item, "IGoo", "[|IG|]oo", PatternMatchKind.Prefix, NavigateToItemKind.Interface, Glyph.InterfacePublic); }); [WpfTheory] [CombinatorialData] public Task FindDelegateInNamespace(TestHost testHost, Composition composition) => TestAsync(testHost, composition, """ namespace Goo { delegate void DoStuff(); } """, async w => { var item = (await _aggregator.GetItemsAsync("DoStuff")).Single(x => x.Kind != "Method"); VerifyNavigateToResultItem(item, "DoStuff", "[|DoStuff|]", PatternMatchKind.Exact, NavigateToItemKind.Delegate, Glyph.DelegateInternal); }); [WpfTheory] [CombinatorialData] public Task FindLambdaExpression(TestHost testHost, Composition composition) => TestAsync(testHost, composition, """ using System; class Goo { Func<int, int> sqr = x => x * x; } """, async w => { var item = (await _aggregator.GetItemsAsync("sqr")).Single(); VerifyNavigateToResultItem(item, "sqr", "[|sqr|]", PatternMatchKind.Exact, NavigateToItemKind.Field, Glyph.FieldPrivate, string.Format(FeaturesResources.in_0_project_1, "Goo", "Test")); }); [WpfTheory] [CombinatorialData] public Task OrderingOfConstructorsAndTypes(TestHost testHost, Composition composition) => TestAsync(testHost, composition, """ class C1 { C1(int i) { } } class C2 { C2(float f) { } static C2() { } } """, async w => { var expecteditems = new List<NavigateToItem> { new("C1", NavigateToItemKind.Class, "csharp", "C1", null, s_emptyPrefixPatternMatch, null), new("C1", NavigateToItemKind.Method, "csharp", "C1", null, s_emptyPrefixPatternMatch, null), new("C2", NavigateToItemKind.Class, "csharp", "C2", null, s_emptyPrefixPatternMatch, null), new("C2", NavigateToItemKind.Method, "csharp", "C2", null, s_emptyPrefixPatternMatch, null), // this is the static ctor new("C2", NavigateToItemKind.Method, "csharp", "C2", null, s_emptyPrefixPatternMatch, null), }; var items = (await _aggregator.GetItemsAsync("C")).ToList(); items.Sort(CompareNavigateToItems); VerifyNavigateToResultItems(expecteditems, items); }); [WpfTheory] [CombinatorialData] public Task StartStopSanity(TestHost testHost, Composition composition) => TestAsync(testHost, composition, """ public class Goo { } """, async w => { // Do one set of queries Assert.Single((await _aggregator.GetItemsAsync("Goo")), x => x.Kind != "Method"); _provider.StopSearch(); // Do the same query again, make sure nothing was left over Assert.Single((await _aggregator.GetItemsAsync("Goo")), x => x.Kind != "Method"); _provider.StopSearch(); // Dispose the provider _provider.Dispose(); }); [WpfTheory] [CombinatorialData] public Task DescriptionItems(TestHost testHost, Composition composition) => TestAsync(testHost, composition, """ public class Goo { } """, async w => { var item = (await _aggregator.GetItemsAsync("G")).Single(x => x.Kind != "Method"); var itemDisplay = item.DisplayFactory.CreateItemDisplay(item); var descriptionItems = itemDisplay.DescriptionItems; void assertDescription(string label, string value) { var descriptionItem = descriptionItems.Single(i => i.Category.Single().Text == label); Assert.Equal(value, descriptionItem.Details.Single().Text); } assertDescription("File:", w.Documents.Single().FilePath); assertDescription("Line:", "3"); // one based line number assertDescription("Project:", "Test"); }); [WpfTheory] [CombinatorialData] public Task TermSplittingTest1(TestHost testHost, Composition composition) => TestAsync(testHost, composition, "class SyllableBreaking {int GetKeyWord; int get_key_word; string get_keyword; int getkeyword; int wake;}", async w => { var expecteditem1 = new NavigateToItem("get_keyword", NavigateToItemKind.Field, "csharp", null, null, s_emptyCamelCaseNonContiguousPrefixPatternMatch_NotCaseSensitive, null); var expecteditem2 = new NavigateToItem("get_key_word", NavigateToItemKind.Field, "csharp", null, null, s_emptyCamelCaseNonContiguousPrefixPatternMatch_NotCaseSensitive, null); var expecteditem3 = new NavigateToItem("GetKeyWord", NavigateToItemKind.Field, "csharp", null, null, s_emptyCamelCasePrefixPatternMatch, null); var expecteditems = new List<NavigateToItem> { expecteditem1, expecteditem2, expecteditem3 }; var items = await _aggregator.GetItemsAsync("GK"); Assert.Equal(expecteditems.Count, items.Count()); VerifyNavigateToResultItems(expecteditems, items); }); [WpfTheory] [CombinatorialData] public Task TermSplittingTest2(TestHost testHost, Composition composition) => TestAsync(testHost, composition, "class SyllableBreaking {int GetKeyWord; int get_key_word; string get_keyword; int getkeyword; int wake;}", async w => { var expecteditem1 = new NavigateToItem("get_key_word", NavigateToItemKind.Field, "csharp", null, null, s_emptyCamelCaseNonContiguousPrefixPatternMatch_NotCaseSensitive, null); var expecteditem2 = new NavigateToItem("GetKeyWord", NavigateToItemKind.Field, "csharp", null, null, s_emptyCamelCaseExactPatternMatch, null); var expecteditems = new List<NavigateToItem> { expecteditem1, expecteditem2 }; var items = await _aggregator.GetItemsAsync("GKW"); VerifyNavigateToResultItems(expecteditems, items); }); [WpfTheory] [CombinatorialData] public Task TermSplittingTest3(TestHost testHost, Composition composition) => TestAsync(testHost, composition, "class SyllableBreaking {int GetKeyWord; int get_key_word; string get_keyword; int getkeyword; int wake;}", async w => { var expecteditem1 = new NavigateToItem("get_key_word", NavigateToItemKind.Field, "csharp", null, null, s_emptyCamelCaseSubstringPatternMatch_NotCaseSensitive, null); var expecteditem2 = new NavigateToItem("GetKeyWord", NavigateToItemKind.Field, "csharp", null, null, s_emptySubstringPatternMatch, null); var expecteditems = new List<NavigateToItem> { expecteditem1, expecteditem2 }; var items = await _aggregator.GetItemsAsync("K W"); VerifyNavigateToResultItems(expecteditems, items); }); [WpfTheory] [CombinatorialData] public Task TermSplittingTest4(TestHost testHost, Composition composition) => TestAsync(testHost, composition, "class SyllableBreaking {int GetKeyWord; int get_key_word; string get_keyword; int getkeyword; int wake;}", async w => { var items = await _aggregator.GetItemsAsync("WKG"); Assert.Empty(items); }); [WpfTheory] [CombinatorialData] public Task TermSplittingTest5(TestHost testHost, Composition composition) => TestAsync(testHost, composition, "class SyllableBreaking {int GetKeyWord; int get_key_word; string get_keyword; int getkeyword; int wake;}", async w => { var item = (await _aggregator.GetItemsAsync("G_K_W")).Single(); VerifyNavigateToResultItem(item, "get_key_word", "[|g|]et[|_k|]ey[|_w|]ord", PatternMatchKind.CamelCaseExact, NavigateToItemKind.Field, Glyph.FieldPrivate); }); [WpfTheory] [CombinatorialData] public Task TermSplittingTest7(TestHost testHost, Composition composition) => TestAsync(testHost, composition, "class SyllableBreaking {int GetKeyWord; int get_key_word; string get_keyword; int getkeyword; int wake;}", async w => { var items = await _aggregator.GetItemsAsync("GTW"); Assert.Empty(items); }); [WpfTheory] [CombinatorialData] public Task TermSplittingTest_DotStarRegex_NoExtractableLiterals(TestHost testHost, Composition composition) => TestAsync(testHost, composition, "class SyllableBreaking {int GetKeyWord; int get_key_word; string get_keyword; int getkeyword; int wake;}", async w => { // G.*W is a valid regex but G and W are single-char literals — no 2+ char literals // can be extracted for pre-filtering, so the regex search is skipped entirely. var items = await _aggregator.GetItemsAsync("G.*W"); Assert.Empty(items); }); [WpfTheory] [CombinatorialData] public Task TermSplittingTest_DotStarRegex_WithExtractableLiterals(TestHost testHost, Composition composition) => TestAsync(testHost, composition, "class SyllableBreaking {int GetKeyWord; int get_key_word; string get_keyword; int getkeyword; int wake;}", async w => { // Ge.*Wo has 2+ char literals ("ge", "wo") so the regex search runs. // Matches any symbol containing "Ge" followed eventually by "Wo" (case-insensitive). var items = await _aggregator.GetItemsAsync("Ge.*Wo"); Assert.Equal(4, items.Count()); Assert.Contains(items, i => i.Name == "GetKeyWord"); Assert.Contains(items, i => i.Name == "get_key_word"); Assert.Contains(items, i => i.Name == "get_keyword"); Assert.Contains(items, i => i.Name == "getkeyword"); }); }