File size: 3,531 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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe 'Test for conversion b/w script block and delegate' -Tags "CI" {
    BeforeAll {
        function lineno
        {
            $MyInvocation.ScriptLineNumber
        }

        function Generate-ArgumentTypesAndParameters
        {
            param([int] $num, [ref] $parameters, [ref]$argumentTypes, [bool]$hasReturn = $false)

            For($j = 1; $j -le $num; $j++)
            {
                $ran = Get-Random -Minimum 1 -Maximum 3
                $type = [Int32]
                switch ($ran)
                {
                    1 { $type = [Int32]; break}
                    2 { $type = [string]; break}
                    3 { $type = [Hashtable];  break}
                }

                $argumentTypes.Value += $type;

                if((-not $hasReturn) -or ($j -lt $num ) )  {
                    $parameters.Value  += Get-Value($type)
                }
            }
        }

        function Get-Value
        {
            param([type] $t)

            if ($t -eq [Int32]) { 100 }
            elseif($t -eq [string]) { 'abc' }
            elseif ($t -eq [Hashtable])  {@{a = 'foo'} }
        }
    }

    #0 arg, no return
    It 'System.Action' {
        ([System.Action]{ $script:gl=lineno; $args.Length | Should -Be 0 }).Invoke()
        ($gl + 1) | Should -BeExactly (lineno)
    }
    # multiple args, no return
    For($i = 1; $i -le 8; $i++)
    {
        $str = 'System.Action`{0}' -f $i
        Context $str {
            $gt = [object].Assembly.GetType($str)

            $parameters = @()
            $argumentTypes=@()
            Generate-ArgumentTypesAndParameters $i ([ref] $parameters) ([ref] $argumentTypes)

            $ct = $gt.MakeGenericType($argumentTypes)

            $func = { $script:gl=lineno; $args.Length | Should -BeExactly $i } -as $ct
            $func.DynamicInvoke($parameters)
            It '$gl + 2' { ($gl + 2) | Should -BeExactly (lineno) }
        }
    }

    #0 arg with return value
    It 'System.Func[Int32]' {
        ([System.Func[Int32]]{ $script:gl=lineno; $args.Length }).Invoke() | Should -Be 0
        ($gl + 1) | Should -BeExactly (lineno)
    }

    It 'System.Func[string]' {
        ([System.Func[string]]{ $script:gl=lineno; 'hello' }).Invoke() | Should -BeExactly 'hello'
        ($gl + 1) | Should -BeExactly (lineno)
    }

    It 'System.Func[hashtable]' {
        (([System.Func[hashtable]]{ $script:gl=lineno; @{a = 'foo' }}).Invoke()).a | Should -BeExactly 'foo'
        ($gl + 1) | Should -BeExactly (lineno)
    }

    #multiple args, differnt return type
    For($i = 2; $i -le 9; $i++)
    {
        $str = 'System.Func`{0}' -f $i
        Context $str {
            $gt = [object].Assembly.GetType($str)

            $parameters = @()
            $argumentTypes=@()
            Generate-ArgumentTypesAndParameters $i ([ref] $parameters) ([ref] $argumentTypes) $true
            $v= Get-Value($argumentTypes[$i-1])
            $ct = $gt.MakeGenericType($argumentTypes)
            $func = { $script:gl=lineno; $null = ($args.Length | Should -BeExactly ($i-1)); $v } -as $ct
            $t = $func.DynamicInvoke($parameters)
            It '$gl + 2' { ($gl + 2) | Should -BeExactly (lineno) }
            if ($argumentTypes[$i-1] -eq [Hashtable] )
            {
                It '$t.a' { $t.a | Should -BeExactly $v.a }
            }
            else
            {
                It '$t' { $t | Should -BeExactly $v }
            }
        }
    }
}