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] [Test]
public void BadWriteEndArray() public void BadWriteEndArray()
{ {
+11 -14
View File
@@ -367,7 +367,7 @@ namespace Newtonsoft.Json.Tests
public static class ExceptionAssert 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 where TException : Exception
{ {
try try
@@ -375,26 +375,23 @@ namespace Newtonsoft.Json.Tests
action(); action();
Assert.Fail("Exception of type {0} expected. No exception thrown.", typeof(TException).Name); Assert.Fail("Exception of type {0} expected. No exception thrown.", typeof(TException).Name);
return null;
} }
catch (TException ex) catch (TException ex)
{ {
if (possibleMessages != null && possibleMessages.Length > 0) if (possibleMessages == null || possibleMessages.Length == 0)
{ {
bool match = false; return ex;
foreach (string possibleMessage in possibleMessages) }
foreach (string possibleMessage in possibleMessages)
{
if (StringAssert.Equals(possibleMessage, ex.Message))
{ {
if (StringAssert.Equals(possibleMessage, ex.Message)) return ex;
{
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);
} }
} }
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) catch (Exception ex)
{ {
+54 -59
View File
@@ -508,64 +508,7 @@ namespace Newtonsoft.Json
/// A null value can be passed to the method for token's that don't have a value, e.g. <see cref="JsonToken.StartObject"/>.</param> /// A null value can be passed to the method for token's that don't have a value, e.g. <see cref="JsonToken.StartObject"/>.</param>
public void WriteToken(JsonToken token, object value) public void WriteToken(JsonToken token, object value)
{ {
WriteTokenInternal(token, value); switch (token)
}
/// <summary>
/// Writes the <see cref="JsonToken"/> token.
/// </summary>
/// <param name="token">The <see cref="JsonToken"/> to write.</param>
public void WriteToken(JsonToken token)
{
WriteTokenInternal(token, null);
}
internal void WriteToken(JsonReader reader, bool writeChildren, bool writeDateConstructorAsDate, bool writeComments)
{
int initialDepth;
if (reader.TokenType == JsonToken.None)
{
initialDepth = -1;
}
else if (!JsonTokenUtils.IsStartToken(reader.TokenType))
{
initialDepth = reader.Depth + 1;
}
else
{
initialDepth = reader.Depth;
}
WriteToken(reader, initialDepth, writeChildren, writeDateConstructorAsDate, writeComments);
}
internal void WriteToken(JsonReader reader, int initialDepth, bool writeChildren, bool writeDateConstructorAsDate, bool writeComments)
{
do
{
// write a JValue date when the constructor is for a date
if (writeDateConstructorAsDate && reader.TokenType == JsonToken.StartConstructor && string.Equals(reader.Value.ToString(), "Date", StringComparison.Ordinal))
{
WriteConstructorDate(reader);
}
else
{
if (reader.TokenType != JsonToken.Comment || writeComments)
{
WriteTokenInternal(reader.TokenType, reader.Value);
}
}
} while (
// stop if we have reached the end of the token being read
initialDepth - 1 < reader.Depth - (JsonTokenUtils.IsEndToken(reader.TokenType) ? 1 : 0)
&& writeChildren
&& reader.Read());
}
private void WriteTokenInternal(JsonToken tokenType, object value)
{
switch (tokenType)
{ {
case JsonToken.None: case JsonToken.None:
// read to next // read to next
@@ -670,10 +613,62 @@ namespace Newtonsoft.Json
} }
break; break;
default: default:
throw MiscellaneousUtils.CreateArgumentOutOfRangeException("TokenType", tokenType, "Unexpected token type."); throw MiscellaneousUtils.CreateArgumentOutOfRangeException(nameof(token), token, "Unexpected token type.");
} }
} }
/// <summary>
/// Writes the <see cref="JsonToken"/> token.
/// </summary>
/// <param name="token">The <see cref="JsonToken"/> to write.</param>
public void WriteToken(JsonToken token)
{
WriteToken(token, null);
}
internal void WriteToken(JsonReader reader, bool writeChildren, bool writeDateConstructorAsDate, bool writeComments)
{
int initialDepth;
if (reader.TokenType == JsonToken.None)
{
initialDepth = -1;
}
else if (!JsonTokenUtils.IsStartToken(reader.TokenType))
{
initialDepth = reader.Depth + 1;
}
else
{
initialDepth = reader.Depth;
}
WriteToken(reader, initialDepth, writeChildren, writeDateConstructorAsDate, writeComments);
}
internal void WriteToken(JsonReader reader, int initialDepth, bool writeChildren, bool writeDateConstructorAsDate, bool writeComments)
{
do
{
// write a JValue date when the constructor is for a date
if (writeDateConstructorAsDate && reader.TokenType == JsonToken.StartConstructor && string.Equals(reader.Value.ToString(), "Date", StringComparison.Ordinal))
{
WriteConstructorDate(reader);
}
else
{
if (reader.TokenType != JsonToken.Comment || writeComments)
{
WriteToken(reader.TokenType, reader.Value);
}
}
} while (
// stop if we have reached the end of the token being read
initialDepth - 1 < reader.Depth - (JsonTokenUtils.IsEndToken(reader.TokenType) ? 1 : 0)
&& writeChildren
&& reader.Read());
}
private void WriteConstructorDate(JsonReader reader) private void WriteConstructorDate(JsonReader reader)
{ {
if (!reader.Read()) if (!reader.Read())