PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/DevCode/MoqaLate.Tests/Unit/ClassTextBuilderTests.cs

http://moqalate.codeplex.com
C# | 637 lines | 527 code | 110 blank | 0 comment | 0 complexity | d1eaa9ee16ef021dabee3cd8f7d04eae MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. using AutoMoq;
  2. using FluentAssertions;
  3. using MoqaLate.CodeModel;
  4. using MoqaLate.ExtensionMethods;
  5. using MoqaLate.MockClassBuilding;
  6. using NUnit.Framework;
  7. namespace MoqaLate.Tests.Unit
  8. {
  9. [TestFixture]
  10. public class ClassTextBuilderTests
  11. {
  12. private IClassTextBuilder _sut;
  13. [SetUp]
  14. public void Setup()
  15. {
  16. var mocker = new AutoMoqer();
  17. _sut = mocker.Resolve<ClassTextBuilder>();
  18. }
  19. [Test]
  20. public void ShouldCreateGenericInterfaceTypes()
  21. {
  22. var spec = new ClassSpecification
  23. {
  24. ClassName = "AwesomeClass",
  25. InterfaceGenericTypes = "<T,K>",
  26. OriginalInterfaceName = "IAwesomeClass",
  27. };
  28. const string expectedClassText =
  29. @"namespace MoqaLate.Autogenerated
  30. {
  31. public partial class AwesomeClass<T,K> : IAwesomeClass<T,K>
  32. {
  33. }
  34. }";
  35. var actualClassText = _sut.Create(spec);
  36. actualClassText.CanonicalString().Should().Be(expectedClassText.CanonicalString());
  37. }
  38. [Test]
  39. public void ShouldCreateEvents()
  40. {
  41. var spec = new ClassSpecification
  42. {
  43. ClassName = "AwesomeClass",
  44. OriginalInterfaceName = "IAwesomeClass",
  45. Events =
  46. {
  47. new Event {Type = "EventHandler", Name = "E1"},
  48. new Event {Type = "Action", Name = "E2"},
  49. new Event {Type = "Action<int>", Name = "E3"},
  50. new Event {Type = "Action<int, string>", Name = "E4"},
  51. new Event {Type = " EventHandler<AssemblyLoadEventArgs>", Name = "E5"}
  52. }
  53. };
  54. const string expectedClassText =
  55. @"namespace MoqaLate.Autogenerated
  56. {
  57. public partial class AwesomeClass : IAwesomeClass
  58. {
  59. public virtual event EventHandler E1;
  60. public virtual event Action E2;
  61. public virtual event Action<int> E3;
  62. public virtual event Action<int, string> E4;
  63. public virtual event EventHandler<AssemblyLoadEventArgs> E5;
  64. }
  65. }";
  66. var actualClassText = _sut.Create(spec);
  67. actualClassText.CanonicalString().Should().Be(expectedClassText.CanonicalString());
  68. }
  69. [Test]
  70. public void ShouldCreateMethods1()
  71. {
  72. var spec = new ClassSpecification
  73. {
  74. ClassName = "AwesomeClass",
  75. OriginalInterfaceName = "IAwesomeClass",
  76. Methods =
  77. {
  78. new Method
  79. {
  80. ReturnType = "string",
  81. Name = "DoStuff1",
  82. Parameters = new MethodParameterList{ new MethodParameter{Type = "int", Name = "p1"},
  83. new MethodParameter{Type = "List<int>", Name="p2"}}
  84. }
  85. }
  86. };
  87. const string expectedClassText =
  88. @"namespace MoqaLate.Autogenerated
  89. {
  90. public partial class AwesomeClass : IAwesomeClass
  91. {
  92. // -------------- DoStuff1 ------------
  93. private string _doStuff1ReturnValue;
  94. private int _doStuff1NumberOfTimesCalled;
  95. public int DoStuff1Parameter_p1_LastCalledWith;
  96. public List<int> DoStuff1Parameter_p2_LastCalledWith;
  97. public virtual void DoStuff1SetReturnValue(string value)
  98. {
  99. _doStuff1ReturnValue = value;
  100. }
  101. public virtual bool DoStuff1WasCalled()
  102. {
  103. return _doStuff1NumberOfTimesCalled > 0;
  104. }
  105. public virtual bool DoStuff1WasCalled(int times)
  106. {
  107. return _doStuff1NumberOfTimesCalled == times;
  108. }
  109. public virtual int DoStuff1TimesCalled()
  110. {
  111. return _doStuff1NumberOfTimesCalled;
  112. }
  113. public virtual bool DoStuff1WasCalledWith(int p1, List<int> p2)
  114. {
  115. return (
  116. p1.Equals(DoStuff1Parameter_p1_LastCalledWith) &&
  117. p2.Equals(DoStuff1Parameter_p2_LastCalledWith)
  118. );
  119. }
  120. public string DoStuff1(int p1, List<int> p2)
  121. {
  122. _doStuff1NumberOfTimesCalled++;
  123. DoStuff1Parameter_p1_LastCalledWith = p1;
  124. DoStuff1Parameter_p2_LastCalledWith = p2;
  125. return _doStuff1ReturnValue;
  126. }
  127. }
  128. }";
  129. var actualClassText = _sut.Create(spec);
  130. actualClassText.CanonicalString().Should().Be(expectedClassText.CanonicalString());
  131. }
  132. [Test]
  133. public void ShouldCreateMethods2()
  134. {
  135. var spec = new ClassSpecification
  136. {
  137. ClassName = "AwesomeClass",
  138. OriginalInterfaceName = "IAwesomeClass",
  139. Methods =
  140. {
  141. new Method {ReturnType = "Func<bool, int>", Name = "DoStuff2"}
  142. }
  143. };
  144. const string expectedClassText =
  145. @"namespace MoqaLate.Autogenerated
  146. {
  147. public partial class AwesomeClass : IAwesomeClass
  148. {
  149. // -------------- DoStuff2 ------------
  150. private Func<bool, int> _doStuff2ReturnValue;
  151. private int _doStuff2NumberOfTimesCalled;
  152. public virtual void DoStuff2SetReturnValue(Func<bool, int> value)
  153. {
  154. _doStuff2ReturnValue = value;
  155. }
  156. public virtual bool DoStuff2WasCalled()
  157. {
  158. return _doStuff2NumberOfTimesCalled > 0;
  159. }
  160. public virtual bool DoStuff2WasCalled(int times)
  161. {
  162. return _doStuff2NumberOfTimesCalled == times;
  163. }
  164. public virtual int DoStuff2TimesCalled()
  165. {
  166. return _doStuff2NumberOfTimesCalled;
  167. }
  168. public Func<bool, int> DoStuff2()
  169. {
  170. _doStuff2NumberOfTimesCalled++;
  171. return _doStuff2ReturnValue;
  172. }
  173. }
  174. }";
  175. var actualClassText = _sut.Create(spec);
  176. actualClassText.CanonicalString().Should().Be(expectedClassText.CanonicalString());
  177. }
  178. [Test]
  179. public void ShouldCreateMethods3()
  180. {
  181. var spec = new ClassSpecification
  182. {
  183. ClassName = "AwesomeClass",
  184. OriginalInterfaceName = "IAwesomeClass",
  185. Methods =
  186. {
  187. new Method {ReturnType = "void", Name = "DoStuff3"}
  188. }
  189. };
  190. const string expectedClassText =
  191. @"namespace MoqaLate.Autogenerated
  192. {
  193. public partial class AwesomeClass : IAwesomeClass
  194. {
  195. // -------------- DoStuff3 ------------
  196. private int _doStuff3NumberOfTimesCalled;
  197. public virtual bool DoStuff3WasCalled()
  198. {
  199. return _doStuff3NumberOfTimesCalled > 0;
  200. }
  201. public virtual bool DoStuff3WasCalled(int times)
  202. {
  203. return _doStuff3NumberOfTimesCalled == times;
  204. }
  205. public virtual int DoStuff3TimesCalled()
  206. {
  207. return _doStuff3NumberOfTimesCalled;
  208. }
  209. public void DoStuff3()
  210. {
  211. _doStuff3NumberOfTimesCalled++;
  212. }
  213. }
  214. }";
  215. var actualClassText = _sut.Create(spec);
  216. actualClassText.CanonicalString().Should().Be(expectedClassText.CanonicalString());
  217. }
  218. [Test]
  219. public void ShouldCreateMethods4()
  220. {
  221. var spec = new ClassSpecification
  222. {
  223. ClassName = "AwesomeClass",
  224. OriginalInterfaceName = "IAwesomeClass",
  225. Methods =
  226. {
  227. new Method
  228. {
  229. ReturnType = "void",
  230. Name = "LetsDoIt",
  231. Parameters = new MethodParameterList{ new MethodParameter{Type = "Func<bool, int>", Name = "p1"},
  232. new MethodParameter{Type = "string", Name="p2"}}
  233. }
  234. }
  235. };
  236. const string expectedClassText =
  237. @"namespace MoqaLate.Autogenerated
  238. {
  239. public partial class AwesomeClass : IAwesomeClass
  240. {
  241. // -------------- LetsDoIt ------------
  242. private int _letsDoItNumberOfTimesCalled;
  243. public Func<bool, int> LetsDoItParameter_p1_LastCalledWith;
  244. public string LetsDoItParameter_p2_LastCalledWith;
  245. public virtual bool LetsDoItWasCalled()
  246. {
  247. return _letsDoItNumberOfTimesCalled > 0;
  248. }
  249. public virtual bool LetsDoItWasCalled(int times)
  250. {
  251. return _letsDoItNumberOfTimesCalled == times;
  252. }
  253. public virtual int LetsDoItTimesCalled()
  254. {
  255. return _letsDoItNumberOfTimesCalled;
  256. }
  257. public virtual bool LetsDoItWasCalledWith(Func<bool, int> p1, string p2)
  258. {
  259. return (
  260. p1.Equals(LetsDoItParameter_p1_LastCalledWith) &&
  261. p2.Equals(LetsDoItParameter_p2_LastCalledWith)
  262. );
  263. }
  264. public void LetsDoIt(Func<bool, int> p1, string p2)
  265. {
  266. _letsDoItNumberOfTimesCalled++;
  267. LetsDoItParameter_p1_LastCalledWith = p1;
  268. LetsDoItParameter_p2_LastCalledWith = p2;
  269. }
  270. }
  271. }";
  272. var actualClassText = _sut.Create(spec);
  273. actualClassText.CanonicalString().Should().Be(expectedClassText.CanonicalString());
  274. }
  275. [Test]
  276. public void ShouldCreateGenericMethods()
  277. {
  278. var spec = new ClassSpecification
  279. {
  280. ClassName = "AwesomeClass",
  281. InterfaceGenericTypes = "<T,K>",
  282. OriginalInterfaceName = "IAwesomeClass",
  283. Methods =
  284. {
  285. new Method{Name="M1", ReturnType="T", Parameters = new MethodParameterList{new MethodParameter{Type="K", Name="input"}}}
  286. }
  287. };
  288. const string expectedClassText =
  289. @"namespace MoqaLate.Autogenerated
  290. {
  291. public partial class AwesomeClass<T,K> : IAwesomeClass<T,K>
  292. {
  293. // -------------- M1 ------------
  294. private T _m1ReturnValue;
  295. private int _m1NumberOfTimesCalled;
  296. public K M1Parameter_input_LastCalledWith;
  297. public virtual void M1SetReturnValue(T value)
  298. {
  299. _m1ReturnValue = value;
  300. }
  301. public virtual bool M1WasCalled()
  302. {
  303. return _m1NumberOfTimesCalled > 0;
  304. }
  305. public virtual bool M1WasCalled(int times)
  306. {
  307. return _m1NumberOfTimesCalled == times;
  308. }
  309. public virtual int M1TimesCalled()
  310. {
  311. return _m1NumberOfTimesCalled;
  312. }
  313. public virtual bool M1WasCalledWith(K input){
  314. return (
  315. input.Equals(M1Parameter_input_LastCalledWith) );
  316. }
  317. public T M1(K input)
  318. {
  319. _m1NumberOfTimesCalled++;
  320. M1Parameter_input_LastCalledWith = input;
  321. return _m1ReturnValue;
  322. }}
  323. }
  324. ";
  325. var actualClassText = _sut.Create(spec);
  326. actualClassText.CanonicalString().Should().Be(expectedClassText.CanonicalString());
  327. }
  328. [Test]
  329. public void ShouldCreateNamespacedClass()
  330. {
  331. var spec = new ClassSpecification
  332. {
  333. ClassName = "AwesomeClass",
  334. OriginalInterfaceName = "IAwesomeClass"
  335. };
  336. const string expectedClassText =
  337. @"namespace MoqaLate.Autogenerated { public partial class AwesomeClass : IAwesomeClass {} }";
  338. var actualClassText = _sut.Create(spec);
  339. actualClassText.CanonicalString().Should().Be(expectedClassText.CanonicalString());
  340. }
  341. [Test]
  342. public void ShouldCreateNamespacesWithUsings()
  343. {
  344. var spec = new ClassSpecification
  345. {
  346. ClassName = "AwesomeClass",
  347. OriginalInterfaceName = "IAwesomeClass",
  348. OriginalInterfaceNamespace = "My.Awesome.Namespace",
  349. Usings = {"FluentAssertions", "MoqaLate.Extensions", "NUnit.Framework"}
  350. };
  351. const string expectedClassText =
  352. @"
  353. using FluentAssertions;
  354. using MoqaLate.Extensions;
  355. using NUnit.Framework;
  356. using My.Awesome.Namespace;
  357. namespace MoqaLate.Autogenerated
  358. {
  359. public partial class AwesomeClass : IAwesomeClass
  360. {
  361. }
  362. }";
  363. var actualClassText = _sut.Create(spec);
  364. actualClassText.CanonicalString().Should().Be(expectedClassText.CanonicalString());
  365. }
  366. [Test]
  367. public void ShouldWorkOkWhenOverloadedMethodsInInterfaceWithSameMethodNameButDiffReturnAndOrMethodSignature()
  368. {
  369. Assert.Inconclusive();
  370. }
  371. [Test]
  372. public void ShouldCreateProperties()
  373. {
  374. var spec = new ClassSpecification
  375. {
  376. ClassName = "AwesomeClass",
  377. OriginalInterfaceName = "IAwesomeClass",
  378. Properties =
  379. {
  380. new Property
  381. {Accessor = PropertyAccessor.GetAndSet, Name = "GandS", Type = "string"},
  382. new Property {Accessor = PropertyAccessor.GetOny, Name = "G", Type = "List<int>"},
  383. new Property {Accessor = PropertyAccessor.SetOny, Name = "S", Type = "float"}
  384. }
  385. };
  386. const string expectedClassText =
  387. @"namespace MoqaLate.Autogenerated
  388. {
  389. public partial class AwesomeClass : IAwesomeClass
  390. {
  391. // ------------ Property GandS
  392. private string _GandS;
  393. public virtual string GandS
  394. {
  395. get
  396. {
  397. return _GandS;
  398. }
  399. set
  400. {
  401. _GandS = value;
  402. }
  403. }
  404. public virtual void __SetGandS(string val)
  405. {
  406. _GandS = val;
  407. }
  408. // ------------ Property G
  409. private List<int> _G;
  410. public virtual List<int> G
  411. {
  412. get
  413. {
  414. return _G;
  415. }
  416. }
  417. public virtual void __SetG(List<int> val)
  418. {
  419. _G = val;
  420. }
  421. // ------------ Property S
  422. private float _S;
  423. public virtual float S
  424. {
  425. set
  426. {
  427. _S = value;
  428. }
  429. }
  430. public virtual void __SetS(float val)
  431. {
  432. _S = val;
  433. }
  434. }
  435. }";
  436. var actualClassText = _sut.Create(spec);
  437. actualClassText.CanonicalString().Should().Be(expectedClassText.CanonicalString());
  438. }
  439. }
  440. }