/src/iSynaptic.Core.UnitTests/Serialization/ResultJsonConverterTests.cs

https://github.com/iSynaptic/iSynaptic.Core · C# · 277 lines · 205 code · 51 blank · 21 comment · 0 complexity · 4855e6da0884fa1de550020cb5dd2b7b MD5 · raw file

  1. // The MIT License
  2. //
  3. // Copyright (c) 2013 Jordan E. Terrell
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. using System;
  23. using System.Collections.Generic;
  24. using FluentAssertions;
  25. using NUnit.Framework;
  26. using Newtonsoft.Json;
  27. using iSynaptic.Commons;
  28. namespace iSynaptic.Serialization
  29. {
  30. [TestFixture]
  31. public class ResultJsonConverterTests
  32. {
  33. private static readonly ResultJsonConverter _converter =
  34. new ResultJsonConverter();
  35. private static readonly JsonSerializer _serializer =
  36. JsonSerializer.Create(new JsonSerializerSettings { Converters = new List<JsonConverter> { _converter } });
  37. [Test]
  38. public void CanConvert_WithResultTypes_ReturnsTrue()
  39. {
  40. _converter.CanConvert(typeof(Result<String, String>))
  41. .Should().BeTrue();
  42. _converter.CanConvert(typeof(Result<Int32, Int32>))
  43. .Should().BeTrue();
  44. }
  45. [Test]
  46. public void CanConvert_WithOpenMaybeType_ReturnsFalse()
  47. {
  48. _converter.CanConvert(typeof(Result<,>))
  49. .Should().BeFalse();
  50. }
  51. [Test]
  52. public void WriteJson_SuccessWithNoValueAndNoObservations_WritesCorrectly()
  53. {
  54. Result<String, String> result = Result.Success();
  55. String json = _serializer.Serialize(result);
  56. json.Should().Be("{\"hasValue\":false,\"wasSuccessful\":true}");
  57. }
  58. [Test]
  59. public void WriteJson_WithUnitValueAndNoUnitObservations_WritesCorrectly()
  60. {
  61. Result<Unit, Unit> result = Result.Success();
  62. String json = _serializer.Serialize(result);
  63. json.Should().Be("{\"value\":{},\"wasSuccessful\":true}");
  64. }
  65. [Test]
  66. public void WriteJson_WithUnitValueAndOneValueObservations_WritesCorrectly()
  67. {
  68. Result<Unit, int> result = Result.Success(42);
  69. String json = _serializer.Serialize(result);
  70. json.Should().Be("{\"value\":{},\"wasSuccessful\":true,\"observations\":[42]}");
  71. }
  72. [Test]
  73. public void WriteJson_WithUnitValueAndManyValueObservations_WritesCorrectly()
  74. {
  75. Result<Unit, int> result = Result.Success(42, 47, 1138, 1337);
  76. String json = _serializer.Serialize(result);
  77. json.Should().Be("{\"value\":{},\"wasSuccessful\":true,\"observations\":[42,47,1138,1337]}");
  78. }
  79. [Test]
  80. public void WriteJson_WithUnitValueAndOneReferenceObservations_WritesCorrectly()
  81. {
  82. Result<Unit, string> result = Result.Success("yo!");
  83. String json = _serializer.Serialize(result);
  84. json.Should().Be("{\"value\":{},\"wasSuccessful\":true,\"observations\":[\"yo!\"]}");
  85. }
  86. [Test]
  87. public void WriteJson_WithUnitValueAndManyReferenceObservations_WritesCorrectly()
  88. {
  89. Result<Unit, string> result = Result.Success("This", "one", "goes", "to", "eleven", "one", "louder");
  90. String json = _serializer.Serialize(result);
  91. json.Should().Be("{\"value\":{},\"wasSuccessful\":true,\"observations\":[\"This\",\"one\",\"goes\",\"to\",\"eleven\",\"one\",\"louder\"]}");
  92. }
  93. [Test]
  94. public void WriteJson_WithUnitObservations_WritesCorrectly()
  95. {
  96. Result<String, Unit> result = "foo".ToResult();
  97. String json = _serializer.Serialize(result);
  98. json.Should().Be("{\"value\":\"foo\",\"wasSuccessful\":true}");
  99. }
  100. [Test]
  101. public void WriteJson_SuccessWithValueAndNoObservations_WritesCorrectly()
  102. {
  103. Result<String, String> result = "Hello, World!".ToResult();
  104. String json = _serializer.Serialize(result);
  105. json.Should().Be("{\"value\":\"Hello, World!\",\"wasSuccessful\":true}");
  106. }
  107. [Test]
  108. public void WriteJson_SuccessWithNoValueAndObservations_WritesCorrectly()
  109. {
  110. Result<String, String> result = Result.Success("It went well!", "Take a break!");
  111. String json = _serializer.Serialize(result);
  112. json.Should().Be("{\"hasValue\":false,\"wasSuccessful\":true,\"observations\":[\"It went well!\",\"Take a break!\"]}");
  113. }
  114. [Test]
  115. public void WriteJson_SuccessWithValueAndObservations_WritesCorrectly()
  116. {
  117. Result<String, String> result = "Hello, World!".ToResult().ObserveMany("It went well!", "Take a break!");
  118. String json = _serializer.Serialize(result);
  119. json.Should().Be("{\"value\":\"Hello, World!\",\"wasSuccessful\":true,\"observations\":[\"It went well!\",\"Take a break!\"]}");
  120. }
  121. [Test]
  122. public void WriteJson_FailureWithNoValueAndNoObservations_WritesCorrectly()
  123. {
  124. Result<String, String> result = Result.Failure();
  125. String json = _serializer.Serialize(result);
  126. json.Should().Be("{\"hasValue\":false,\"wasSuccessful\":false}");
  127. }
  128. [Test]
  129. public void WriteJson_FailureWithValueAndNoObservations_WritesCorrectly()
  130. {
  131. Result<String, String> result = "Hello, World!".ToResult().Fail();
  132. String json = _serializer.Serialize(result);
  133. json.Should().Be("{\"value\":\"Hello, World!\",\"wasSuccessful\":false}");
  134. }
  135. [Test]
  136. public void WriteJson_FailureWithNoValueAndObservations_WritesCorrectly()
  137. {
  138. Result<String, String> result = Result.Failure("Something bad happened!", "Really bad!");
  139. String json = _serializer.Serialize(result);
  140. json.Should().Be("{\"hasValue\":false,\"wasSuccessful\":false,\"observations\":[\"Something bad happened!\",\"Really bad!\"]}");
  141. }
  142. [Test]
  143. public void WriteJson_FailureWithValueAndObservations_WritesCorrectly()
  144. {
  145. Result<String, String> result = "Hello, World!".ToResult().Fail().ObserveMany("Something bad happened!", "Really bad!");
  146. String json = _serializer.Serialize(result);
  147. json.Should().Be("{\"value\":\"Hello, World!\",\"wasSuccessful\":false,\"observations\":[\"Something bad happened!\",\"Really bad!\"]}");
  148. }
  149. [Test]
  150. public void ReadJson_SuccessWithNoValueAndNoObservations_ReturnsCorrectly()
  151. {
  152. String input = "{\"hasValue\":false,\"wasSuccessful\":true}";
  153. Result<String, String> result = _serializer.Deserialize<Result<String, String>>(input);
  154. result.HasValue.Should().BeFalse();
  155. result.WasSuccessful.Should().BeTrue();
  156. result.Observations.Should().BeEmpty();
  157. }
  158. [Test]
  159. public void ReadJson_SuccessWithValueAndNoObservations_ReturnsCorrectly()
  160. {
  161. String input = "{\"value\":\"Hello, World!\",\"wasSuccessful\":true}";
  162. Result<String, String> result = _serializer.Deserialize<Result<String, String>>(input);
  163. result.HasValue.Should().BeTrue();
  164. result.Value.Should().Be("Hello, World!");
  165. result.WasSuccessful.Should().BeTrue();
  166. result.Observations.Should().BeEmpty();
  167. }
  168. [Test]
  169. public void ReadJson_SuccessWithNoValueAndObservations_WritesCorrectly()
  170. {
  171. String input = "{\"hasValue\":false,\"wasSuccessful\":true,\"observations\":[\"It went well!\",\"Take a break!\"]}";
  172. Result<String, String> result = _serializer.Deserialize<Result<String, String>>(input);
  173. result.HasValue.Should().BeFalse();
  174. result.WasSuccessful.Should().BeTrue();
  175. result.Observations.Should().Contain("It went well!", "Take a break!");
  176. }
  177. [Test]
  178. public void ReadJson_SuccessWithValueAndObservations_WritesCorrectly()
  179. {
  180. String input = "{\"value\":\"Hello, World!\",\"wasSuccessful\":true,\"observations\":[\"It went well!\",\"Take a break!\"]}";
  181. Result<String, String> result = _serializer.Deserialize<Result<String, String>>(input);
  182. result.HasValue.Should().BeTrue();
  183. result.Value.Should().Be("Hello, World!");
  184. result.WasSuccessful.Should().BeTrue();
  185. result.Observations.Should().Contain("It went well!", "Take a break!");
  186. }
  187. [Test]
  188. public void ReadJson_FailureWithNoValueAndNoObservations_WritesCorrectly()
  189. {
  190. String input = "{\"hasValue\":false,\"wasSuccessful\":false}";
  191. Result<String, String> result = _serializer.Deserialize<Result<String, String>>(input);
  192. result.HasValue.Should().BeFalse();
  193. result.WasSuccessful.Should().BeFalse();
  194. result.Observations.Should().BeEmpty();
  195. }
  196. [Test]
  197. public void ReadJson_FailureWithValueAndNoObservations_WritesCorrectly()
  198. {
  199. String input = "{\"value\":\"Hello, World!\",\"wasSuccessful\":false}";
  200. Result<String, String> result = _serializer.Deserialize<Result<String, String>>(input);
  201. result.HasValue.Should().BeTrue();
  202. result.Value.Should().Be("Hello, World!");
  203. result.WasSuccessful.Should().BeFalse();
  204. result.Observations.Should().BeEmpty();
  205. }
  206. [Test]
  207. public void ReadJson_FailureWithNoValueAndObservations_WritesCorrectly()
  208. {
  209. String input = "{\"hasValue\":false,\"wasSuccessful\":false,\"observations\":[\"Something bad happened!\",\"Really bad!\"]}";
  210. Result<String, String> result = _serializer.Deserialize<Result<String, String>>(input);
  211. result.HasValue.Should().BeFalse();
  212. result.WasSuccessful.Should().BeFalse();
  213. result.Observations.Should().Contain("Something bad happened!", "Really bad!");
  214. }
  215. [Test]
  216. public void ReadJson_FailureWithValueAndObservations_WritesCorrectly()
  217. {
  218. String input = "{\"value\":\"Hello, World!\",\"wasSuccessful\":false,\"observations\":[\"Something bad happened!\",\"Really bad!\"]}";
  219. Result<String, String> result = _serializer.Deserialize<Result<String, String>>(input);
  220. result.HasValue.Should().BeTrue();
  221. result.Value.Should().Be("Hello, World!");
  222. result.WasSuccessful.Should().BeFalse();
  223. result.Observations.Should().Contain("Something bad happened!", "Really bad!");
  224. }
  225. }
  226. }