/
githubmirror
/
roslyn
Обзор
Документация
Войти
/
githubmirror
/
roslyn
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Analyzers/VisualBasic/Tests/UseObjectInitializer/UseObjectInitializerTests.vb
730 строк
17 KB
Cyrus Najmabadi
Working VB
16 янв 2026, 17:05
Не верифицирован
16 янв 2026, 17:05
a2c8b9b
Код
Авторство
О чём код?
' 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 VerifyVB = Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions.VisualBasicCodeFixVerifier(Of Microsoft.CodeAnalysis.VisualBasic.UseObjectInitializer.VisualBasicUseObjectInitializerDiagnosticAnalyzer, Microsoft.CodeAnalysis.VisualBasic.UseObjectInitializer.VisualBasicUseObjectInitializerCodeFixProvider) Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.Diagnostics.UseObjectInitializer <Trait(Traits.Feature, Traits.Features.CodeActionsUseObjectInitializer)> Public Class UseObjectInitializerTests Private Shared Async Function TestMissingInRegularAndScriptAsync(testCode As String) As Task Await New VerifyVB.Test With { .TestCode = testCode, .FixedCode = testCode }.RunAsync() End Function <Fact> Public Async Function TestOnVariableDeclarator() As Task Dim testCode = " Class C Dim i As Integer Sub M() Dim c = [|New|] C() [|c|].i = 1 End Sub End Class" Dim fixedCode = " Class C Dim i As Integer Sub M() Dim c = New C With { .i = 1 } End Sub End Class" Await New VerifyVB.Test With { .TestCode = testCode, .FixedCode = fixedCode }.RunAsync() End Function <Fact> Public Async Function TestOnVariableDeclarator2() As Task Dim testCode = " Class C Dim i As Integer Sub M() Dim c As [|New|] C() [|c|].i = 1 End Sub End Class" Dim fixedCode = " Class C Dim i As Integer Sub M() Dim c As New C With { .i = 1 } End Sub End Class" Await New VerifyVB.Test With { .TestCode = testCode, .FixedCode = fixedCode }.RunAsync() End Function <Fact> Public Async Function TestOnAssignmentExpression() As Task Dim testCode = " Class C Dim i As Integer Sub M() Dim c as C = Nothing c = [|New|] C() [|c|].i = 1 End Sub End Class" Dim fixedCode = " Class C Dim i As Integer Sub M() Dim c as C = Nothing c = New C With { .i = 1 } End Sub End Class" Await New VerifyVB.Test With { .TestCode = testCode, .FixedCode = fixedCode }.RunAsync() End Function <Fact> Public Async Function TestStopOnDuplicateMember() As Task Dim testCode = " Class C Dim i As Integer Sub M() Dim c = [|New|] C() [|c|].i = 1 c.i = 2 End Sub End Class" Dim fixedCode = " Class C Dim i As Integer Sub M() Dim c = New C With { .i = 1 } c.i = 2 End Sub End Class" Await New VerifyVB.Test With { .TestCode = testCode, .FixedCode = fixedCode }.RunAsync() End Function <Fact> Public Async Function TestComplexInitializer() As Task Dim testCode = " Class C Dim i As Integer Dim j As Integer Sub M() Dim array As C() array(0) = [|New|] C() [|array(0)|].i = 1 [|array(0)|].j = 2 End Sub End Class" Dim fixedCode = " Class C Dim i As Integer Dim j As Integer Sub M() Dim array As C() array(0) = New C With { .i = 1, .j = 2 } End Sub End Class" Await New VerifyVB.Test With { .TestCode = testCode, .FixedCode = fixedCode }.RunAsync() End Function <Fact> Public Async Function TestNotOnCompoundAssignment() As Task Dim testCode = " Class C Dim i As Integer Dim j As Integer Sub M() Dim c = [|New|] C() [|c|].i = 1 c.j += 1 End Sub End Class" Dim fixedCode = " Class C Dim i As Integer Dim j As Integer Sub M() Dim c = New C With { .i = 1 } c.j += 1 End Sub End Class" Await New VerifyVB.Test With { .TestCode = testCode, .FixedCode = fixedCode }.RunAsync() End Function <Fact, WorkItem("https://github.com/dotnet/roslyn/issues/39146")> Public Async Function TestWithExistingInitializer() As Task Dim testCode = " Class C Dim i As Integer Dim j As Integer Sub M() Dim c = [|New|] C() With { .i = 1 } [|c|].j = 1 End Sub End Class" Dim fixedCode = " Class C Dim i As Integer Dim j As Integer Sub M() Dim c = New C With { .i = 1, .j = 1 } End Sub End Class" Await New VerifyVB.Test With { .TestCode = testCode, .FixedCode = fixedCode }.RunAsync() End Function <Fact, WorkItem("https://github.com/dotnet/roslyn/issues/39146")> Public Async Function TestWithExistingInitializerNotIfAlreadyInitialized() As Task Dim testCode = " Class C Dim i As Integer Dim j As Integer Sub M() Dim c = [|New|] C() With { .i = 1 } [|c|].j = 1 c.i = 2 End Sub End Class" Dim fixedCode = " Class C Dim i As Integer Dim j As Integer Sub M() Dim c = New C With { .i = 1, .j = 1 } c.i = 2 End Sub End Class" Await New VerifyVB.Test With { .TestCode = testCode, .FixedCode = fixedCode }.RunAsync() End Function <Fact, WorkItem("https://github.com/dotnet/roslyn/issues/15012")> Public Async Function TestMissingIfImplicitMemberAccessWouldChange() As Task Await TestMissingInRegularAndScriptAsync( " imports system.diagnostics Class C Sub M() With New String(Nothing) Dim x As ProcessStartInfo = New ProcessStartInfo() x.Arguments = .Length.ToString() End With End Sub End Class") End Function <Fact, WorkItem("https://github.com/dotnet/roslyn/issues/15012")> Public Async Function TestIfImplicitMemberAccessWouldNotChange() As Task Dim testCode = " imports system.diagnostics Class C Sub M() Dim x As ProcessStartInfo = [|New|] ProcessStartInfo() [|x|].Arguments = {|BC30491:Sub() With New String(Nothing) Dim a = .Length.ToString() End With End Sub()|} End Sub End Class" Dim fixedCode = " imports system.diagnostics Class C Sub M() Dim x As ProcessStartInfo = New ProcessStartInfo With { .Arguments = {|BC30491:Sub() With New String(Nothing) Dim a = .Length.ToString() End With End Sub()|} } End Sub End Class" Await New VerifyVB.Test With { .TestCode = testCode, .FixedCode = fixedCode }.RunAsync() End Function <Fact> Public Async Function TestFixAllInDocument() As Task Dim testCode = " Class C Dim i As Integer Dim j As Integer Sub M() Dim array As C() array(0) = [|New|] C() [|array(0)|].i = 1 [|array(0)|].j = 2 array(1) = [|New|] C() [|array(1)|].i = 3 [|array(1)|].j = 4 End Sub End Class" Dim fixedCode = " Class C Dim i As Integer Dim j As Integer Sub M() Dim array As C() array(0) = New C With { .i = 1, .j = 2 } array(1) = New C With { .i = 3, .j = 4 } End Sub End Class" Await New VerifyVB.Test With { .TestCode = testCode, .FixedCode = fixedCode }.RunAsync() End Function <Fact> Public Async Function TestTrivia1() As Task Dim testCode = " Class C Dim i As Integer Dim j As Integer Sub M() Dim c = [|New|] C() [|c|].i = 1 ' Goo [|c|].j = 2 ' Bar End Sub End Class" Dim fixedCode = " Class C Dim i As Integer Dim j As Integer Sub M() Dim c = New C With { .i = 1, ' Goo .j = 2 ' Bar } End Sub End Class" Await New VerifyVB.Test With { .TestCode = testCode, .FixedCode = fixedCode }.RunAsync() End Function <Fact, WorkItem("https://github.com/dotnet/roslyn/issues/15525")> Public Async Function TestTrivia2() As Task Dim testCode = " Class C Sub M(Reader as String) Dim XmlAppConfigReader As [|New|] XmlTextReader(Reader) ' Required by Fxcop rule CA3054 - DoNotAllowDTDXmlTextReader [|XmlAppConfigReader|].x = 0 [|XmlAppConfigReader|].y = 1 End Sub End Class class XmlTextReader public sub new(reader as string) end sub public x as integer public y as integer end class " Dim fixedCode = " Class C Sub M(Reader as String) ' Required by Fxcop rule CA3054 - DoNotAllowDTDXmlTextReader Dim XmlAppConfigReader As New XmlTextReader(Reader) With { .x = 0, .y = 1 } End Sub End Class class XmlTextReader public sub new(reader as string) end sub public x as integer public y as integer end class " Await New VerifyVB.Test With { .TestCode = testCode, .FixedCode = fixedCode }.RunAsync() End Function <Fact, WorkItem("https://github.com/dotnet/roslyn/issues/15525")> Public Async Function TestTrivia3() As Task Dim testCode = " Class C Sub M(Reader as String) Dim XmlAppConfigReader As [|New|] XmlTextReader(Reader) ' Required by Fxcop rule CA3054 - DoNotAllowDTDXmlTextReader [|XmlAppConfigReader|].x = 0 ' Bar [|XmlAppConfigReader|].y = 1 End Sub End Class class XmlTextReader public sub new(reader as string) end sub public x as integer public y as integer end class " Dim fixedCode = " Class C Sub M(Reader as String) ' Required by Fxcop rule CA3054 - DoNotAllowDTDXmlTextReader ' Bar Dim XmlAppConfigReader As New XmlTextReader(Reader) With { .x = 0, .y = 1 } End Sub End Class class XmlTextReader public sub new(reader as string) end sub public x as integer public y as integer end class " Await New VerifyVB.Test With { .TestCode = testCode, .FixedCode = fixedCode }.RunAsync() End Function <Fact, WorkItem("https://devdiv.visualstudio.com/DevDiv/_workitems?id=401322")> Public Async Function TestSharedMember() As Task Dim testCode = " Class C Dim x As Integer Shared y As Integer Sub M() Dim z = [|New|] C() [|z|].x = 1 z.y = 2 End Sub End Class " Dim fixedCode = " Class C Dim x As Integer Shared y As Integer Sub M() Dim z = New C With { .x = 1 } z.y = 2 End Sub End Class " Await New VerifyVB.Test With { .TestCode = testCode, .FixedCode = fixedCode }.RunAsync() End Function <Fact, WorkItem("https://github.com/dotnet/roslyn/issues/23368")> Public Async Function TestWithExplicitImplementedInterfaceMembers1() As Task Await TestMissingInRegularAndScriptAsync( " class C Sub Bar() Dim c As IExample = New Goo c.Name = String.Empty End Sub End Class Interface IExample Property Name As String Property LastName As String End Interface Class Goo Implements IExample Private Property Name As String Implements IExample.Name Public Property LastName As String Implements IExample.LastName End Class ") End Function <Fact, WorkItem("https://github.com/dotnet/roslyn/issues/23368")> Public Async Function TestWithExplicitImplementedInterfaceMembers2() As Task Await TestMissingInRegularAndScriptAsync( " class C Sub Bar() Dim c As IExample = New Goo c.Name = String.Empty c.LastName = String.Empty End Sub End Class Interface IExample Property Name As String Property LastName As String End Interface Class Goo Implements IExample Private Property Name As String Implements IExample.Name Public Property LastName As String Implements IExample.LastName End Class ") End Function <Fact, WorkItem("https://github.com/dotnet/roslyn/issues/23368")> Public Async Function TestWithExplicitImplementedInterfaceMembers3() As Task Dim testCode = " class C Sub Bar() Dim c As IExample = [|New|] Goo [|c|].LastName = String.Empty c.Name = String.Empty End Sub End Class Interface IExample Property Name As String Property LastName As String End Interface Class Goo Implements IExample Private Property Name As String Implements IExample.Name Public Property LastName As String Implements IExample.LastName End Class " Dim fixedCode = " class C Sub Bar() Dim c As IExample = New Goo With { .LastName = String.Empty } c.Name = String.Empty End Sub End Class Interface IExample Property Name As String Property LastName As String End Interface Class Goo Implements IExample Private Property Name As String Implements IExample.Name Public Property LastName As String Implements IExample.LastName End Class " Await New VerifyVB.Test With { .TestCode = testCode, .FixedCode = fixedCode }.RunAsync() End Function <Fact, WorkItem("https://github.com/dotnet/roslyn/issues/23368")> Public Async Function TestWithExplicitImplementedInterfaceMembers4() As Task Dim testCode = " class C Sub Bar() Dim c As IExample = [|New|] Goo [|c|].LastName = String.Empty c.Name = String.Empty End Sub End Class Interface IExample Property Name As String Property LastName As String End Interface Class Goo Implements IExample Private Property Name As String Implements IExample.Name Public Property MyLastName As String Implements IExample.LastName End Class " Dim fixedCode = " class C Sub Bar() Dim c As IExample = New Goo With { .MyLastName = String.Empty } c.Name = String.Empty End Sub End Class Interface IExample Property Name As String Property LastName As String End Interface Class Goo Implements IExample Private Property Name As String Implements IExample.Name Public Property MyLastName As String Implements IExample.LastName End Class " Await New VerifyVB.Test With { .TestCode = testCode, .FixedCode = fixedCode }.RunAsync() End Function <Fact, WorkItem("https://github.com/dotnet/roslyn/issues/46665")> Public Async Function TestIndentationOfMultiLineExpressions1() As Task Dim testCode = " Class C Dim S As String Dim T as String Sub M(i as integer) Dim c = [|New|] C() [|c|].S = i _ .ToString() _ .ToString() [|c|].T = i. ToString(). ToString() End Sub End Class" Dim fixedCode = " Class C Dim S As String Dim T as String Sub M(i as integer) Dim c = New C With { .S = i _ .ToString() _ .ToString(), .T = i. ToString(). ToString() } End Sub End Class" Await New VerifyVB.Test With { .TestCode = testCode, .FixedCode = fixedCode }.RunAsync() End Function <Fact, WorkItem("https://github.com/dotnet/roslyn/issues/46665")> Public Async Function TestIndentationOfMultiLineExpressions2() As Task Dim testCode = " Class C Dim S As String Dim T as String Sub M(i as integer) Dim c = [|New|] C() [|c|].S = i _ .ToString() _ .ToString() [|c|].T = i.ToString(). ToString() End Sub End Class" Dim fixedCode = " Class C Dim S As String Dim T as String Sub M(i as integer) Dim c = New C With { .S = i _ .ToString() _ .ToString(), .T = i.ToString(). ToString() } End Sub End Class" Await New VerifyVB.Test With { .TestCode = testCode, .FixedCode = fixedCode }.RunAsync() End Function End Class End Namespace