<commit_before>﻿' 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.

Imports Microsoft.CodeAnalysis.CodeRefactorings
Imports Microsoft.CodeAnalysis.VisualBasic.CodeRefactorings.ConvertConversionOperators

Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.CodeRefactorings.ConvertConversionOperators
    <Trait(Traits.Feature, Traits.Features.ConvertConversionOperators)>
    Public Class ConvertConversionOperatorFromCTypeTests
        Inherits AbstractVisualBasicCodeActionTest

        Protected Overrides Function CreateCodeRefactoringProvider(workspace As Workspace, parameters As TestParameters) As CodeRefactoringProvider
            Return New VisualBasicConvertConversionOperatorFromCTypeCodeRefactoringProvider()
        End Function

        <Fact>
        Public Async Function ConvertFromCTypeToTryCast() As Task
            Dim markup =
<File>
Module Program
    Sub M()
        Dim x = CType(1[||], Object)
    End Sub
End Module
</File>

            Dim expected =
<File>
Module Program
    Sub M()
        Dim x = TryCast(1, Object)
    End Sub
End Module
</File>

            Await TestAsync(markup, expected)
        End Function

        <Fact>
        Public Async Function ConvertFromCTypeNoConversionIfTypeIsValueType() As Task
            Dim markup =
<File>
Module Program
    Sub M()
        Dim x = CType(1[||], Byte)
    End Sub
End Module
</File>

            Await TestMissingAsync(markup)
        End Function

        <Fact>
        Public Async Function ConvertFromCTypeNoConversionIfTypeIsValueType_GenericTypeConstraint() As Task
            Dim markup =
<File>
Module Program
    Sub M(Of T As Structure)()
        Dim x = CType([||]1, T)
    End Sub
End Module
</File>

            Await TestMissingAsync(markup)
        End Function

        <Fact>
        Public Async Function ConvertFromCTypeConversionIfTypeIsRefernceType_Constraint() As Task
            Dim markup =
<File>
Module Program
    Sub M(Of T As Class)()
        Dim x = CType([||]1, T)
    End Sub
End Module
</File>
            Dim expected =
<File>
Module Program
    Sub M(Of T As Class)()
        Dim x = TryCast(1, T)
    End Sub
End Module
</File>

            Await TestAsync(markup, expected)
        End Function

        <Fact>
        Public Async Function ConvertFromCTypeNoConversionIfTypeIsMissing() As Task
            Dim markup =
<File>
Module Program
    Sub M()
        Dim x = CType([||]1, MissingType)
    End Sub
End Module
</File>

            Await TestMissingAsync(markup)
        End Function

        <Fact>
        Public Async Function ConvertFromCTypeNoConversionIfTypeIsValueType_GenericUnconstraint() As Task
            Dim markup =
<File>
Module Program
    Sub M(Of T)()
        Dim x = CType([||]1, T)
    End Sub
End Module
</File>

            Await TestMissingAsync(markup)
        End Function

        <Fact>
        Public Async Function ConvertFromCBoolIsNotOffered() As Task
            Dim markup =
<File>
Module Program
    Sub M()
        Dim x = CBool([||]1)
    End Sub
End Module
</File>

            Await TestMissingAsync(markup)
        End Function

        <Theory>
        <InlineData("CType(CType(1, [||]object), C)",
                    "CType(TryCast(1, object), C)")>
        <InlineData("CType(CType(1, object), [||]C)",
                    "TryCast(CType(1, object), C)")>
        Public Async Function ConvertFromCTypeNested(cTypeExpression As String, converted As String) As Task
            Dim markup =
<File>
Public Class C
End Class

Module Program
    Sub M()
        Dim x = <%= cTypeExpression %>
    End Sub
End Module
</File>

            Dim fixed =
<File>
Public Class C
End Class

Module Program
    Sub M()
        Dim x = <%= converted %>
    End Sub
End Module
</File>

            Await TestAsync(markup, fixed)
        End Function
    End Class
End Namespace<commit_msg>Use "VerifyVB" in ConvertConversionOperatorFromCTypeTests<commit_after>﻿' 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.

Imports Microsoft.CodeAnalysis.Testing
Imports VerifyVB = Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions.VisualBasicCodeRefactoringVerifier(Of
    Microsoft.CodeAnalysis.VisualBasic.CodeRefactorings.ConvertConversionOperators.VisualBasicConvertConversionOperatorFromCTypeCodeRefactoringProvider)

Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.CodeRefactorings.ConvertConversionOperators
    <Trait(Traits.Feature, Traits.Features.ConvertConversionOperators)>
    Public Class ConvertConversionOperatorFromCTypeTests

        <Fact>
        Public Async Function ConvertFromCTypeToTryCast() As Task
            Dim markup = "
Module Program
    Sub M()
        Dim x = CType(1[||], Object)
    End Sub
End Module
"

            Dim expected = "
Module Program
    Sub M()
        Dim x = TryCast(1, Object)
    End Sub
End Module
"
            Await New VerifyVB.Test With
            {
                .TestCode = markup,
                .FixedCode = expected
            }.RunAsync()
        End Function

        <Fact>
        Public Async Function ConvertFromCTypeNoConversionIfTypeIsValueType() As Task
            Dim markup = "
Module Program
    Sub M()
        Dim x = CType(1[||], Byte)
    End Sub
End Module
"

            Await New VerifyVB.Test With
            {
                .TestCode = markup,
                .FixedCode = markup,
                .OffersEmptyRefactoring = False
            }.RunAsync()
        End Function

        <Fact>
        Public Async Function ConvertFromCTypeNoConversionIfTypeIsValueType_GenericTypeConstraint() As Task
            Dim markup = "
Module Program
    Sub M(Of T As Structure)()
        Dim o = new Object()
        Dim x = CType([||]o, T)
    End Sub
End Module
"

            Await New VerifyVB.Test With
            {
                .TestCode = markup,
                .FixedCode = markup,
                .OffersEmptyRefactoring = False
            }.RunAsync()
        End Function

        <Fact>
        Public Async Function ConvertFromCTypeConversionIfTypeIsRefernceType_Constraint() As Task
            Dim markup = "
Module Program
    Sub M(Of T As Class)()
        Dim o = new Object()
        Dim x = CType([||]o, T)
    End Sub
End Module
"
            Dim expected = "
Module Program
    Sub M(Of T As Class)()
        Dim o = new Object()
        Dim x = TryCast(o, T)
    End Sub
End Module
"

            Await New VerifyVB.Test With
            {
                .TestCode = markup,
                .FixedCode = expected
            }.RunAsync()
        End Function

        <Fact>
        Public Async Function ConvertFromCTypeNoConversionIfTypeIsMissing() As Task
            Dim markup = "
Module Program
    Sub M()
        Dim x = CType([||]1, {|#0:MissingType|})
    End Sub
End Module
"

            Dim verify = New VerifyVB.Test With
            {
                .OffersEmptyRefactoring = False
            }
            verify.TestState.Sources.Add(markup)
            verify.TestState.ExpectedDiagnostics.Add(DiagnosticResult.CompilerError("BC30002").WithLocation(0).WithArguments("MissingType"))
            verify.FixedState.Sources.Add(markup)
            verify.FixedState.ExpectedDiagnostics.Add(DiagnosticResult.CompilerError("BC30002").WithLocation(0).WithArguments("MissingType"))
            Await verify.RunAsync()
        End Function

        <Fact>
        Public Async Function ConvertFromCTypeNoConversionIfTypeIsValueType_GenericUnconstraint() As Task
            Dim markup = "
Module Program
    Sub M(Of T)()
        Dim o = new Object()
        Dim x = CType([||]o, T)
    End Sub
End Module
"

            Await New VerifyVB.Test With
            {
                .TestCode = markup,
                .FixedCode = markup,
                .OffersEmptyRefactoring = False
            }.RunAsync()
        End Function

        <Fact>
        Public Async Function ConvertFromCBoolIsNotOffered() As Task
            Dim markup = "
Module Program
    Sub M()
        Dim x = CBool([||]1)
    End Sub
End Module
"

            Await New VerifyVB.Test With
            {
                .TestCode = markup,
                .FixedCode = markup,
                .OffersEmptyRefactoring = False
            }.RunAsync()
        End Function

        <Theory>
        <InlineData("CType(CType(1, [||]object), C)",
                    "CType(TryCast(1, object), C)")>
        <InlineData("CType(CType(1, object), [||]C)",
                    "TryCast(CType(1, object), C)")>
        Public Async Function ConvertFromCTypeNested(cTypeExpression As String, converted As String) As Task
            Dim markup = "
Public Class C
End Class

Module Program
    Sub M()
        Dim x = " + cTypeExpression + "
    End Sub
End Module
"

            Dim fixed = "
Public Class C
End Class

Module Program
    Sub M()
        Dim x = " + converted + "
    End Sub
End Module
"

            Await New VerifyVB.Test With
            {
                .TestCode = markup,
                .FixedCode = fixed
            }.RunAsync()
        End Function
    End Class
End Namespace
{'fraction_non_alphanumeric': 0.0782099094299414, 'fraction_numerical': 0.0037293553542887587, 'mean_word_length': 2.121383438643166, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5533813', 'n_tokens_mistral': 2633, 'n_tokens_neox': 2539, 'n_words': 781}