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

/tests/MongoDB.Bson.Tests/Jira/CSharp783Tests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 381 lines | 315 code | 46 blank | 20 comment | 0 complexity | 99a1991f431edc875df219b9c418f227 MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2010-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System.Collections.Generic;
  16. using System.Linq;
  17. using FluentAssertions;
  18. using MongoDB.Bson.Serialization;
  19. using MongoDB.Bson.Serialization.Attributes;
  20. using MongoDB.Bson.Serialization.Serializers;
  21. using Xunit;
  22. namespace MongoDB.Bson.Tests.Jira.CSharp783
  23. {
  24. public class CSharp783DiscriminatedInterfaceTests
  25. {
  26. // nested types
  27. private class C
  28. {
  29. [BsonSerializer(typeof(DiscriminatedInterfaceSerializer<ISet<int>>))]
  30. public ISet<int> S { get; set; }
  31. }
  32. // public methods
  33. [Fact]
  34. public void TestEmptyHashSet()
  35. {
  36. var c = new C { S = new HashSet<int>() };
  37. var json = c.ToJson();
  38. var expected = "{ 'S' : { '_t' : 'System.Collections.Generic.HashSet`1[System.Int32]', '_v' : [] } }".Replace("'", "\"");
  39. Assert.Equal(expected, json);
  40. var r = BsonSerializer.Deserialize<C>(json);
  41. Assert.NotNull(r.S);
  42. Assert.IsType<HashSet<int>>(r.S);
  43. Assert.Equal(0, r.S.Count);
  44. }
  45. [Fact]
  46. public void TestEmptySortedSet()
  47. {
  48. var c = new C { S = new SortedSet<int>() };
  49. var json = c.ToJson();
  50. var expected = "{ 'S' : { '_t' : 'System.Collections.Generic.SortedSet`1[System.Int32]', '_v' : [] } }".Replace("'", "\"");
  51. Assert.Equal(expected, json);
  52. var r = BsonSerializer.Deserialize<C>(json);
  53. Assert.NotNull(r.S);
  54. Assert.IsType<SortedSet<int>>(r.S);
  55. Assert.Equal(0, r.S.Count);
  56. }
  57. [Fact]
  58. public void TestNull()
  59. {
  60. var c = new C { S = null };
  61. var json = c.ToJson();
  62. var expected = "{ 'S' : null }".Replace("'", "\"");
  63. Assert.Equal(expected, json);
  64. var r = BsonSerializer.Deserialize<C>(json);
  65. Assert.Equal(null, r.S);
  66. }
  67. [Fact]
  68. public void TestHashSetOneInt()
  69. {
  70. var c = new C { S = new HashSet<int> { 1 } };
  71. var json = c.ToJson();
  72. var expected = "{ 'S' : { '_t' : 'System.Collections.Generic.HashSet`1[System.Int32]', '_v' : [1] } }".Replace("'", "\"");
  73. Assert.Equal(expected, json);
  74. var r = BsonSerializer.Deserialize<C>(json);
  75. Assert.NotNull(r.S);
  76. Assert.IsType<HashSet<int>>(r.S);
  77. Assert.Equal(1, r.S.Count);
  78. Assert.Equal(1, r.S.ElementAt(0));
  79. }
  80. [Theory]
  81. [InlineData(1, 2)]
  82. [InlineData(2, 1)]
  83. public void TestHashSetTwoInts(int x, int y)
  84. {
  85. var c = new C { S = new HashSet<int> { x, y } };
  86. var json = c.ToJson();
  87. var expected = new[]
  88. {
  89. "{ 'S' : { '_t' : 'System.Collections.Generic.HashSet`1[System.Int32]', '_v' : [1, 2] } }",
  90. "{ 'S' : { '_t' : 'System.Collections.Generic.HashSet`1[System.Int32]', '_v' : [2, 1] } }"
  91. };
  92. Assert.True(expected.Select(e => e.Replace("'", "\"")).Contains(json));
  93. var r = BsonSerializer.Deserialize<C>(json);
  94. Assert.NotNull(r.S);
  95. Assert.IsType<HashSet<int>>(r.S);
  96. Assert.Equal(2, r.S.Count);
  97. Assert.Equal(c.S, r.S);
  98. }
  99. [Fact]
  100. public void TestSortedSetOneInt()
  101. {
  102. var c = new C { S = new SortedSet<int> { 1 } };
  103. var json = c.ToJson();
  104. var expected = "{ 'S' : { '_t' : 'System.Collections.Generic.SortedSet`1[System.Int32]', '_v' : [1] } }".Replace("'", "\"");
  105. Assert.Equal(expected, json);
  106. var r = BsonSerializer.Deserialize<C>(json);
  107. Assert.NotNull(r.S);
  108. Assert.IsType<SortedSet<int>>(r.S);
  109. Assert.Equal(1, r.S.Count);
  110. Assert.Equal(1, r.S.ElementAt(0));
  111. }
  112. [Theory]
  113. [InlineData(1, 2)]
  114. [InlineData(2, 1)]
  115. public void TestSortedSetTwoInts(int x, int y)
  116. {
  117. var c = new C { S = new SortedSet<int> { x, y } };
  118. var json = c.ToJson();
  119. var expected = "{ 'S' : { '_t' : 'System.Collections.Generic.SortedSet`1[System.Int32]', '_v' : [1, 2] } }".Replace("'", "\"");
  120. Assert.Equal(expected, json);
  121. var r = BsonSerializer.Deserialize<C>(json);
  122. Assert.NotNull(r.S);
  123. Assert.IsType<SortedSet<int>>(r.S);
  124. Assert.Equal(2, r.S.Count);
  125. Assert.Equal(c.S, r.S);
  126. }
  127. }
  128. public class CSharp783ImpliedHashSetImplementationTests
  129. {
  130. // nested types
  131. private class C
  132. {
  133. public ISet<int> S { get; set; }
  134. }
  135. // public methods
  136. [Fact]
  137. public void TestEmptyHashSet()
  138. {
  139. var c = new C { S = new HashSet<int>() };
  140. var json = c.ToJson();
  141. var expected = "{ 'S' : [] }".Replace("'", "\"");
  142. Assert.Equal(expected, json);
  143. var r = BsonSerializer.Deserialize<C>(json);
  144. Assert.NotNull(r.S);
  145. Assert.IsType<HashSet<int>>(r.S);
  146. Assert.Equal(0, r.S.Count);
  147. }
  148. [Fact]
  149. public void TestEmptySortedSet()
  150. {
  151. var c = new C { S = new SortedSet<int>() };
  152. var json = c.ToJson();
  153. var expected = "{ 'S' : [] }".Replace("'", "\"");
  154. Assert.Equal(expected, json);
  155. var r = BsonSerializer.Deserialize<C>(json);
  156. Assert.NotNull(r.S);
  157. Assert.IsType<HashSet<int>>(r.S);
  158. Assert.Equal(0, r.S.Count);
  159. }
  160. [Fact]
  161. public void TestNull()
  162. {
  163. var c = new C { S = null };
  164. var json = c.ToJson();
  165. var expected = "{ 'S' : null }".Replace("'", "\"");
  166. Assert.Equal(expected, json);
  167. var r = BsonSerializer.Deserialize<C>(json);
  168. Assert.Equal(null, r.S);
  169. }
  170. [Fact]
  171. public void TestHashSetOneInt()
  172. {
  173. var c = new C { S = new HashSet<int> { 1 } };
  174. var json = c.ToJson();
  175. var expected = "{ 'S' : [1] }".Replace("'", "\"");
  176. Assert.Equal(expected, json);
  177. var r = BsonSerializer.Deserialize<C>(json);
  178. Assert.NotNull(r.S);
  179. Assert.IsType<HashSet<int>>(r.S);
  180. Assert.Equal(1, r.S.Count);
  181. Assert.Equal(1, r.S.ElementAt(0));
  182. }
  183. [Theory]
  184. [InlineData(1, 2)]
  185. [InlineData(2, 1)]
  186. public void TestHashSetTwoInts(int x, int y)
  187. {
  188. var c = new C { S = new HashSet<int> { x, y } };
  189. var json = c.ToJson();
  190. var expected = new[]
  191. {
  192. "{ 'S' : [1, 2] }",
  193. "{ 'S' : [2, 1] }"
  194. };
  195. Assert.True(expected.Select(e => e.Replace("'", "\"")).Contains(json));
  196. var r = BsonSerializer.Deserialize<C>(json);
  197. Assert.NotNull(r.S);
  198. Assert.IsType<HashSet<int>>(r.S);
  199. Assert.Equal(2, r.S.Count);
  200. Assert.Equal(c.S, r.S);
  201. }
  202. [Fact]
  203. public void TestSortedSetOneInt()
  204. {
  205. var c = new C { S = new SortedSet<int> { 1 } };
  206. var json = c.ToJson();
  207. var expected = "{ 'S' : [1] }".Replace("'", "\"");
  208. Assert.Equal(expected, json);
  209. var r = BsonSerializer.Deserialize<C>(json);
  210. Assert.NotNull(r.S);
  211. Assert.IsType<HashSet<int>>(r.S);
  212. Assert.Equal(1, r.S.Count);
  213. Assert.Equal(1, r.S.ElementAt(0));
  214. }
  215. [Theory]
  216. [InlineData(1, 2)]
  217. [InlineData(2, 1)]
  218. public void TestSortedSetTwoInts(int x, int y)
  219. {
  220. var c = new C { S = new SortedSet<int> { x, y } };
  221. var json = c.ToJson();
  222. var expected = string.Format("{{ 'S' : [1, 2] }}").Replace("'", "\""); // always sorted
  223. Assert.Equal(expected, json);
  224. var r = BsonSerializer.Deserialize<C>(json);
  225. Assert.NotNull(r.S);
  226. Assert.IsType<HashSet<int>>(r.S);
  227. Assert.Equal(2, r.S.Count);
  228. r.S.Should().BeEquivalentTo(c.S);
  229. }
  230. }
  231. public class CSharp783ImpliedSortedSetImplementationTests
  232. {
  233. // nested types
  234. private class C
  235. {
  236. [BsonSerializer(typeof(ImpliedImplementationInterfaceSerializer<ISet<int>, SortedSet<int>>))]
  237. public ISet<int> S { get; set; }
  238. }
  239. // public methods
  240. [Fact]
  241. public void TestEmptyHashSet()
  242. {
  243. var c = new C { S = new HashSet<int>() };
  244. var json = c.ToJson();
  245. var expected = "{ 'S' : [] }".Replace("'", "\"");
  246. Assert.Equal(expected, json);
  247. var r = BsonSerializer.Deserialize<C>(json);
  248. Assert.NotNull(r.S);
  249. Assert.IsType<SortedSet<int>>(r.S);
  250. Assert.Equal(0, r.S.Count);
  251. }
  252. [Fact]
  253. public void TestEmptySortedSet()
  254. {
  255. var c = new C { S = new SortedSet<int>() };
  256. var json = c.ToJson();
  257. var expected = "{ 'S' : [] }".Replace("'", "\"");
  258. Assert.Equal(expected, json);
  259. var r = BsonSerializer.Deserialize<C>(json);
  260. Assert.NotNull(r.S);
  261. Assert.IsType<SortedSet<int>>(r.S);
  262. Assert.Equal(0, r.S.Count);
  263. }
  264. [Fact]
  265. public void TestNull()
  266. {
  267. var c = new C { S = null };
  268. var json = c.ToJson();
  269. var expected = "{ 'S' : null }".Replace("'", "\"");
  270. Assert.Equal(expected, json);
  271. var r = BsonSerializer.Deserialize<C>(json);
  272. Assert.Equal(null, r.S);
  273. }
  274. [Fact]
  275. public void TestHashSetOneInt()
  276. {
  277. var c = new C { S = new HashSet<int> { 1 } };
  278. var json = c.ToJson();
  279. var expected = "{ 'S' : [1] }".Replace("'", "\"");
  280. Assert.Equal(expected, json);
  281. var r = BsonSerializer.Deserialize<C>(json);
  282. Assert.NotNull(r.S);
  283. Assert.IsType<SortedSet<int>>(r.S);
  284. Assert.Equal(1, r.S.Count);
  285. Assert.Equal(1, r.S.ElementAt(0));
  286. }
  287. [Theory]
  288. [InlineData(1, 2)]
  289. [InlineData(2, 1)]
  290. public void TestHashSetTwoInts(int x, int y)
  291. {
  292. var c = new C { S = new HashSet<int> { x, y } };
  293. var json = c.ToJson();
  294. var expected = new[]
  295. {
  296. "{ 'S' : [1, 2] }",
  297. "{ 'S' : [2, 1] }"
  298. };
  299. Assert.True(expected.Select(e => e.Replace("'", "\"")).Contains(json));
  300. var r = BsonSerializer.Deserialize<C>(json);
  301. Assert.NotNull(r.S);
  302. Assert.IsType<SortedSet<int>>(r.S);
  303. Assert.Equal(2, r.S.Count);
  304. r.S.Should().BeEquivalentTo(c.S);
  305. }
  306. [Fact]
  307. public void TestSortedSetOneInt()
  308. {
  309. var c = new C { S = new SortedSet<int> { 1 } };
  310. var json = c.ToJson();
  311. var expected = "{ 'S' : [1] }".Replace("'", "\"");
  312. Assert.Equal(expected, json);
  313. var r = BsonSerializer.Deserialize<C>(json);
  314. Assert.NotNull(r.S);
  315. Assert.IsType<SortedSet<int>>(r.S);
  316. Assert.Equal(1, r.S.Count);
  317. Assert.Equal(1, r.S.ElementAt(0));
  318. }
  319. [Theory]
  320. [InlineData(1, 2)]
  321. [InlineData(2, 1)]
  322. public void TestSortedSetTwoInts(int x, int y)
  323. {
  324. var c = new C { S = new SortedSet<int> { x, y } };
  325. var json = c.ToJson();
  326. var expected = string.Format("{{ 'S' : [1, 2] }}").Replace("'", "\""); // always sorted
  327. Assert.Equal(expected, json);
  328. var r = BsonSerializer.Deserialize<C>(json);
  329. Assert.NotNull(r.S);
  330. Assert.IsType<SortedSet<int>>(r.S);
  331. Assert.Equal(2, r.S.Count);
  332. Assert.Equal(c.S, r.S);
  333. }
  334. }
  335. }