11// Copyright (c) .NET Foundation. All rights reserved.
22// Licensed under the MIT License. See License.txt in the project root for license information.
33
4+ using System ;
45using System . Linq ;
56using Microsoft . Azure . WebJobs . Script . Description ;
67using Microsoft . CodeAnalysis ;
@@ -14,7 +15,7 @@ public class CSharpFunctionSignatureTests
1415 [ Fact ]
1516 public void Matches_IsTrue_WhenParametersAreEqual ( )
1617 {
17- var function1 = @"using System;
18+ var function1 = @"using System;
1819public static void Run(string id, out string output)
1920{
2021 output = string.Empty;
@@ -27,22 +28,33 @@ public static void Run(string id, out string output)
2728 output = result;
2829}" ;
2930
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 ) ;
3232
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+ }
3436
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+ }" ;
3746
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+ }" ;
4053
41- var signature1 = compilation1 . GetEntryPointSignature ( new FunctionEntryPointResolver ( ) ) ;
42- var signature2 = compilation2 . GetEntryPointSignature ( new FunctionEntryPointResolver ( ) ) ;
54+ Tuple < FunctionSignature , FunctionSignature > signatures = GetFunctionSignatures ( function1 , function2 ) ;
4355
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 ( ) ) ;
4658 }
4759
4860 [ Fact ]
@@ -61,6 +73,14 @@ public static void Run(string id, out string output)
6173 output = result;
6274}" ;
6375
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+ {
6484 var tree1 = CSharpSyntaxTree . ParseText ( function1 , CSharpParseOptions . Default . WithKind ( SourceCodeKind . Script ) ) ;
6585 var tree2 = CSharpSyntaxTree . ParseText ( function2 , CSharpParseOptions . Default . WithKind ( SourceCodeKind . Script ) ) ;
6686
@@ -75,8 +95,7 @@ public static void Run(string id, out string output)
7595 var signature1 = compilation1 . GetEntryPointSignature ( new FunctionEntryPointResolver ( ) ) ;
7696 var signature2 = compilation2 . GetEntryPointSignature ( new FunctionEntryPointResolver ( ) ) ;
7797
78- Assert . False ( signature1 . Equals ( signature2 ) ) ;
79- Assert . NotEqual ( signature1 . GetHashCode ( ) , signature2 . GetHashCode ( ) ) ;
98+ return Tuple . Create ( signature1 , signature2 ) ;
8099 }
81100
82101 [ Fact ]
@@ -97,22 +116,10 @@ public static void Run( System.String id ,
97116 output = result;
98117}" ;
99118
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 ) ;
110120
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 ( ) ) ;
116123 }
117124
118125 [ Fact ]
@@ -148,6 +155,54 @@ public static void Run(string id, ICollection<Test> test1)
148155
149156}
150157
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+
151206public class Test
152207{
153208 public string Id { get; set; }
0 commit comments