Correct ParamName in exception for invalid JsonToken on WriteToken
Fixes #755 Also consolidates WriteTokenInternal into matching WriteToken (slightly reduce assembly size). Also extend ExceptionAssert.Throws to return exception for further examination.
This commit is contained in:
@@ -897,6 +897,19 @@ Parameter name: value");
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TokenTypeOutOfRange()
|
||||
{
|
||||
using (JsonWriter jsonWriter = new JsonTextWriter(new StringWriter()))
|
||||
{
|
||||
ArgumentOutOfRangeException ex = ExceptionAssert.Throws<ArgumentOutOfRangeException>(() => jsonWriter.WriteToken((JsonToken)int.MinValue));
|
||||
Assert.AreEqual("token", ex.ParamName);
|
||||
|
||||
ex = ExceptionAssert.Throws<ArgumentOutOfRangeException>(() => jsonWriter.WriteToken((JsonToken)int.MinValue, "test"));
|
||||
Assert.AreEqual("token", ex.ParamName);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void BadWriteEndArray()
|
||||
{
|
||||
|
||||
@@ -367,7 +367,7 @@ namespace Newtonsoft.Json.Tests
|
||||
|
||||
public static class ExceptionAssert
|
||||
{
|
||||
public static void Throws<TException>(Action action, params string[] possibleMessages)
|
||||
public static TException Throws<TException>(Action action, params string[] possibleMessages)
|
||||
where TException : Exception
|
||||
{
|
||||
try
|
||||
@@ -375,26 +375,23 @@ namespace Newtonsoft.Json.Tests
|
||||
action();
|
||||
|
||||
Assert.Fail("Exception of type {0} expected. No exception thrown.", typeof(TException).Name);
|
||||
return null;
|
||||
}
|
||||
catch (TException ex)
|
||||
{
|
||||
if (possibleMessages != null && possibleMessages.Length > 0)
|
||||
if (possibleMessages == null || possibleMessages.Length == 0)
|
||||
{
|
||||
bool match = false;
|
||||
foreach (string possibleMessage in possibleMessages)
|
||||
return ex;
|
||||
}
|
||||
foreach (string possibleMessage in possibleMessages)
|
||||
{
|
||||
if (StringAssert.Equals(possibleMessage, ex.Message))
|
||||
{
|
||||
if (StringAssert.Equals(possibleMessage, ex.Message))
|
||||
{
|
||||
match = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!match)
|
||||
{
|
||||
throw new Exception("Unexpected exception message." + Environment.NewLine + "Expected one of: " + string.Join(Environment.NewLine, possibleMessages) + Environment.NewLine + "Got: " + ex.Message + Environment.NewLine + Environment.NewLine + ex);
|
||||
return ex;
|
||||
}
|
||||
}
|
||||
|
||||
throw new Exception("Unexpected exception message." + Environment.NewLine + "Expected one of: " + string.Join(Environment.NewLine, possibleMessages) + Environment.NewLine + "Got: " + ex.Message + Environment.NewLine + Environment.NewLine + ex);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user