/src/Nancy.Tests/Unit/DynamicDictionaryFixture.cs

https://github.com/factormystic/Nancy · C# · 769 lines · 444 code · 171 blank · 154 comment · 0 complexity · 07e7690d035f5088af843e3707bb8934 MD5 · raw file

  1. namespace Nancy.Tests.Unit
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using Xunit;
  7. public class DynamicDictionaryFixture
  8. {
  9. private readonly dynamic dictionary;
  10. public DynamicDictionaryFixture()
  11. {
  12. this.dictionary = new DynamicDictionary();
  13. this.dictionary["TestString"] = "Testing";
  14. this.dictionary["TestInt"] = 2;
  15. }
  16. [Fact]
  17. public void Should_create_instance_from_dictionary()
  18. {
  19. // Given
  20. var values = new Dictionary<string, object>
  21. {
  22. { "foo", 10 },
  23. { "bar", "some value" },
  24. };
  25. // When
  26. dynamic instance = DynamicDictionary.Create(values);
  27. // Then
  28. ((int)GetIntegerValue(instance.foo)).ShouldEqual(10);
  29. ((string)GetStringValue(instance.bar)).ShouldEqual("some value");
  30. }
  31. [Fact]
  32. public void Should_strip_dash_from_name_when_using_indexer_to_add_value()
  33. {
  34. // Given
  35. this.dictionary["foo-bar"] = 10;
  36. // When
  37. int result = GetIntegerValue(this.dictionary.foobar);
  38. // Then
  39. result.ShouldEqual(10);
  40. }
  41. [Fact]
  42. public void Should_be_able_to_retrieve_value_for_key_containing_dash_when_using_indexer()
  43. {
  44. // Given
  45. this.dictionary["foo-bar"] = 10;
  46. // When
  47. int result = GetIntegerValue(this.dictionary["foo-bar"]);
  48. // Then
  49. result.ShouldEqual(10);
  50. }
  51. [Fact]
  52. public void Should_be_able_to_retrive_value_stores_with_dash_using_key_without_dash_when_using_indexer()
  53. {
  54. // Given
  55. this.dictionary["foo-bar"] = 10;
  56. // When
  57. int result = GetIntegerValue(this.dictionary["foobar"]);
  58. // Then
  59. result.ShouldEqual(10);
  60. }
  61. [Fact]
  62. public void Should_be_able_to_retrive_value_stores_using_dash_using_key_with_dash_when_using_indexer()
  63. {
  64. // Given
  65. this.dictionary["foobar"] = 10;
  66. // When
  67. int result = GetIntegerValue(this.dictionary["foo-bar"]);
  68. // Then
  69. result.ShouldEqual(10);
  70. }
  71. [Fact]
  72. public void Should_retrieving_non_existing_index_should_return_empty_value()
  73. {
  74. // Given
  75. var value = this.dictionary["nonexisting"];
  76. // When
  77. bool result = value.HasValue;
  78. // Then
  79. result.ShouldBeFalse();
  80. }
  81. [Fact]
  82. public void Should_retrieving_non_existing_member_should_return_empty_value()
  83. {
  84. // Given
  85. var value = this.dictionary.nonexisting;
  86. // When
  87. bool result = value.HasValue;
  88. // Then
  89. result.ShouldBeFalse();
  90. }
  91. [Fact]
  92. public void Should_implicitly_cast_to_string_when_value_is_retrieved_as_member()
  93. {
  94. // Given
  95. this.dictionary.value = "foo";
  96. // When
  97. string result = GetStringValue(this.dictionary.value);
  98. // Then
  99. result.ShouldEqual("foo");
  100. }
  101. [Fact]
  102. public void Should_implicitly_cast_to_string_when_value_is_retrieved_as_index()
  103. {
  104. // Given
  105. this.dictionary.value = "foo";
  106. // When
  107. string result = GetStringValue(this.dictionary["value"]);
  108. // Then
  109. result.ShouldEqual("foo");
  110. }
  111. [Fact]
  112. public void Should_implicitly_cast_to_integer_when_value_is_retrieved_as_member()
  113. {
  114. // Given
  115. this.dictionary.value = 10;
  116. // When
  117. int result = GetIntegerValue(this.dictionary.value);
  118. // Then
  119. result.ShouldEqual(10);
  120. }
  121. [Fact]
  122. public void Should_implicitly_cast_to_integer_when_value_is_retrieved_as_index()
  123. {
  124. // Given
  125. this.dictionary.value = 10;
  126. // When
  127. int result = GetIntegerValue(this.dictionary["value"]);
  128. // Then
  129. result.ShouldEqual(10);
  130. }
  131. [Fact]
  132. public void Should_implicitly_cast_to_integer_when_value_is_string_and_is_retrieved_as_member()
  133. {
  134. // Given
  135. this.dictionary.value = "10";
  136. // When
  137. int result = GetIntegerValue(this.dictionary.value);
  138. // Then
  139. result.ShouldEqual(10);
  140. }
  141. [Fact]
  142. public void Should_implicitly_cast_to_integer_when_value_is_string_and_is_retrieved_as_index()
  143. {
  144. // Given
  145. this.dictionary.value = "10";
  146. // When
  147. int result = GetIntegerValue(this.dictionary["value"]);
  148. // Then
  149. result.ShouldEqual(10);
  150. }
  151. [Fact]
  152. public void Should_implicitly_cast_to_guid_when_value_is_retrieved_as_member()
  153. {
  154. // Given
  155. var id = Guid.NewGuid();
  156. this.dictionary.value = id;
  157. // When
  158. Guid result = GetGuidValue(this.dictionary.value);
  159. // Then
  160. result.ShouldEqual(id);
  161. }
  162. [Fact]
  163. public void Should_implicitly_cast_to_guid_when_value_is_retrieved_as_index()
  164. {
  165. // Given
  166. var id = Guid.NewGuid();
  167. this.dictionary.value = id;
  168. // When
  169. Guid result = GetGuidValue(this.dictionary["value"]);
  170. // Then
  171. result.ShouldEqual(id);
  172. }
  173. [Fact]
  174. public void Should_implicitly_cast_to_guid_when_value_is_string_and_is_retrieved_as_member()
  175. {
  176. // Given
  177. var id = Guid.NewGuid();
  178. this.dictionary.value = id.ToString("N");
  179. // When
  180. Guid result = GetGuidValue(this.dictionary.value);
  181. // Then
  182. result.ShouldEqual(id);
  183. }
  184. [Fact]
  185. public void Should_implicitly_cast_to_guid_when_value_is_string_and_is_retrieved_as_index()
  186. {
  187. // Given
  188. var id = Guid.NewGuid();
  189. this.dictionary.value = id.ToString("N");
  190. // When
  191. Guid result = GetGuidValue(this.dictionary["value"]);
  192. // Then
  193. result.ShouldEqual(id);
  194. }
  195. [Fact]
  196. public void Should_implicitly_cast_to_datetime_when_value_is_retrieved_as_member()
  197. {
  198. // Given
  199. var date = DateTime.Now;
  200. this.dictionary.value = date;
  201. // When
  202. DateTime result = GetDateTimeValue(this.dictionary.value);
  203. // Then
  204. result.ShouldEqual(date);
  205. }
  206. [Fact]
  207. public void Should_implicitly_cast_to_datetime_when_value_is_retrieved_as_index()
  208. {
  209. // Given
  210. var date = DateTime.Now;
  211. this.dictionary.value = date;
  212. // When
  213. DateTime result = GetDateTimeValue(this.dictionary["value"]);
  214. // Then
  215. result.ShouldEqual(date);
  216. }
  217. [Fact]
  218. public void Should_implicitly_cast_to_datetime_when_value_is_string_and_is_retrieved_as_member()
  219. {
  220. // Given
  221. var date = DateTime.Now;
  222. this.dictionary.value = date.ToString();
  223. // When
  224. DateTime result = GetDateTimeValue(this.dictionary.value);
  225. // Then
  226. result.ShouldEqual(date);
  227. }
  228. [Fact]
  229. public void Should_implicitly_cast_to_datetime_when_value_is_string_and_is_retrieved_as_index()
  230. {
  231. // Given
  232. var date = DateTime.Now;
  233. this.dictionary.value = date.ToString();
  234. // When
  235. DateTime result = GetDateTimeValue(this.dictionary["value"]);
  236. // Then
  237. result.ShouldEqual(date);
  238. }
  239. [Fact]
  240. public void Should_implicitly_cast_to_timespan_when_value_is_retrieved_as_member()
  241. {
  242. // Given
  243. var span = new TimeSpan(1, 2, 3, 4);
  244. this.dictionary.value = span;
  245. // When
  246. TimeSpan result = GetTimeSpanValue(this.dictionary.value);
  247. // Then
  248. result.ShouldEqual(span);
  249. }
  250. [Fact]
  251. public void Should_implicitly_cast_to_timespan_when_value_is_retrieved_as_index()
  252. {
  253. // Given
  254. var span = new TimeSpan(1, 2, 3, 4);
  255. this.dictionary.value = span;
  256. // When
  257. TimeSpan result = GetTimeSpanValue(this.dictionary["value"]);
  258. // Then
  259. result.ShouldEqual(span);
  260. }
  261. [Fact]
  262. public void Should_implicitly_cast_to_timespan_when_value_is_string_and_is_retrieved_as_member()
  263. {
  264. // Given
  265. var span = new TimeSpan(1, 2, 3, 4);
  266. this.dictionary.value = span.ToString();
  267. // When
  268. TimeSpan result = GetTimeSpanValue(this.dictionary.value);
  269. // Then
  270. result.ShouldEqual(span);
  271. }
  272. [Fact]
  273. public void Should_implicitly_cast_to_timespan_when_value_is_string_and_is_retrieved_as_index()
  274. {
  275. // Given
  276. var span = new TimeSpan(1, 2, 3, 4);
  277. this.dictionary.value = span.ToString();
  278. // When
  279. TimeSpan result = GetTimeSpanValue(this.dictionary["value"]);
  280. // Then
  281. result.ShouldEqual(span);
  282. }
  283. [Fact]
  284. public void Should_implicitly_cast_to_long_when_value_is_retrieved_as_member()
  285. {
  286. // Given
  287. this.dictionary.value = 10L;
  288. // When
  289. long result = GetLongValue(this.dictionary.value);
  290. // Then
  291. result.ShouldEqual(10L);
  292. }
  293. [Fact]
  294. public void Should_implicitly_cast_to_long_when_value_is_retrieved_as_index()
  295. {
  296. // Given
  297. this.dictionary.value = 10L;
  298. // When
  299. long result = GetLongValue(this.dictionary["value"]);
  300. // Then
  301. result.ShouldEqual(10L);
  302. }
  303. [Fact]
  304. public void Should_implicitly_cast_to_long_when_value_is_string_and_is_retrieved_as_member()
  305. {
  306. // Given
  307. this.dictionary.value = "10";
  308. // When
  309. long result = GetLongValue(this.dictionary.value);
  310. // Then
  311. result.ShouldEqual(10L);
  312. }
  313. [Fact]
  314. public void Should_implicitly_cast_to_long_when_value_is_string_and_is_retrieved_as_index()
  315. {
  316. // Given
  317. this.dictionary.value = "10";
  318. // When
  319. long result = GetLongValue(this.dictionary["value"]);
  320. // Then
  321. result.ShouldEqual(10L);
  322. }
  323. [Fact]
  324. public void Should_implicitly_cast_to_float_when_value_is_retrieved_as_member()
  325. {
  326. // Given
  327. this.dictionary.value = 10f;
  328. // When
  329. float result = GetFloatValue(this.dictionary.value);
  330. // Then
  331. result.ShouldEqual(10f);
  332. }
  333. [Fact]
  334. public void Should_implicitly_cast_to_float_when_value_is_retrieved_as_index()
  335. {
  336. // Given
  337. this.dictionary.value = 10f;
  338. // When
  339. float result = GetFloatValue(this.dictionary["value"]);
  340. // Then
  341. result.ShouldEqual(10f);
  342. }
  343. [Fact]
  344. public void Should_implicitly_cast_to_float_when_value_is_string_and_is_retrieved_as_member()
  345. {
  346. // Given
  347. this.dictionary.value = "10";
  348. // When
  349. float result = GetFloatValue(this.dictionary.value);
  350. // Then
  351. result.ShouldEqual(10f);
  352. }
  353. [Fact]
  354. public void Should_implicitly_cast_to_float_when_value_is_string_and_is_retrieved_as_index()
  355. {
  356. // Given
  357. this.dictionary.value = "10";
  358. // When
  359. float result = GetFloatValue(this.dictionary["value"]);
  360. // Then
  361. result.ShouldEqual(10f);
  362. }
  363. [Fact]
  364. public void Should_implicitly_cast_to_decimal_when_value_is_retrieved_as_member()
  365. {
  366. // Given
  367. this.dictionary.value = 10m;
  368. // When
  369. decimal result = GetDecimalValue(this.dictionary.value);
  370. // Then
  371. result.ShouldEqual(10m);
  372. }
  373. [Fact]
  374. public void Should_implicitly_cast_to_decimal_when_value_is_retrieved_as_index()
  375. {
  376. // Given
  377. this.dictionary.value = 10m;
  378. // When
  379. decimal result = GetDecimalValue(this.dictionary["value"]);
  380. // Then
  381. result.ShouldEqual(10m);
  382. }
  383. [Fact]
  384. public void Should_implicitly_cast_to_decimal_when_value_is_string_and_is_retrieved_as_member()
  385. {
  386. // Given
  387. this.dictionary.value = "10";
  388. // When
  389. decimal result = GetDecimalValue(this.dictionary.value);
  390. // Then
  391. result.ShouldEqual(10m);
  392. }
  393. [Fact]
  394. public void Should_implicitly_cast_to_decimal_when_value_is_string_and_is_retrieved_as_index()
  395. {
  396. // Given
  397. this.dictionary.value = "10";
  398. // When
  399. decimal result = GetDecimalValue(this.dictionary["value"]);
  400. // Then
  401. result.ShouldEqual(10m);
  402. }
  403. [Fact]
  404. public void Should_implicitly_cast_to_double_when_value_is_retrieved_as_member()
  405. {
  406. // Given
  407. this.dictionary.value = 10d;
  408. // When
  409. double result = GetDoubleValue(this.dictionary.value);
  410. // Then
  411. result.ShouldEqual(10d);
  412. }
  413. [Fact]
  414. public void Should_implicitly_cast_to_double_when_value_is_retrieved_as_index()
  415. {
  416. // Given
  417. this.dictionary.value = 10d;
  418. // When
  419. double result = GetDoubleValue(this.dictionary["value"]);
  420. // Then
  421. result.ShouldEqual(10d);
  422. }
  423. [Fact]
  424. public void Should_implicitly_cast_to_double_when_value_is_string_and_is_retrieved_as_member()
  425. {
  426. // Given
  427. this.dictionary.value = "10";
  428. // When
  429. double result = GetDoubleValue(this.dictionary.value);
  430. // Then
  431. result.ShouldEqual(10d);
  432. }
  433. [Fact]
  434. public void Should_implicitly_cast_to_double_when_value_is_string_and_is_retrieved_as_index()
  435. {
  436. // Given
  437. this.dictionary.value = "10";
  438. // When
  439. double result = GetDoubleValue(this.dictionary["value"]);
  440. // Then
  441. result.ShouldEqual(10d);
  442. }
  443. private static double GetDoubleValue(double value)
  444. {
  445. return value;
  446. }
  447. private static decimal GetDecimalValue(decimal value)
  448. {
  449. return value;
  450. }
  451. private static float GetFloatValue(float value)
  452. {
  453. return value;
  454. }
  455. private static long GetLongValue(long value)
  456. {
  457. return value;
  458. }
  459. private static TimeSpan GetTimeSpanValue(TimeSpan value)
  460. {
  461. return value;
  462. }
  463. private static DateTime GetDateTimeValue(DateTime value)
  464. {
  465. return value;
  466. }
  467. private static Guid GetGuidValue(Guid value)
  468. {
  469. return value;
  470. }
  471. private static int GetIntegerValue(int value)
  472. {
  473. return value;
  474. }
  475. private static string GetStringValue(string value)
  476. {
  477. return value;
  478. }
  479. [Fact]
  480. public void Should_return_actual_string_value_when_tostring_called_on_string_entry()
  481. {
  482. // Given, When
  483. string result = dictionary.TestString.ToString();
  484. // Then
  485. result.ShouldEqual("Testing");
  486. }
  487. [Fact]
  488. public void Should_return_string_representation_of_value_when_tostring_called_on_int_entry()
  489. {
  490. // Given, When
  491. string result = dictionary.TestInt.ToString();
  492. // Then
  493. result.ShouldEqual("2");
  494. }
  495. [Fact]
  496. public void Should_support_dynamic_properties()
  497. {
  498. // Given
  499. dynamic parameters = new DynamicDictionary();
  500. parameters.test = 10;
  501. // When
  502. var value = (int)parameters.test;
  503. // Then
  504. value.ShouldEqual(10);
  505. }
  506. [Fact]
  507. public void Should_support_dynamic_casting_of_properties_to_ints()
  508. {
  509. //Given
  510. dynamic parameters = new DynamicDictionary();
  511. parameters.test = "10";
  512. // When
  513. var value = (int)parameters.test;
  514. // Then
  515. value.ShouldEqual(10);
  516. }
  517. [Fact]
  518. public void Should_support_dynamic_casting_of_properties_to_guids()
  519. {
  520. //Given
  521. dynamic parameters = new DynamicDictionary();
  522. var guid = Guid.NewGuid();
  523. parameters.test = guid.ToString();
  524. // When
  525. var value = (Guid)parameters.test;
  526. // Then
  527. value.ShouldEqual(guid);
  528. }
  529. [Fact]
  530. public void Should_support_dynamic_casting_of_properties_to_timespans()
  531. {
  532. //Given
  533. dynamic parameters = new DynamicDictionary();
  534. parameters.test = new TimeSpan(1, 2, 3, 4).ToString();
  535. // When
  536. var value = (TimeSpan)parameters.test;
  537. // Then
  538. value.ShouldEqual(new TimeSpan(1, 2, 3, 4));
  539. }
  540. [Fact]
  541. public void Should_support_dynamic_casting_of_properties_to_datetimes()
  542. {
  543. //Given
  544. dynamic parameters = new DynamicDictionary();
  545. parameters.test = new DateTime(2001, 3, 4);
  546. // When
  547. var value = (DateTime)parameters.test;
  548. // Then
  549. value.ShouldEqual(new DateTime(2001, 3, 4));
  550. }
  551. [Fact]
  552. public void Should_support_dynamic_casting_of_nullable_properties()
  553. {
  554. //Given
  555. dynamic parameters = new DynamicDictionary();
  556. var guid = Guid.NewGuid();
  557. parameters.test = guid.ToString();
  558. // When
  559. var value = (Guid?)parameters.test;
  560. // Then
  561. value.ShouldEqual(guid);
  562. }
  563. [Fact]
  564. public void Should_support_implicit_casting()
  565. {
  566. // Given
  567. dynamic parameters = new DynamicDictionary();
  568. parameters.test = "10";
  569. // When
  570. int value = parameters.test;
  571. // Then
  572. value.ShouldEqual(10);
  573. }
  574. [Fact]
  575. public void Should_support_casting_when_using_indexer_to_set_values()
  576. {
  577. // Given
  578. dynamic parameters = new DynamicDictionary();
  579. parameters["test"] = "10";
  580. // When
  581. int value = parameters.test;
  582. // Then
  583. value.ShouldEqual(10);
  584. }
  585. [Fact]
  586. public void Should_support_GetDynamicMemberNames()
  587. {
  588. // Given
  589. dynamic parameters = new DynamicDictionary();
  590. parameters["test"] = "10";
  591. parameters["rest"] = "20";
  592. // When
  593. var names = ((DynamicDictionary) parameters).GetDynamicMemberNames();
  594. // Then
  595. Assert.True(names.SequenceEqual(new[] {"test", "rest"}));
  596. }
  597. }
  598. }