Skip to content

Commit bcb5efc

Browse files
author
sridharn
committed
Cleanup for CSHARP-379
1 parent f30dd0b commit bcb5efc

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

Bson/BsonUtils.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,23 @@
2424

2525
namespace MongoDB.Bson
2626
{
27+
2728
/// <summary>
2829
/// A static class containing BSON utility methods.
2930
/// </summary>
3031
public static class BsonUtils
3132
{
3233

34+
// public static methods
35+
3336
/// <summary>
3437
/// Parses a hex string into its equivalent byte array.
3538
/// </summary>
3639
/// <param name="s">The hex string to parse.</param>
3740
/// <returns>The byte equivalent of the hex string.</returns>
3841
public static byte[] ParseHexString(string s)
3942
{
40-
if (string.IsNullOrEmpty(s))
43+
if (s == null)
4144
{
4245
throw new ArgumentNullException("s");
4346
}
@@ -59,7 +62,8 @@ public static byte[] ParseHexString(string s)
5962
catch (FormatException e)
6063
{
6164
throw new FormatException(
62-
string.Format("Invalid hex string. Problem with substring {0} starting at position {1}",
65+
string.Format("Invalid hex string {0}. Problem with substring {1} starting at position {2}",
66+
s,
6367
hex,
6468
2 * i),
6569
e);

BsonUnitTests/BsonUtilsTests.cs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,11 @@ public void TestParseHexStringNull()
127127
}
128128

129129
[Test]
130-
[ExpectedException(typeof(ArgumentNullException))]
131130
public void TestParseHexStringEmpty()
132131
{
132+
byte[] expected = new byte[0];
133133
var actual = BsonUtils.ParseHexString(string.Empty);
134+
Assert.AreEqual(expected, actual);
134135
}
135136

136137
[Test]
@@ -168,17 +169,20 @@ public void TestParseHexStringInvalid2()
168169
[Test]
169170
public void TestTryParseHexStringNull()
170171
{
171-
byte[] expected;
172-
BsonUtils.TryParseHexString(null, out expected);
173-
Assert.IsNull(expected);
172+
byte[] actual;
173+
var result = BsonUtils.TryParseHexString(null, out actual);
174+
Assert.IsFalse(result);
175+
Assert.IsNull(actual);
174176
}
175177

176178
[Test]
177179
public void TestTryParseHexStringEmpty()
178180
{
179-
byte[] expected;
180-
var actual = BsonUtils.TryParseHexString(string.Empty, out expected);
181-
Assert.IsNull(expected);
181+
byte[] expected = new byte[0];
182+
byte[] actual;
183+
var result = BsonUtils.TryParseHexString(string.Empty, out actual);
184+
Assert.IsTrue(result);
185+
Assert.AreEqual(expected, actual);
182186
}
183187

184188
[Test]
@@ -187,7 +191,8 @@ public void TestTryParseHexString()
187191
var expected = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 255 };
188192
var value = "000102030405060708090a0b0c0d0e0f10ff";
189193
byte[] actual;
190-
BsonUtils.TryParseHexString(value, out actual);
194+
var result = BsonUtils.TryParseHexString(value, out actual);
195+
Assert.IsTrue(result);
191196
Assert.AreEqual(expected, actual);
192197
}
193198

@@ -197,24 +202,27 @@ public void TestTryParseHexStringOdd()
197202
var expected = new byte[] { 0, 15 };
198203
var value = "00f";
199204
byte[] actual;
200-
BsonUtils.TryParseHexString(value, out actual);
205+
var result = BsonUtils.TryParseHexString(value, out actual);
206+
Assert.IsTrue(result);
201207
Assert.AreEqual(expected, actual);
202208
}
203209

204210
[Test]
205211
public void TestTryParseHexStringInvalid()
206212
{
207-
byte[] expected;
208-
var actual = BsonUtils.TryParseHexString("1G", out expected);
209-
Assert.IsNull(expected);
213+
byte[] actual;
214+
var result = BsonUtils.TryParseHexString("1G", out actual);
215+
Assert.IsFalse(result);
216+
Assert.IsNull(actual);
210217
}
211218

212219
[Test]
213220
public void TestTryParseHexStringInvalid2()
214221
{
215-
byte[] expected;
216-
var actual = BsonUtils.TryParseHexString("00 1", out expected);
217-
Assert.IsNull(expected);
222+
byte[] actual;
223+
var result = BsonUtils.TryParseHexString("00 1", out actual);
224+
Assert.IsFalse(result);
225+
Assert.IsNull(actual);
218226
}
219227

220228
}

0 commit comments

Comments
 (0)