|
1 | | -/* Copyright 2010-2014 MongoDB Inc. |
| 1 | +/* Copyright 2010-2016 MongoDB Inc. |
2 | 2 | * |
3 | 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
4 | 4 | * you may not use this file except in compliance with the License. |
|
14 | 14 | */ |
15 | 15 |
|
16 | 16 | using System; |
17 | | - |
18 | | -using MongoDB.Bson; |
| 17 | +using System.Linq.Expressions; |
| 18 | +using FluentAssertions; |
19 | 19 | using MongoDB.Bson.Serialization; |
20 | 20 | using MongoDB.Bson.Serialization.Conventions; |
21 | | -using MongoDB.Bson.Serialization.Options; |
| 21 | +using MongoDB.Bson.Serialization.Serializers; |
22 | 22 | using Xunit; |
23 | 23 |
|
24 | 24 | namespace MongoDB.Bson.Tests.Serialization.Conventions |
25 | 25 | { |
26 | 26 | public class EnumRepresentationConventionTests |
27 | 27 | { |
28 | | - private enum WorkDays { |
29 | | - Monday, |
30 | | - Wednesday, |
31 | | - Friday |
32 | | - }; |
| 28 | + public enum E { A, B }; |
33 | 29 |
|
34 | | - private class TestClass |
| 30 | + public class C |
35 | 31 | { |
36 | | - public string NonEnum { get; set; } |
37 | | - public WorkDays DefaultEnum { get; set; } |
38 | | - public WorkDays ChangedRepresentationEnum { get; set; } |
| 32 | + public E E { get; set; } |
| 33 | + public E? NE { get; set; } |
| 34 | + public int I { get; set; } |
| 35 | + } |
| 36 | + |
| 37 | + [Theory] |
| 38 | + [InlineData(BsonType.Int32)] |
| 39 | + [InlineData(BsonType.Int64)] |
| 40 | + public void Apply_should_configure_serializer_when_member_is_an_enum(BsonType representation) |
| 41 | + { |
| 42 | + var subject = new EnumRepresentationConvention(representation); |
| 43 | + var memberMap = CreateMemberMap(c => c.E); |
| 44 | + |
| 45 | + subject.Apply(memberMap); |
| 46 | + |
| 47 | + var serializer = (EnumSerializer<E>)memberMap.GetSerializer(); |
| 48 | + serializer.Representation.Should().Be(representation); |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + [Theory] |
| 53 | + [InlineData(BsonType.Int32)] |
| 54 | + [InlineData(BsonType.Int64)] |
| 55 | + public void Apply_should_configure_serializer_when_member_is_a_nullable_enum(BsonType representation) |
| 56 | + { |
| 57 | + var subject = new EnumRepresentationConvention(representation); |
| 58 | + var memberMap = CreateMemberMap(c => c.NE); |
| 59 | + |
| 60 | + subject.Apply(memberMap); |
| 61 | + |
| 62 | + var serializer = (IChildSerializerConfigurable)memberMap.GetSerializer(); |
| 63 | + var childSerializer = (EnumSerializer<E>)serializer.ChildSerializer; |
| 64 | + childSerializer.Representation.Should().Be(representation); |
| 65 | + } |
| 66 | + |
| 67 | + [Fact] |
| 68 | + public void Apply_should_do_nothing_when_member_is_not_an_enum() |
| 69 | + { |
| 70 | + var subject = new EnumRepresentationConvention(BsonType.String); |
| 71 | + var memberMap = CreateMemberMap(c => c.I); |
| 72 | + var serializer = memberMap.GetSerializer(); |
| 73 | + |
| 74 | + subject.Apply(memberMap); |
| 75 | + |
| 76 | + memberMap.GetSerializer().Should().BeSameAs(serializer); |
39 | 77 | } |
40 | 78 |
|
41 | 79 | [Theory] |
42 | 80 | [InlineData(0)] |
43 | 81 | [InlineData(BsonType.Int32)] |
44 | 82 | [InlineData(BsonType.Int64)] |
45 | 83 | [InlineData(BsonType.String)] |
46 | | - public void TestConvention(BsonType value) |
| 84 | + public void constructor_should_initialize_instance_when_representation_is_valid(BsonType representation) |
47 | 85 | { |
48 | | - var convention = new EnumRepresentationConvention(value); |
49 | | - var classMap = new BsonClassMap<TestClass>(); |
50 | | - var nonEnumMemberMap = classMap.MapMember(x => x.NonEnum); |
51 | | - classMap.MapMember(x => x.DefaultEnum); |
52 | | - var changedEnumMemberMap = classMap.MapMember(x => x.ChangedRepresentationEnum); |
53 | | - convention.Apply(nonEnumMemberMap); |
54 | | - convention.Apply(changedEnumMemberMap); |
55 | | - Assert.Equal(value, ((IRepresentationConfigurable)(changedEnumMemberMap.GetSerializer())).Representation); |
| 86 | + var subject = new EnumRepresentationConvention(representation); |
| 87 | + |
| 88 | + subject.Representation.Should().Be(representation); |
56 | 89 | } |
57 | 90 |
|
58 | | - [Fact] |
59 | | - public void TestConventionOverride() |
| 91 | + [Theory] |
| 92 | + [InlineData(BsonType.Decimal128)] |
| 93 | + [InlineData(BsonType.Double)] |
| 94 | + public void constructor_should_throw_when_representation_is_not_valid(BsonType representation) |
60 | 95 | { |
61 | | - var int64Convention = new EnumRepresentationConvention(BsonType.Int64); |
62 | | - var strConvention = new EnumRepresentationConvention(BsonType.String); |
63 | | - var classMap = new BsonClassMap<TestClass>(); |
64 | | - var memberMap = classMap.MapMember(x => x.ChangedRepresentationEnum); |
65 | | - int64Convention.Apply(memberMap); |
66 | | - strConvention.Apply(memberMap); |
67 | | - Assert.Equal(BsonType.String, ((IRepresentationConfigurable)(memberMap.GetSerializer())).Representation); |
| 96 | + var exception = Record.Exception(() => new EnumRepresentationConvention(representation)); |
| 97 | + |
| 98 | + var argumentException = exception.Should().BeOfType<ArgumentException>().Subject; |
| 99 | + argumentException.ParamName.Should().Be("representation"); |
68 | 100 | } |
69 | 101 |
|
70 | | - [Fact] |
71 | | - public void TestConventionConstruction() |
| 102 | + // private methods |
| 103 | + private BsonMemberMap CreateMemberMap<TMember>(Expression<Func<C, TMember>> member) |
72 | 104 | { |
73 | | - foreach (BsonType val in Enum.GetValues(typeof(BsonType))) |
74 | | - { |
75 | | - if ((val == 0) || |
76 | | - (val == BsonType.String) || |
77 | | - (val == BsonType.Int32) || |
78 | | - (val == BsonType.Int64)) |
79 | | - { |
80 | | - new EnumRepresentationConvention(val); |
81 | | - } |
82 | | - else |
83 | | - { |
84 | | - Assert.Throws<ArgumentException>(() => { new EnumRepresentationConvention(val); }); |
85 | | - } |
86 | | - } |
| 105 | + var classMap = new BsonClassMap<C>(); |
| 106 | + return classMap.MapMember(member); |
87 | 107 | } |
88 | 108 | } |
89 | 109 | } |
0 commit comments