PageRenderTime 57ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Main/Core/Test/AddInTreeTests/AddInTreeLoadingTests.cs

http://github.com/icsharpcode/SharpDevelop
C# | 333 lines | 282 code | 30 blank | 21 comment | 0 complexity | 73b587d7ce3bd69e0799e1074d352eaf MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, CPL-1.0, LGPL-2.1
  1. // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.Collections.Generic;
  20. using System.IO;
  21. using System.Linq;
  22. using NUnit.Framework;
  23. using Rhino.Mocks;
  24. namespace ICSharpCode.Core.Tests.AddInTreeTests.Tests
  25. {
  26. [TestFixture]
  27. public class AddInTreeLoadingTests
  28. {
  29. IAddInTree addInTree = MockRepository.GenerateStrictMock<IAddInTree>();
  30. #region AddIn node tests
  31. [Test]
  32. public void TestEmptyAddInTreeLoading()
  33. {
  34. string addInText = @"<AddIn/>";
  35. AddIn addIn = AddIn.Load(addInTree, new StringReader(addInText));
  36. }
  37. [Test]
  38. public void TestAddInProperties()
  39. {
  40. string addInText = @"
  41. <AddIn name = 'SharpDevelop Core'
  42. author = 'Mike Krueger'
  43. copyright = 'GPL'
  44. url = 'http://www.icsharpcode.net'
  45. description = 'SharpDevelop core module'
  46. version = '1.0.0'/>";
  47. AddIn addIn = AddIn.Load(addInTree, new StringReader(addInText));
  48. Assert.AreEqual(addIn.Properties["name"], "SharpDevelop Core");
  49. Assert.AreEqual(addIn.Properties["author"], "Mike Krueger");
  50. Assert.AreEqual(addIn.Properties["copyright"], "GPL");
  51. Assert.AreEqual(addIn.Properties["url"], "http://www.icsharpcode.net");
  52. Assert.AreEqual(addIn.Properties["description"], "SharpDevelop core module");
  53. Assert.AreEqual(addIn.Properties["version"], "1.0.0");
  54. }
  55. #endregion
  56. #region Runtime section tests
  57. [Test]
  58. public void TestEmtpyRuntimeSection()
  59. {
  60. string addInText = @"<AddIn><Runtime/></AddIn>";
  61. AddIn addIn = AddIn.Load(addInTree, new StringReader(addInText));
  62. }
  63. [Test]
  64. public void TestEmtpyRuntimeSection2()
  65. {
  66. string addInText = @"<AddIn> <!-- Comment1 --> <Runtime> <!-- Comment2 --> </Runtime> <!-- Comment3 --> </AddIn>";
  67. AddIn addIn = AddIn.Load(addInTree, new StringReader(addInText));
  68. }
  69. [Test]
  70. public void TestRuntimeSectionImport()
  71. {
  72. string addInText = @"
  73. <AddIn>
  74. <Runtime>
  75. <Import assembly = 'Test.dll'/>
  76. </Runtime>
  77. </AddIn>";
  78. AddIn addIn = AddIn.Load(addInTree, new StringReader(addInText));
  79. Assert.AreEqual(1, addIn.Runtimes.Count);
  80. Assert.AreEqual(addIn.Runtimes[0].Assembly, "Test.dll");
  81. }
  82. [Test]
  83. public void TestRuntimeSectionComplexImport()
  84. {
  85. string addInText = @"
  86. <AddIn>
  87. <Runtime>
  88. <Import assembly = '../bin/SharpDevelop.Base.dll'>
  89. <Doozer name='MyDoozer' class = 'ICSharpCode.Core.ClassDoozer'/>
  90. <ConditionEvaluator name='MyCompare' class = 'ICSharpCode.Core.CompareCondition'/>
  91. <Doozer name='Test' class = 'ICSharpCode.Core.ClassDoozer2'/>
  92. <ConditionEvaluator name='Condition2' class = 'Condition2Class'/>
  93. </Import>
  94. </Runtime>
  95. </AddIn>";
  96. AddIn addIn = AddIn.Load(addInTree, new StringReader(addInText));
  97. Assert.AreEqual(1, addIn.Runtimes.Count);
  98. Assert.AreEqual(addIn.Runtimes[0].Assembly, "../bin/SharpDevelop.Base.dll");
  99. Assert.AreEqual(addIn.Runtimes[0].DefinedDoozers.Count(), 2);
  100. Assert.AreEqual(addIn.Runtimes[0].DefinedDoozers.ElementAt(0).Key, "MyDoozer");
  101. Assert.AreEqual(addIn.Runtimes[0].DefinedDoozers.ElementAt(1).Key, "Test");
  102. Assert.AreEqual(addIn.Runtimes[0].DefinedConditionEvaluators.Count(), 2);
  103. Assert.AreEqual(addIn.Runtimes[0].DefinedConditionEvaluators.ElementAt(0).Key, "MyCompare");
  104. Assert.AreEqual(addIn.Runtimes[0].DefinedConditionEvaluators.ElementAt(1).Key, "Condition2");
  105. }
  106. #endregion
  107. #region Path section tests
  108. [Test]
  109. public void TestEmptyPathSection()
  110. {
  111. string addInText = @"
  112. <AddIn>
  113. <Path name = '/Path1'/>
  114. <Path name = '/Path2'/>
  115. <Path name = '/Path1/SubPath'/>
  116. </AddIn>";
  117. AddIn addIn = AddIn.Load(addInTree, new StringReader(addInText));
  118. Assert.AreEqual(3, addIn.Paths.Count);
  119. Assert.IsNotNull(addIn.Paths["/Path1"]);
  120. Assert.IsNotNull(addIn.Paths["/Path2"]);
  121. Assert.IsNotNull(addIn.Paths["/Path1/SubPath"]);
  122. }
  123. [Test]
  124. public void TestSimpleCodon()
  125. {
  126. string addInText = @"
  127. <AddIn>
  128. <Path name = '/Path1'>
  129. <Simple id ='Simple' attr='a' attr2='b'/>
  130. </Path>
  131. </AddIn>";
  132. AddIn addIn = AddIn.Load(addInTree, new StringReader(addInText));
  133. Assert.AreEqual(1, addIn.Paths.Count);
  134. Assert.IsNotNull(addIn.Paths["/Path1"]);
  135. List<Codon> codons = addIn.Paths["/Path1"].Codons.ToList();
  136. Assert.AreEqual(1, codons.Count);
  137. Assert.AreEqual("Simple", codons[0].Name);
  138. Assert.AreEqual("Simple", codons[0].Id);
  139. Assert.AreEqual("a", codons[0].Properties["attr"]);
  140. Assert.AreEqual("b", codons[0].Properties["attr2"]);
  141. }
  142. [Test]
  143. public void TestSubCodons()
  144. {
  145. string addInText = @"
  146. <AddIn>
  147. <Path name = '/Path1'>
  148. <Sub id='Path2'>
  149. <Codon2 id='Sub2'/>
  150. </Sub>
  151. </Path>
  152. </AddIn>";
  153. AddIn addIn = AddIn.Load(addInTree, new StringReader(addInText));
  154. Assert.AreEqual(2, addIn.Paths.Count);
  155. Assert.IsNotNull(addIn.Paths["/Path1"]);
  156. List<Codon> codons1 = addIn.Paths["/Path1"].Codons.ToList();
  157. Assert.AreEqual(1, codons1.Count);
  158. Assert.AreEqual("Sub", codons1[0].Name);
  159. Assert.AreEqual("Path2", codons1[0].Id);
  160. Assert.IsNotNull(addIn.Paths["/Path1/Path2"]);
  161. List<Codon> codons2 = addIn.Paths["/Path1/Path2"].Codons.ToList();
  162. Assert.AreEqual(1, codons2.Count);
  163. Assert.AreEqual("Codon2", codons2[0].Name);
  164. Assert.AreEqual("Sub2", codons2[0].Id);
  165. }
  166. [Test]
  167. public void TestSubCodonsWithCondition()
  168. {
  169. string addInText = @"
  170. <AddIn>
  171. <Path name = '/Path1'>
  172. <Condition name='Equal' string='a' equal='b'>
  173. <Sub id='Path2'>
  174. <Codon2 id='Sub2'/>
  175. </Sub>
  176. </Condition>
  177. </Path>
  178. </AddIn>";
  179. AddIn addIn = AddIn.Load(addInTree, new StringReader(addInText));
  180. Assert.AreEqual(2, addIn.Paths.Count);
  181. Assert.IsNotNull(addIn.Paths["/Path1"]);
  182. List<Codon> codons1 = addIn.Paths["/Path1"].Codons.ToList();
  183. Assert.AreEqual(1, codons1.Count);
  184. Assert.AreEqual("Sub", codons1[0].Name);
  185. Assert.AreEqual("Path2", codons1[0].Id);
  186. Assert.AreEqual(1, codons1[0].Conditions.Count);
  187. Assert.IsNotNull(addIn.Paths["/Path1/Path2"]);
  188. List<Codon> codons2 = addIn.Paths["/Path1/Path2"].Codons.ToList();
  189. Assert.AreEqual(1, codons2.Count);
  190. Assert.AreEqual("Codon2", codons2[0].Name);
  191. Assert.AreEqual("Sub2", codons2[0].Id);
  192. // condition is not inherited lexically
  193. Assert.AreEqual(0, codons2[0].Conditions.Count);
  194. }
  195. [Test]
  196. public void TestSimpleCondition()
  197. {
  198. string addInText = @"
  199. <AddIn>
  200. <Path name = '/Path1'>
  201. <Condition name='Equal' string='a' equal='b'>
  202. <Simple id ='Simple' attr='a' attr2='b'/>
  203. </Condition>
  204. </Path>
  205. </AddIn>";
  206. AddIn addIn = AddIn.Load(addInTree, new StringReader(addInText));
  207. Assert.AreEqual(1, addIn.Paths.Count, "Paths != 1");
  208. ExtensionPath path = addIn.Paths["/Path1"];
  209. Assert.IsNotNull(path);
  210. Codon codon = path.Codons.Single();
  211. Assert.AreEqual("Simple", codon.Name);
  212. Assert.AreEqual("Simple", codon.Id);
  213. Assert.AreEqual("a", codon["attr"]);
  214. Assert.AreEqual("b", codon["attr2"]);
  215. // Test for condition.
  216. Assert.AreEqual(1, codon.Conditions.Count);
  217. Condition condition = codon.Conditions[0] as Condition;
  218. Assert.IsNotNull(condition);
  219. Assert.AreEqual("Equal", condition.Name);
  220. Assert.AreEqual("a", condition["string"]);
  221. Assert.AreEqual("b", condition["equal"]);
  222. }
  223. [Test]
  224. public void TestStackedCondition()
  225. {
  226. string addInText = @"
  227. <AddIn>
  228. <Path name = '/Path1'>
  229. <Condition name='Equal' string='a' equal='b'>
  230. <Condition name='StackedCondition' string='1' equal='2'>
  231. <Simple id ='Simple' attr='a' attr2='b'/>
  232. </Condition>
  233. <Simple id ='Simple2' attr='a' attr2='b'/>
  234. </Condition>
  235. <Simple id ='Simple3' attr='a' attr2='b'/>
  236. </Path>
  237. </AddIn>";
  238. AddIn addIn = AddIn.Load(addInTree, new StringReader(addInText));
  239. Assert.AreEqual(1, addIn.Paths.Count);
  240. ExtensionPath path = addIn.Paths["/Path1"];
  241. Assert.IsNotNull(path);
  242. Assert.AreEqual(3, path.Codons.Count());
  243. Codon codon = path.Codons.ElementAt(0);
  244. Assert.AreEqual("Simple", codon.Name);
  245. Assert.AreEqual("Simple", codon.Id);
  246. Assert.AreEqual("a", codon["attr"]);
  247. Assert.AreEqual("b", codon["attr2"]);
  248. // Test for condition
  249. Assert.AreEqual(2, codon.Conditions.Count);
  250. Condition condition = codon.Conditions[1] as Condition;
  251. Assert.IsNotNull(condition);
  252. Assert.AreEqual("Equal", condition.Name);
  253. Assert.AreEqual("a", condition["string"]);
  254. Assert.AreEqual("b", condition["equal"]);
  255. condition = codon.Conditions[0] as Condition;
  256. Assert.IsNotNull(condition);
  257. Assert.AreEqual("StackedCondition", condition.Name);
  258. Assert.AreEqual("1", condition["string"]);
  259. Assert.AreEqual("2", condition["equal"]);
  260. codon = path.Codons.ElementAt(1);
  261. Assert.AreEqual(1, codon.Conditions.Count);
  262. condition = codon.Conditions[0] as Condition;
  263. Assert.IsNotNull(condition);
  264. Assert.AreEqual("Equal", condition.Name);
  265. Assert.AreEqual("a", condition["string"]);
  266. Assert.AreEqual("b", condition["equal"]);
  267. codon = path.Codons.ElementAt(2);
  268. Assert.AreEqual(0, codon.Conditions.Count);
  269. }
  270. [Test]
  271. public void TestComplexCondition()
  272. {
  273. string addInText = @"
  274. <AddIn>
  275. <Path name = '/Path1'>
  276. <ComplexCondition>
  277. <And>
  278. <Not><Condition name='Equal' string='a' equal='b'/></Not>
  279. <Or>
  280. <Condition name='Equal' string='a' equal='b'/>
  281. <Condition name='Equal' string='a' equal='b'/>
  282. <Condition name='Equal' string='a' equal='b'/>
  283. </Or>
  284. </And>
  285. <Simple id ='Simple' attr='a' attr2='b'/>
  286. </ComplexCondition>
  287. </Path>
  288. </AddIn>";
  289. AddIn addIn = AddIn.Load(addInTree, new StringReader(addInText));
  290. Assert.AreEqual(1, addIn.Paths.Count);
  291. ExtensionPath path = addIn.Paths["/Path1"];
  292. Assert.IsNotNull(path);
  293. Codon codon = path.Codons.Single();
  294. Assert.AreEqual("Simple", codon.Name);
  295. Assert.AreEqual("Simple", codon.Id);
  296. Assert.AreEqual("a", codon["attr"]);
  297. Assert.AreEqual("b", codon["attr2"]);
  298. // Test for condition.
  299. Assert.AreEqual(1, codon.Conditions.Count);
  300. }
  301. #endregion
  302. }
  303. }