-Added Path to JsonReaderException/JsonSerializationException messages

-Added JsonException
-Fixed all Exceptions thrown to be more specific
This commit is contained in:
James Newton-King
2012-04-21 17:25:44 +12:00
parent c120bf530d
commit a7bddf989f
69 changed files with 1400 additions and 1161 deletions
+120 -112
View File
@@ -23,7 +23,7 @@ namespace Newtonsoft.Json.Tests.Linq
[Test]
public void Clear()
{
JArray a = new JArray { 1 };
JArray a = new JArray {1};
Assert.AreEqual(1, a.Count);
a.Clear();
@@ -44,7 +44,7 @@ namespace Newtonsoft.Json.Tests.Linq
{
JValue v = new JValue(1);
JArray a = new JArray { v };
JArray a = new JArray {v};
Assert.AreEqual(false, a.Contains(new JValue(2)));
Assert.AreEqual(false, a.Contains(new JValue(1)));
@@ -63,64 +63,62 @@ namespace Newtonsoft.Json.Tests.Linq
JToken[] a = new JToken[5];
((ICollection<JToken>)j).CopyTo(a, 1);
((ICollection<JToken>) j).CopyTo(a, 1);
Assert.AreEqual(null, a[0]);
Assert.AreEqual(1, (int)a[1]);
Assert.AreEqual(1, (int) a[1]);
Assert.AreEqual(2, (int)a[2]);
Assert.AreEqual(2, (int) a[2]);
Assert.AreEqual(3, (int)a[3]);
Assert.AreEqual(3, (int) a[3]);
Assert.AreEqual(null, a[4]);
}
[Test]
[ExpectedException(typeof(ArgumentNullException)
#if !NETFX_CORE
, ExpectedMessage = @"Value cannot be null.
Parameter name: array"
#endif
)]
public void GenericCollectionCopyToNullArrayShouldThrow()
{
JArray j = new JArray();
((ICollection<JToken>)j).CopyTo(null, 0);
ExceptionAssert.Throws<ArgumentNullException>(
@"Value cannot be null.
Parameter name: array",
() =>
{
((ICollection<JToken>) j).CopyTo(null, 0);
});
}
[Test]
[ExpectedException(typeof(ArgumentOutOfRangeException)
#if !NETFX_CORE
, ExpectedMessage = @"arrayIndex is less than 0.
Parameter name: arrayIndex"
#endif
)]
public void GenericCollectionCopyToNegativeArrayIndexShouldThrow()
{
JArray j = new JArray();
((ICollection<JToken>)j).CopyTo(new JToken[1], -1);
ExceptionAssert.Throws<ArgumentOutOfRangeException>(
@"arrayIndex is less than 0.
Parameter name: arrayIndex",
() =>
{
((ICollection<JToken>) j).CopyTo(new JToken[1], -1);
});
}
[Test]
[ExpectedException(typeof(ArgumentException)
#if !NETFX_CORE
, ExpectedMessage = @"arrayIndex is equal to or greater than the length of array."
#endif
)]
public void GenericCollectionCopyToArrayIndexEqualGreaterToArrayLengthShouldThrow()
{
JArray j = new JArray();
((ICollection<JToken>)j).CopyTo(new JToken[1], 1);
ExceptionAssert.Throws<ArgumentException>(
@"arrayIndex is equal to or greater than the length of array.",
() =>
{
((ICollection<JToken>) j).CopyTo(new JToken[1], 1);
});
}
[Test]
[ExpectedException(typeof(ArgumentException)
#if !NETFX_CORE
, ExpectedMessage = @"The number of elements in the source JObject is greater than the available space from arrayIndex to the end of the destination array."
#endif
)]
public void GenericCollectionCopyToInsufficientArrayCapacity()
{
JArray j = new JArray();
@@ -128,7 +126,12 @@ Parameter name: arrayIndex"
j.Add(new JValue(2));
j.Add(new JValue(3));
((ICollection<JToken>)j).CopyTo(new JToken[3], 1);
ExceptionAssert.Throws<ArgumentException>(
@"The number of elements in the source JObject is greater than the available space from arrayIndex to the end of the destination array.",
() =>
{
((ICollection<JToken>)j).CopyTo(new JToken[3], 1);
});
}
[Test]
@@ -200,29 +203,31 @@ Parameter name: arrayIndex"
}
[Test]
[ExpectedException(typeof(ArgumentOutOfRangeException)
#if !NETFX_CORE
, ExpectedMessage = @"Index is equal to or greater than Count.
Parameter name: index"
#endif
)]
public void RemoveAtOutOfRangeIndexShouldError()
{
JArray j = new JArray();
j.RemoveAt(0);
ExceptionAssert.Throws<ArgumentOutOfRangeException>(
@"Index is equal to or greater than Count.
Parameter name: index",
() =>
{
j.RemoveAt(0);
});
}
[Test]
[ExpectedException(typeof(ArgumentOutOfRangeException)
#if !NETFX_CORE
, ExpectedMessage = @"Index is less than 0.
Parameter name: index"
#endif
)]
public void RemoveAtNegativeIndexShouldError()
{
JArray j = new JArray();
j.RemoveAt(-1);
ExceptionAssert.Throws<ArgumentOutOfRangeException>(
@"Index is less than 0.
Parameter name: index",
() =>
{
j.RemoveAt(-1);
});
}
[Test]
@@ -292,33 +297,35 @@ Parameter name: index"
JArray j = new JArray();
j.Insert(0, null);
Assert.AreEqual(null, ((JValue)j[0]).Value);
Assert.AreEqual(null, ((JValue) j[0]).Value);
}
[Test]
[ExpectedException(typeof(ArgumentOutOfRangeException)
#if !NETFX_CORE
, ExpectedMessage = @"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"
#endif
)]
public void InsertNegativeIndexShouldThrow()
{
JArray j = new JArray();
j.Insert(-1, new JValue(1));
ExceptionAssert.Throws<ArgumentOutOfRangeException>(
@"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index",
() =>
{
j.Insert(-1, new JValue(1));
});
}
[Test]
[ExpectedException(typeof(ArgumentOutOfRangeException)
#if !NETFX_CORE
, ExpectedMessage = @"Index must be within the bounds of the List.
Parameter name: index"
#endif
)]
public void InsertOutOfRangeIndexShouldThrow()
{
JArray j = new JArray();
j.Insert(2, new JValue(1));
ExceptionAssert.Throws<ArgumentOutOfRangeException>(
@"Index must be within the bounds of the List.
Parameter name: index",
() =>
{
j.Insert(2, new JValue(1));
});
}
[Test]
@@ -344,15 +351,16 @@ Parameter name: index"
}
[Test]
[ExpectedException(typeof(Exception)
#if !NETFX_CORE
, ExpectedMessage = "Error reading JArray from JsonReader. Current JsonReader item is not an array: StartObject"
#endif
)]
public void Parse_ShouldThrowOnUnexpectedToken()
{
string json = @"{""prop"":""value""}";
JArray.Parse(json);
ExceptionAssert.Throws<JsonReaderException>(
"Error reading JArray from JsonReader. Current JsonReader item is not an array: StartObject. Path '', line 1, position 1.",
() =>
{
JArray.Parse(json);
});
}
public class ListItemFields
@@ -367,24 +375,24 @@ Parameter name: index"
string itemZeroText = "Zero text";
IEnumerable<ListItemFields> t = new List<ListItemFields>
{
new ListItemFields { ListItemText = "First", ListItemValue = 1 },
new ListItemFields { ListItemText = "Second", ListItemValue = 2 },
new ListItemFields { ListItemText = "Third", ListItemValue = 3 }
};
{
new ListItemFields {ListItemText = "First", ListItemValue = 1},
new ListItemFields {ListItemText = "Second", ListItemValue = 2},
new ListItemFields {ListItemText = "Third", ListItemValue = 3}
};
JObject optionValues =
new JObject(
new JProperty("options",
new JArray(
new JObject(
new JProperty("text", itemZeroText),
new JProperty("value", "0")),
new JObject(
new JProperty("options",
new JArray(
new JObject(
new JProperty("text", itemZeroText),
new JProperty("value", "0")),
from r in t
orderby r.ListItemValue
select new JObject(
new JProperty("text", r.ListItemText),
new JProperty("value", r.ListItemValue.ToString())))));
new JProperty("text", r.ListItemText),
new JProperty("value", r.ListItemValue.ToString())))));
string result = "myOptions = " + optionValues.ToString();
@@ -418,12 +426,12 @@ Parameter name: index"
int i = 1;
foreach (JToken token in a)
{
Assert.AreEqual(i, (int)token);
Assert.AreEqual(i, (int) token);
i++;
}
}
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
[Test]
public void ITypedListGetItemProperties()
@@ -447,21 +455,21 @@ Parameter name: index"
a.Add(a);
Assert.AreEqual(3, a.Count);
Assert.AreEqual(1, (int)a[0]);
Assert.AreEqual(2, (int)a[1]);
Assert.AreEqual(1, (int) a[0]);
Assert.AreEqual(2, (int) a[1]);
Assert.AreNotSame(a, a[2]);
}
[Test]
[ExpectedException(typeof(ArgumentException)
#if !NETFX_CORE
, ExpectedMessage = @"Set JArray values with invalid key value: ""badvalue"". Array position index expected."
#endif
)]
public void SetValueWithInvalidIndex()
{
JArray a = new JArray();
a["badvalue"] = new JValue(3);
ExceptionAssert.Throws<ArgumentException>(
@"Set JArray values with invalid key value: ""badvalue"". Array position index expected.",
() =>
{
JArray a = new JArray();
a["badvalue"] = new JValue(3);
});
}
[Test]
@@ -469,35 +477,35 @@ Parameter name: index"
{
object key = 0;
JArray a = new JArray((object)null);
JArray a = new JArray((object) null);
a[key] = new JValue(3);
Assert.AreEqual(3, (int)a[key]);
Assert.AreEqual(3, (int) a[key]);
}
[Test]
public void ReplaceAll()
{
JArray a = new JArray(new [] { 1, 2, 3 });
JArray a = new JArray(new[] {1, 2, 3});
Assert.AreEqual(3, a.Count);
Assert.AreEqual(1, (int)a[0]);
Assert.AreEqual(2, (int)a[1]);
Assert.AreEqual(3, (int)a[2]);
Assert.AreEqual(1, (int) a[0]);
Assert.AreEqual(2, (int) a[1]);
Assert.AreEqual(3, (int) a[2]);
a.ReplaceAll(1);
Assert.AreEqual(1, a.Count);
Assert.AreEqual(1, (int)a[0]);
Assert.AreEqual(1, (int) a[0]);
}
[Test]
[ExpectedException(typeof(Exception)
#if !NETFX_CORE
, ExpectedMessage = "Unexpected end of content while loading JArray."
#endif
)]
public void ParseIncomplete()
{
JArray.Parse("[1");
ExceptionAssert.Throws<JsonReaderException>(
"Unexpected end of content while loading JArray. Path '[0]', line 1, position 2.",
() =>
{
JArray.Parse("[1");
});
}
[Test]
@@ -508,16 +516,11 @@ Parameter name: index"
array.Insert(1, 456);
Assert.AreEqual(2, array.Count);
Assert.AreEqual(123, (int)array[0]);
Assert.AreEqual(456, (int)array[1]);
Assert.AreEqual(123, (int) array[0]);
Assert.AreEqual(456, (int) array[1]);
}
[Test]
[ExpectedException(typeof(JsonReaderException)
#if !NETFX_CORE
, ExpectedMessage = "Additional text encountered after finished reading JSON content: ,. Line 5, position 2."
#endif
)]
public void ParseAdditionalContent()
{
string json = @"[
@@ -526,7 +529,12 @@ Parameter name: index"
""Large""
], 987987";
JArray.Parse(json);
ExceptionAssert.Throws<JsonReaderException>(
"Additional text encountered after finished reading JSON content: ,. Path '', line 5, position 2.",
() =>
{
JArray.Parse(json);
});
}
}
}