|
| 1 | +/* Copyright 2016 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 | + |
| 16 | +using System; |
| 17 | +using System.Collections.Generic; |
| 18 | +using System.Linq; |
| 19 | +using System.Reflection; |
| 20 | +using System.Text; |
| 21 | +using System.Threading.Tasks; |
| 22 | +using FluentAssertions; |
| 23 | +using MongoDB.Bson.Serialization; |
| 24 | +using MongoDB.Bson.Serialization.Attributes; |
| 25 | +using Xunit; |
| 26 | + |
| 27 | +namespace MongoDB.Bson.Tests.Serialization.Attributes |
| 28 | +{ |
| 29 | + public class BsonConstructorWithNoNamesProvidedTests |
| 30 | + { |
| 31 | + [Fact] |
| 32 | + public void constructor_with_int_should_be_mapped_correctly() |
| 33 | + { |
| 34 | + var classMap = BsonClassMap.LookupClassMap(typeof(C)); |
| 35 | + |
| 36 | + var constructorInfo = typeof(C).GetTypeInfo().GetConstructor(new[] { typeof(int) }); |
| 37 | + var creatorMap = classMap.CreatorMaps.Where(c => c.MemberInfo == constructorInfo).SingleOrDefault(); |
| 38 | + creatorMap.Should().NotBeNull(); |
| 39 | + var expectedArguments = new[] |
| 40 | + { |
| 41 | + typeof(C).GetTypeInfo().GetProperty("X") |
| 42 | + }; |
| 43 | + creatorMap.Arguments.Should().Equal(expectedArguments); |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public void constructor_with_int_and_string_should_be_mapped_correctly() |
| 48 | + { |
| 49 | + var classMap = BsonClassMap.LookupClassMap(typeof(C)); |
| 50 | + |
| 51 | + var constructorInfo = typeof(C).GetTypeInfo().GetConstructor(new[] { typeof(int), typeof(string) }); |
| 52 | + var creatorMap = classMap.CreatorMaps.Where(c => c.MemberInfo == constructorInfo).SingleOrDefault(); |
| 53 | + creatorMap.Should().NotBeNull(); |
| 54 | + var expectedArguments = new[] |
| 55 | + { |
| 56 | + typeof(C).GetTypeInfo().GetProperty("X"), |
| 57 | + typeof(C).GetTypeInfo().GetProperty("Y") |
| 58 | + }; |
| 59 | + creatorMap.Arguments.Should().Equal(expectedArguments); |
| 60 | + } |
| 61 | + |
| 62 | + [Fact] |
| 63 | + public void constructor_with_string_should_be_mapped_correctly() |
| 64 | + { |
| 65 | + var classMap = BsonClassMap.LookupClassMap(typeof(C)); |
| 66 | + |
| 67 | + var constructorInfo = typeof(C).GetTypeInfo().GetConstructor(new[] { typeof(string) }); |
| 68 | + var creatorMap = classMap.CreatorMaps.Where(c => c.MemberInfo == constructorInfo).SingleOrDefault(); |
| 69 | + creatorMap.Should().NotBeNull(); |
| 70 | + var expectedArguments = new[] |
| 71 | + { |
| 72 | + typeof(C).GetTypeInfo().GetProperty("Y") |
| 73 | + }; |
| 74 | + creatorMap.Arguments.Should().Equal(expectedArguments); |
| 75 | + } |
| 76 | + |
| 77 | + // nested types |
| 78 | + private class C |
| 79 | + { |
| 80 | + [BsonConstructor] |
| 81 | + public C(int x) { } |
| 82 | + |
| 83 | + [BsonConstructor] |
| 84 | + public C(string y) { } |
| 85 | + |
| 86 | + [BsonConstructor] |
| 87 | + public C(int x, string y) { } |
| 88 | + |
| 89 | + public int X { get; private set; } |
| 90 | + public string Y { get; private set; } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + public class BsonConstructorAttributeWhenArgumentNameDoesNotExistTests |
| 95 | + { |
| 96 | + [Fact] |
| 97 | + public void Apply_should_throw() |
| 98 | + { |
| 99 | + var exception = Record.Exception(() => BsonClassMap.LookupClassMap(typeof(C))); |
| 100 | + |
| 101 | + exception.Should().BeOfType<BsonSerializationException>(); |
| 102 | + } |
| 103 | + |
| 104 | + // nested types |
| 105 | + private class C |
| 106 | + { |
| 107 | + [BsonConstructor("X")] |
| 108 | + public C(int y) |
| 109 | + { |
| 110 | + } |
| 111 | + |
| 112 | + public int Y { get; private set; } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + public class BsonConstructorAttributeWhenParameterNameDoesNotExistTests |
| 117 | + { |
| 118 | + [Fact] |
| 119 | + public void Apply_should_throw() |
| 120 | + { |
| 121 | + var exception = Record.Exception(() => BsonClassMap.LookupClassMap(typeof(C))); |
| 122 | + |
| 123 | + exception.Should().BeOfType<BsonSerializationException>(); |
| 124 | + } |
| 125 | + |
| 126 | + // nested types |
| 127 | + private class C |
| 128 | + { |
| 129 | + [BsonConstructor] |
| 130 | + public C(int x) |
| 131 | + { |
| 132 | + } |
| 133 | + |
| 134 | + public int Y { get; private set; } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + public class BsonConstructorAttributeWhenArgumentsCountIsWrongTests |
| 139 | + { |
| 140 | + [Fact] |
| 141 | + public void Apply_should_throw() |
| 142 | + { |
| 143 | + var exception = Record.Exception(() => BsonClassMap.LookupClassMap(typeof(C))); |
| 144 | + |
| 145 | + var argumentException = exception.Should().BeOfType<ArgumentException>().Subject; |
| 146 | + argumentException.ParamName.Should().Be("arguments"); |
| 147 | + } |
| 148 | + |
| 149 | + // nested types |
| 150 | + private class C |
| 151 | + { |
| 152 | + [BsonConstructor("X", "Y")] |
| 153 | + public C(int y) |
| 154 | + { |
| 155 | + } |
| 156 | + |
| 157 | + public int X { get; private set; } |
| 158 | + public int Y { get; private set; } |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments