/main/tests/MonoDevelop.Core.Tests/MonoDevelop.Projects/PortableLibraryTests.cs

https://github.com/directhex/monodevelop · C# · 119 lines · 69 code · 17 blank · 33 comment · 0 complexity · 0a8c7e439bbb73b9a54b97e45a2e7ed1 MD5 · raw file

  1. //
  2. // PortableLibraryTests.cs
  3. //
  4. // Author:
  5. // Lluis Sanchez <llsan@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 System.IO;
  28. using System.Xml;
  29. using NUnit.Framework;
  30. using UnitTests;
  31. using MonoDevelop.Core;
  32. using System.Linq;
  33. using MonoDevelop.Projects.MSBuild;
  34. using System.Threading.Tasks;
  35. using MonoDevelop.Core.Serialization;
  36. using MonoDevelop.Projects.Extensions;
  37. namespace MonoDevelop.Projects
  38. {
  39. [TestFixture]
  40. public class PortableLibraryTests: TestBase
  41. {
  42. [Test]
  43. public async Task LoadPortableLibrary ()
  44. {
  45. string solFile = Util.GetSampleProject ("portable-library", "portable-library.sln");
  46. Solution sol = (Solution)await Services.ProjectService.ReadWorkspaceItem (Util.GetMonitor (), solFile);
  47. var p = sol.FindProjectByName ("PortableLibrary");
  48. Assert.IsInstanceOf<DotNetProject> (p);
  49. var pl = (DotNetProject)p;
  50. Assert.AreEqual (".NETPortable", pl.GetDefaultTargetFrameworkId ().Identifier);
  51. sol.Dispose ();
  52. }
  53. [Test]
  54. public async Task BuildPortableLibrary ()
  55. {
  56. string solFile = Util.GetSampleProject ("portable-library", "portable-library.sln");
  57. Solution sol = (Solution)await Services.ProjectService.ReadWorkspaceItem (Util.GetMonitor (), solFile);
  58. var res = await sol.Build (Util.GetMonitor (), "Debug");
  59. Assert.IsNull (res.Errors.FirstOrDefault ()?.ToString ());
  60. sol.Dispose ();
  61. }
  62. [Test]
  63. public async Task PortableLibraryImplicitReferences ()
  64. {
  65. string solFile = Util.GetSampleProject ("portable-library", "portable-library.sln");
  66. Solution sol = (Solution)await Services.ProjectService.ReadWorkspaceItem (Util.GetMonitor (), solFile);
  67. var p = (DotNetProject)sol.FindProjectByName ("PortableLibrary");
  68. var refs = (await p.GetReferencedAssemblies (p.Configurations [0].Selector)).Select (r => r.FilePath.FileName).ToArray ();
  69. sol.Dispose ();
  70. }
  71. /// <summary>
  72. /// With a PCL project having no references if you add a reference, then remove, then add it
  73. /// again then the saved project file will end up no references.
  74. /// </summary>
  75. [Test]
  76. public async Task AddingRemovingAndThenAddingReferenceToPortableLibrarySavesReferenceToFile ()
  77. {
  78. string solFile = Util.GetSampleProject ("portable-library", "portable-library.sln");
  79. Solution sol = (Solution)await Services.ProjectService.ReadWorkspaceItem (Util.GetMonitor (), solFile);
  80. var p = sol.FindProjectByName ("PortableLibrary") as DotNetProject;
  81. Assert.AreEqual (0, p.References.Count);
  82. // Add System.Xml reference.
  83. p.References.Add (ProjectReference.CreateAssemblyReference ("System.Xml"));
  84. await p.SaveAsync (Util.GetMonitor ());
  85. Assert.AreEqual (1, p.References.Count);
  86. // Remove System.Xml reference so no references remain.
  87. p.References.RemoveAt (0);
  88. await p.SaveAsync (Util.GetMonitor ());
  89. Assert.AreEqual (0, p.References.Count);
  90. // Add System.Xml reference again.
  91. p.References.Add (ProjectReference.CreateAssemblyReference ("System.Xml"));
  92. await p.SaveAsync (Util.GetMonitor ());
  93. Assert.AreEqual (1, p.References.Count);
  94. // Ensure the references are saved to the file.
  95. sol = (Solution)await Services.ProjectService.ReadWorkspaceItem (Util.GetMonitor (), solFile);
  96. p = sol.FindProjectByName ("PortableLibrary") as DotNetProject;
  97. Assert.AreEqual (1, p.References.Count);
  98. Assert.AreEqual ("System.Xml", p.References [0].Include);
  99. sol.Dispose ();
  100. }
  101. }
  102. }