-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
@@ -86,16 +86,16 @@ namespace Newtonsoft.Json.Tests.Linq
}
[Test]
[ExpectedException(typeof(ArgumentException)
#if !NETFX_CORE
, ExpectedMessage = "Could not determine JSON object type for type System.String[]."
#endif
)]
public void JObjectPropertyNameWithNonToken()
{
dynamic d = new JObject();
ExceptionAssert.Throws<ArgumentException>(
"Could not determine JSON object type for type System.String[].",
() =>
{
dynamic d = new JObject();
d.First = new [] {"One", "II", "3"};
d.First = new[] { "One", "II", "3" };
});
}
[Test]
+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);
});
}
}
}
+76 -62
View File
@@ -113,16 +113,16 @@ namespace Newtonsoft.Json.Tests.Linq
}
[Test]
[ExpectedException(typeof(ArgumentException)
#if !NETFX_CORE
, ExpectedMessage = "Can not add property PropertyNameValue to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object."
#endif
)]
public void DuplicatePropertyNameShouldThrow()
{
JObject o = new JObject();
o.Add("PropertyNameValue", null);
o.Add("PropertyNameValue", null);
ExceptionAssert.Throws<ArgumentException>(
"Can not add property PropertyNameValue to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object.",
() =>
{
JObject o = new JObject();
o.Add("PropertyNameValue", null);
o.Add("PropertyNameValue", null);
});
}
[Test]
@@ -227,57 +227,57 @@ namespace Newtonsoft.Json.Tests.Linq
}
[Test]
[ExpectedException(typeof(ArgumentNullException)
#if !NETFX_CORE
, ExpectedMessage = @"Value cannot be null.
Parameter name: array"
#endif
)]
public void GenericCollectionCopyToNullArrayShouldThrow()
{
JObject o = new JObject();
((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(null, 0);
ExceptionAssert.Throws<ArgumentException>(
@"Value cannot be null.
Parameter name: array",
() =>
{
JObject o = new JObject();
((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(null, 0);
});
}
[Test]
[ExpectedException(typeof(ArgumentOutOfRangeException)
#if !NETFX_CORE
, ExpectedMessage = @"arrayIndex is less than 0.
Parameter name: arrayIndex"
#endif
)]
public void GenericCollectionCopyToNegativeArrayIndexShouldThrow()
{
JObject o = new JObject();
((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(new KeyValuePair<string, JToken>[1], -1);
ExceptionAssert.Throws<ArgumentOutOfRangeException>(
@"arrayIndex is less than 0.
Parameter name: arrayIndex",
() =>
{
JObject o = new JObject();
((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(new KeyValuePair<string, 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()
{
JObject o = new JObject();
((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(new KeyValuePair<string, JToken>[1], 1);
ExceptionAssert.Throws<ArgumentException>(
@"arrayIndex is equal to or greater than the length of array.",
() =>
{
JObject o = new JObject();
((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(new KeyValuePair<string, 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()
{
JObject o = new JObject();
o.Add("PropertyNameValue", new JValue(1));
o.Add("PropertyNameValue2", new JValue(2));
o.Add("PropertyNameValue3", new JValue(3));
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.",
() =>
{
JObject o = new JObject();
o.Add("PropertyNameValue", new JValue(1));
o.Add("PropertyNameValue2", new JValue(2));
o.Add("PropertyNameValue3", new JValue(3));
((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(new KeyValuePair<string, JToken>[3], 1);
((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(new KeyValuePair<string, JToken>[3], 1);
});
}
[Test]
@@ -363,7 +363,7 @@ Parameter name: arrayIndex"
[Test]
public void Parse_ShouldThrowOnUnexpectedToken()
{
ExceptionAssert.Throws<Exception>("Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray",
ExceptionAssert.Throws<JsonReaderException>("Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.",
() =>
{
string json = @"[""prop""]";
@@ -400,15 +400,13 @@ Parameter name: arrayIndex"
public void Blog()
{
ExceptionAssert.Throws<JsonReaderException>(
"Invalid property identifier character: ]. Line 3, position 5.",
"Invalid property identifier character: ]. Path 'name', line 3, position 5.",
() =>
{
JObject person = JObject.Parse(@"{
JObject.Parse(@"{
""name"": ""James"",
]!#$THIS IS: BAD JSON![{}}}}]
}");
// Invalid property identifier character: ]. Line 3, position 9.
});
}
@@ -1271,19 +1269,27 @@ Parameter name: arrayIndex"
}
[Test]
[ExpectedException(typeof(NotSupportedException))]
public void IBindingListApplySort()
{
IBindingList l = new JObject();
l.ApplySort(null, ListSortDirection.Ascending);
ExceptionAssert.Throws<NotSupportedException>(
"Specified method is not supported.",
() =>
{
IBindingList l = new JObject();
l.ApplySort(null, ListSortDirection.Ascending);
});
}
[Test]
[ExpectedException(typeof(NotSupportedException))]
public void IBindingListRemoveSort()
{
IBindingList l = new JObject();
l.RemoveSort();
ExceptionAssert.Throws<NotSupportedException>(
"Specified method is not supported.",
() =>
{
IBindingList l = new JObject();
l.RemoveSort();
});
}
[Test]
@@ -1295,11 +1301,15 @@ Parameter name: arrayIndex"
}
[Test]
[ExpectedException(typeof(NotSupportedException))]
public void IBindingListFind()
{
IBindingList l = new JObject();
l.Find(null, null);
ExceptionAssert.Throws<NotSupportedException>(
"Specified method is not supported.",
() =>
{
IBindingList l = new JObject();
l.Find(null, null);
});
}
[Test]
@@ -1310,11 +1320,15 @@ Parameter name: arrayIndex"
}
[Test]
[ExpectedException(typeof(Exception), ExpectedMessage = "Could not determine new value to add to 'Newtonsoft.Json.Linq.JObject'.")]
public void IBindingListAddNew()
{
IBindingList l = new JObject();
l.AddNew();
ExceptionAssert.Throws<JsonException>(
"Could not determine new value to add to 'Newtonsoft.Json.Linq.JObject'.",
() =>
{
IBindingList l = new JObject();
l.AddNew();
});
}
[Test]
@@ -1580,7 +1594,7 @@ Parameter name: arrayIndex"
[Test]
public void NumberTooBigForInt64()
{
ExceptionAssert.Throws<JsonReaderException>("JSON integer 307953220000517141511 is too large or small for an Int64. Line 1, position 30.",
ExceptionAssert.Throws<JsonReaderException>("JSON integer 307953220000517141511 is too large or small for an Int64. Path 'code', line 1, position 30.",
() =>
{
string json = @"{""code"": 307953220000517141511}";
@@ -1592,7 +1606,7 @@ Parameter name: arrayIndex"
[Test]
public void ParseIncomplete()
{
ExceptionAssert.Throws<Exception>("Unexpected end of content while loading JObject.",
ExceptionAssert.Throws<Exception>("Unexpected end of content while loading JObject. Path 'foo', line 1, position 6.",
() =>
{
JObject.Parse("{ foo:");
@@ -1631,7 +1645,7 @@ Parameter name: arrayIndex"
[Test]
public void LoadFromNestedObjectIncomplete()
{
ExceptionAssert.Throws<Exception>("Unexpected end of content while loading JObject.",
ExceptionAssert.Throws<JsonReaderException>("Unexpected end of content while loading JObject. Path 'short.error.code', line 6, position 15.",
() =>
{
string jsonText = @"{
@@ -1729,7 +1743,7 @@ Parameter name: arrayIndex"
[Test]
public void ParseAdditionalContent()
{
ExceptionAssert.Throws<JsonReaderException>("Additional text encountered after finished reading JSON content: ,. Line 10, position 2.",
ExceptionAssert.Throws<JsonReaderException>("Additional text encountered after finished reading JSON content: ,. Path '', line 10, position 2.",
() =>
{
string json = @"{
+60 -60
View File
@@ -59,25 +59,25 @@ namespace Newtonsoft.Json.Tests.Linq
}
[Test]
[ExpectedException(typeof(Exception)
#if !NETFX_CORE
, ExpectedMessage = @"Unexpected character while parsing path indexer: ["
#endif
)]
public void BadCharactersInIndexer()
{
new JPath("Blah[[0]].Two.Three[1].Four");
ExceptionAssert.Throws<JsonException>(
@"Unexpected character while parsing path indexer: [",
() =>
{
new JPath("Blah[[0]].Two.Three[1].Four");
});
}
[Test]
[ExpectedException(typeof(Exception)
#if !NETFX_CORE
, ExpectedMessage = @"Path ended with open indexer. Expected ]"
#endif
)]
public void UnclosedIndexer()
{
new JPath("Blah[0");
ExceptionAssert.Throws<JsonException>(
@"Path ended with open indexer. Expected ]",
() =>
{
new JPath("Blah[0");
});
}
[Test]
@@ -102,25 +102,25 @@ namespace Newtonsoft.Json.Tests.Linq
}
[Test]
[ExpectedException(typeof(Exception)
#if !NETFX_CORE
, ExpectedMessage = "Empty path indexer."
#endif
)]
public void EmptyIndexer()
{
new JPath("[]");
ExceptionAssert.Throws<JsonException>(
"Empty path indexer.",
() =>
{
new JPath("[]");
});
}
[Test]
[ExpectedException(typeof(Exception)
#if !NETFX_CORE
, ExpectedMessage = "Unexpected character while parsing path: ]"
#endif
)]
public void IndexerCloseInProperty()
{
new JPath("]");
ExceptionAssert.Throws<JsonException>(
"Unexpected character while parsing path: ]",
() =>
{
new JPath("]");
});
}
[Test]
@@ -135,14 +135,14 @@ namespace Newtonsoft.Json.Tests.Linq
}
[Test]
[ExpectedException(typeof(Exception)
#if !NETFX_CORE
, ExpectedMessage = "Unexpected character following indexer: B"
#endif
)]
public void MissingDotAfterIndexer()
{
new JPath("[1]Blah");
ExceptionAssert.Throws<JsonException>(
"Unexpected character following indexer: B",
() =>
{
new JPath("[1]Blah");
});
}
[Test]
@@ -178,17 +178,17 @@ namespace Newtonsoft.Json.Tests.Linq
}
[Test]
[ExpectedException(typeof(Exception)
#if !NETFX_CORE
, ExpectedMessage = @"Index 1 not valid on JObject."
#endif
)]
public void EvaluateIndexerOnObjectWithError()
{
JObject o = new JObject(
new JProperty("Blah", 1));
o.SelectToken("[1]", true);
ExceptionAssert.Throws<JsonException>(
@"Index 1 not valid on JObject.",
() =>
{
o.SelectToken("[1]", true);
});
}
[Test]
@@ -201,43 +201,43 @@ namespace Newtonsoft.Json.Tests.Linq
}
[Test]
[ExpectedException(typeof(Exception)
#if !NETFX_CORE
, ExpectedMessage = @"Property 'BlahBlah' not valid on JArray."
#endif
)]
public void EvaluatePropertyOnArrayWithError()
{
JArray a = new JArray(1, 2, 3, 4, 5);
a.SelectToken("BlahBlah", true);
ExceptionAssert.Throws<JsonException>(
@"Property 'BlahBlah' not valid on JArray.",
() =>
{
a.SelectToken("BlahBlah", true);
});
}
[Test]
[ExpectedException(typeof(IndexOutOfRangeException)
#if !NETFX_CORE
, ExpectedMessage = @"Index 1 outside the bounds of JConstructor."
#endif
)]
public void EvaluateConstructorOutOfBoundsIndxerWithError()
{
JConstructor c = new JConstructor("Blah");
c.SelectToken("[1]", true);
ExceptionAssert.Throws<IndexOutOfRangeException>(
@"Index 1 outside the bounds of JConstructor.",
() =>
{
c.SelectToken("[1]", true);
});
}
[Test]
[ExpectedException(typeof(Exception)
#if !NETFX_CORE
, ExpectedMessage = "Property 'Missing' does not exist on JObject."
#endif
)]
public void EvaluateMissingPropertyWithError()
{
JObject o = new JObject(
new JProperty("Blah", 1));
o.SelectToken("Missing", true);
ExceptionAssert.Throws<JsonException>(
"Property 'Missing' does not exist on JObject.",
() =>
{
o.SelectToken("Missing", true);
});
}
[Test]
@@ -250,16 +250,16 @@ namespace Newtonsoft.Json.Tests.Linq
}
[Test]
[ExpectedException(typeof(IndexOutOfRangeException)
#if !NETFX_CORE
, ExpectedMessage = "Index 1000 outside the bounds of JArray."
#endif
)]
public void EvaluateArrayOutOfBoundsIndxerWithError()
{
JArray a = new JArray(1, 2, 3, 4, 5);
a.SelectToken("[1000].Ha", true);
ExceptionAssert.Throws<IndexOutOfRangeException>(
"Index 1000 outside the bounds of JArray.",
() =>
{
a.SelectToken("[1000].Ha", true);
});
}
[Test]
@@ -42,15 +42,15 @@ namespace Newtonsoft.Json.Tests.Linq
int? index = null;
l.ListChanged += (sender, args) =>
{
listChangedType = args.ListChangedType;
index = args.NewIndex;
};
{
listChangedType = args.ListChangedType;
index = args.NewIndex;
};
p.Value = 1;
Assert.AreEqual(ListChangedType.ItemChanged, listChangedType.Value);
Assert.AreEqual(0, index.Value);
Assert.AreEqual(0, index.Value);
}
#endif
@@ -64,45 +64,45 @@ namespace Newtonsoft.Json.Tests.Linq
}
[Test]
[ExpectedException(typeof(Exception)
#if !NETFX_CORE
, ExpectedMessage = "Cannot add or remove items from Newtonsoft.Json.Linq.JProperty."
#endif
)]
public void IListClear()
{
JProperty p = new JProperty("TestProperty", null);
IList l = p;
l.Clear();
ExceptionAssert.Throws<JsonException>(
"Cannot add or remove items from Newtonsoft.Json.Linq.JProperty.",
() =>
{
l.Clear();
});
}
[Test]
[ExpectedException(typeof(Exception)
#if !NETFX_CORE
, ExpectedMessage = "Newtonsoft.Json.Linq.JProperty cannot have multiple values."
#endif
)]
public void IListAdd()
{
JProperty p = new JProperty("TestProperty", null);
IList l = p;
l.Add(null);
ExceptionAssert.Throws<JsonException>(
"Newtonsoft.Json.Linq.JProperty cannot have multiple values.",
() =>
{
l.Add(null);
});
}
[Test]
[ExpectedException(typeof(Exception)
#if !NETFX_CORE
, ExpectedMessage = "Cannot add or remove items from Newtonsoft.Json.Linq.JProperty."
#endif
)]
public void IListRemove()
{
JProperty p = new JProperty("TestProperty", null);
IList l = p;
l.Remove(p.Value);
ExceptionAssert.Throws<JsonException>(
"Cannot add or remove items from Newtonsoft.Json.Linq.JProperty.",
() =>
{
l.Remove(p.Value);
});
}
[Test]
@@ -136,25 +136,25 @@ namespace Newtonsoft.Json.Tests.Linq
[Test]
public void MultiContentConstructor()
{
JProperty p = new JProperty("error", new List<string> { "one", "two" });
JProperty p = new JProperty("error", new List<string> {"one", "two"});
JArray a = (JArray) p.Value;
Assert.AreEqual(a.Count, 2);
Assert.AreEqual("one", (string)a[0]);
Assert.AreEqual("two", (string)a[1]);
Assert.AreEqual("one", (string) a[0]);
Assert.AreEqual("two", (string) a[1]);
}
[Test]
[ExpectedException(typeof(Exception)
#if !NETFX_CORE
, ExpectedMessage = "Newtonsoft.Json.Linq.JProperty cannot have multiple values."
#endif
)]
public void IListGenericAdd()
{
IList<JToken> t = new JProperty("error", new List<string> { "one", "two" });
t.Add(1);
t.Add(2);
IList<JToken> t = new JProperty("error", new List<string> {"one", "two"});
ExceptionAssert.Throws<JsonException>(
"Newtonsoft.Json.Linq.JProperty cannot have multiple values.",
() =>
{
t.Add(1);
});
}
}
}
}
@@ -123,11 +123,6 @@ namespace Newtonsoft.Json.Tests.Linq
}
[Test]
[ExpectedException(typeof(JsonReaderException)
#if !NETFX_CORE
, ExpectedMessage = "Could not convert string to DateTimeOffset: blablahbla. Line 1, position 22."
#endif
)]
public void ReadAsDateTimeOffsetBadString()
{
string json = @"{""Offset"":""blablahbla""}";
@@ -142,15 +137,15 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
reader.ReadAsDateTimeOffset();
ExceptionAssert.Throws<JsonReaderException>(
"Could not convert string to DateTimeOffset: blablahbla. Path 'Offset', line 1, position 22.",
() =>
{
reader.ReadAsDateTimeOffset();
});
}
[Test]
[ExpectedException(typeof(JsonReaderException)
#if !NETFX_CORE
, ExpectedMessage = "Error reading date. Unexpected token: Boolean. Line 1, position 14."
#endif
)]
public void ReadAsDateTimeOffsetBoolean()
{
string json = @"{""Offset"":true}";
@@ -165,7 +160,12 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
reader.ReadAsDateTimeOffset();
ExceptionAssert.Throws<JsonReaderException>(
"Error reading date. Unexpected token: Boolean. Path 'Offset', line 1, position 14.",
() =>
{
reader.ReadAsDateTimeOffset();
});
}
[Test]
@@ -311,26 +311,27 @@ namespace Newtonsoft.Json.Tests.Linq
[Test]
public void ReadBytesFailure()
{
ExceptionAssert.Throws<JsonReaderException>("Error reading bytes. Unexpected token: Integer.",
() =>
{
JObject o =
new JObject(
new JProperty("Test1", 1)
);
ExceptionAssert.Throws<JsonReaderException>(
"Error reading bytes. Unexpected token: Integer. Path 'Test1'.",
() =>
{
JObject o =
new JObject(
new JProperty("Test1", 1)
);
using (JTokenReader jsonReader = new JTokenReader(o))
{
jsonReader.Read();
Assert.AreEqual(JsonToken.StartObject, jsonReader.TokenType);
using (JTokenReader jsonReader = new JTokenReader(o))
{
jsonReader.Read();
Assert.AreEqual(JsonToken.StartObject, jsonReader.TokenType);
jsonReader.Read();
Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType);
Assert.AreEqual("Test1", jsonReader.Value);
jsonReader.Read();
Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType);
Assert.AreEqual("Test1", jsonReader.Value);
jsonReader.ReadAsBytes();
}
});
jsonReader.ReadAsBytes();
}
});
}
public class HasBytes
@@ -480,11 +481,6 @@ namespace Newtonsoft.Json.Tests.Linq
}
[Test]
[ExpectedException(typeof(JsonReaderException)
#if !NETFX_CORE
, ExpectedMessage = "Could not convert string to integer: hi. Line 1, position 12."
#endif
)]
public void ReadAsInt32BadString()
{
string json = @"{""Name"":""hi""}";
@@ -499,18 +495,15 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
reader.ReadAsInt32();
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
Assert.AreEqual(typeof(int), reader.ValueType);
Assert.AreEqual(1, reader.Value);
ExceptionAssert.Throws<JsonReaderException>(
"Could not convert string to integer: hi. Path 'Name', line 1, position 12.",
() =>
{
reader.ReadAsInt32();
});
}
[Test]
[ExpectedException(typeof(JsonReaderException)
#if !NETFX_CORE
, ExpectedMessage = "Error reading integer. Unexpected token: Boolean. Line 1, position 12."
#endif
)]
public void ReadAsInt32Boolean()
{
string json = @"{""Name"":true}";
@@ -525,7 +518,12 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
reader.ReadAsInt32();
ExceptionAssert.Throws<JsonReaderException>(
"Error reading integer. Unexpected token: Boolean. Path 'Name', line 1, position 12.",
() =>
{
reader.ReadAsInt32();
});
}
[Test]
@@ -550,11 +548,6 @@ namespace Newtonsoft.Json.Tests.Linq
}
[Test]
[ExpectedException(typeof(JsonReaderException)
#if !NETFX_CORE
, ExpectedMessage = "Could not convert string to decimal: blah. Line 1, position 14."
#endif
)]
public void ReadAsDecimalBadString()
{
string json = @"{""Name"":""blah""}";
@@ -569,15 +562,15 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
reader.ReadAsDecimal();
ExceptionAssert.Throws<JsonReaderException>(
"Could not convert string to decimal: blah. Path 'Name', line 1, position 14.",
() =>
{
reader.ReadAsDecimal();
});
}
[Test]
[ExpectedException(typeof(JsonReaderException)
#if !NETFX_CORE
, ExpectedMessage = "Error reading decimal. Unexpected token: Boolean. Line 1, position 12."
#endif
)]
public void ReadAsDecimalBoolean()
{
string json = @"{""Name"":true}";
@@ -592,7 +585,12 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
reader.ReadAsDecimal();
ExceptionAssert.Throws<JsonReaderException>(
"Error reading decimal. Unexpected token: Boolean. Path 'Name', line 1, position 12.",
() =>
{
reader.ReadAsDecimal();
});
}
[Test]
@@ -565,15 +565,15 @@ namespace Newtonsoft.Json.Tests.Linq
}
[Test]
[ExpectedException(typeof(ArgumentException)
#if !NETFX_CORE
, ExpectedMessage = "Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject."
#endif
)]
public void AddValueToObject()
{
JObject o = new JObject();
o.Add(5);
ExceptionAssert.Throws<ArgumentException>(
"Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject.",
() =>
{
JObject o = new JObject();
o.Add(5);
});
}
[Test]
@@ -770,7 +770,7 @@ namespace Newtonsoft.Json.Tests.Linq
[Test]
public void ParseAdditionalContent()
{
ExceptionAssert.Throws<JsonReaderException>("Additional text encountered after finished reading JSON content: ,. Line 5, position 2.",
ExceptionAssert.Throws<JsonReaderException>("Additional text encountered after finished reading JSON content: ,. Path '', line 5, position 2.",
() =>
{
string json = @"[