' 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.Collections.Immutable Imports System.Linq Imports Roslyn.Test.Utilities Public Class VisualBasicParseOptionsTests Inherits BasicTestBase Private Sub TestProperty(Of T)(factory As Func(Of VisualBasicParseOptions, T, VisualBasicParseOptions), getter As Func(Of VisualBasicParseOptions, T), validValue As T) Dim oldOpt1 = VisualBasicParseOptions.Default Dim newOpt1 = factory(oldOpt1, validValue) Dim newOpt2 = factory(newOpt1, validValue) Assert.Equal(validValue, getter(newOpt1)) Assert.Same(newOpt2, newOpt1) End Sub Public Sub WithXxx() TestProperty(Function(old, value) old.WithKind(value), Function(opt) opt.Kind, SourceCodeKind.Script) TestProperty(Function(old, value) old.WithLanguageVersion(value), Function(opt) opt.LanguageVersion, LanguageVersion.VisualBasic9) TestProperty(Function(old, value) old.WithDocumentationMode(value), Function(opt) opt.DocumentationMode, DocumentationMode.None) End Sub Public Sub WithLatestLanguageVersion() Dim oldOpt1 = VisualBasicParseOptions.Default Dim newOpt1 = oldOpt1.WithLanguageVersion(LanguageVersion.Latest) Dim newOpt2 = newOpt1.WithLanguageVersion(LanguageVersion.Latest) Assert.Equal(LanguageVersion.Latest.MapSpecifiedToEffectiveVersion, oldOpt1.LanguageVersion) Assert.Equal(LanguageVersion.Latest.MapSpecifiedToEffectiveVersion, newOpt1.LanguageVersion) Assert.Equal(LanguageVersion.Latest.MapSpecifiedToEffectiveVersion, newOpt2.LanguageVersion) newOpt1 = oldOpt1.WithLanguageVersion(LanguageVersion.Default) newOpt2 = newOpt1.WithLanguageVersion(LanguageVersion.Default) Assert.Equal(LanguageVersion.Latest.MapSpecifiedToEffectiveVersion, oldOpt1.LanguageVersion) Assert.Equal(LanguageVersion.Latest.MapSpecifiedToEffectiveVersion, newOpt1.LanguageVersion) Assert.Equal(LanguageVersion.Latest.MapSpecifiedToEffectiveVersion, newOpt2.LanguageVersion) End Sub Public Sub WithPreprocessorSymbols() Dim syms = ImmutableArray.Create(New KeyValuePair(Of String, Object)("A", 1), New KeyValuePair(Of String, Object)("B", 2), New KeyValuePair(Of String, Object)("C", 3)) TestProperty(Function(old, value) old.WithPreprocessorSymbols(value), Function(opt) opt.PreprocessorSymbols, syms) Assert.Equal(0, VisualBasicParseOptions.Default.WithPreprocessorSymbols(syms).WithPreprocessorSymbols(CType(Nothing, ImmutableArray(Of KeyValuePair(Of String, Object)))).PreprocessorSymbols.Length) Assert.Equal(0, VisualBasicParseOptions.Default.WithPreprocessorSymbols(syms).WithPreprocessorSymbols(DirectCast(Nothing, IEnumerable(Of KeyValuePair(Of String, Object)))).PreprocessorSymbols.Length) Assert.Equal(0, VisualBasicParseOptions.Default.WithPreprocessorSymbols(syms).WithPreprocessorSymbols(DirectCast(Nothing, KeyValuePair(Of String, Object)())).PreprocessorSymbols.Length) End Sub Public Sub PredefinedPreprocessorSymbolsTests() Dim options = VisualBasicParseOptions.Default Dim empty = ImmutableArray.Create(Of KeyValuePair(Of String, Object))() Dim symbols = AddPredefinedPreprocessorSymbols(OutputKind.NetModule) AssertEx.SetEqual({New KeyValuePair(Of String, Object)("VBC_VER", PredefinedPreprocessorSymbols.CurrentVersionNumber), New KeyValuePair(Of String, Object)("TARGET", "module")}, symbols.AsEnumerable) ' if the symbols are already there, don't change their values symbols = AddPredefinedPreprocessorSymbols(OutputKind.DynamicallyLinkedLibrary, symbols) AssertEx.SetEqual({New KeyValuePair(Of String, Object)("VBC_VER", PredefinedPreprocessorSymbols.CurrentVersionNumber), New KeyValuePair(Of String, Object)("TARGET", "module")}, symbols.AsEnumerable) symbols = AddPredefinedPreprocessorSymbols(OutputKind.WindowsApplication, {New KeyValuePair(Of String, Object)("VBC_VER", "Foo"), New KeyValuePair(Of String, Object)("TARGET", 123)}) AssertEx.SetEqual({New KeyValuePair(Of String, Object)("VBC_VER", "Foo"), New KeyValuePair(Of String, Object)("TARGET", 123)}, symbols.AsEnumerable) symbols = AddPredefinedPreprocessorSymbols(OutputKind.WindowsApplication, New KeyValuePair(Of String, Object)("VBC_VER", "Foo"), New KeyValuePair(Of String, Object)("TARGET", 123)) AssertEx.SetEqual({New KeyValuePair(Of String, Object)("VBC_VER", "Foo"), New KeyValuePair(Of String, Object)("TARGET", 123)}, symbols.AsEnumerable) symbols = AddPredefinedPreprocessorSymbols(OutputKind.ConsoleApplication, empty) AssertEx.SetEqual({New KeyValuePair(Of String, Object)("VBC_VER", PredefinedPreprocessorSymbols.CurrentVersionNumber), New KeyValuePair(Of String, Object)("TARGET", "exe")}, symbols.AsEnumerable) symbols = AddPredefinedPreprocessorSymbols(OutputKind.WindowsApplication, empty) AssertEx.SetEqual({New KeyValuePair(Of String, Object)("VBC_VER", PredefinedPreprocessorSymbols.CurrentVersionNumber), New KeyValuePair(Of String, Object)("TARGET", "winexe")}, symbols.AsEnumerable) End Sub Public Sub CurrentVersionNumber() Dim highest = System.Enum. GetValues(GetType(LanguageVersion)). Cast(Of LanguageVersion). Select(Function(x) CInt(x)). Where(Function(x) x <> LanguageVersion.Latest). Max() Assert.Equal(highest, CInt(PredefinedPreprocessorSymbols.CurrentVersionNumber)) End Sub Public Sub PredefinedPreprocessorSymbols_Win8() Dim options = VisualBasicParseOptions.Default Dim symbols = AddPredefinedPreprocessorSymbols(OutputKind.WindowsRuntimeApplication) AssertEx.SetEqual({New KeyValuePair(Of String, Object)("VBC_VER", PredefinedPreprocessorSymbols.CurrentVersionNumber), New KeyValuePair(Of String, Object)("TARGET", "appcontainerexe")}, symbols.AsEnumerable) symbols = AddPredefinedPreprocessorSymbols(OutputKind.WindowsRuntimeMetadata) AssertEx.SetEqual({New KeyValuePair(Of String, Object)("VBC_VER", PredefinedPreprocessorSymbols.CurrentVersionNumber), New KeyValuePair(Of String, Object)("TARGET", "winmdobj")}, symbols.AsEnumerable) End Sub Public Sub ParseOptionsPass() ParseAndVerify() ParseAndVerify() ParseAndVerify() ParseAndVerify() End Sub Public Sub BC30208ERR_ExpectedOptionCompare() ParseAndVerify(, ) End Sub Public Sub BC30979ERR_InvalidOptionInfer() ParseAndVerify(, ) End Sub Public Sub BC31141ERR_InvalidOptionStrictCustom() ParseAndVerify(, ) End Sub Public Sub BC30620ERR_InvalidOptionStrict_FollowedByAssemblyAttribute() ParseAndVerify( ]]>, ) End Sub Public Sub BC30627ERR_OptionStmtWrongOrder() ParseAndVerify(, ) End Sub Public Sub BC30206ERR_ExpectedForOptionStmt_NullReferenceException() ParseAndVerify(, ) ParseAndVerify(, ) End Sub Public Sub BC30205ERR_ExpectedEOS_ParseOption_ExtraSyntaxAtEOL() ParseAndVerify(, ) End Sub ''' ''' If this test fails, please update the ''' And methods to ''' make sure they are doing the right thing with your New field And then update the baseline ''' here. ''' Public Sub TestFieldsForEqualsAndGetHashCode() ReflectionAssert.AssertPublicAndInternalFieldsAndProperties( (GetType(VisualBasicParseOptions)), "Features", "Language", "LanguageVersion", "PreprocessorSymbolNames", "PreprocessorSymbols", "SpecifiedLanguageVersion") End Sub Public Sub SpecifiedKindIsMappedCorrectly() Dim options = New VisualBasicParseOptions(kind:=SourceCodeKind.Regular) Assert.Equal(SourceCodeKind.Regular, options.Kind) Assert.Equal(SourceCodeKind.Regular, options.SpecifiedKind) options.Errors.Verify() options = New VisualBasicParseOptions(kind:=SourceCodeKind.Script) Assert.Equal(SourceCodeKind.Script, options.Kind) Assert.Equal(SourceCodeKind.Script, options.SpecifiedKind) options.Errors.Verify() #Disable Warning BC40000 ' SourceCodeKind.Interactive is obsolete options = New VisualBasicParseOptions(kind:=SourceCodeKind.Interactive) Assert.Equal(SourceCodeKind.Script, options.Kind) Assert.Equal(SourceCodeKind.Interactive, options.SpecifiedKind) #Enable Warning BC40000 ' SourceCodeKind.Interactive is obsolete options.Errors.Verify(Diagnostic(ERRID.WRN_BadSourceCodeKind).WithArguments("Interactive").WithLocation(1, 1)) End Sub Public Sub TwoOptionsWithDifferentSpecifiedKindShouldNotHaveTheSameHashCodes() Dim options1 = New VisualBasicParseOptions(kind:=SourceCodeKind.Script) Dim options2 = New VisualBasicParseOptions(kind:=SourceCodeKind.Script) Assert.Equal(options1.GetHashCode(), options2.GetHashCode()) ' They both map internally to SourceCodeKind.Script #Disable Warning BC40000 ' SourceCodeKind.Interactive is obsolete options1 = New VisualBasicParseOptions(kind:=SourceCodeKind.Script) options2 = New VisualBasicParseOptions(kind:=SourceCodeKind.Interactive) #Enable Warning BC40000 ' SourceCodeKind.Interactive Is obsolete Assert.NotEqual(options1.GetHashCode(), options2.GetHashCode()) End Sub Public Sub TwoOptionsWithDifferentSpecifiedKindShouldNotBeEqual() Dim options1 = New VisualBasicParseOptions(kind:=SourceCodeKind.Script) Dim options2 = New VisualBasicParseOptions(kind:=SourceCodeKind.Script) Assert.True(options1.Equals(options2)) ' They both map internally to SourceCodeKind.Script #Disable Warning BC40000 ' SourceCodeKind.Interactive is obsolete options1 = New VisualBasicParseOptions(kind:=SourceCodeKind.Script) options2 = New VisualBasicParseOptions(kind:=SourceCodeKind.Interactive) #Enable Warning BC40000 ' SourceCodeKind.Interactive Is obsolete Assert.False(options1.Equals(options2)) End Sub Public Sub BadSourceCodeKindShouldProduceDiagnostics() #Disable Warning BC40000 ' Type Or member Is obsolete Dim options = New VisualBasicParseOptions(kind:=SourceCodeKind.Interactive) #Enable Warning BC40000 ' Type Or member Is obsolete options.Errors.Verify(Diagnostic(ERRID.WRN_BadSourceCodeKind).WithArguments("Interactive").WithLocation(1, 1)) End Sub Public Sub BadDocumentationModeShouldProduceDiagnostics() Dim options = New VisualBasicParseOptions(documentationMode:=DirectCast(CType(100, Byte), DocumentationMode)) options.Errors.Verify(Diagnostic(ERRID.ERR_BadDocumentationMode).WithArguments("100").WithLocation(1, 1)) End Sub Public Sub BadLanguageVersionShouldProduceDiagnostics() Dim options = New VisualBasicParseOptions(languageVersion:=DirectCast(10000, LanguageVersion)) options.Errors.Verify(Diagnostic(ERRID.ERR_BadLanguageVersion).WithArguments("10000").WithLocation(1, 1)) End Sub Public Sub BadPreProcessorSymbolsShouldProduceDiagnostics() Dim symbols = New Dictionary(Of String, Object) symbols.Add("test", Nothing) symbols.Add("1", Nothing) Dim options = New VisualBasicParseOptions(preprocessorSymbols:=symbols) options.Errors.Verify(Diagnostic(ERRID.ERR_ProjectCCError1).WithArguments("Identifier expected.", "1").WithLocation(1, 1)) End Sub Public Sub BadSourceCodeKindShouldProduceDiagnostics_WithDimiation() #Disable Warning BC40000 ' Type Or member Is obsolete Dim options = New VisualBasicParseOptions().WithKind(SourceCodeKind.Interactive) #Enable Warning BC40000 ' Type Or member Is obsolete options.Errors.Verify(Diagnostic(ERRID.WRN_BadSourceCodeKind).WithArguments("Interactive").WithLocation(1, 1)) End Sub Public Sub BadDocumentationModeShouldProduceDiagnostics_WithDimiation() Dim options = New VisualBasicParseOptions().WithDocumentationMode(DirectCast(CType(100, Byte), DocumentationMode)) options.Errors.Verify(Diagnostic(ERRID.ERR_BadDocumentationMode).WithArguments("100").WithLocation(1, 1)) End Sub Public Sub BadLanguageVersionShouldProduceDiagnostics_WithDimiation() Dim options = New VisualBasicParseOptions().WithLanguageVersion(DirectCast(10000, LanguageVersion)) options.Errors.Verify(Diagnostic(ERRID.ERR_BadLanguageVersion).WithArguments("10000").WithLocation(1, 1)) End Sub Public Sub BadPreProcessorSymbolsShouldProduceDiagnostics_EmptyString() Dim symbols = New Dictionary(Of String, Object) symbols.Add("", Nothing) Dim options = New VisualBasicParseOptions().WithPreprocessorSymbols(symbols) options.Errors.Verify(Diagnostic(ERRID.ERR_ProjectCCError1).WithArguments("Identifier expected.", "").WithLocation(1, 1)) End Sub Public Sub BadPreProcessorSymbolsShouldProduceDiagnostics_WhiteSpacetring() Dim symbols = New Dictionary(Of String, Object) symbols.Add(" ", Nothing) Dim options = New VisualBasicParseOptions().WithPreprocessorSymbols(symbols) options.Errors.Verify(Diagnostic(ERRID.ERR_ProjectCCError1).WithArguments("Identifier expected.", " ").WithLocation(1, 1)) End Sub Public Sub BadPreProcessorSymbolsShouldProduceDiagnostics_SymbolWithDots() Dim symbols = New Dictionary(Of String, Object) symbols.Add("Good", Nothing) symbols.Add("Bad.Symbol", Nothing) Dim options = New VisualBasicParseOptions().WithPreprocessorSymbols(symbols) options.Errors.Verify(Diagnostic(ERRID.ERR_ProjectCCError1).WithArguments("Identifier expected.", "Bad.Symbol").WithLocation(1, 1)) End Sub Public Sub BadPreProcessorSymbolsShouldProduceDiagnostics_SymbolWithSlashes() Dim symbols = New Dictionary(Of String, Object) symbols.Add("Good", Nothing) symbols.Add("Bad\\Symbol", Nothing) Dim options = New VisualBasicParseOptions().WithPreprocessorSymbols(symbols) options.Errors.Verify(Diagnostic(ERRID.ERR_ProjectCCError1).WithArguments("Identifier expected.", "Bad\\Symbol").WithLocation(1, 1)) End Sub End Class