PageRenderTime 128ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/VisualStudio/ABB.SrcML.VisualStudio.SrcMLService.IntegrationTests/SrcMLServiceCPlusPlusTests.cs

https://github.com/nkcsgexi/SrcML.NET
C# | 173 lines | 125 code | 27 blank | 21 comment | 6 complexity | d3d4172aa86ccf0ff71881f13ed4aa27 MD5 | raw file
  1. using ABB.SrcML.Test.Utilities;
  2. using EnvDTE;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using Microsoft.VSSDK.Tools.VsIdeTesting;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. namespace ABB.SrcML.VisualStudio.SrcMLService.IntegrationTests {
  14. [TestClass]
  15. public class SrcMLServiceCPlusPlusTests : IInvoker {
  16. private const string TestSolutionName = "TestCPPSolution";
  17. private static Solution TestSolution;
  18. private static object TestLock;
  19. private static string TestSolutionPath = Path.Combine(TestSolutionName, TestSolutionName + ".sln");
  20. [ClassInitialize]
  21. public static void ClassSetup(TestContext testContext) {
  22. // Create a local copy of the solution
  23. FileUtils.CopyDirectory(Path.Combine(TestConstants.InputFolderPath, TestSolutionName), TestSolutionName);
  24. TestLock = new object();
  25. }
  26. [TestInitialize]
  27. public void Setup() {
  28. TestSolution = VsIdeTestHostContext.Dte.Solution;
  29. Assert.IsNotNull(TestSolution, "Could not get the solution");
  30. TestSolution.Open(Path.GetFullPath(TestSolutionPath));
  31. TestHelpers.TestScaffold.Service.StartMonitoring();
  32. }
  33. [TestCleanup]
  34. public void Cleanup() {
  35. TestHelpers.TestScaffold.Service.StopMonitoring();
  36. TestSolution.Close();
  37. }
  38. [TestMethod]
  39. [HostType("VS IDE")]
  40. public void TestCppServiceStartup() {
  41. Assert.IsTrue(TestHelpers.WaitForServiceToFinish(TestHelpers.TestScaffold.Service, 5000));
  42. var archive = TestHelpers.TestScaffold.Service.GetSrcMLArchive();
  43. Assert.IsNotNull(archive, "Could not get the SrcML Archive");
  44. Assert.AreEqual(4, archive.FileUnits.Count(), "There should only be four files in the srcML archive");
  45. }
  46. [TestMethod]
  47. [HostType("VS IDE")]
  48. public void TestCppFileOperations() {
  49. // setup
  50. Project project = TestHelpers.GetProjects(TestSolution).FirstOrDefault();
  51. Assert.IsNotNull(project, "Couldn't get the project");
  52. var archive = TestHelpers.TestScaffold.Service.GetSrcMLArchive();
  53. Assert.IsNotNull(archive, "Could not get the SrcML Archive");
  54. AutoResetEvent resetEvent = new AutoResetEvent(false);
  55. string expectedFilePath = null;
  56. FileEventType expectedEventType = FileEventType.FileDeleted;
  57. EventHandler<FileEventRaisedArgs> action = (o, e) => {
  58. lock(TestLock) {
  59. if(e.FilePath.Equals(expectedFilePath, StringComparison.InvariantCultureIgnoreCase) && e.EventType == expectedEventType) {
  60. resetEvent.Set();
  61. }
  62. }
  63. };
  64. TestHelpers.TestScaffold.Service.SourceFileChanged += action;
  65. // add a file
  66. var fileTemplate = Path.Combine(TestConstants.TemplatesFolder, "NewCPPClass1.cpp");
  67. expectedFilePath = Path.Combine(Path.GetDirectoryName(project.FullName), "NewCPPClass1.cpp");
  68. expectedEventType = FileEventType.FileAdded;
  69. File.Copy(fileTemplate, expectedFilePath);
  70. var item = project.ProjectItems.AddFromFile(expectedFilePath);
  71. // project.Save();
  72. Assert.IsTrue(resetEvent.WaitOne(500));
  73. Assert.IsTrue(archive.ContainsFile(expectedFilePath));
  74. Assert.IsFalse(archive.IsOutdated(expectedFilePath));
  75. //// rename a file
  76. //string oldFilePath = expectedFilePath;
  77. //expectedFilePath = Path.Combine(Path.GetDirectoryName(project.FullName), "NewCPPClass2.cpp");
  78. //expectedEventType = FileEventType.FileAdded;
  79. //item = TestSolution.FindProjectItem(oldFilePath);
  80. //item.SaveAs(expectedFilePath);
  81. //File.Delete(oldFilePath);
  82. //project.Save();
  83. //Assert.IsTrue(resetEvent.WaitOne(500));
  84. //Assert.IsTrue(archive.ContainsFile(expectedFilePath), "The archive should contain {0}", expectedFilePath);
  85. //Assert.IsFalse(archive.ContainsFile(oldFilePath), "the archive should not contain {0}", oldFilePath);
  86. //Assert.IsFalse(archive.IsOutdated(expectedFilePath), String.Format("{0} is outdated", expectedFilePath));
  87. // delete the file
  88. expectedEventType = FileEventType.FileDeleted;
  89. item = TestSolution.FindProjectItem(expectedFilePath);
  90. item.Delete();
  91. project.Save();
  92. Assert.IsTrue(resetEvent.WaitOne(500));
  93. Assert.IsFalse(archive.IsOutdated(expectedFilePath));
  94. //Assert.AreEqual(2, archive.FileUnits.Count());
  95. TestHelpers.TestScaffold.Service.SourceFileChanged -= action;
  96. }
  97. [TestMethod]
  98. [HostType("VS IDE")]
  99. public void TestCppProjectOperations() {
  100. var archive = TestHelpers.TestScaffold.Service.GetSrcMLArchive();
  101. AutoResetEvent resetEvent = new AutoResetEvent(false);
  102. var testProjectName = "ConsoleApplication1";
  103. var expectedProjectDirectory = Path.GetFullPath(Path.Combine(TestSolutionName, testProjectName));
  104. var expectedEventType = FileEventType.FileAdded;
  105. HashSet<string> expectedFiles = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase) {
  106. Path.Combine(expectedProjectDirectory, "ConsoleApplication1.cpp"),
  107. Path.Combine(expectedProjectDirectory, "stdafx.cpp"),
  108. Path.Combine(expectedProjectDirectory, "stdafx.h"),
  109. Path.Combine(expectedProjectDirectory, "targetver.h"),
  110. };
  111. EventHandler<FileEventRaisedArgs> action = (o, e) => {
  112. lock(TestLock) {
  113. if(expectedFiles.Contains(Path.GetFullPath(e.FilePath)) && e.EventType == expectedEventType) {
  114. resetEvent.Set();
  115. }
  116. }
  117. };
  118. TestHelpers.TestScaffold.Service.SourceFileChanged += action;
  119. var projectTemplate = Path.GetFullPath(Path.Combine(TestConstants.TemplatesFolder, testProjectName, testProjectName, testProjectName + ".vcxproj"));
  120. // add a new project
  121. var addedProject = TestSolution.AddFromTemplate(projectTemplate, expectedProjectDirectory, testProjectName);
  122. addedProject.Save();
  123. Assert.IsTrue(resetEvent.WaitOne(500));
  124. Assert.IsTrue(resetEvent.WaitOne(500));
  125. Assert.IsTrue(resetEvent.WaitOne(500));
  126. Assert.IsTrue(resetEvent.WaitOne(500));
  127. foreach(var expectedFile in expectedFiles) {
  128. Assert.IsTrue(File.Exists(expectedFile));
  129. Assert.IsTrue(archive.ContainsFile(expectedFile));
  130. Assert.IsFalse(archive.IsOutdated(expectedFile));
  131. }
  132. // remove the project
  133. expectedEventType = FileEventType.FileDeleted;
  134. TestSolution.Remove(addedProject);
  135. Assert.IsTrue(resetEvent.WaitOne(500));
  136. //Assert.IsTrue(resetEvent.WaitOne(1000));
  137. foreach(var expectedFile in expectedFiles) {
  138. Assert.IsFalse(archive.ContainsFile(expectedFile));
  139. }
  140. TestHelpers.TestScaffold.Service.SourceFileChanged -= action;
  141. }
  142. public void Invoke(MethodInvoker globalSystemWindowsFormsMethodInvoker) {
  143. UIThreadInvoker.Invoke(globalSystemWindowsFormsMethodInvoker);
  144. }
  145. }
  146. }