-Refactor ExceptionAssert
This commit is contained in:
@@ -44,8 +44,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
[Test]
|
||||
public void DeserializeInvalidDataTable()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonException>("Unexpected JSON token when reading DataTable. Expected StartArray, got Integer. Path 'pending_count', line 1, position 19.",
|
||||
() => JsonConvert.DeserializeObject<DataSet>("{\"pending_count\":23,\"completed_count\":45}"));
|
||||
ExceptionAssert.Throws<JsonException>(() => JsonConvert.DeserializeObject<DataSet>("{\"pending_count\":23,\"completed_count\":45}"), "Unexpected JSON token when reading DataTable. Expected StartArray, got Integer. Path 'pending_count', line 1, position 19.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -81,50 +81,43 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
[Test]
|
||||
public void DeserializeBasicUnion_NoMatch()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>("No union type found with the name 'abcdefg'. Path 'Case', line 1, position 17.",
|
||||
() => JsonConvert.DeserializeObject<Currency>(@"{""Case"":""abcdefg"",""Fields"":[]}"));
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => JsonConvert.DeserializeObject<Currency>(@"{""Case"":""abcdefg"",""Fields"":[]}"), "No union type found with the name 'abcdefg'. Path 'Case', line 1, position 17.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeBasicUnion_MismatchedFieldCount()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>("The number of field values does not match the number of properties definied by union 'AUD'. Path '', line 1, position 27.",
|
||||
() => JsonConvert.DeserializeObject<Currency>(@"{""Case"":""AUD"",""Fields"":[1]}"));
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => JsonConvert.DeserializeObject<Currency>(@"{""Case"":""AUD"",""Fields"":[1]}"), "The number of field values does not match the number of properties definied by union 'AUD'. Path '', line 1, position 27.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeBasicUnion_NoCaseName()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>("No 'Case' property with union name found. Path '', line 1, position 14.",
|
||||
() => JsonConvert.DeserializeObject<Currency>(@"{""Fields"":[1]}"));
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => JsonConvert.DeserializeObject<Currency>(@"{""Fields"":[1]}"), "No 'Case' property with union name found. Path '', line 1, position 14.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeBasicUnion_NoFields()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>("No 'Fields' property with union fields found. Path '', line 1, position 14.",
|
||||
() => JsonConvert.DeserializeObject<Currency>(@"{""Case"":""AUD""}"));
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => JsonConvert.DeserializeObject<Currency>(@"{""Case"":""AUD""}"), "No 'Fields' property with union fields found. Path '', line 1, position 14.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeBasicUnion_UnexpectedEnd()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>("Unexpected end when reading union. Path 'Case', line 1, position 8.",
|
||||
() => JsonConvert.DeserializeObject<Currency>(@"{""Case"":"));
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => JsonConvert.DeserializeObject<Currency>(@"{""Case"":"), "Unexpected end when reading union. Path 'Case', line 1, position 8.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeBasicUnion_FieldsObject()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>("Union fields must been an array. Path 'Fields', line 1, position 24.",
|
||||
() => JsonConvert.DeserializeObject<Currency>(@"{""Case"":""AUD"",""Fields"":{}}"));
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => JsonConvert.DeserializeObject<Currency>(@"{""Case"":""AUD"",""Fields"":{}}"), "Union fields must been an array. Path 'Fields', line 1, position 24.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeBasicUnion_UnexpectedProperty()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>("Unexpected property 'Case123' found when reading union. Path 'Case123', line 1, position 11.",
|
||||
() => JsonConvert.DeserializeObject<Currency>(@"{""Case123"":""AUD""}"));
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => JsonConvert.DeserializeObject<Currency>(@"{""Case123"":""AUD""}"), "Unexpected property 'Case123' found when reading union. Path 'Case123', line 1, position 11.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -224,12 +224,11 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
[Test]
|
||||
public void DeserializeNullToNonNullable()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>("Cannot convert null value to System.DateTime. Path 'DateTimeField', line 1, position 38.",
|
||||
() =>
|
||||
{
|
||||
DateTimeTestClass c2 =
|
||||
JsonConvert.DeserializeObject<DateTimeTestClass>(@"{""PreField"":""Pre"",""DateTimeField"":null,""DateTimeOffsetField"":null,""PostField"":""Post""}", new IsoDateTimeConverter() { DateTimeStyles = DateTimeStyles.AssumeUniversal });
|
||||
});
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() =>
|
||||
{
|
||||
DateTimeTestClass c2 =
|
||||
JsonConvert.DeserializeObject<DateTimeTestClass>(@"{""PreField"":""Pre"",""DateTimeField"":null,""DateTimeOffsetField"":null,""PostField"":""Post""}", new IsoDateTimeConverter() { DateTimeStyles = DateTimeStyles.AssumeUniversal });
|
||||
}, "Cannot convert null value to System.DateTime. Path 'DateTimeField', line 1, position 38.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -102,12 +102,11 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
[Test]
|
||||
public void DeserializeNullToNonNullable()
|
||||
{
|
||||
ExceptionAssert.Throws<Exception>("Cannot convert null value to System.DateTime. Path 'DateTimeField', line 1, position 38.",
|
||||
() =>
|
||||
{
|
||||
DateTimeTestClass c2 =
|
||||
JsonConvert.DeserializeObject<DateTimeTestClass>(@"{""PreField"":""Pre"",""DateTimeField"":null,""DateTimeOffsetField"":null,""PostField"":""Post""}", new JavaScriptDateTimeConverter());
|
||||
});
|
||||
ExceptionAssert.Throws<Exception>(() =>
|
||||
{
|
||||
DateTimeTestClass c2 =
|
||||
JsonConvert.DeserializeObject<DateTimeTestClass>(@"{""PreField"":""Pre"",""DateTimeField"":null,""DateTimeOffsetField"":null,""PostField"":""Post""}", new JavaScriptDateTimeConverter());
|
||||
}, "Cannot convert null value to System.DateTime. Path 'DateTimeField', line 1, position 38.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -55,8 +55,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
[Test]
|
||||
public void DeserializeUnexpectedEnd()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>("Unexpected end when reading KeyValuePair. Path 'Key', line 1, position 14.",
|
||||
() => JsonConvert.DeserializeObject<KeyValuePair<string, int>>(@"{""Key"": ""123"","));
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => JsonConvert.DeserializeObject<KeyValuePair<string, int>>(@"{""Key"": ""123"","), "Unexpected end when reading KeyValuePair. Path 'Key', line 1, position 14.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -104,16 +104,15 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
[Test]
|
||||
public void NamedEnumDuplicateTest()
|
||||
{
|
||||
ExceptionAssert.Throws<Exception>("Enum name 'Third' already exists on enum 'NamedEnumDuplicate'.",
|
||||
() =>
|
||||
ExceptionAssert.Throws<Exception>(() =>
|
||||
{
|
||||
EnumContainer<NamedEnumDuplicate> c = new EnumContainer<NamedEnumDuplicate>
|
||||
{
|
||||
EnumContainer<NamedEnumDuplicate> c = new EnumContainer<NamedEnumDuplicate>
|
||||
{
|
||||
Enum = NamedEnumDuplicate.First
|
||||
};
|
||||
Enum = NamedEnumDuplicate.First
|
||||
};
|
||||
|
||||
JsonConvert.SerializeObject(c, Formatting.Indented, new StringEnumConverter());
|
||||
});
|
||||
JsonConvert.SerializeObject(c, Formatting.Indented, new StringEnumConverter());
|
||||
}, "Enum name 'Third' already exists on enum 'NamedEnumDuplicate'.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -347,14 +346,12 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
{
|
||||
string json = "{ \"Value\" : \"Three\" }";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Error converting value ""Three"" to type 'Newtonsoft.Json.Tests.Converters.StringEnumConverterTests+MyEnum'. Path 'Value', line 1, position 19.",
|
||||
() =>
|
||||
{
|
||||
var serializer = new JsonSerializer();
|
||||
serializer.Converters.Add(new StringEnumConverter());
|
||||
serializer.Deserialize<Bucket>(new JsonTextReader(new StringReader(json)));
|
||||
});
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() =>
|
||||
{
|
||||
var serializer = new JsonSerializer();
|
||||
serializer.Converters.Add(new StringEnumConverter());
|
||||
serializer.Deserialize<Bucket>(new JsonTextReader(new StringReader(json)));
|
||||
}, @"Error converting value ""Three"" to type 'Newtonsoft.Json.Tests.Converters.StringEnumConverterTests+MyEnum'. Path 'Value', line 1, position 19.");
|
||||
}
|
||||
|
||||
public class Bucket
|
||||
|
||||
@@ -881,17 +881,13 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
[Test]
|
||||
public void NoRootObject()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"XmlNodeConverter can only convert JSON that begins with an object.",
|
||||
() => { XmlDocument newDoc = (XmlDocument)JsonConvert.DeserializeXmlNode(@"[1]"); });
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => { XmlDocument newDoc = (XmlDocument)JsonConvert.DeserializeXmlNode(@"[1]"); }, "XmlNodeConverter can only convert JSON that begins with an object.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RootObjectMultipleProperties()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifing a DeserializeRootElementName.",
|
||||
() => { XmlDocument newDoc = (XmlDocument)JsonConvert.DeserializeXmlNode(@"{Prop1:1,Prop2:2}"); });
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => { XmlDocument newDoc = (XmlDocument)JsonConvert.DeserializeXmlNode(@"{Prop1:1,Prop2:2}"); }, "JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifing a DeserializeRootElementName.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -1007,9 +1003,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
{
|
||||
string json = @"{""count"": 773840,""photos"": null}";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifing a DeserializeRootElementName.",
|
||||
() => { JsonConvert.DeserializeXmlNode(json); });
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => { JsonConvert.DeserializeXmlNode(json); }, "JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifing a DeserializeRootElementName.");
|
||||
}
|
||||
|
||||
#if !NET20
|
||||
@@ -1018,9 +1012,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
{
|
||||
string json = @"{""count"": 773840,""photos"": null}";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifing a DeserializeRootElementName.",
|
||||
() => { JsonConvert.DeserializeXNode(json); });
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => { JsonConvert.DeserializeXNode(json); }, "JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifing a DeserializeRootElementName.");
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1481,9 +1473,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
}
|
||||
}";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"XmlNodeConverter cannot convert JSON with an empty property name to XML.",
|
||||
() => { DeserializeXmlNode(json); });
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => { DeserializeXmlNode(json); }, "XmlNodeConverter cannot convert JSON with an empty property name to XML.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
Reference in New Issue
Block a user