1
1
// Copyright (c) .NET Foundation. All rights reserved.
2
2
// Licensed under the MIT License. See License.txt in the project root for license information.
3
3
4
+ using System ;
4
5
using System . Linq ;
5
6
using Microsoft . Azure . WebJobs . Script . Description ;
6
7
using Microsoft . CodeAnalysis ;
@@ -14,7 +15,7 @@ public class CSharpFunctionSignatureTests
14
15
[ Fact ]
15
16
public void Matches_IsTrue_WhenParametersAreEqual ( )
16
17
{
17
- var function1 = @"using System;
18
+ var function1 = @"using System;
18
19
public static void Run(string id, out string output)
19
20
{
20
21
output = string.Empty;
@@ -27,22 +28,33 @@ public static void Run(string id, out string output)
27
28
output = result;
28
29
}" ;
29
30
30
- var tree1 = CSharpSyntaxTree . ParseText ( function1 , CSharpParseOptions . Default . WithKind ( SourceCodeKind . Script ) ) ;
31
- var tree2 = CSharpSyntaxTree . ParseText ( function2 , CSharpParseOptions . Default . WithKind ( SourceCodeKind . Script ) ) ;
31
+ Tuple < FunctionSignature , FunctionSignature > signatures = GetFunctionSignatures ( function1 , function2 ) ;
32
32
33
- var references = new MetadataReference [ ] { MetadataReference . CreateFromFile ( typeof ( string ) . Assembly . Location ) } ;
33
+ Assert . True ( signatures . Item1 . Equals ( signatures . Item2 ) ) ;
34
+ Assert . Equal ( signatures . Item1 . GetHashCode ( ) , signatures . Item2 . GetHashCode ( ) ) ;
35
+ }
34
36
35
- var compilation1 = new Script . Description . CSharpCompilation ( CodeAnalysis . CSharp . CSharpCompilation . Create ( "test1" , references : references )
36
- . AddSyntaxTrees ( tree1 ) ) ;
37
+ [ Fact ]
38
+ public void Matches_IsFalse_WhenReturnTypesAreDifferent ( )
39
+ {
40
+ var function1 = @"using System;
41
+ public static string Run(string id, out string output)
42
+ {
43
+ output = string.Empty;
44
+ return string.Empty;
45
+ }" ;
37
46
38
- var compilation2 = new Script . Description . CSharpCompilation ( CodeAnalysis . CSharp . CSharpCompilation . Create ( "test2" , references : references )
39
- . AddSyntaxTrees ( tree2 ) ) ;
47
+ var function2 = @"using System;
48
+ public static void Run(string id, out string output)
49
+ {
50
+ string result = string.Empty;
51
+ output = result;
52
+ }" ;
40
53
41
- var signature1 = compilation1 . GetEntryPointSignature ( new FunctionEntryPointResolver ( ) ) ;
42
- var signature2 = compilation2 . GetEntryPointSignature ( new FunctionEntryPointResolver ( ) ) ;
54
+ Tuple < FunctionSignature , FunctionSignature > signatures = GetFunctionSignatures ( function1 , function2 ) ;
43
55
44
- Assert . True ( signature1 . Equals ( signature2 ) ) ;
45
- Assert . Equal ( signature1 . GetHashCode ( ) , signature2 . GetHashCode ( ) ) ;
56
+ Assert . False ( signatures . Item1 . Equals ( signatures . Item2 ) ) ;
57
+ Assert . NotEqual ( signatures . Item1 . GetHashCode ( ) , signatures . Item2 . GetHashCode ( ) ) ;
46
58
}
47
59
48
60
[ Fact ]
@@ -61,6 +73,14 @@ public static void Run(string id, out string output)
61
73
output = result;
62
74
}" ;
63
75
76
+ Tuple < FunctionSignature , FunctionSignature > signatures = GetFunctionSignatures ( function1 , function2 ) ;
77
+
78
+ Assert . False ( signatures . Item1 . Equals ( signatures . Item2 ) ) ;
79
+ Assert . NotEqual ( signatures . Item1 . GetHashCode ( ) , signatures . Item2 . GetHashCode ( ) ) ;
80
+ }
81
+
82
+ private Tuple < FunctionSignature , FunctionSignature > GetFunctionSignatures ( string function1 , string function2 )
83
+ {
64
84
var tree1 = CSharpSyntaxTree . ParseText ( function1 , CSharpParseOptions . Default . WithKind ( SourceCodeKind . Script ) ) ;
65
85
var tree2 = CSharpSyntaxTree . ParseText ( function2 , CSharpParseOptions . Default . WithKind ( SourceCodeKind . Script ) ) ;
66
86
@@ -75,8 +95,7 @@ public static void Run(string id, out string output)
75
95
var signature1 = compilation1 . GetEntryPointSignature ( new FunctionEntryPointResolver ( ) ) ;
76
96
var signature2 = compilation2 . GetEntryPointSignature ( new FunctionEntryPointResolver ( ) ) ;
77
97
78
- Assert . False ( signature1 . Equals ( signature2 ) ) ;
79
- Assert . NotEqual ( signature1 . GetHashCode ( ) , signature2 . GetHashCode ( ) ) ;
98
+ return Tuple . Create ( signature1 , signature2 ) ;
80
99
}
81
100
82
101
[ Fact ]
@@ -97,22 +116,10 @@ public static void Run( System.String id ,
97
116
output = result;
98
117
}" ;
99
118
100
- var tree1 = CSharpSyntaxTree . ParseText ( function1 , CSharpParseOptions . Default . WithKind ( SourceCodeKind . Script ) ) ;
101
- var tree2 = CSharpSyntaxTree . ParseText ( function2 , CSharpParseOptions . Default . WithKind ( SourceCodeKind . Script ) ) ;
102
-
103
- var references = new MetadataReference [ ] { MetadataReference . CreateFromFile ( typeof ( string ) . Assembly . Location ) } ;
104
-
105
- var compilation1 = new Script . Description . CSharpCompilation ( CodeAnalysis . CSharp . CSharpCompilation . Create ( "test1" , references : references )
106
- . AddSyntaxTrees ( tree1 ) ) ;
107
-
108
- var compilation2 = new Script . Description . CSharpCompilation ( CodeAnalysis . CSharp . CSharpCompilation . Create ( "test2" , references : references )
109
- . AddSyntaxTrees ( tree2 ) ) ;
119
+ Tuple < FunctionSignature , FunctionSignature > signatures = GetFunctionSignatures ( function1 , function2 ) ;
110
120
111
- var signature1 = compilation1 . GetEntryPointSignature ( new FunctionEntryPointResolver ( ) ) ;
112
- var signature2 = compilation2 . GetEntryPointSignature ( new FunctionEntryPointResolver ( ) ) ;
113
-
114
- Assert . True ( signature1 . Equals ( signature2 ) ) ;
115
- Assert . Equal ( signature1 . GetHashCode ( ) , signature2 . GetHashCode ( ) ) ;
121
+ Assert . True ( signatures . Item1 . Equals ( signatures . Item2 ) ) ;
122
+ Assert . Equal ( signatures . Item1 . GetHashCode ( ) , signatures . Item2 . GetHashCode ( ) ) ;
116
123
}
117
124
118
125
[ Fact ]
@@ -148,6 +155,54 @@ public static void Run(string id, ICollection<Test> test1)
148
155
149
156
}
150
157
158
+ public class Test
159
+ {
160
+ public string Id { get; set; }
161
+ }" ;
162
+
163
+ var tree = CSharpSyntaxTree . ParseText ( function1 , CSharpParseOptions . Default . WithKind ( SourceCodeKind . Script ) ) ;
164
+ var references = new MetadataReference [ ] { MetadataReference . CreateFromFile ( typeof ( string ) . Assembly . Location ) } ;
165
+ var compilation = CodeAnalysis . CSharp . CSharpCompilation . Create ( "test1" , references : references ) . AddSyntaxTrees ( tree ) ;
166
+
167
+ var signature1 = new Script . Description . CSharpCompilation ( compilation ) . GetEntryPointSignature ( new FunctionEntryPointResolver ( ) ) ;
168
+
169
+ Assert . True ( signature1 . HasLocalTypeReference ) ;
170
+ }
171
+
172
+ [ Fact ]
173
+ public void Matches_IsTrue_WhenUsingLocalTypesInReturn ( )
174
+ {
175
+ var function1 = @"using System;
176
+ using System.Collections.Generic;
177
+ public static Test Run(string id)
178
+ {
179
+ return null;
180
+ }
181
+
182
+ public class Test
183
+ {
184
+ public string Id { get; set; }
185
+ }" ;
186
+
187
+ var tree = CSharpSyntaxTree . ParseText ( function1 , CSharpParseOptions . Default . WithKind ( SourceCodeKind . Script ) ) ;
188
+ var references = new MetadataReference [ ] { MetadataReference . CreateFromFile ( typeof ( string ) . Assembly . Location ) } ;
189
+ var compilation = CodeAnalysis . CSharp . CSharpCompilation . Create ( "test1" , references : references ) . AddSyntaxTrees ( tree ) ;
190
+
191
+ var signature1 = new Script . Description . CSharpCompilation ( compilation ) . GetEntryPointSignature ( new FunctionEntryPointResolver ( ) ) ;
192
+
193
+ Assert . True ( signature1 . HasLocalTypeReference ) ;
194
+ }
195
+
196
+ [ Fact ]
197
+ public void Matches_IsTrue_WhenUsingLocalTypesInGenericReturn ( )
198
+ {
199
+ var function1 = @"using System;
200
+ using System.Collections.Generic;
201
+ public static List<Test> Run(string id)
202
+ {
203
+ return null;
204
+ }
205
+
151
206
public class Test
152
207
{
153
208
public string Id { get; set; }
0 commit comments