PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Workspaces/CoreTest/FindAllDeclarationsTests.TestSolutionsAndProject.cs

https://github.com/EkardNT/Roslyn
C# | 272 lines | 253 code | 18 blank | 1 comment | 9 complexity | cef98f7311af509b103890d1548ca846 MD5 | raw file
  1// Copyright (c) Microsoft Open Technologies, Inc.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.
  2
  3using System;
  4using System.Collections.Generic;
  5using System.Linq;
  6using Microsoft.CodeAnalysis.Text;
  7using Xunit;
  8
  9namespace Microsoft.CodeAnalysis.UnitTests
 10{
 11    public partial class FindAllDeclarationsTests
 12    {
 13        private static void Verify(string searchTerm, bool respectCase, WorkspaceKind workspaceKind, IEnumerable<ISymbol> declarations, params string[] expectedResults)
 14        {
 15            var actualResultCount = declarations.Count();
 16            var expectedResultCount = expectedResults.Length;
 17            Assert.True(expectedResultCount == actualResultCount,
 18                string.Format("Search term '{0}' expected '{1}' results, found '{2}.  Ignore case was set to '{3}', Workspace {4} was used",
 19                    searchTerm,
 20                    expectedResultCount,
 21                    actualResultCount,
 22                    respectCase,
 23                    Enum.GetName(typeof(WorkspaceKind), workspaceKind)));
 24            if (actualResultCount > 0)
 25            {
 26                VerifyResults(declarations, expectedResults);
 27            }
 28        }
 29
 30        private static void Verify(WorkspaceKind workspaceKind, IEnumerable<ISymbol> declarations, params string[] expectedResults)
 31        {
 32            var actualResultCount = declarations.Count();
 33            var expectedResultCount = expectedResults.Length;
 34            Assert.True(expectedResultCount == actualResultCount,
 35                string.Format("Expected '{0}' results, found '{1}. Workspace {2} was used",
 36                    expectedResultCount,
 37                    actualResultCount,
 38                    Enum.GetName(typeof(WorkspaceKind), workspaceKind)));
 39            if (actualResultCount > 0)
 40            {
 41                VerifyResults(declarations, expectedResults);
 42            }
 43        }
 44
 45        private static void VerifyResults(IEnumerable<ISymbol> declarations, string[] expectedResults)
 46        {
 47            declarations = declarations.OrderBy(d => d.ToString());
 48            expectedResults = expectedResults.OrderBy(r => r).ToArray();
 49
 50            for (int i = 0; i < expectedResults.Length; i++)
 51            {
 52                var actualResult = declarations.ElementAt(i).ToString();
 53                var expectedResult = expectedResults[i];
 54                Assert.True(
 55                    string.Equals(actualResult, expectedResult, StringComparison.Ordinal),
 56                    string.Format("Expected result to be {0} was {1}", expectedResult, actualResult));
 57            }
 58        }
 59
 60        private static void VerifyInnerExceptionArgumentNull(AggregateException ex, string argName)
 61        {
 62            var exception = ex.InnerException as ArgumentNullException;
 63            Assert.True(exception != null, string.Format("Expected InnterException to be 'System.ArgumentNullException' was '{0}'", ex.InnerException.ToString()));
 64            Assert.True(exception.ParamName.Contains(argName), string.Format("Expected InnerException ParamName to contain '{0}', actual ParamName is: '{1}'", argName, exception.ParamName));
 65        }
 66
 67        private static void VerifyInnerExceptionIsType<T>(Exception ex) where T : Exception
 68        {
 69            Assert.True(ex.InnerException is T, string.Format("Expected InnterException to be '{0}' was '{1}'", typeof(T).Name, ex.InnerException.ToString()));
 70        }
 71
 72        private static Solution CreateSolution()
 73        {
 74            return new CustomWorkspace().CurrentSolution;
 75        }
 76
 77        private static Solution GetSingleProjectSolution(params string[] sourceTexts)
 78        {
 79            var pid = ProjectId.CreateNewId();
 80            var solution = CreateSolution()
 81                    .AddProject(pid, "TestCases", "TestCases", LanguageNames.CSharp)
 82                    .AddMetadataReference(pid, MscorlibRef);
 83            for (int i = 0; i < sourceTexts.Length; i++)
 84            {
 85                var did = DocumentId.CreateNewId(pid);
 86                solution = solution.AddDocument(did, "foo" + i + ".cs", SourceText.From(sourceTexts[i]));
 87            }
 88
 89            return solution;
 90        }
 91
 92        private static Solution GetMultipleProjectSolution(params string[] sourceTexts)
 93        {
 94            var solution = CreateSolution();
 95            for (int i = 0; i < sourceTexts.Length; i++)
 96            {
 97                var pid = ProjectId.CreateNewId();
 98                var did = DocumentId.CreateNewId(pid);
 99                solution = solution
100                    .AddProject(pid, "TestCases" + i, "TestCases" + i, LanguageNames.CSharp)
101                    .AddMetadataReference(pid, MscorlibRef);
102                solution = solution.AddDocument(did, "foo" + i + ".cs", SourceText.From(sourceTexts[i]));
103            }
104
105            return solution;
106        }
107
108        private static Solution GetSolution(WorkspaceKind workspaceKind)
109        {
110            switch (workspaceKind)
111            {
112                case WorkspaceKind.SingleClass:
113                    return GetSingleProjectSolution(SingleClass);
114                case WorkspaceKind.SingleClassWithSingleMethod:
115                    return GetSingleProjectSolution(SingleClassWithSingleMethod);
116                case WorkspaceKind.SingleClassWithSingleProperty:
117                    return GetSingleProjectSolution(SingleClassWithSingleProperty);
118                case WorkspaceKind.SingleClassWithSingleField:
119                    return GetSingleProjectSolution(SingleClassWithSingleField);
120                case WorkspaceKind.TwoProjectsEachWithASingleClassWithSingleMethod:
121                    return GetMultipleProjectSolution(SingleClassWithSingleMethod, SingleClassWithSingleMethod);
122                case WorkspaceKind.TwoProjectsEachWithASingleClassWithSingleProperty:
123                    return GetMultipleProjectSolution(SingleClassWithSingleProperty, SingleClassWithSingleProperty);
124                case WorkspaceKind.TwoProjectsEachWithASingleClassWithSingleField:
125                    return GetMultipleProjectSolution(SingleClassWithSingleField, SingleClassWithSingleField);
126                case WorkspaceKind.NestedClass:
127                    return GetSingleProjectSolution(NestedClass);
128                case WorkspaceKind.TwoNamespacesWithIdenticalClases:
129                    return GetSingleProjectSolution(Namespace1, Namespace2);
130                default:
131                    return null;
132            }
133        }
134
135        private static Project GetProject(WorkspaceKind workspaceKind)
136        {
137            return GetSolution(workspaceKind).Projects.First();
138        }
139
140        public enum WorkspaceKind
141        {
142            SingleClass,
143            SingleClassWithSingleMethod,
144            SingleClassWithSingleProperty,
145            SingleClassWithSingleField,
146            TwoProjectsEachWithASingleClassWithSingleMethod,
147            TwoProjectsEachWithASingleClassWithSingleProperty,
148            TwoProjectsEachWithASingleClassWithSingleField,
149            NestedClass,
150            TwoNamespacesWithIdenticalClases
151        }
152
153        private const string SingleClass =
154    @"
155using System;
156using System.Collections.Generic;
157using System.Linq;
158using System.Text;
159using System.Threading.Tasks;
160
161namespace TestCases
162{
163    class TestCase
164    {
165
166    }
167}
168            ";
169        private const string SingleClassWithSingleMethod =
170    @"
171using System;
172using System.Collections.Generic;
173using System.Linq;
174using System.Text;
175using System.Threading.Tasks;
176
177namespace TestCases
178{
179    class TestCase
180    {
181        static void Test(string[] args)
182        {
183
184        }
185    }
186}
187            ";
188        private const string SingleClassWithSingleProperty =
189    @"
190using System;
191using System.Collections.Generic;
192using System.Linq;
193using System.Text;
194using System.Threading.Tasks;
195
196namespace TestCases
197{
198    class TestCase
199    {
200        public int TestProperty{ get; set; }
201    }
202}
203            ";
204        private const string SingleClassWithSingleField =
205    @"
206using System;
207using System.Collections.Generic;
208using System.Linq;
209using System.Text;
210using System.Threading.Tasks;
211
212namespace TestCases
213{
214    class TestCase
215    {
216        private int TestField = 0;
217    }
218}
219            ";
220        private const string NestedClass =
221    @"
222using System;
223using System.Collections.Generic;
224using System.Linq;
225using System.Text;
226using System.Threading.Tasks;
227
228namespace TestCases
229{
230    class TestCase
231    {
232        class InnerTestCase
233        {
234
235        }
236    }
237}
238            ";
239
240        private const string Namespace1 =
241@"
242using System;
243using System.Collections.Generic;
244using System.Linq;
245using System.Text;
246using System.Threading.Tasks;
247
248namespace TestCase1
249{
250    class TestCase
251    {
252    }
253}
254            ";
255
256        private const string Namespace2 =
257@"
258using System;
259using System.Collections.Generic;
260using System.Linq;
261using System.Text;
262using System.Threading.Tasks;
263
264namespace TestCase2
265{
266    class TestCase
267    {
268    }
269}
270            ";
271    }
272}