' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. Imports System.Threading.Tasks Imports Microsoft.CodeAnalysis Imports Roslyn.Test.Utilities Namespace Microsoft.VisualStudio.LanguageServices.UnitTests.CodeModel.CSharp Public Class CodeParameterTests Inherits AbstractCodeParameterTests #Region "AddAttribute tests" Public Async Function TestAddAttribute1() As Task Dim code = class C { void Foo(string $$s) { } } Dim expected = class C { void Foo([Out()] string s) { } } Await TestAddAttribute(code, expected, New AttributeData With {.Name = "Out"}) End Function Public Async Function TestAddAttribute2() As Task Dim code = class C { void Foo([Out()]string $$s) { } } Dim expected = class C { void Foo([Foo()][Out()]string s) { } } Await TestAddAttribute(code, expected, New AttributeData With {.Name = "Foo"}) End Function #End Region #Region "DefaultValue tests" Public Async Function TestDefaultValue1() As Task Dim code = class C { void M(string $$s = "Foo") { } } Await TestDefaultValue(code, """Foo""") End Function Public Async Function TestDefaultValue_ExternalCodeParameter_NoDefaultValue() As Task Dim code = class C : System.Console { void M(string $$s = "Foo") { } } Await TestElement(code, Sub(codeParameter) Dim method = TryCast(codeParameter.Parent, EnvDTE80.CodeFunction2) Assert.NotNull(method) Dim containingType = TryCast(method.Parent, EnvDTE80.CodeClass2) Assert.NotNull(containingType) Dim baseType = TryCast(containingType.Bases.Item(1), EnvDTE80.CodeClass2) Assert.NotNull(baseType) Dim [overloads] = CType(baseType.Members.Item("WriteLine"), EnvDTE80.CodeFunction2).Overloads Dim method2 = CType([overloads].Item(2), EnvDTE80.CodeFunction2) Dim defaultValue = CType(method2.Parameters.Item(1), EnvDTE80.CodeParameter2).DefaultValue End Sub) End Function #End Region #Region "FullName tests" Public Async Function TestFullName1() As Task Dim code = class C { void Foo(string $$s) { } } Await TestFullName(code, "s") End Function #End Region #Region "Kind tests" Public Async Function TestKind1() As Task Dim code = class C { void Foo(string $$s) { } } Await TestKind(code, EnvDTE.vsCMElement.vsCMElementParameter) End Function #End Region #Region "ParameterKind tests" Public Async Function TestParameterKind_None() As Task Dim code = class C { void Foo(string $$s) { } } Await TestParameterKind(code, EnvDTE80.vsCMParameterKind.vsCMParameterKindNone) End Function Public Async Function TestParameterKind_Ref() As Task Dim code = class C { void Foo(ref string $$s) { } } Await TestParameterKind(code, EnvDTE80.vsCMParameterKind.vsCMParameterKindRef) End Function Public Async Function TestParameterKind_Out() As Task Dim code = class C { void Foo(out string $$s) { } } Await TestParameterKind(code, EnvDTE80.vsCMParameterKind.vsCMParameterKindOut) End Function Public Async Function TestParameterKind_ParamArray() As Task Dim code = class C { void Foo(params string[] $$s) { } } Await TestParameterKind(code, EnvDTE80.vsCMParameterKind.vsCMParameterKindParamArray) End Function Public Async Function TestParameterKind_Optional() As Task Dim code = class C { void Foo(string $$s = "Foo") { } } Await TestParameterKind(code, EnvDTE80.vsCMParameterKind.vsCMParameterKindOptional) End Function Public Async Function TestParameterKind_OptionalAndRef() As Task Dim code = class C { void Foo(ref string $$s = "Foo") { } } Await TestParameterKind(code, EnvDTE80.vsCMParameterKind.vsCMParameterKindOptional Or EnvDTE80.vsCMParameterKind.vsCMParameterKindRef) End Function #End Region #Region "Parent tests" Public Async Function TestParent1() As Task Dim code = class C { void M(string $$s) { } } Await TestParent(code, IsElement("M", kind:=EnvDTE.vsCMElement.vsCMElementFunction)) End Function Public Async Function TestParent2() As Task Dim code = delegate void Foo(int $$i); Await TestParent(code, IsElement("Foo", kind:=EnvDTE.vsCMElement.vsCMElementDelegate)) End Function Public Async Function TestParent3() As Task Dim code = class C { int this[int $$i] { get { return 0; } } } Await TestParent(code, IsElement("this", kind:=EnvDTE.vsCMElement.vsCMElementProperty)) End Function #End Region #Region "Type tests" Public Async Function TestType1() As Task Dim code = class C { public void Foo(int i$$ = 0) { } } Await TestTypeProp(code, New CodeTypeRefData With { .AsString = "int", .AsFullName = "System.Int32", .CodeTypeFullName = "System.Int32", .TypeKind = EnvDTE.vsCMTypeRef.vsCMTypeRefInt }) End Function #End Region #Region "Set ParameterKind tests" Public Async Function TestSetParameterKind_In() As Task Dim code = class C { void M(out string $$s) { } } Dim expected = class C { void M(string s) { } } Await TestSetParameterKind(code, expected, EnvDTE80.vsCMParameterKind.vsCMParameterKindIn) End Function Public Async Function TestSetParameterKind_None() As Task Dim code = class C { void M(out string $$s) { } } Dim expected = class C { void M(string s) { } } Await TestSetParameterKind(code, expected, EnvDTE80.vsCMParameterKind.vsCMParameterKindNone) End Function Public Async Function TestSetParameterKind_Out() As Task Dim code = class C { void M(ref string $$s) { } } Dim expected = class C { void M(out string s) { } } Await TestSetParameterKind(code, expected, EnvDTE80.vsCMParameterKind.vsCMParameterKindOut) End Function Public Async Function TestSetParameterKind_Ref() As Task Dim code = class C { void M(string $$s) { } } Dim expected = class C { void M(ref string s) { } } Await TestSetParameterKind(code, expected, EnvDTE80.vsCMParameterKind.vsCMParameterKindRef) End Function Public Async Function TestSetParameterKind_Params() As Task Dim code = class C { void M(string[] $$s) { } } Dim expected = class C { void M(params string[] s) { } } Await TestSetParameterKind(code, expected, EnvDTE80.vsCMParameterKind.vsCMParameterKindParamArray) End Function Public Async Function TestSetParameterKind_ParamsInvalid() As Task Dim code = class C { void M(string $$s) { } } Dim expected = class C { void M(string s) { } } Await TestSetParameterKind(code, expected, EnvDTE80.vsCMParameterKind.vsCMParameterKindParamArray, ThrowsArgumentException(Of EnvDTE80.vsCMParameterKind)()) End Function Public Async Function TestSetParameterKind_Optional() As Task Dim code = class C { void M(string $$s) { } } Dim expected = class C { void M(ref string s) { } } Await TestSetParameterKind(code, expected, EnvDTE80.vsCMParameterKind.vsCMParameterKindRef Or EnvDTE80.vsCMParameterKind.vsCMParameterKindOptional) End Function Public Async Function TestSetParameterKind_Same() As Task Dim code = class C { void M(out string $$s) { } } Dim expected = class C { void M(out string s) { } } Await TestSetParameterKind(code, expected, EnvDTE80.vsCMParameterKind.vsCMParameterKindOut) End Function #End Region #Region "Set DefaultValue tests" Public Async Function TestSetDefaultValue1() As Task Dim code = class C { void M(string $$s) { } } Dim expected = class C { void M(string s = "Foo") { } } Await TestSetDefaultValue(code, expected, """Foo""") End Function Public Async Function TestSetDefaultValue_ReplaceExisting() As Task Dim code = class C { void M(string $$s = "Bar") { } } Dim expected = class C { void M(string s = "Foo") { } } Await TestSetDefaultValue(code, expected, """Foo""") End Function Public Async Function TestSetDefaultValue_None() As Task Dim code = class C { void M(string $$s = "Bar") { } } Dim expected = class C { void M(string s) { } } Await TestSetDefaultValue(code, expected, "") End Function #End Region #Region "Set Type tests" Public Async Function TestSetType1() As Task Dim code = class C { public void Foo(int $$i) { } } Dim expected = class C { public void Foo(byte?[,] i) { } } Await TestSetTypeProp(code, expected, "byte?[,]") End Function Public Async Function TestSetType2() As Task Dim code = delegate void Foo(int $$i) { } Dim expected = delegate void Foo(byte?[,] i) { } Await TestSetTypeProp(code, expected, "byte?[,]") End Function #End Region Public Async Function TestTypeDescriptor_GetProperties() As Task Dim code = class C { void M(int $$p) { } } Dim expectedPropertyNames = {"DTE", "Collection", "Name", "FullName", "ProjectItem", "Kind", "IsCodeType", "InfoLocation", "Children", "Language", "StartPoint", "EndPoint", "ExtenderNames", "ExtenderCATID", "Parent", "Type", "Attributes", "DocComment", "ParameterKind", "DefaultValue"} Await TestPropertyDescriptors(code, expectedPropertyNames) End Function Protected Overrides ReadOnly Property LanguageName As String Get Return LanguageNames.CSharp End Get End Property End Class End Namespace