PageRenderTime 48ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/main/tests/Ide.Tests/MonoDevelop.Ide/TypeSystemServiceTests.cs

https://github.com/mhutch/monodevelop
C# | 173 lines | 122 code | 23 blank | 28 comment | 5 complexity | 625187a89848f6dc7a0ab9c2c45fba0a MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, GPL-2.0, CC-BY-SA-3.0, MIT, LGPL-2.1, Apache-2.0
  1. //
  2. // TypeSystemServiceTests.cs
  3. //
  4. // Author:
  5. // Mike Krüger <mikkrg@microsoft.com>
  6. //
  7. // Copyright (c) 2017 Microsoft
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. using System;
  27. using NUnit.Framework;
  28. using System.Collections.Generic;
  29. using UnitTests;
  30. using MonoDevelop.Ide.TypeSystem;
  31. using MonoDevelop.Projects;
  32. using MonoDevelop.Core;
  33. using System.Threading.Tasks;
  34. using System.Linq;
  35. using System.Threading;
  36. using Microsoft.CodeAnalysis.Host;
  37. using Microsoft.CodeAnalysis.SolutionCrawler;
  38. using Microsoft.CodeAnalysis.SolutionSize;
  39. namespace MonoDevelop.Ide
  40. {
  41. [TestFixture]
  42. class TypeSystemServiceTests : IdeTestBase
  43. {
  44. class TrackTestProject : DotNetProject
  45. {
  46. readonly string type;
  47. protected override void OnGetTypeTags (HashSet<string> types)
  48. {
  49. types.Add (type);
  50. }
  51. protected override DotNetCompilerParameters OnCreateCompilationParameters (DotNetProjectConfiguration config, ConfigurationKind kind)
  52. {
  53. throw new NotImplementedException ();
  54. }
  55. protected override ClrVersion [] OnGetSupportedClrVersions ()
  56. {
  57. throw new NotImplementedException ();
  58. }
  59. public TrackTestProject (string language, string type) : base(language)
  60. {
  61. this.type = type;
  62. Initialize (this);
  63. }
  64. }
  65. [Test]
  66. public void TestOuptutTracking_ProjectType ()
  67. {
  68. TypeSystemService.AddOutputTrackingNode (new TypeSystemOutputTrackingNode { ProjectType = "TestProjectType" });
  69. Assert.IsFalse (TypeSystemService.IsOutputTrackedProject (new TrackTestProject ("C#", "Bar")));
  70. Assert.IsTrue (TypeSystemService.IsOutputTrackedProject (new TrackTestProject ("C#", "TestProjectType")));
  71. }
  72. [Test]
  73. public void TestOuptutTracking_LanguageName ()
  74. {
  75. TypeSystemService.AddOutputTrackingNode (new TypeSystemOutputTrackingNode { LanguageName = "IL" });
  76. Assert.IsTrue (TypeSystemService.IsOutputTrackedProject (new TrackTestProject ("IL", "Bar")));
  77. }
  78. [Test]
  79. public async Task ProjectReferencingOutputTrackedReference()
  80. {
  81. string solFile = Util.GetSampleProject("csharp-app-fsharp-lib", "csappfslib.sln");
  82. using (Solution sol = (Solution)await Services.ProjectService.ReadWorkspaceItem (Util.GetMonitor (), solFile)) {
  83. var fsharpLibrary = sol.Items.FirstOrDefault (pr => pr.Name == "fslib") as DotNetProject;
  84. Assert.IsTrue (TypeSystemService.IsOutputTrackedProject (fsharpLibrary));
  85. }
  86. }
  87. [Test]
  88. public async Task TestWorkspacePersistentStorageLocationService ()
  89. {
  90. string solFile = Util.GetSampleProject ("console-project", "ConsoleProject.sln");
  91. using (Solution sol = (Solution)await Services.ProjectService.ReadWorkspaceItem (Util.GetMonitor (), solFile))
  92. using (var ws = await TypeSystemServiceTestExtensions.LoadSolution (sol)) {
  93. var storageLocationService = (MonoDevelopPersistentStorageLocationService)ws.Services.GetService<IPersistentStorageLocationService> ();
  94. Assert.That (storageLocationService.TryGetStorageLocation (ws.CurrentSolution.Id), Is.Not.Null.Or.Empty);
  95. }
  96. }
  97. [Test]
  98. public async Task TestWorkspacePersistentStorage ()
  99. {
  100. string solFile = Util.GetSampleProject ("console-project", "ConsoleProject.sln");
  101. using (Solution sol = (Solution)await Services.ProjectService.ReadWorkspaceItem (Util.GetMonitor (), solFile))
  102. using (var ws = await TypeSystemServiceTestExtensions.LoadSolution (sol)) {
  103. var storageLocationService = (MonoDevelopPersistentStorageLocationService)ws.Services.GetService<IPersistentStorageLocationService> ();
  104. var storageLocation = System.IO.Path.Combine (
  105. storageLocationService.TryGetStorageLocation (ws.CurrentSolution.Id),
  106. "sqlite3",
  107. "storage.ide");
  108. if (System.IO.File.Exists (storageLocation))
  109. System.IO.File.Delete (storageLocation);
  110. var solutionSizeTracker = (IIncrementalAnalyzerProvider)Composition.CompositionManager.GetExportedValue<ISolutionSizeTracker> ();
  111. // This will return the tracker, since it's a singleton.
  112. var analyzer = solutionSizeTracker.CreateIncrementalAnalyzer (ws);
  113. // We need this hack because we can't guess when the work coordinator will run the incremental analyzers.
  114. await analyzer.NewSolutionSnapshotAsync (ws.CurrentSolution, CancellationToken.None);
  115. foreach (var projectFile in sol.GetAllProjects ().SelectMany (x => x.Files.Where (file => file.BuildAction == BuildAction.Compile))) {
  116. var projectId = ws.GetProjectId (projectFile.Project);
  117. var docId = ws.GetDocumentId (projectId, projectFile.FilePath);
  118. var doc = ws.GetDocument (docId);
  119. if (!doc.SupportsSyntaxTree)
  120. continue;
  121. await Microsoft.CodeAnalysis.FindSymbols.SyntaxTreeIndex.PrecalculateAsync (doc, CancellationToken.None);
  122. }
  123. var fi = new System.IO.FileInfo (storageLocation);
  124. Assert.That (fi.Length, Is.GreaterThan (0));
  125. }
  126. }
  127. [Test]
  128. public async Task TestWorkspaceImmediatelyAvailable ()
  129. {
  130. //Initialize IdeApp so IdeApp.Workspace is not null
  131. if (!IdeApp.IsInitialized)
  132. IdeApp.Initialize (new ProgressMonitor ());
  133. string solFile = Util.GetSampleProject ("console-project", "ConsoleProject.sln");
  134. var tcs = new TaskCompletionSource<bool> ();
  135. IdeApp.Workspace.SolutionLoaded += (s, e) => {
  136. var workspace = TypeSystemService.GetWorkspace (e.Solution);
  137. Assert.IsNotNull (workspace);
  138. Assert.AreNotSame (workspace, TypeSystemService.emptyWorkspace);
  139. workspace.Dispose ();
  140. tcs.SetResult (true);
  141. };
  142. try {
  143. await IdeApp.Workspace.OpenWorkspaceItem (solFile);
  144. await tcs.Task;
  145. } finally {
  146. await IdeApp.Workspace.Close (false);
  147. }
  148. }
  149. }
  150. }