PageRenderTime 95ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/v3/src/MbUnit/MbUnit.Tests/Framework/AssertTest_Collections.cs

http://mb-unit.googlecode.com/
C# | 550 lines | 464 code | 72 blank | 14 comment | 11 complexity | 8aae20e459493e4e66da6bc4d281607b MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, 0BSD, IPL-1.0, LGPL-2.1, MIT, CC-BY-SA-3.0, GPL-2.0, CPL-1.0, GPL-3.0
  1. // Copyright 2005-2010 Gallio Project - http://www.gallio.org/
  2. // Portions Copyright 2000-2004 Jonathan de Halleux
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. using System;
  16. using System.Collections;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using System.Text;
  20. using Gallio.Common.Collections;
  21. using Gallio.Framework.Assertions;
  22. using MbUnit.Framework;
  23. namespace MbUnit.Tests.Framework
  24. {
  25. [TestsOn(typeof(Assert))]
  26. public class AssertTest_Collections : BaseAssertTest
  27. {
  28. #region AreElementsEqual
  29. [Test]
  30. public void AreElementsEqual_with_strings()
  31. {
  32. Assert.AreElementsEqual("12", "12");
  33. }
  34. [Test]
  35. public void AreElementsEqual_with_different_types()
  36. {
  37. Assert.AreElementsEqual(new[] { 1, 2 }, new List<int> { 1, 2 });
  38. }
  39. [Test]
  40. public void AreElementsEqual_with_custom_comparer()
  41. {
  42. Assert.AreElementsEqual("12", "34", (expected, actual) => expected + 2 == actual);
  43. }
  44. [Test]
  45. public void AreElementsEqual_fails_when_elements_are_in_different_order()
  46. {
  47. AssertionFailure[] failures = Capture(()
  48. => Assert.AreElementsEqual(new[] { 1, 2 }, new List<int> { 2, 1 }));
  49. Assert.Count(1, failures);
  50. Assert.AreEqual("Expected elements to be equal but they differ in at least one position.", failures[0].Description);
  51. Assert.AreEqual("Expected Sequence", failures[0].LabeledValues[0].Label);
  52. Assert.AreEqual("[1, 2]", failures[0].LabeledValues[0].FormattedValue.ToString());
  53. Assert.AreEqual("Actual Sequence", failures[0].LabeledValues[1].Label);
  54. Assert.AreEqual("[2, 1]", failures[0].LabeledValues[1].FormattedValue.ToString());
  55. Assert.AreEqual("Element Index", failures[0].LabeledValues[2].Label);
  56. Assert.AreEqual("0", failures[0].LabeledValues[2].FormattedValue.ToString());
  57. Assert.AreEqual("Expected Element", failures[0].LabeledValues[3].Label);
  58. Assert.AreEqual("1", failures[0].LabeledValues[3].FormattedValue.ToString());
  59. Assert.AreEqual("Actual Element", failures[0].LabeledValues[4].Label);
  60. Assert.AreEqual("2", failures[0].LabeledValues[4].FormattedValue.ToString());
  61. }
  62. [Test]
  63. public void AreElementsEqual_fails_with_custom_message()
  64. {
  65. AssertionFailure[] failures = Capture(() => Assert.AreElementsEqual("1", "2", "{0} message", "custom"));
  66. Assert.Count(1, failures);
  67. Assert.AreEqual("custom message", failures[0].Message);
  68. }
  69. #endregion
  70. #region AreElementsNotEqual
  71. [Test]
  72. public void AreElementsNotEqual_with_strings()
  73. {
  74. Assert.AreElementsNotEqual("12", "1");
  75. }
  76. [Test]
  77. public void AreElementsNotEqual_with_different_types()
  78. {
  79. Assert.AreElementsNotEqual(new[] { 1, 2 }, new List<int> { 1, 3 });
  80. }
  81. [Test]
  82. public void AreElementsNotEqual_with_custom_comparer()
  83. {
  84. Assert.AreElementsNotEqual("12", "12", (expected, actual) => expected + 2 == actual);
  85. }
  86. [Test]
  87. public void AreElementsNotEqual_fails_when_elements_are_in_different_order()
  88. {
  89. AssertionFailure[] failures = Capture(()
  90. => Assert.AreElementsNotEqual(new[] { 1, 2 }, new List<int> { 1, 2 }));
  91. Assert.Count(1, failures);
  92. Assert.AreEqual("Expected the unexpected and actual sequence to have different elements but all elements were equal.", failures[0].Description);
  93. Assert.AreEqual("Unexpected Sequence", failures[0].LabeledValues[0].Label);
  94. Assert.AreEqual("[1, 2]", failures[0].LabeledValues[0].FormattedValue.ToString());
  95. Assert.AreEqual("Actual Sequence", failures[0].LabeledValues[1].Label);
  96. Assert.AreEqual("[1, 2]", failures[0].LabeledValues[1].FormattedValue.ToString());
  97. }
  98. [Test]
  99. public void AreElementsNotEqual_fails_with_custom_message()
  100. {
  101. AssertionFailure[] failures = Capture(() => Assert.AreElementsNotEqual("2", "2", "{0} message", "custom"));
  102. Assert.Count(1, failures);
  103. Assert.AreEqual("custom message", failures[0].Message);
  104. }
  105. #endregion
  106. #region AreElementsEqualIgnoringOrder
  107. [Test]
  108. public void AreElementsEqualIgnoringOrder_with_strings()
  109. {
  110. Assert.AreElementsEqualIgnoringOrder("122", "212");
  111. }
  112. [Test]
  113. public void AreElementsEqualIgnoringOrder_with_different_types()
  114. {
  115. Assert.AreElementsEqualIgnoringOrder(new[] { 2, 2, 3, 1 }, new List<int> { 1, 3, 2, 2 });
  116. }
  117. [Test]
  118. public void AreElementsEqualIgnoringOrder_with_custom_comparer()
  119. {
  120. Assert.AreElementsEqualIgnoringOrder("12", "43", (expected, actual) => expected + 2 == actual);
  121. }
  122. [Test]
  123. public void AreElementsEqualIgnoringOrder_fails_when_excess_or_missing_elements()
  124. {
  125. AssertionFailure[] failures = Capture(()
  126. => Assert.AreElementsEqualIgnoringOrder(new[] { 1, 2, 3, 2, 3, 1 }, new List<int> { 4, 2, 1, 1, 4, 1, 4 }));
  127. Assert.Count(1, failures);
  128. Assert.AreEqual("Expected elements to be equal but possibly in a different order.", failures[0].Description);
  129. Assert.AreEqual("Equal Elements", failures[0].LabeledValues[0].Label);
  130. Assert.AreEqual("[1, 1, 2]", failures[0].LabeledValues[0].FormattedValue.ToString());
  131. Assert.AreEqual("Excess Elements", failures[0].LabeledValues[1].Label);
  132. Assert.AreEqual("[1, 4, 4, 4]", failures[0].LabeledValues[1].FormattedValue.ToString());
  133. Assert.AreEqual("Missing Elements", failures[0].LabeledValues[2].Label);
  134. Assert.AreEqual("[2, 3, 3]", failures[0].LabeledValues[2].FormattedValue.ToString());
  135. }
  136. [Test]
  137. public void AreElementsEqualIgnoringOrder_fails_with_custom_message()
  138. {
  139. AssertionFailure[] failures = Capture(() => Assert.AreElementsEqualIgnoringOrder("1", "2", "{0} message", "custom"));
  140. Assert.Count(1, failures);
  141. Assert.AreEqual("custom message", failures[0].Message);
  142. }
  143. #endregion
  144. #region AreElementsSame
  145. [Test]
  146. public void AreElementsSame_with_objects()
  147. {
  148. var o = new object();
  149. Assert.AreElementsSame(new[] { o }, new[] { o });
  150. }
  151. [Test]
  152. public void AreElementsSame_with_different_types()
  153. {
  154. var o1 = new object();
  155. var o2 = new object();
  156. Assert.AreElementsSame(new[] { o1, o2 }, new List<object> { o1, o2 });
  157. }
  158. [Test]
  159. public void AreElementsSame_fails_when_elements_are_in_different_order()
  160. {
  161. var o1 = new object();
  162. var o2 = new object();
  163. AssertionFailure[] failures = Capture(()
  164. => Assert.AreElementsSame(new[] { o1, o2 }, new List<object> { o2, o1 }));
  165. Assert.Count(1, failures);
  166. Assert.AreEqual("Expected elements to be referentially equal but they differ in at least one position.", failures[0].Description);
  167. }
  168. [Test]
  169. public void AreElementsSame_fails_with_custom_message()
  170. {
  171. var o1 = new object();
  172. var o2 = new object();
  173. AssertionFailure[] failures = Capture(() => Assert.AreElementsSame(new[] { o1 }, new[] { o2 }, "{0} message", "custom"));
  174. Assert.Count(1, failures);
  175. Assert.AreEqual("custom message", failures[0].Message);
  176. }
  177. #endregion
  178. #region AreElementsNotSame
  179. [Test]
  180. public void AreElementsNotSame_with_objects()
  181. {
  182. var o1 = new object();
  183. var o2 = new object();
  184. Assert.AreElementsNotSame(new[] { o1 }, new[] { o2 });
  185. }
  186. [Test]
  187. public void AreElementsNotSame_with_different_types()
  188. {
  189. var o1 = new object();
  190. var o2 = new object();
  191. var o3 = new object();
  192. Assert.AreElementsNotSame(new[] { o1, o2 }, new List<object> { o1, o3 });
  193. }
  194. [Test]
  195. public void AreElementsNotSame_fails_with_custom_message()
  196. {
  197. var o = new object();
  198. AssertionFailure[] failures = Capture(() => Assert.AreElementsNotSame(new[] { o }, new[] { o }, "{0} message", "custom"));
  199. Assert.Count(1, failures);
  200. Assert.AreEqual("custom message", failures[0].Message);
  201. }
  202. #endregion
  203. #region AreElementsSameIgnoringOrder
  204. [Test]
  205. public void AreElementsSameIgnoringOrder_with_different_types()
  206. {
  207. var o1 = new object();
  208. var o2 = new object();
  209. var o3 = new object();
  210. Assert.AreElementsSameIgnoringOrder(new[] { o2, o2, o3, o1 }, new List<object> { o1, o3, o2, o2 });
  211. }
  212. [Test]
  213. public void AreElementsSameIgnoringOrder_fails_when_excess_or_missing_elements()
  214. {
  215. var o1 = new object();
  216. var o2 = new object();
  217. var o3 = new object();
  218. var o4 = new object();
  219. AssertionFailure[] failures = Capture(()
  220. => Assert.AreElementsSameIgnoringOrder(new[] { o1, o2, o3, o2, o3, o1 }, new List<object> { o4, o2, o1, o1, o4, o1, o4 }));
  221. Assert.Count(1, failures);
  222. Assert.AreEqual("Expected elements to be referentially equal but possibly in a different order.", failures[0].Description);
  223. }
  224. [Test]
  225. public void AreElementsSameIgnoringOrder_fails_with_custom_message()
  226. {
  227. var o1 = new object();
  228. var o2 = new object();
  229. AssertionFailure[] failures = Capture(() => Assert.AreElementsSameIgnoringOrder(new[] { o1 }, new[] { o2 }, "{0} message", "custom"));
  230. Assert.Count(1, failures);
  231. Assert.AreEqual("custom message", failures[0].Message);
  232. }
  233. #endregion
  234. #region Contains (for collections)
  235. [Test]
  236. public void Contains_generic_ICollection_int_test()
  237. {
  238. Assert.Contains(new List<int>(new[] { 1, 2, 3 }), 2);
  239. }
  240. [Test]
  241. [Row("2", new[] { "1", "2", "3" })]
  242. [Row(null, new[] { "1", "2", null, "3" })]
  243. public void Contains_list_string_test(string testValue, string[] list)
  244. {
  245. Assert.Contains(new List<string>(list), testValue);
  246. }
  247. [Test]
  248. [Row(new[] { 1, 2, 3 }, "[1, 2, 3]")]
  249. [Row(new[] { 1, 2 }, "[1, 2]")]
  250. [Row(new[] { 1, 2, 3, 5 }, "[1, 2, 3, 5]")]
  251. public void Contains_fails_when_test_value_is_not_in_the_list(int[] listItems, string expectedCollection)
  252. {
  253. AssertionFailure[] failures = Capture(() =>
  254. Assert.Contains(new List<int>(listItems), 4));
  255. Assert.Count(1, failures);
  256. Assert.AreEqual("Expected the value to appear within the enumeration.", failures[0].Description);
  257. Assert.AreEqual("Expected Value", failures[0].LabeledValues[0].Label);
  258. Assert.AreEqual("4", failures[0].LabeledValues[0].FormattedValue.ToString());
  259. Assert.AreEqual("Enumeration", failures[0].LabeledValues[1].Label);
  260. Assert.AreEqual(expectedCollection, failures[0].LabeledValues[1].FormattedValue.ToString());
  261. }
  262. [Test]
  263. [Row("test", new[] { "1", "2", "3" }, "\"test\"", "[\"1\", \"2\", \"3\"]")]
  264. [Row(null, new[] { "1", "2", "3" }, "null", "[\"1\", \"2\", \"3\"]")]
  265. public void Contains_fails_when_test_value_is_not_in_the_string_list(string testValue, string[] listItems, string expectedLabledValue, string expectedCollection)
  266. {
  267. AssertionFailure[] failures = Capture(() =>
  268. Assert.Contains(new List<string>(listItems), testValue));
  269. Assert.Count(1, failures);
  270. Assert.AreEqual("Expected the value to appear within the enumeration.", failures[0].Description);
  271. Assert.AreEqual("Expected Value", failures[0].LabeledValues[0].Label);
  272. Assert.AreEqual(expectedLabledValue, failures[0].LabeledValues[0].FormattedValue.ToString());
  273. Assert.AreEqual("Enumeration", failures[0].LabeledValues[1].Label);
  274. Assert.AreEqual(expectedCollection, failures[0].LabeledValues[1].FormattedValue.ToString());
  275. }
  276. [Test]
  277. public void ContainsKey_dictionary_int_test()
  278. {
  279. Assert.ContainsKey(new Dictionary<int, int>
  280. {
  281. {1, 1},
  282. {2, 2}
  283. }, 2);
  284. }
  285. [Test]
  286. public void ContainsKey_fails_when_test_value_is_not_in_the_dictionary()
  287. {
  288. var dictionary = new Dictionary<int, string>
  289. {
  290. { 1, "1" },
  291. { 2, "2`" },
  292. };
  293. AssertionFailure[] failures = Capture(() =>
  294. Assert.ContainsKey(dictionary, 0));
  295. Assert.Count(1, failures);
  296. Assert.AreEqual("Expected the key to appear within the dictionary.", failures[0].Description);
  297. Assert.AreEqual("Expected Key", failures[0].LabeledValues[0].Label);
  298. Assert.AreEqual("0", failures[0].LabeledValues[0].FormattedValue.ToString());
  299. Assert.AreEqual("Dictionary", failures[0].LabeledValues[1].Label);
  300. Assert.AreEqual("[1: \"1\", 2: \"2`\"]", failures[0].LabeledValues[1].FormattedValue.ToString());
  301. }
  302. [Test]
  303. public void Contains_fails_when_test_value_is_not_in_the_dictionary_key_is_reference_type()
  304. {
  305. var dictionary = new Dictionary<List<int>, string>
  306. {
  307. { new List<int>(new[] {1, 2}), "1" },
  308. { new List<int>(new[] {3, 4}), "2`" },
  309. };
  310. AssertionFailure[] failures = Capture(() =>
  311. Assert.ContainsKey(dictionary, new List<int>(new[] { 5 })));
  312. Assert.Count(1, failures);
  313. Assert.AreEqual("Expected the key to appear within the dictionary.", failures[0].Description);
  314. Assert.AreEqual("Expected Key", failures[0].LabeledValues[0].Label);
  315. Assert.AreEqual("[5]", failures[0].LabeledValues[0].FormattedValue.ToString());
  316. Assert.AreEqual("Dictionary", failures[0].LabeledValues[1].Label);
  317. Assert.AreEqual("[[1, 2]: \"1\", [3, 4]: \"2`\"]",
  318. failures[0].LabeledValues[1].FormattedValue.ToString());
  319. }
  320. [Test]
  321. public void Contains_fails_with_custom_message()
  322. {
  323. AssertionFailure[] failures = Capture(() =>
  324. Assert.Contains(new List<int>(new[] { 1, 2, 3 }), 5, "{0} message.", "custom"));
  325. Assert.Count(1, failures);
  326. Assert.AreEqual("custom message.", failures[0].Message);
  327. }
  328. #endregion
  329. #region ForAll
  330. [Test]
  331. public void ForAll_should_pass()
  332. {
  333. var data = new[] { "Athos", "Porthos", "Aramis" };
  334. Assert.ForAll(data, x => x.Contains("a") || x.Contains("o"));
  335. }
  336. [Test]
  337. public void ForAll_should_fail()
  338. {
  339. var data = new[] { "Athos", "Porthos", "Aramis" };
  340. AssertionFailure[] failures = Capture(() => Assert.ForAll(data, x => x.StartsWith("A")));
  341. Assert.Count(1, failures);
  342. Assert.AreEqual("Expected all the elements of the sequence to meet the specified condition, but at least one failed.", failures[0].Description);
  343. }
  344. [Test]
  345. public void ForAllAction_should_pass()
  346. {
  347. var data = new[] { "Athos", "Porthos", "Aramis" };
  348. Assert.ForAll(data, x => { Assert.Contains(x, "s"); });
  349. }
  350. [Test]
  351. public void ForAllAction_should_fail()
  352. {
  353. var data = new[] { "Athos", "Porthos", "Aramis", "D'Artagnan" };
  354. AssertionFailure[] failures = Capture(() => Assert.ForAll(data, x => { Assert.StartsWith(x, "A"); }));
  355. Assert.Count(1, failures);
  356. Assert.AreEqual("Expected all the elements of the sequence to pass assert validations, but at least one failed.", failures[0].Description);
  357. Assert.Count(2, failures[0].InnerFailures);
  358. }
  359. [Test]
  360. public void ForAllActionIndex_should_fail()
  361. {
  362. var data = new[] { "Athos", "Porthos", "Aramis", "D'Artagnan" };
  363. AssertionFailure[] failures = Capture(() => Assert.ForAll(data, (value, index) =>
  364. {
  365. if (index < 3)
  366. Assert.StartsWith(value, "A");
  367. }));
  368. Assert.Count(1, failures);
  369. Assert.AreEqual("Expected all the elements of the sequence to pass assert validations, but at least one failed.", failures[0].Description);
  370. Assert.Count(1, failures[0].InnerFailures); // The D'Artagnan should not be checked because only the first three entries are validated.
  371. }
  372. [Test]
  373. public void ForAll_should_fail_with_custom_message()
  374. {
  375. var data = new[] { "Athos", "Porthos", "Aramis" };
  376. AssertionFailure[] failures = Capture(() => Assert.ForAll(data, x => x.Contains("x"),"{0} message.","Custom"));
  377. Assert.Count(1, failures);
  378. Assert.AreEqual("Custom message.", failures[0].Message);
  379. }
  380. #endregion
  381. #region Exists
  382. [Test]
  383. public void Exists_should_pass()
  384. {
  385. var data = new[] { "Athos", "Porthos", "Aramis" };
  386. Assert.Exists(data, x => x.Contains("th"));
  387. }
  388. [Test]
  389. public void Exists_should_fail()
  390. {
  391. var data = new[] { "Athos", "Porthos", "Aramis" };
  392. AssertionFailure[] failures = Capture(() => Assert.Exists(data, x => x == "D'Artagnan"));
  393. Assert.Count(1, failures);
  394. Assert.AreEqual("Expected at least one element of the sequence to meet the specified condition, but none passed.", failures[0].Description);
  395. }
  396. [Test]
  397. public void Exists_should_fail_with_custom_message()
  398. {
  399. var data = new[] { "Athos", "Porthos", "Aramis" };
  400. AssertionFailure[] failures = Capture(() => Assert.Exists(data, x => x == "D'Artagnan","{0} message.","Custom"));
  401. Assert.Count(1, failures);
  402. Assert.AreEqual("Custom message.", failures[0].Message);
  403. }
  404. #endregion
  405. #region DoesNotExist
  406. [Test]
  407. public void DoesNotExist_should_pass()
  408. {
  409. var data = new[] { "Athos", "Porthos", "Aramis" };
  410. Assert.DoesNotExist(data, x => x.Contains("D'A"));
  411. }
  412. [Test]
  413. public void DoesNotExist_should_fail()
  414. {
  415. var data = new[] { "Athos", "Porthos", "Aramis" };
  416. AssertionFailure[] failures = Capture(() => Assert.DoesNotExist(data, x => x == "Porthos"));
  417. Assert.Count(1, failures);
  418. Assert.AreEqual("Expected none of the elements of the sequence to meet the specified condition, but one did.", failures[0].Description);
  419. }
  420. [Test]
  421. public void DoesNotExist_should_fail_with_custom_message()
  422. {
  423. var data = new[] { "Athos", "Porthos", "Aramis" };
  424. AssertionFailure[] failures = Capture(() => Assert.DoesNotExist(data, x => x == "Porthos","{0} message.","Custom"));
  425. Assert.Count(1, failures);
  426. Assert.AreEqual("Custom message.", failures[0].Message);
  427. }
  428. [Test]
  429. [ExpectedArgumentNullException]
  430. public void DoesNotExist_throws_exception_with_null_values_argument()
  431. {
  432. string[] data = null;
  433. Assert.DoesNotExist(data, x => x == "Porthos", "{0} message.", "Custom");
  434. }
  435. [Test]
  436. [ExpectedArgumentNullException]
  437. public void DoesNotExist_throws_exception_with_null_predicate_argument()
  438. {
  439. var data = new[] { "Athos", "Porthos", "Aramis" };
  440. Assert.DoesNotExist(data, null);
  441. }
  442. #endregion
  443. #region IsEmpty
  444. private static IEnumerable GetEmptyEnumeration()
  445. {
  446. yield break;
  447. }
  448. [Test]
  449. public void IsEmpty_should_pass()
  450. {
  451. Assert.IsEmpty(EmptyArray<int>.Instance);
  452. Assert.IsEmpty(new List<int>());
  453. Assert.IsEmpty(GetEmptyEnumeration());
  454. }
  455. [Test]
  456. public void IsEmpty_should_fail()
  457. {
  458. AssertionFailure[] failures = Capture(() => Assert.IsEmpty(new[] { 123, 456, 789 }));
  459. Assert.Count(1, failures);
  460. Assert.Like(failures[0].Description, @"^Expected the sequence to be empty but \d+ counting strateg(y has|ies have) failed.$");
  461. Assert.AreEqual("Expected Value", failures[0].LabeledValues[0].Label);
  462. Assert.AreEqual("0", failures[0].LabeledValues[0].FormattedValue.ToString());
  463. for (int i = 1; i <= failures[0].LabeledValues.Count - 1; i++)
  464. {
  465. Assert.Like(failures[0].LabeledValues[i].Label, @"^Actual Value \([\w ]+\)?$");
  466. Assert.AreEqual("3", failures[0].LabeledValues[i].FormattedValue.ToString());
  467. }
  468. }
  469. [Test]
  470. public void IsEmpty_should_fail_with_custom_message()
  471. {
  472. AssertionFailure[] failures = Capture(() => Assert.IsEmpty(new[] { 13, 46, 79 }, "{0} message.", "Custom"));
  473. Assert.Count(1, failures);
  474. Assert.AreEqual("Custom message.", failures[0].Message);
  475. }
  476. #endregion
  477. }
  478. }