Skip to content

Commit 44c0f79

Browse files
author
sridharn
committed
More unit tests. Some code cleanup
1 parent 0a8ed7f commit 44c0f79

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

Bson/BsonUtils.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public static DateTime ToDateTimeFromMillisecondsSinceEpoch(long millisecondsSin
5757
millisecondsSinceEpoch > BsonConstants.DateTimeMaxValueMillisecondsSinceEpoch)
5858
{
5959
var message = string.Format(
60-
"The value {0} for the BsonDateTime MillisecondsSinceEpoch is outside the range that can be converted to a .NET DateTime.",
60+
"The value {0} for the BsonDateTime MillisecondsSinceEpoch is outside the"+
61+
"range that can be converted to a .NET DateTime.",
6162
millisecondsSinceEpoch);
6263
throw new ArgumentOutOfRangeException("millisecondsSinceEpoch", message);
6364
}
@@ -80,6 +81,10 @@ public static DateTime ToDateTimeFromMillisecondsSinceEpoch(long millisecondsSin
8081
/// <returns>A hex string.</returns>
8182
public static string ToHexString(byte[] bytes)
8283
{
84+
if (bytes == null)
85+
{
86+
throw new ArgumentNullException("bytes");
87+
}
8388
var sb = new StringBuilder(bytes.Length * 2);
8489
foreach (var b in bytes)
8590
{
@@ -125,14 +130,7 @@ public static DateTime ToLocalTime(DateTime dateTime, DateTimeKind kind)
125130
public static long ToMillisecondsSinceEpoch(DateTime dateTime)
126131
{
127132
var utcDateTime = ToUniversalTime(dateTime);
128-
if (utcDateTime == DateTime.MaxValue)
129-
{
130-
return BsonConstants.DateTimeMaxValueMillisecondsSinceEpoch;
131-
}
132-
else
133-
{
134-
return (utcDateTime - BsonConstants.UnixEpoch).Ticks / 10000;
135-
}
133+
return (utcDateTime - BsonConstants.UnixEpoch).Ticks / 10000;
136134
}
137135

138136
/// <summary>
@@ -161,6 +159,7 @@ public static DateTime ToUniversalTime(DateTime dateTime)
161159
return dateTime.ToUniversalTime();
162160
}
163161
}
162+
164163
}
165164

166165
/// <summary>

BsonUnitTests/BsonUtilsTests.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,46 @@ public void TestMinToMillisConversion()
7878
var actual = BsonUtils.ToMillisecondsSinceEpoch(DateTime.MinValue);
7979
Assert.AreEqual(BsonConstants.DateTimeMinValueMillisecondsSinceEpoch, actual);
8080
}
81+
82+
[Test]
83+
public void TestToUniversalTimeUTCNow()
84+
{
85+
var expected = DateTime.UtcNow;
86+
var actual = BsonUtils.ToUniversalTime(expected);
87+
Assert.AreEqual(expected, actual);
88+
}
89+
90+
[Test]
91+
public void TestToUniversalTimeMax()
92+
{
93+
var expected = DateTime.MaxValue;
94+
var actual = BsonUtils.ToUniversalTime(expected);
95+
Assert.AreEqual(expected, actual);
96+
}
97+
98+
[Test]
99+
public void TestToUniversalTimeMin()
100+
{
101+
var expected = DateTime.MinValue;
102+
var actual = BsonUtils.ToUniversalTime(expected);
103+
Assert.AreEqual(expected, actual);
104+
}
105+
106+
[Test]
107+
public void TestToHexString()
108+
{
109+
var value = new byte[] { 0, 1,2, 3, 4, 5 ,6 ,7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 255 };
110+
var expected = "000102030405060708090a0b0c0d0e0f10ff";
111+
var actual = BsonUtils.ToHexString(value);
112+
Assert.AreEqual(expected, actual);
113+
}
114+
115+
[Test]
116+
[ExpectedException(typeof(ArgumentNullException))]
117+
public void TestToHexStringNull()
118+
{
119+
var actual = BsonUtils.ToHexString(null);
120+
}
121+
81122
}
82123
}

0 commit comments

Comments
 (0)