File size: 6,301 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
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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

Describe ".NET Method Binding Tests" -tags CI {
    BeforeAll {
        Add-Type -TypeDefinition @'
using System;
using System.Runtime.InteropServices;

namespace CLRBindingTests;

public class TestClass
{
    public int Prop { get; }

    public TestClass(int value = 1)
    {
        Prop = value;
    }

    public static string StaticWithDefaultExpected() => StaticWithDefault();
    public static string StaticWithDefault(string value = "foo") => value;

    public static string StaticWithOptionalAndValueExpected() => StaticWithOptionalAndValue();
    public static string StaticWithOptionalAndValue([Optional, DefaultParameterValue("bar")] string value) => value;

    public static string StaticWithOptionalExpected() => StaticWithOptional();
    public static string StaticWithOptional([Optional] string value) => value;

    public static int PrimitiveTypeWithInDefault(in int value = default) => value;

    public static Guid ValueTypeWithInDefault(in Guid value = default) => value;

    public static string RefTypeWithInDefault(in string value = default) => value;

    public object InstanceWithDefaultExpected() => InstanceWithDefault();
    public object InstanceWithDefault(object value = null) => value;

    public object InstanceWithOptionalAndValueExpected() => InstanceWithOptionalAndValue();
    public object InstanceWithOptionalAndValue([Optional, DefaultParameterValue("foo")] object value) => value;

    public object InstanceWithOptionalExpected() => InstanceWithOptional();
    public object InstanceWithOptional([Optional] object value) => value;

    public string MultipleArgsWithDefaultExpected(string prefix) => MultipleArgsWithDefault(prefix);
    public string MultipleArgsWithDefault(string prefix, string extra = "abc") => $"{prefix}{extra}";

    public string MultipleArgsWithOptionalAndValueExpected(string prefix) => MultipleArgsWithOptionalAndValue(prefix);
    public string MultipleArgsWithOptionalAndValue(string prefix, [Optional, DefaultParameterValue("def")] string extra) => $"{prefix}{extra}";

    public string MultipleArgsWithOptionalExpected(string prefix) => MultipleArgsWithOptional(prefix);
    public string MultipleArgsWithOptional(string prefix, [Optional] string extra) => $"{prefix}{extra}";
}

public class TestClassCstorWithOptionalAndValue
{
    public int Prop { get; }

    public TestClassCstorWithOptionalAndValue([Optional, DefaultParameterValue(2)] int value)
    {
        Prop = value;
    }
}

public class TestClassCstorWithOptional
{
    public int Prop { get; }

    public TestClassCstorWithOptional([Optional] int value)
    {
        Prop = value;
    }
}
'@
    }

    It "Binds to constructor with default argument" {
        $c = [CLRBindingTests.TestClass]::new()
        $c.Prop | Should -Be 1
    }

    It "Binds to constructor with Optional with DefaultValue argument" {
        $c = [CLRBindingTests.TestClassCstorWithOptionalAndValue]::new()
        $c.Prop | Should -Be 2
    }

    It "Binds to constructor with Optional argument" {
        $c = [CLRBindingTests.TestClassCstorWithOptional]::new()
        $c.Prop | Should -Be 0
    }

    It "Binds to static method with default argument" {
        $expected = [CLRBindingTests.TestClass]::StaticWithDefaultExpected()
        $actual = [CLRBindingTests.TestClass]::StaticWithDefault()
        $actual | Should -Be $expected
    }

    It "Binds to static method with Optional with DefaultValue argument" {
        $expected = [CLRBindingTests.TestClass]::StaticWithOptionalAndValueExpected()
        $actual = [CLRBindingTests.TestClass]::StaticWithOptionalAndValue()
        $actual | Should -Be $expected
    }

    It "Binds to static method with Optional argument" {
        $expected = [CLRBindingTests.TestClass]::StaticWithOptionalExpected()
        $actual = [CLRBindingTests.TestClass]::StaticWithOptional()
        $actual | Should -Be $expected
    }

    It "Binds to static method with primitive type with in modifier and default argument" {
        $actual = [CLRBindingTests.TestClass]::PrimitiveTypeWithInDefault()
        $actual | Should -Be 0
    }

    It "Binds to static method with value type with in modifier and default argument" {
        $actual = [CLRBindingTests.TestClass]::ValueTypeWithInDefault()
        $actual | Should -Be ([Guid]::Empty)
    }

    It "Binds to static method with ref type with in modifier and default argument" {
        $actual = [CLRBindingTests.TestClass]::RefTypeWithInDefault()
        $null -eq $actual | Should -BeTrue
    }

    It "Binds to instance method with default argument" {
        $c = [CLRBindingTests.TestClass]::new()

        $expected = $c.InstanceWithDefaultExpected()
        $actual = $c.InstanceWithDefault()
        $actual | Should -Be $expected
    }

    It "Binds to instance method with Optional with DefaultValue argument" {
        $c = [CLRBindingTests.TestClass]::new()

        $expected = $c.InstanceWithOptionalAndValueExpected()
        $actual = $c.InstanceWithOptionalAndValue()
        $actual | Should -Be $expected
    }

    It "Binds to instance method with Optional argument" {
        $c = [CLRBindingTests.TestClass]::new()

        $expected = $c.InstanceWithOptionalExpected()
        $actual = $c.InstanceWithOptional()
        $actual | Should -Be $expected
    }

    It "Binds to instance method with normal arg and default argument" {
        $c = [CLRBindingTests.TestClass]::new()

        $expected = $c.MultipleArgsWithDefaultExpected("prefix")
        $actual = $c.MultipleArgsWithDefault("prefix")
        $actual | Should -Be $expected
    }

    It "Binds to instance method with Optional with normal arg and DefaultValue argument" {
        $c = [CLRBindingTests.TestClass]::new()

        $expected = $c.MultipleArgsWithOptionalAndValueExpected("prefix")
        $actual = $c.MultipleArgsWithOptionalAndValue("prefix")
        $actual | Should -Be $expected
    }

    It "Binds to instance method with normal arg and Optional argument" {
        $c = [CLRBindingTests.TestClass]::new()

        $expected = $c.MultipleArgsWithOptionalExpected("prefix")
        $actual = $c.MultipleArgsWithOptional("prefix")
        $actual | Should -Be $expected
    }
}