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

add more miscellaneous tests for checked operators #60727

Merged
merged 2 commits into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,103 @@ public R()

await GenerateAndVerifySourceAsync(metadataSource, symbolName, LanguageNames.CSharp, expected: expected, signaturesOnly: signaturesOnly);
}

/// <summary>
/// This test must be updated when we switch to a new version of the decompiler that supports checked ops.
/// </summary>
[Theory, CombinatorialData, Trait(Traits.Feature, Traits.Features.MetadataAsSource)]
[WorkItem(42986, "https://github.com/dotnet/roslyn/issues/42986")]
public async Task TestCheckedOperators(bool signaturesOnly)
{
var metadataSource = @"
public class C
{
public static explicit operator string(C x) => throw new System.Exception();

public static explicit operator checked string(C x) => throw new System.Exception();

public static C operator -(C x) => throw new System.Exception();

public static C operator checked -(C x) => throw new System.Exception();

public static C operator +(C x, C y) => throw new System.Exception();

public static C operator checked +(C x, C y) => throw new System.Exception();
}";
var symbolName = "C";

var expected = signaturesOnly switch
{
true => $@"#region {FeaturesResources.Assembly} ReferencedAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// {CodeAnalysisResources.InMemoryAssembly}
#endregion

public class [|C|]
{{
public C();

public static C operator +(C x, C y);
public static C operator checked +(C x, C y);
public static C operator -(C x);
public static C operator checked -(C x);

public static explicit operator string(C x);
public static explicit operator checked string(C x);
}}",
false => $@"#region {FeaturesResources.Assembly} ReferencedAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// {CodeAnalysisResources.InMemoryAssembly}
// Decompiled with ICSharpCode.Decompiler {ICSharpCodeDecompilerVersion}
#endregion

using System;
using System.Runtime.CompilerServices;

public class [|C|]
{{
public static explicit operator string(C x)
{{
throw new Exception();
}}

[SpecialName]
public static string op_CheckedExplicit(C x)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is not yet a version of the decompiler that supports checked operators, so for now just asserting the only behavior we have.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 That's exactly what I decided to do for static interface members.

{{
throw new Exception();
}}

public static C operator -(C x)
{{
throw new Exception();
}}

[SpecialName]
public static C op_CheckedUnaryNegation(C x)
{{
throw new Exception();
}}

public static C operator +(C x, C y)
{{
throw new Exception();
}}

[SpecialName]
public static C op_CheckedAddition(C x, C y)
{{
throw new Exception();
}}
}}
#if false // {CSharpEditorResources.Decompilation_log}
{string.Format(CSharpEditorResources._0_items_in_cache, 6)}
------------------
{string.Format(CSharpEditorResources.Resolve_0, "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")}
{string.Format(CSharpEditorResources.Found_single_assembly_0, "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")}
{string.Format(CSharpEditorResources.Load_from_0, "mscorlib.v4_6_1038_0.dll")}
#endif",
};

await GenerateAndVerifySourceAsync(metadataSource, symbolName, LanguageNames.CSharp, languageVersion: "Preview", metadataLanguageVersion: "Preview", expected: expected, signaturesOnly: signaturesOnly);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -306,5 +306,65 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.NavigationBar
Item("C", Glyph.ClassInternal, children:={
Item("M(string? s, IEnumerable<string?> e)", Glyph.MethodPrivate)}))
End Function

<Theory, CombinatorialData, Trait(Traits.Feature, Traits.Features.NavigationBar), WorkItem(59458, "https://github.com/dotnet/roslyn/issues/59458")>
Public Async Function TestCheckedBinaryOperator(host As TestHost) As Task
Await AssertSelectedItemsAreAsync(
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
class C
{
public static C operator +(C x, C y) => throw new System.Exception();

public static C operator checked +(C x, C y) => throw new System.Exception();$$
}
</Document>
</Project>
</Workspace>,
host,
Item("C", Glyph.ClassInternal), False,
Item("operator checked +(C x, C y)", Glyph.Operator), False)
End Function

<Theory, CombinatorialData, Trait(Traits.Feature, Traits.Features.NavigationBar), WorkItem(59458, "https://github.com/dotnet/roslyn/issues/59458")>
Public Async Function TestCheckedUnaryOperator(host As TestHost) As Task
Await AssertSelectedItemsAreAsync(
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
class C
{
public static C operator -(C x) => throw new System.Exception();

public static C operator checked -(C x) => throw new System.Exception();$$
}
</Document>
</Project>
</Workspace>,
host,
Item("C", Glyph.ClassInternal), False,
Item("operator checked -(C x)", Glyph.Operator), False)
End Function

<Theory, CombinatorialData, Trait(Traits.Feature, Traits.Features.NavigationBar), WorkItem(59458, "https://github.com/dotnet/roslyn/issues/59458")>
Public Async Function TestCheckedCastOperator(host As TestHost) As Task
Await AssertSelectedItemsAreAsync(
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
class C
{
public static explicit operator string(C x) => throw new System.Exception();

public static explicit operator checked string(C x) => throw new System.Exception();$$
}
</Document>
</Project>
</Workspace>,
host,
Item("C", Glyph.ClassInternal), False,
Item("explicit operator checked string(C x)", Glyph.Operator), False)
End Function
End Class
End Namespace
Original file line number Diff line number Diff line change
Expand Up @@ -1526,5 +1526,86 @@ namespace EditorFunctionalityHelper
End Using
End Sub

<WorkItem(59458, "https://github.com/dotnet/roslyn/issues/59458")>
<WpfFact, Trait(Traits.Feature, Traits.Features.ObjectBrowser)>
Public Sub TestCheckedBinaryOperator()
Dim code =
<Code>
class C
{
public static C operator +(C x, C y) => throw new System.Exception();

public static C operator checked +(C x, C y) => throw new System.Exception();
}
</Code>

Using state = CreateLibraryManager(GetWorkspaceDefinition(code))
Dim library = state.GetLibrary()

Dim list = library.GetProjectList()
list.VerifyNames("CSharpAssembly1")

list = list.GetTypeList(0)
list.VerifyNames("C")

list = list.GetMemberList(0)
list.VerifyNames(AddressOf IsImmediateMember, "operator +(C, C)", "operator checked +(C, C)")
End Using
End Sub

<WorkItem(59458, "https://github.com/dotnet/roslyn/issues/59458")>
<WpfFact, Trait(Traits.Feature, Traits.Features.ObjectBrowser)>
Public Sub TestCheckedUnaryOperator()
Dim code =
<Code>
class C
{
public static C operator -(C x) => throw new System.Exception();

public static C operator checked -(C x) => throw new System.Exception();
}
</Code>

Using state = CreateLibraryManager(GetWorkspaceDefinition(code))
Dim library = state.GetLibrary()

Dim list = library.GetProjectList()
list.VerifyNames("CSharpAssembly1")

list = list.GetTypeList(0)
list.VerifyNames("C")

list = list.GetMemberList(0)
list.VerifyNames(AddressOf IsImmediateMember, "operator -(C)", "operator checked -(C)")
End Using
End Sub

<WorkItem(59458, "https://github.com/dotnet/roslyn/issues/59458")>
<WpfFact, Trait(Traits.Feature, Traits.Features.ObjectBrowser)>
Public Sub TestCheckedCastOperator()
Dim code =
<Code>
class C
{
public static explicit operator string(C x) => throw new System.Exception();

public static explicit operator checked string(C x) => throw new System.Exception();$$
}
</Code>

Using state = CreateLibraryManager(GetWorkspaceDefinition(code))
Dim library = state.GetLibrary()

Dim list = library.GetProjectList()
list.VerifyNames("CSharpAssembly1")

list = list.GetTypeList(0)
list.VerifyNames("C")

list = list.GetMemberList(0)
list.VerifyNames(AddressOf IsImmediateMember, "explicit operator string(C)", "explicit operator checked string(C)")
End Using
End Sub

End Class
End Namespace