VisualBasicParseOptionsTests.vb 12.1 KB
Newer Older
1
' Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.
P
Pilchie 已提交
2 3

Imports System.Collections.Immutable
4
Imports System.Linq
P
Pilchie 已提交
5 6
Imports Roslyn.Test.Utilities

7 8 9
Public Class VisualBasicParseOptionsTests
    Inherits BasicTestBase

A
angocke 已提交
10 11
    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
P
Pilchie 已提交
12 13 14 15 16 17 18 19 20 21 22 23
        Dim newOpt1 = factory(oldOpt1, validValue)
        Dim newOpt2 = factory(newOpt1, validValue)
        Assert.Equal(validValue, getter(newOpt1))
        Assert.Same(newOpt2, newOpt1)
    End Sub

    <Fact>
    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)

A
angocke 已提交
24 25
        Assert.Throws(Of ArgumentOutOfRangeException)(Function() VisualBasicParseOptions.Default.WithKind(DirectCast(Integer.MaxValue, SourceCodeKind)))
        Assert.Throws(Of ArgumentOutOfRangeException)(Function() VisualBasicParseOptions.Default.WithLanguageVersion(DirectCast(1000, LanguageVersion)))
26 27
    End Sub

28 29 30 31 32 33 34 35 36 37
    <Fact>
    Public Sub WithLatestLanguageVersion()
        Dim oldOpt1 = VisualBasicParseOptions.Default
        Dim newOpt1 = oldOpt1.WithLanguageVersion(LanguageVersion.Latest)
        Dim newOpt2 = newOpt1.WithLanguageVersion(LanguageVersion.Latest)
        Assert.Equal(LanguageVersion.Latest.MapLatestToVersion, oldOpt1.LanguageVersion)
        Assert.Equal(LanguageVersion.Latest.MapLatestToVersion, newOpt1.LanguageVersion)
        Assert.Equal(LanguageVersion.Latest.MapLatestToVersion, newOpt2.LanguageVersion)
    End Sub

38 39 40 41 42 43 44 45
    <Fact>
    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)

A
angocke 已提交
46 47 48
        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)
49 50 51 52 53

        Dim syms2 = {New KeyValuePair(Of String, Object)("A", 1),
                     New KeyValuePair(Of String, Object)("B", New List(Of String)()),
                     New KeyValuePair(Of String, Object)("C", 3)}

A
angocke 已提交
54 55
        Assert.Throws(Of ArgumentException)(Function() New VisualBasicParseOptions(preprocessorSymbols:=syms2))
        Assert.Throws(Of ArgumentException)(Function() VisualBasicParseOptions.Default.WithPreprocessorSymbols(syms2))
P
Pilchie 已提交
56 57 58 59
    End Sub

    <Fact>
    Public Sub ConstructorValidation()
A
angocke 已提交
60 61
        Assert.Throws(Of ArgumentOutOfRangeException)(Function() New VisualBasicParseOptions(kind:=DirectCast(Int32.MaxValue, SourceCodeKind)))
        Assert.Throws(Of ArgumentOutOfRangeException)(Function() New VisualBasicParseOptions(languageVersion:=DirectCast(1000, LanguageVersion)))
P
Pilchie 已提交
62 63
    End Sub

J
Jared Parsons 已提交
64
    <Fact, WorkItem(546206, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/546206")>
P
Pilchie 已提交
65 66 67 68
    Public Sub InvalidDefineSymbols()

        ' Command line: error BC31030: Project-level conditional compilation constant 'xxx' is not valid: Identifier expected

69
        Dim syms = ImmutableArray.Create(New KeyValuePair(Of String, Object)("", 1))
A
angocke 已提交
70
        Assert.Throws(Of ArgumentException)(Function() New VisualBasicParseOptions(preprocessorSymbols:=syms))
P
Pilchie 已提交
71

72
        syms = ImmutableArray.Create(New KeyValuePair(Of String, Object)(" ", 1))
A
angocke 已提交
73
        Assert.Throws(Of ArgumentException)(Function() New VisualBasicParseOptions(preprocessorSymbols:=syms))
P
Pilchie 已提交
74

75 76
        syms = ImmutableArray.Create(New KeyValuePair(Of String, Object)("Good", 1),
                                     New KeyValuePair(Of String, Object)(Nothing, 2))
A
angocke 已提交
77
        Assert.Throws(Of ArgumentException)(Function() New VisualBasicParseOptions(preprocessorSymbols:=syms))
P
Pilchie 已提交
78

79 80
        syms = ImmutableArray.Create(New KeyValuePair(Of String, Object)("Good", 1),
                                     New KeyValuePair(Of String, Object)("Bad.Symbol", 2))
A
angocke 已提交
81
        Assert.Throws(Of ArgumentException)(Function() New VisualBasicParseOptions(preprocessorSymbols:=syms))
P
Pilchie 已提交
82

83 84 85
        syms = ImmutableArray.Create(New KeyValuePair(Of String, Object)("123", 1),
                                     New KeyValuePair(Of String, Object)("Bad/Symbol", 2),
                                     New KeyValuePair(Of String, Object)("Good", 3))
A
angocke 已提交
86
        Assert.Throws(Of ArgumentException)(Function() New VisualBasicParseOptions(preprocessorSymbols:=syms))
P
Pilchie 已提交
87 88 89
    End Sub

    <Fact>
90
    Public Sub PredefinedPreprocessorSymbolsTests()
A
angocke 已提交
91
        Dim options = VisualBasicParseOptions.Default
P
Pilchie 已提交
92 93 94
        Dim empty = ImmutableArray.Create(Of KeyValuePair(Of String, Object))()

        Dim symbols = AddPredefinedPreprocessorSymbols(OutputKind.NetModule)
95
        AssertEx.SetEqual({New KeyValuePair(Of String, Object)("VBC_VER", PredefinedPreprocessorSymbols.CurrentVersionNumber), New KeyValuePair(Of String, Object)("TARGET", "module")}, symbols.AsEnumerable)
P
Pilchie 已提交
96 97 98

        ' if the symbols are already there, don't change their values
        symbols = AddPredefinedPreprocessorSymbols(OutputKind.DynamicallyLinkedLibrary, symbols)
99
        AssertEx.SetEqual({New KeyValuePair(Of String, Object)("VBC_VER", PredefinedPreprocessorSymbols.CurrentVersionNumber), New KeyValuePair(Of String, Object)("TARGET", "module")}, symbols.AsEnumerable)
P
Pilchie 已提交
100 101 102 103 104 105 106 107 108 109

        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)
110
        AssertEx.SetEqual({New KeyValuePair(Of String, Object)("VBC_VER", PredefinedPreprocessorSymbols.CurrentVersionNumber), New KeyValuePair(Of String, Object)("TARGET", "exe")}, symbols.AsEnumerable)
P
Pilchie 已提交
111 112

        symbols = AddPredefinedPreprocessorSymbols(OutputKind.WindowsApplication, empty)
113 114 115 116 117 118 119 120 121
        AssertEx.SetEqual({New KeyValuePair(Of String, Object)("VBC_VER", PredefinedPreprocessorSymbols.CurrentVersionNumber), New KeyValuePair(Of String, Object)("TARGET", "winexe")}, symbols.AsEnumerable)
    End Sub

    <Fact>
    Public Sub CurrentVersionNumber()
        Dim highest = System.Enum.
            GetValues(GetType(LanguageVersion)).
            Cast(Of LanguageVersion).
            Select(Function(x) CInt(x)).
122
            Where(Function(x) x <> LanguageVersion.Latest).
123 124 125
            Max()

        Assert.Equal(highest, CInt(PredefinedPreprocessorSymbols.CurrentVersionNumber))
P
Pilchie 已提交
126 127
    End Sub

128
    <Fact>
P
Pilchie 已提交
129
    Public Sub PredefinedPreprocessorSymbols_Win8()
A
angocke 已提交
130
        Dim options = VisualBasicParseOptions.Default
P
Pilchie 已提交
131

132 133
        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)
P
Pilchie 已提交
134

135 136
        symbols = AddPredefinedPreprocessorSymbols(OutputKind.WindowsRuntimeMetadata)
        AssertEx.SetEqual({New KeyValuePair(Of String, Object)("VBC_VER", PredefinedPreprocessorSymbols.CurrentVersionNumber), New KeyValuePair(Of String, Object)("TARGET", "winmdobj")}, symbols.AsEnumerable)
P
Pilchie 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    End Sub

    <Fact>
    Public Sub ParseOptionsPass()
        ParseAndVerify(<![CDATA[
                Option strict
                Option strict on
                Option strict off
            ]]>)

        ParseAndVerify(<![CDATA[
                        Option infer
                        Option infer on
                        option infer off
                    ]]>)

        ParseAndVerify(<![CDATA[
                option explicit
                Option explicit On
                Option explicit off
            ]]>)

        ParseAndVerify(<![CDATA[
                Option compare text
                Option compare binary
            ]]>)
    End Sub

    <Fact()>
    Public Sub BC30208ERR_ExpectedOptionCompare()

        ParseAndVerify(<![CDATA[
                Option text
            ]]>,
            <errors>
                <error id="30208"/>
            </errors>)
    End Sub

    <Fact()>
    Public Sub BC30979ERR_InvalidOptionInfer()
        ParseAndVerify(<![CDATA[
                Option infer xyz
            ]]>,
            <errors>
                <error id="30979"/>
            </errors>)
    End Sub

    <Fact()>
    Public Sub BC31141ERR_InvalidOptionStrictCustom()
        ParseAndVerify(<![CDATA[
                Option strict custom
            ]]>,
            <errors>
                <error id="31141"/>
            </errors>)
    End Sub

J
Jared Parsons 已提交
196
    <Fact, WorkItem(536060, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/536060")>
P
Pilchie 已提交
197 198 199 200 201 202 203 204 205 206
    Public Sub BC30620ERR_InvalidOptionStrict_FollowedByAssemblyAttribute()
        ParseAndVerify(<![CDATA[
            Option Strict False
            <Assembly: CLSCompliant(True)> 
        ]]>,
        <errors>
            <error id="30620"/>
        </errors>)
    End Sub

J
Jared Parsons 已提交
207
    <Fact, WorkItem(536067, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/536067")>
P
Pilchie 已提交
208 209 210 211 212 213 214 215 216 217
    Public Sub BC30627ERR_OptionStmtWrongOrder()
        ParseAndVerify(<![CDATA[
            Imports System
            Option Infer On
        ]]>,
        <errors>
            <error id="30627"/>
        </errors>)
    End Sub

J
Jared Parsons 已提交
218
    <Fact, WorkItem(536362, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/536362")>
P
Pilchie 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
    Public Sub BC30206ERR_ExpectedForOptionStmt_NullReferenceException()
        ParseAndVerify(<![CDATA[
            Option
        ]]>,
        <errors>
            <error id="30206"/>
        </errors>)

        ParseAndVerify(<![CDATA[
                Option on
            ]]>,
    <errors>
        <error id="30206"/>
    </errors>)
    End Sub

J
Jared Parsons 已提交
235
    <Fact, WorkItem(536432, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/536432")>
P
Pilchie 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
    Public Sub BC30205ERR_ExpectedEOS_ParseOption_ExtraSyntaxAtEOL()
        ParseAndVerify(<![CDATA[
            Option Infer On O
        ]]>,
        <errors>
            <error id="30205"/>
        </errors>)
    End Sub

    ''' <summary>
    ''' If this test fails, please update the <see cref="ParseOptions.GetHashCode" />
    ''' And <see cref="ParseOptions.Equals" /> methods to
    ''' make sure they are doing the right thing with your New field And then update the baseline
    ''' here.
    ''' </summary>
    <Fact>
    Public Sub TestFieldsForEqualsAndGetHashCode()
        ReflectionAssert.AssertPublicAndInternalFieldsAndProperties(
A
angocke 已提交
254
                (GetType(VisualBasicParseOptions)),
N
nmgafter 已提交
255
                "Features",
P
Pilchie 已提交
256 257 258 259 260
                "LanguageVersion",
                "PreprocessorSymbolNames",
                "PreprocessorSymbols")
    End Sub
End Class