-Refactor ExceptionAssert

This commit is contained in:
James Newton-King
2014-09-12 21:33:53 +12:00
parent 4b9ce67ab7
commit dfb2ac17a5
43 changed files with 979 additions and 1482 deletions
@@ -419,29 +419,27 @@ namespace Newtonsoft.Json.Tests.Bson
[Test]
public void ReadAsInt32BadString()
{
ExceptionAssert.Throws<JsonReaderException>(
"Could not convert string to integer: a. Path '[0]'.",
() =>
{
byte[] data = HexToBytes("20-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-02-32-00-02-00-00-00-63-00-00");
ExceptionAssert.Throws<JsonReaderException>(() =>
{
byte[] data = HexToBytes("20-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-02-32-00-02-00-00-00-63-00-00");
MemoryStream ms = new MemoryStream(data);
BsonReader reader = new BsonReader(ms);
MemoryStream ms = new MemoryStream(data);
BsonReader reader = new BsonReader(ms);
Assert.AreEqual(false, reader.ReadRootValueAsArray);
Assert.AreEqual(DateTimeKind.Local, reader.DateTimeKindHandling);
Assert.AreEqual(false, reader.ReadRootValueAsArray);
Assert.AreEqual(DateTimeKind.Local, reader.DateTimeKindHandling);
reader.ReadRootValueAsArray = true;
reader.DateTimeKindHandling = DateTimeKind.Utc;
reader.ReadRootValueAsArray = true;
reader.DateTimeKindHandling = DateTimeKind.Utc;
Assert.AreEqual(true, reader.ReadRootValueAsArray);
Assert.AreEqual(DateTimeKind.Utc, reader.DateTimeKindHandling);
Assert.AreEqual(true, reader.ReadRootValueAsArray);
Assert.AreEqual(DateTimeKind.Utc, reader.DateTimeKindHandling);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
reader.ReadAsInt32();
});
reader.ReadAsInt32();
}, "Could not convert string to integer: a. Path '[0]'.");
}
[Test]
@@ -415,57 +415,53 @@ namespace Newtonsoft.Json.Tests.Bson
[Test]
public void WriteComment()
{
ExceptionAssert.Throws<JsonWriterException>("Cannot write JSON comment as BSON. Path ''.",
() =>
{
MemoryStream ms = new MemoryStream();
BsonWriter writer = new BsonWriter(ms);
ExceptionAssert.Throws<JsonWriterException>(() =>
{
MemoryStream ms = new MemoryStream();
BsonWriter writer = new BsonWriter(ms);
writer.WriteStartArray();
writer.WriteComment("fail");
});
writer.WriteStartArray();
writer.WriteComment("fail");
}, "Cannot write JSON comment as BSON. Path ''.");
}
[Test]
public void WriteConstructor()
{
ExceptionAssert.Throws<JsonWriterException>("Cannot write JSON constructor as BSON. Path ''.",
() =>
{
MemoryStream ms = new MemoryStream();
BsonWriter writer = new BsonWriter(ms);
ExceptionAssert.Throws<JsonWriterException>(() =>
{
MemoryStream ms = new MemoryStream();
BsonWriter writer = new BsonWriter(ms);
writer.WriteStartArray();
writer.WriteStartConstructor("fail");
});
writer.WriteStartArray();
writer.WriteStartConstructor("fail");
}, "Cannot write JSON constructor as BSON. Path ''.");
}
[Test]
public void WriteRaw()
{
ExceptionAssert.Throws<JsonWriterException>("Cannot write raw JSON as BSON. Path ''.",
() =>
{
MemoryStream ms = new MemoryStream();
BsonWriter writer = new BsonWriter(ms);
ExceptionAssert.Throws<JsonWriterException>(() =>
{
MemoryStream ms = new MemoryStream();
BsonWriter writer = new BsonWriter(ms);
writer.WriteStartArray();
writer.WriteRaw("fail");
});
writer.WriteStartArray();
writer.WriteRaw("fail");
}, "Cannot write raw JSON as BSON. Path ''.");
}
[Test]
public void WriteRawValue()
{
ExceptionAssert.Throws<JsonWriterException>("Cannot write raw JSON as BSON. Path ''.",
() =>
{
MemoryStream ms = new MemoryStream();
BsonWriter writer = new BsonWriter(ms);
ExceptionAssert.Throws<JsonWriterException>(() =>
{
MemoryStream ms = new MemoryStream();
BsonWriter writer = new BsonWriter(ms);
writer.WriteStartArray();
writer.WriteRawValue("fail");
});
writer.WriteStartArray();
writer.WriteRawValue("fail");
}, "Cannot write raw JSON as BSON. Path ''.");
}
[Test]
@@ -689,17 +685,16 @@ namespace Newtonsoft.Json.Tests.Bson
[Test]
public void WriteValueOutsideOfObjectOrArray()
{
ExceptionAssert.Throws<JsonWriterException>("Error writing String value. BSON must start with an Object or Array. Path ''.",
() =>
{
MemoryStream stream = new MemoryStream();
ExceptionAssert.Throws<JsonWriterException>(() =>
{
MemoryStream stream = new MemoryStream();
using (BsonWriter writer = new BsonWriter(stream))
{
writer.WriteValue("test");
writer.Flush();
}
});
using (BsonWriter writer = new BsonWriter(stream))
{
writer.WriteValue("test");
writer.Flush();
}
}, "Error writing String value. BSON must start with an Object or Array. Path ''.");
}
[Test]
@@ -757,11 +752,10 @@ namespace Newtonsoft.Json.Tests.Bson
BsonWriter writer = new BsonWriter(ms);
ExceptionAssert.Throws<JsonWriterException>("Error writing Binary value. BSON must start with an Object or Array. Path ''.",
() =>
{
serializer.Serialize(writer, b);
});
ExceptionAssert.Throws<JsonWriterException>(() =>
{
serializer.Serialize(writer, b);
}, "Error writing Binary value. BSON must start with an Object or Array. Path ''.");
}
public class GuidTestClass