|
15 | 15 |
|
16 | 16 | using System;
|
17 | 17 | using System.Linq;
|
18 |
| -using MongoDB.Bson; |
| 18 | +using FluentAssertions; |
| 19 | +using MongoDB.Bson.TestHelpers.XunitExtensions; |
19 | 20 | using Xunit;
|
20 | 21 |
|
21 | 22 | namespace MongoDB.Bson.Tests
|
22 | 23 | {
|
23 | 24 | public class BsonBinaryDataTests
|
24 | 25 | {
|
| 26 | + [Theory] |
| 27 | + [ParameterAttributeData] |
| 28 | + public void constructor_should_throw_when_bytes_is_null( |
| 29 | + [Range(1, 3)] int overload) |
| 30 | + { |
| 31 | + var bytes = (byte[])null; |
| 32 | + |
| 33 | + Exception exception = null; |
| 34 | + switch (overload) |
| 35 | + { |
| 36 | + case 1: exception = Record.Exception(() => new BsonBinaryData(bytes)); break; |
| 37 | + case 2: exception = Record.Exception(() => new BsonBinaryData(bytes, BsonBinarySubType.Binary)); break; |
| 38 | + case 3: exception = Record.Exception(() => new BsonBinaryData(bytes, BsonBinarySubType.Binary, GuidRepresentation.Unspecified)); break; |
| 39 | + } |
| 40 | + |
| 41 | + var e = exception.Should().BeOfType<ArgumentNullException>().Subject; |
| 42 | + e.ParamName.Should().Be("bytes"); |
| 43 | + } |
| 44 | + |
| 45 | + [Theory] |
| 46 | + [ParameterAttributeData] |
| 47 | + public void constructor_should_throw_when_bytes_length_is_not_16_and_sub_type_is_uuid( |
| 48 | + [Values(BsonBinarySubType.UuidLegacy, BsonBinarySubType.UuidStandard)] BsonBinarySubType subType, |
| 49 | + [Values(0, 15, 17)] int length, |
| 50 | + [Range(1, 2)] int overload) |
| 51 | + { |
| 52 | + var bytes = new byte[length]; |
| 53 | + var guidRepresentation = subType == BsonBinarySubType.UuidLegacy ? GuidRepresentation.CSharpLegacy : GuidRepresentation.Standard; |
| 54 | + |
| 55 | + Exception exception = null; |
| 56 | + switch (overload) |
| 57 | + { |
| 58 | + case 1: exception = Record.Exception(() => new BsonBinaryData(bytes, subType)); break; |
| 59 | + case 2: exception = Record.Exception(() => new BsonBinaryData(bytes, subType, guidRepresentation)); break; |
| 60 | + } |
| 61 | + |
| 62 | + var e = exception.Should().BeOfType<ArgumentException>().Subject; |
| 63 | + e.Message.Should().StartWith($"Length must be 16, not {length}, when subType is {subType}."); |
| 64 | + e.ParamName.Should().Be("bytes"); |
| 65 | + } |
| 66 | + |
| 67 | + [Theory] |
| 68 | + [ParameterAttributeData] |
| 69 | + public void constructor_should_throw_when_sub_type_is_uuid_and_guid_representation_is_invalid( |
| 70 | + [Values(5)] GuidRepresentation guidRepresentation, |
| 71 | + [Range(1, 2)] int overload) |
| 72 | + { |
| 73 | + var bytes = new byte[16]; |
| 74 | + var guid = Guid.Empty; |
| 75 | + |
| 76 | + Exception exception = null; |
| 77 | + switch (overload) |
| 78 | + { |
| 79 | + case 1: exception = Record.Exception(() => new BsonBinaryData(bytes, BsonBinarySubType.UuidLegacy, guidRepresentation)); break; |
| 80 | + case 2: exception = Record.Exception(() => new BsonBinaryData(guid, guidRepresentation)); break; |
| 81 | + } |
| 82 | + |
| 83 | + var e = exception.Should().BeOfType<ArgumentException>().Subject; |
| 84 | + e.Message.Should().StartWith($"Invalid guidRepresentation: 5."); |
| 85 | + e.ParamName.Should().Be("guidRepresentation"); |
| 86 | + } |
| 87 | + |
| 88 | + [Theory] |
| 89 | + [InlineData(BsonBinarySubType.UuidLegacy, GuidRepresentation.Standard, "GuidRepresentation Standard is only valid with subType UuidStandard, not with subType UuidLegacy.")] |
| 90 | + [InlineData(BsonBinarySubType.UuidStandard, GuidRepresentation.CSharpLegacy, "GuidRepresentation CSharpLegacy is only valid with subType UuidLegacy, not with subType UuidStandard.")] |
| 91 | + [InlineData(BsonBinarySubType.UuidStandard, GuidRepresentation.JavaLegacy, "GuidRepresentation JavaLegacy is only valid with subType UuidLegacy, not with subType UuidStandard.")] |
| 92 | + [InlineData(BsonBinarySubType.UuidStandard, GuidRepresentation.PythonLegacy, "GuidRepresentation PythonLegacy is only valid with subType UuidLegacy, not with subType UuidStandard.")] |
| 93 | + [InlineData(BsonBinarySubType.UuidStandard, GuidRepresentation.Unspecified, "GuidRepresentation Unspecified is only valid with subType UuidLegacy, not with subType UuidStandard.")] |
| 94 | + public void constructor_should_throw_when_sub_type_is_uuid_and_guid_representation_is_invalid_with_sub_type(BsonBinarySubType subType, GuidRepresentation guidRepresentation, string expectedMessage) |
| 95 | + { |
| 96 | + var bytes = new byte[16]; |
| 97 | + |
| 98 | + var exception = Record.Exception(() => new BsonBinaryData(bytes, subType, guidRepresentation)); |
| 99 | + |
| 100 | + var e = exception.Should().BeOfType<ArgumentException>().Subject; |
| 101 | + e.Message.Should().StartWith(expectedMessage); |
| 102 | + e.ParamName.Should().Be("guidRepresentation"); |
| 103 | + } |
| 104 | + |
| 105 | + [Theory] |
| 106 | + [ParameterAttributeData] |
| 107 | + public void constructor_should_throw_when_sub_type_is_not_uuid_and_guid_representation_is_not_unspecified( |
| 108 | + [Values(BsonBinarySubType.Binary)] BsonBinarySubType subType, |
| 109 | + [Values(GuidRepresentation.CSharpLegacy, GuidRepresentation.Standard)] GuidRepresentation guidRepresentation) |
| 110 | + { |
| 111 | + var bytes = new byte[0]; |
| 112 | + |
| 113 | + var exception = Record.Exception(() => new BsonBinaryData(bytes, subType, guidRepresentation)); |
| 114 | + |
| 115 | + var e = exception.Should().BeOfType<ArgumentException>().Subject; |
| 116 | + e.Message.Should().StartWith($"GuidRepresentation must be Unspecified, not {guidRepresentation}, when subType is not UuidStandard or UuidLegacy."); |
| 117 | + e.ParamName.Should().Be("guidRepresentation"); |
| 118 | + } |
| 119 | + |
25 | 120 | [Fact]
|
26 | 121 | public void TestCreateNull()
|
27 | 122 | {
|
|
0 commit comments