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:
Jon Hanna
2015-12-20 21:34:21 +00:00
parent dca5260334
commit bf2e5a29aa
3 changed files with 78 additions and 73 deletions
@@ -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()
{
+11 -14
View File
@@ -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)
{