File size: 5,652 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | # Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# There is an automatic 'using namespace system' which is
# tested by this test, so don't uncomment the following:
#using namespace System
using namespace System.Threading
using namespace System.Timers
using namespace System.Diagnostics
# Test parsing more than one using statement on one line
using namespace System.Diagnostics; using namespace System.Runtime.CompilerServices
using namespace System.Collections.Generic
# Flags is System.FlagsAttribute
# This tests our implicit 'using namespace System'
# despite having other explicit using namespace statements.
[Flags()]
enum E1
{
E1 = 0x01
E2 = 0x02
E4 = 0x04
}
# Test attributes that won't be found w/o using, but w/o implicit Attribute suffix
[CompilerGenerated()]
class C1
{
[Thread][CompilerGenerated()]$Thread
[Int32][CompilerGenerated()]$Int
#[ElapsedEventHandler][CompilerGenerated()]$EventHandler
}
# Test attributes that won't be found w/o using, but w/ implicit Attribute suffix
[CompilerGeneratedAttribute()]
class C2
{
[Thread][CompilerGeneratedAttribute()]$Thread
[Int32][CompilerGeneratedAttribute()]$Int
#[ElapsedEventHandler][CompilerGeneratedAttribute()]$EventHandler
}
Describe "Using Namespace" -Tags "CI" {
It "Type literals w/ using namespace" {
[Thread].FullName | Should -Be System.Threading.Thread
[Int32].FullName | Should -Be System.Int32
#[ElapsedEventHandler].FullName | Should -Be System.Timers.ElapsedEventHandler
[C1].GetProperty("Thread").PropertyType.FullName | Should -Be System.Threading.Thread
[C1].GetProperty("Int").PropertyType.FullName | Should -Be System.Int32
# [C1].GetProperty("EventHandler").PropertyType.FullName | Should -Be System.Timers.ElapsedEventHandler
}
It "Covert string to Type w/ using namespace" {
("Thread" -as [Type]).FullName | Should -Be System.Threading.Thread
("Int32" -as [Type]).FullName | Should -Be System.Int32
# ("ElapsedEventHandler" -as [Type]).FullName | Should -Be System.Timers.ElapsedEventHandler
New-Object Int32 | Should -Be 0
New-Object CompilerGeneratedAttribute | Should -Be System.Runtime.CompilerServices.CompilerGeneratedAttribute
}
It "Attributes w/ using namespace" -Pending {
function foo
{
[DebuggerStepThrough()]
param(
[CompilerGeneratedAttribute()]
$a,
[CompilerGenerated()]
$b
)
"OK"
}
foo | Should -Be OK
$cmdInfo = Get-Commmand foo
$cmdInfo.ScriptBlock.Attributes[0] | Should -Be System.Diagnostics.DebuggerStepThroughAttribute
$cmdInfo.Parameters['a'].Attributes[1] | Should -Be System.Runtime.CompilerServices.CompilerGeneratedAttribute
$cmdInfo.Parameters['b'].Attributes[1] | Should -Be System.Runtime.CompilerServices.CompilerGeneratedAttribute
[C1].GetProperty("Thread").GetCustomAttributesData()[0].AttributeType.FullName | Should -Be System.Runtime.CompilerServices.CompilerGeneratedAttribute
[C1].GetProperty("Int").GetCustomAttributesData()[0].AttributeType.FullName | Should -Be System.Runtime.CompilerServices.CompilerGeneratedAttribute
# [C1].GetProperty("EventHandler").GetCustomAttributesData()[0].AttributeType.FullName | Should -Be System.Runtime.CompilerServices.CompilerGeneratedAttribute
[C2].GetProperty("Thread").GetCustomAttributesData()[0].AttributeType.FullName | Should -Be System.Runtime.CompilerServices.CompilerGeneratedAttribute
[C2].GetProperty("Int").GetCustomAttributesData()[0].AttributeType.FullName | Should -Be System.Runtime.CompilerServices.CompilerGeneratedAttribute
# [C2].GetProperty("EventHandler").GetCustomAttributesData()[0].AttributeType.FullName | Should -Be System.Runtime.CompilerServices.CompilerGeneratedAttribute
[C1].GetCustomAttributesData()[0].AttributeType.FullName | Should -Be System.Runtime.CompilerServices.CompilerGeneratedAttribute
[C2].GetCustomAttributesData()[0].AttributeType.FullName | Should -Be System.Runtime.CompilerServices.CompilerGeneratedAttribute
[E1].GetCustomAttributesData()[0].AttributeType.FullName | Should -Be System.FlagsAttribute
}
It "Ambiguous type reference" {
{ [ThreadState] } | Should -Throw -ErrorId AmbiguousTypeReference
}
It "Parameters" {
function foo([Thread]$t = $null) { 42 }
foo | Should -Be 42
$mod = New-Module -Name UsingNamespaceModule -ScriptBlock {
function Set-Thread([Thread]$t = $null)
{
44
}
}
Import-Module $mod
Set-Thread | Should -Be 44
Remove-Module $mod
}
It "Generics" {
function foo([List[string]]$l)
{
$l | Should -Be "a string"
}
$l = [List[string]]::new()
$l.Add("a string")
foo $l
}
ShouldBeParseError "1; using namespace System" UsingMustBeAtStartOfScript 3
ShouldBeParseError "using namespace Foo = System" UsingStatementNotSupported 0
ShouldBeParseError "using namespace ''" InvalidNamespaceValue 16
ShouldBeParseError "using namespace [System]" InvalidNamespaceValue 16
ShouldBeParseError "using namespace ',System'" InvalidNamespaceValue 16
ShouldBeParseError "using namespace ']System'" InvalidNamespaceValue 16
# TODO: add diagnostic (low pri)
# ShouldBeParseError "using namespace System; using namespace System" UsingNamespaceAlreadySpecified 24
}
|