PageRenderTime 66ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/src/XMakeBuildEngine/UnitTestsPublicOM/Definition/EditingElementsReferencedByOrReferences_Tests.cs

https://gitlab.com/Banul/la-project1
C# | 350 lines | 272 code | 40 blank | 38 comment | 12 complexity | 442a2b3eb588b7d8951d067d05815716 MD5 | raw file
  1. // Copyright (c) Microsoft. All rights reserved.
  2. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
  3. //-----------------------------------------------------------------------
  4. // </copyright>
  5. // <summary>Tests for editing elements that are related to other XML elements</summary>
  6. //-----------------------------------------------------------------------
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Globalization;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Xml;
  13. using Microsoft.Build.Construction;
  14. using Microsoft.Build.Evaluation;
  15. using Xunit;
  16. namespace Microsoft.Build.UnitTests.OM.Definition
  17. {
  18. /// <summary>
  19. /// Tests around editing elements that are referenced by others or the ones that references others.
  20. /// </summary>
  21. public class EditingElementsReferencedByOrReferences_Tests
  22. {
  23. /// <summary>
  24. /// Changes the item type on an item used with the at operator.
  25. /// </summary>
  26. [Fact]
  27. public void ChangeItemTypeInReferencedItem()
  28. {
  29. Project project = GetProject(
  30. @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
  31. <ItemGroup>
  32. <I Include=""X"" />
  33. <I Include=""@(I);Y"" />
  34. </ItemGroup>
  35. </Project>");
  36. ProjectItem item = project.GetItems("I").Where(i => i.UnevaluatedInclude == "X").First();
  37. item.ItemType = "J";
  38. string expected =
  39. @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
  40. <ItemGroup>
  41. <J Include=""X"" />
  42. <I Include=""@(I);Y"" />
  43. </ItemGroup>
  44. </Project>";
  45. Helpers.VerifyAssertProjectContent(expected, project.Xml, false);
  46. project.ReevaluateIfNecessary();
  47. IEnumerable<ProjectItem> items = project.GetItems("I");
  48. Assert.Equal(1, items.Count()); // "Wrong number of items after changing type"
  49. Assert.Equal("Y", items.First().EvaluatedInclude); // "Wrong evaluated include after changing type"
  50. }
  51. /// <summary>
  52. /// Removes an item in a ; separated list. It blows up the list.
  53. /// </summary>
  54. [Fact]
  55. public void RemoveItemInList()
  56. {
  57. Project project = GetProject(
  58. @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
  59. <ItemGroup>
  60. <I Include=""X"" />
  61. <I Include=""@(I);Y;Z"" />
  62. </ItemGroup>
  63. </Project>");
  64. ProjectItem item = project.GetItems("I").Where(i => i.EvaluatedInclude == "Y").First();
  65. project.RemoveItem(item);
  66. string expected =
  67. @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
  68. <ItemGroup>
  69. <I Include=""X"" />
  70. <I Include=""X"" />
  71. <I Include=""Z"" />
  72. </ItemGroup>
  73. </Project>";
  74. Helpers.VerifyAssertProjectContent(expected, project.Xml, false);
  75. }
  76. /// <summary>
  77. /// Renames an item in a ; separated list. It blows up the list.
  78. /// </summary>
  79. [Fact]
  80. public void RenameItemInList()
  81. {
  82. Project project = GetProject(
  83. @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
  84. <ItemGroup>
  85. <I Include=""X"" />
  86. <I Include=""@(I);Y"" />
  87. </ItemGroup>
  88. </Project>");
  89. ProjectItem item = project.GetItems("I").Where(i => i.EvaluatedInclude == "Y").First();
  90. item.Rename("Z");
  91. string expected =
  92. @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
  93. <ItemGroup>
  94. <I Include=""X"" />
  95. <I Include=""X"" />
  96. <I Include=""Z"" />
  97. </ItemGroup>
  98. </Project>";
  99. Helpers.VerifyAssertProjectContent(expected, project.Xml, false);
  100. }
  101. /// <summary>
  102. /// Removes metadata duplicated in item.
  103. /// </summary>
  104. [Fact]
  105. public void RemoveMetadata1()
  106. {
  107. Project project = GetProject(
  108. @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
  109. <ItemDefinitionGroup>
  110. <I>
  111. <M>A</M>
  112. </I>
  113. </ItemDefinitionGroup>
  114. <ItemGroup>
  115. <I Include=""X"">
  116. <M>%(M);B</M>
  117. <M>%(M);C</M>
  118. </I>
  119. <I Include=""Y"">
  120. <M>%(M);D</M>
  121. </I>
  122. </ItemGroup>
  123. </Project>");
  124. ProjectItem item1 = project.GetItems("I").Where(i => i.EvaluatedInclude == "X").First();
  125. Assert.Equal("A;B;C", item1.GetMetadataValue("M")); // "Invalid metadata at start"
  126. ProjectItem item2 = project.GetItems("I").Where(i => i.EvaluatedInclude == "Y").First();
  127. Assert.Equal("A;D", item2.GetMetadataValue("M")); // "Invalid metadata at start"
  128. item1.RemoveMetadata("M");
  129. string expected =
  130. @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
  131. <ItemDefinitionGroup>
  132. <I>
  133. <M>A</M>
  134. </I>
  135. </ItemDefinitionGroup>
  136. <ItemGroup>
  137. <I Include=""X"">
  138. <M>%(M);B</M>
  139. </I>
  140. <I Include=""Y"">
  141. <M>%(M);D</M>
  142. </I>
  143. </ItemGroup>
  144. </Project>";
  145. Helpers.VerifyAssertProjectContent(expected, project.Xml, false);
  146. }
  147. /// <summary>
  148. /// Removes duplicated metadata and checks evaluation.
  149. /// </summary>
  150. [Fact]
  151. public void RemoveMetadata2()
  152. {
  153. Project project = GetProject(
  154. @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
  155. <ItemDefinitionGroup>
  156. <I>
  157. <M>A</M>
  158. </I>
  159. </ItemDefinitionGroup>
  160. <ItemGroup>
  161. <I Include=""X"">
  162. <M>%(M);B</M>
  163. <M>%(M);C</M>
  164. </I>
  165. <I Include=""Y"">
  166. <M>%(M);D</M>
  167. </I>
  168. </ItemGroup>
  169. </Project>");
  170. ProjectItem item1 = project.GetItems("I").Where(i => i.EvaluatedInclude == "X").First();
  171. item1.RemoveMetadata("M");
  172. string expected =
  173. @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
  174. <ItemDefinitionGroup>
  175. <I>
  176. <M>A</M>
  177. </I>
  178. </ItemDefinitionGroup>
  179. <ItemGroup>
  180. <I Include=""X"">
  181. <M>%(M);B</M>
  182. </I>
  183. <I Include=""Y"">
  184. <M>%(M);D</M>
  185. </I>
  186. </ItemGroup>
  187. </Project>";
  188. Helpers.VerifyAssertProjectContent(expected, project.Xml, false);
  189. project.ReevaluateIfNecessary();
  190. item1 = project.GetItems("I").Where(i => i.EvaluatedInclude == "X").First();
  191. Assert.Equal("A;B", item1.GetMetadataValue("M")); // "Invalid metadata after first removal"
  192. ProjectItem item2 = project.GetItems("I").Where(i => i.EvaluatedInclude == "Y").First();
  193. Assert.Equal("A;D", item2.GetMetadataValue("M")); // "Invalid metadata after first removal"
  194. }
  195. /// <summary>
  196. /// Removes metadata but still keep inherited one from item definition.
  197. /// </summary>
  198. [Fact]
  199. public void RemoveMetadata3()
  200. {
  201. Project project = GetProject(
  202. @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
  203. <ItemDefinitionGroup>
  204. <I>
  205. <M>A</M>
  206. </I>
  207. </ItemDefinitionGroup>
  208. <ItemGroup>
  209. <I Include=""X"">
  210. <M>%(M);B</M>
  211. <M>%(M);C</M>
  212. </I>
  213. <I Include=""Y"">
  214. <M>%(M);D</M>
  215. </I>
  216. </ItemGroup>
  217. </Project>");
  218. ProjectItem item1 = project.GetItems("I").Where(i => i.EvaluatedInclude == "X").First();
  219. item1.RemoveMetadata("M");
  220. project.ReevaluateIfNecessary();
  221. ProjectItem item2 = project.GetItems("I").Where(i => i.EvaluatedInclude == "Y").First();
  222. item2.RemoveMetadata("M");
  223. string expected =
  224. @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
  225. <ItemDefinitionGroup>
  226. <I>
  227. <M>A</M>
  228. </I>
  229. </ItemDefinitionGroup>
  230. <ItemGroup>
  231. <I Include=""X"">
  232. <M>%(M);B</M>
  233. </I>
  234. <I Include=""Y"" />
  235. </ItemGroup>
  236. </Project>";
  237. Helpers.VerifyAssertProjectContent(expected, project.Xml, false);
  238. project.ReevaluateIfNecessary();
  239. item1 = project.GetItems("I").Where(i => i.EvaluatedInclude == "X").First();
  240. Assert.Equal("A;B", item1.GetMetadataValue("M")); // "Invalid metadata after second removal"
  241. item2 = project.GetItems("I").Where(i => i.EvaluatedInclude == "Y").First();
  242. Assert.Equal("A", item2.GetMetadataValue("M")); // "Invalid metadata after second removal"
  243. }
  244. /// <summary>
  245. /// Removes metadata referenced with % qualification.
  246. /// </summary>
  247. [Fact]
  248. public void RemoveReferencedMetadata()
  249. {
  250. Project project = GetProject(
  251. @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
  252. <ItemGroup>
  253. <I Include=""i"">
  254. <M>m</M>
  255. <N>%(I.M)</N>
  256. </I>
  257. </ItemGroup>
  258. </Project>");
  259. ProjectItem item = project.GetItems("I").First();
  260. Assert.Equal("m", item.GetMetadataValue("N")); // "Wrong metadata value at startup"
  261. item.RemoveMetadata("M");
  262. string expected =
  263. @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
  264. <ItemGroup>
  265. <I Include=""i"">
  266. <N>%(I.M)</N>
  267. </I>
  268. </ItemGroup>
  269. </Project>";
  270. Helpers.VerifyAssertProjectContent(expected, project.Xml, false);
  271. project.ReevaluateIfNecessary();
  272. item = project.GetItems("I").First();
  273. ProjectMetadata metadata = item.GetMetadata("N");
  274. Assert.Equal("%(I.M)", metadata.UnevaluatedValue); // "Unevaluated value is wrong"
  275. Assert.Equal(String.Empty, metadata.EvaluatedValue); // "Evaluated value is wrong"
  276. }
  277. /// <summary>
  278. /// Removes duplicated property.
  279. /// </summary>
  280. [Fact]
  281. public void RemoveProperty()
  282. {
  283. Project project = GetProject(
  284. @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
  285. <PropertyGroup>
  286. <P>A</P>
  287. <P>$(P)B</P>
  288. </PropertyGroup>
  289. </Project>");
  290. ProjectProperty property = project.GetProperty("P");
  291. project.RemoveProperty(property);
  292. string expected =
  293. @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
  294. <PropertyGroup>
  295. <P>A</P>
  296. </PropertyGroup>
  297. </Project>";
  298. Helpers.VerifyAssertProjectContent(expected, project.Xml, false);
  299. }
  300. /// <summary>
  301. /// Creates a new project the given contents.
  302. /// </summary>
  303. /// <param name="contents">The contents for the project.</param>
  304. /// <returns>The project contents.</returns>
  305. private Project GetProject(string contents)
  306. {
  307. return new Project(XmlReader.Create(new StringReader(contents)));
  308. }
  309. }
  310. }