PageRenderTime 88ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/MongoDB.Bson.Tests/Serialization/Serializers/EnumerableInterfaceImplementerSerializerTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 82 lines | 56 code | 11 blank | 15 comment | 0 complexity | 1993d141cccc7c02e8ebc9d3c07bf24d MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2015-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;
  16. using System.Collections.Generic;
  17. using System.IO;
  18. using FluentAssertions;
  19. using MongoDB.Bson.IO;
  20. using MongoDB.Bson.Serialization;
  21. using MongoDB.Bson.Serialization.Serializers;
  22. using Xunit;
  23. namespace MongoDB.Bson.Tests.Serialization.Serializers
  24. {
  25. public class EnumerableInterfaceImplementerSerializerTests
  26. {
  27. public class C : IEnumerable<C>
  28. {
  29. public int Id;
  30. public List<C> Children;
  31. public IEnumerator<C> GetEnumerator()
  32. {
  33. return Children.GetEnumerator();
  34. }
  35. IEnumerator IEnumerable.GetEnumerator()
  36. {
  37. return GetEnumerator();
  38. }
  39. }
  40. [Fact]
  41. public void LookupSerializer_should_not_throw_StackOverflowException()
  42. {
  43. var serializer = BsonSerializer.LookupSerializer<C>();
  44. serializer.Should().BeOfType<EnumerableInterfaceImplementerSerializer<C, C>>();
  45. var itemSerializer = ((EnumerableInterfaceImplementerSerializer<C, C>)serializer).ItemSerializer;
  46. itemSerializer.Should().BeSameAs(serializer);
  47. }
  48. [Fact]
  49. public void Serialize_should_return_expected_result()
  50. {
  51. var subject = CreateSubject();
  52. using (var stringWriter = new StringWriter())
  53. using (var jsonWriter = new JsonWriter(stringWriter))
  54. {
  55. var context = BsonSerializationContext.CreateRoot(jsonWriter);
  56. var value = new C { Id = 1, Children = new List<C> { new C { Id = 2, Children = new List<C>() } } };
  57. subject.Serialize(context, value);
  58. var json = stringWriter.ToString();
  59. json.Should().Be("[[]]");
  60. }
  61. }
  62. private IBsonSerializer<C> CreateSubject()
  63. {
  64. // create subject without using the global serializer registry
  65. var serializerRegistry = new BsonSerializerRegistry();
  66. var subject = new EnumerableInterfaceImplementerSerializer<C, C>(serializerRegistry);
  67. serializerRegistry.RegisterSerializer(typeof(C), subject);
  68. return subject;
  69. }
  70. }
  71. }