Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Semantic snippets - propg and propi snippets #65979

Merged
merged 13 commits into from
Feb 3, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// 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.

using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.CodeAnalysis.Testing;
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Completion.CompletionProviders.Snippets
{
public abstract class AbstractCSharpAutoPropertyCompletionProviderTests : AbstractCSharpSnippetCompletionProviderTests
{
protected abstract string GetDefaultPropertyText(string propertyName);

[WpfFact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task MissingInNamespace()
{
await VerifyPropertyAbsenceAsync("""
namespace Namespace
{
$$
}
""");
}

[WpfFact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task MissingInFilescopedNamespace()
{
await VerifyPropertyAbsenceAsync("""
namespace Namespace;

$$
""");
}

[WpfFact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task MissingInTopLevelContext()
{
await VerifyPropertyAbsenceAsync("""
System.Console.WriteLine();
$$
""");
}

[WpfFact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task InsertSnippetInClass()
{
await VerifyDefaultPropertyAsync("""
class MyClass
{
$$
}
""");
}

[WpfFact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task InsertSnippetInRecord()
{
await VerifyDefaultPropertyAsync("""
record MyRecord
{
$$
}
""");
}

[WpfFact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task InsertSnippetInStruct()
{
await VerifyDefaultPropertyAsync("""
struct MyStruct
{
$$
}
""");
}

// This case might produce non-default results for different snippets (e.g. no `set` accessor in 'propg' snippet),
// so it is tested separately for all of them
[WpfFact, Trait(Traits.Feature, Traits.Features.Completion)]
public abstract Task InsertSnippetInInterface();

[WpfFact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task InsertSnippetNaming()
{
await VerifyDefaultPropertyAsync("""
class MyClass
{
public int MyProperty { get; set; }
$$
}
""", "MyProperty1");
}

[WpfFact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task MissingInEnum()
{
await VerifyPropertyAbsenceAsync("""
enum MyEnum
{
$$
}
""");
}

[WpfFact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task MissingInMethod()
{
await VerifyPropertyAbsenceAsync("""
class Program
{
public void Method()
{
$$
}
}
""");
}

[WpfFact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task MissingInConstructor()
{
await VerifyPropertyAbsenceAsync("""
class Program
{
public Program()
{
$$
}
}
""");
}

private Task VerifyPropertyAbsenceAsync(string markup) => VerifyItemIsAbsentAsync(markup, ItemToCommit);

protected async Task VerifyPropertyAsync(string markup, string propertyText)
{
TestFileMarkupParser.GetPosition(markup, out var code, out var position);
var expectedCode = code.Insert(position, propertyText + "$$");
await VerifyCustomCommitProviderAsync(markup, ItemToCommit, expectedCode);
}

protected Task VerifyDefaultPropertyAsync(string markup, string propertyName = "MyProperty")
=> VerifyPropertyAsync(markup, GetDefaultPropertyText(propertyName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,179 +2,25 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Completion.CompletionProviders.Snippets
{
public class CSharpPropSnippetCompletionProviderTests : AbstractCSharpSnippetCompletionProviderTests
public class CSharpPropSnippetCompletionProviderTests : AbstractCSharpAutoPropertyCompletionProviderTests
{
protected override string ItemToCommit => "prop";

[WpfFact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task PropSnippetMissingInNamespace()
{
var markupBeforeCommit =
@"namespace Namespace
{
$$
}";

await VerifyItemIsAbsentAsync(markupBeforeCommit, ItemToCommit);
}
protected override string GetDefaultPropertyText(string propertyName)
=> $"public int {propertyName} {{ get; set; }}";

[WpfFact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task PropSnippetMissingInFilescopedNamespace()
public override async Task InsertSnippetInInterface()
{
var markupBeforeCommit =
@"namespace Namespace;

$$";

await VerifyItemIsAbsentAsync(markupBeforeCommit, ItemToCommit);
}

[WpfFact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task PropSnippetMissingInTopLevelContext()
{
var markupBeforeCommit =
@"System.Console.WriteLine();
$$";

await VerifyItemIsAbsentAsync(markupBeforeCommit, ItemToCommit);
}

[WpfFact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task InsertPropSnippetInClassTest()
{
var markupBeforeCommit =
@"class MyClass
{
$$
}";

var expectedCodeAfterCommit =
@"class MyClass
{
public int MyProperty { get; set; }$$
}";
await VerifyCustomCommitProviderAsync(markupBeforeCommit, ItemToCommit, expectedCodeAfterCommit);
}

[WpfFact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task InsertPropSnippetInRecordTest()
{
var markupBeforeCommit =
@"record MyRecord
{
$$
}";

var expectedCodeAfterCommit =
@"record MyRecord
{
public int MyProperty { get; set; }$$
}";
await VerifyCustomCommitProviderAsync(markupBeforeCommit, ItemToCommit, expectedCodeAfterCommit);
}

[WpfFact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task InsertPropSnippetInStructTest()
{
var markupBeforeCommit =
@"struct MyStruct
{
$$
}";

var expectedCodeAfterCommit =
@"struct MyStruct
{
public int MyProperty { get; set; }$$
}";
await VerifyCustomCommitProviderAsync(markupBeforeCommit, ItemToCommit, expectedCodeAfterCommit);
}

[WpfFact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task InsertPropSnippetInInterfaceTest()
{
var markupBeforeCommit =
@"interface MyInterface
{
$$
}";

var expectedCodeAfterCommit =
@"interface MyInterface
{
public int MyProperty { get; set; }$$
}";
await VerifyCustomCommitProviderAsync(markupBeforeCommit, ItemToCommit, expectedCodeAfterCommit);
}

[WpfFact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task InsertPropSnippetNamingTest()
{
var markupBeforeCommit =
@"class MyClass
{
public int MyProperty { get; set; }
$$
}";

var expectedCodeAfterCommit =
@"class MyClass
{
public int MyProperty { get; set; }
public int MyProperty1 { get; set; }$$
}";
await VerifyCustomCommitProviderAsync(markupBeforeCommit, ItemToCommit, expectedCodeAfterCommit);
}

[WpfFact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task NoPropSnippetInEnumTest()
{
var markupBeforeCommit =
@"enum MyEnum
{
$$
}";

await VerifyItemIsAbsentAsync(markupBeforeCommit, ItemToCommit);
}

[WpfFact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task NoPropSnippetInMethodTest()
{
var markupBeforeCommit =
@"class Program
{
public void Method()
{
$$
}
}";
await VerifyItemIsAbsentAsync(markupBeforeCommit, ItemToCommit);
}

[WpfFact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task NoPropSnippetInConstructorTest()
{
var markupBeforeCommit =
@"class Program
{
public Program()
{
$$
}
}";
await VerifyItemIsAbsentAsync(markupBeforeCommit, ItemToCommit);
await VerifyDefaultPropertyAsync("""
interface MyInterface
{
$$
}
""");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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.

using System.Threading.Tasks;

namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Completion.CompletionProviders.Snippets
{
public class CSharpPropgSnippetCompletionProviderTests : AbstractCSharpAutoPropertyCompletionProviderTests
{
protected override string ItemToCommit => "propg";

protected override string GetDefaultPropertyText(string propertyName)
=> $"public int {propertyName} {{ get; private set; }}";

public override async Task InsertSnippetInInterface()
{
// Ensure we don't generate redundant `set` accessor when executed in interface
await VerifyPropertyAsync("""
interface MyInterface
{
$$
}
""", "public int MyProperty { get; }");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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.

using System.Threading.Tasks;

namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Completion.CompletionProviders.Snippets
{
public class CSharpPropiSnippetCompletionProviderTests : AbstractCSharpAutoPropertyCompletionProviderTests
{
protected override string ItemToCommit => "propi";

protected override string GetDefaultPropertyText(string propertyName)
=> $"public int {propertyName} {{ get; init; }}";

public override async Task InsertSnippetInInterface()
{
await VerifyDefaultPropertyAsync("""
interface MyInterface
{
$$
}
""");
}
}
}
Loading