From bf2e5a29aab3c0798c8a341a03bb82e797c06d09 Mon Sep 17 00:00:00 2001 From: Jon Hanna Date: Sun, 20 Dec 2015 21:34:21 +0000 Subject: [PATCH] =?UTF-8?q?Correct=20ParamName=20in=20exception=20for=20in?= =?UTF-8?q?valid=20JsonToken=20on=20=E2=80=8EWriteToken?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #755 Also consolidates WriteTokenInternal into matching WriteToken (slightly reduce assembly size). Also extend ExceptionAssert.Throws to return exception for further examination. --- .../JsonTextWriterTest.cs | 13 ++ Src/Newtonsoft.Json.Tests/TestFixtureBase.cs | 25 ++-- Src/Newtonsoft.Json/JsonWriter.cs | 113 +++++++++--------- 3 files changed, 78 insertions(+), 73 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs b/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs index d92681b8..59b877d9 100644 --- a/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs @@ -897,6 +897,19 @@ Parameter name: value"); } } + [Test] + public void TokenTypeOutOfRange() + { + using (JsonWriter jsonWriter = new JsonTextWriter(new StringWriter())) + { + ArgumentOutOfRangeException ex = ExceptionAssert.Throws(() => jsonWriter.WriteToken((JsonToken)int.MinValue)); + Assert.AreEqual("token", ex.ParamName); + + ex = ExceptionAssert.Throws(() => jsonWriter.WriteToken((JsonToken)int.MinValue, "test")); + Assert.AreEqual("token", ex.ParamName); + } + } + [Test] public void BadWriteEndArray() { diff --git a/Src/Newtonsoft.Json.Tests/TestFixtureBase.cs b/Src/Newtonsoft.Json.Tests/TestFixtureBase.cs index f2afa241..14876574 100644 --- a/Src/Newtonsoft.Json.Tests/TestFixtureBase.cs +++ b/Src/Newtonsoft.Json.Tests/TestFixtureBase.cs @@ -367,7 +367,7 @@ namespace Newtonsoft.Json.Tests public static class ExceptionAssert { - public static void Throws(Action action, params string[] possibleMessages) + public static TException Throws(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) { diff --git a/Src/Newtonsoft.Json/JsonWriter.cs b/Src/Newtonsoft.Json/JsonWriter.cs index a6bc5342..9e3bda81 100644 --- a/Src/Newtonsoft.Json/JsonWriter.cs +++ b/Src/Newtonsoft.Json/JsonWriter.cs @@ -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. . public void WriteToken(JsonToken token, object value) { - WriteTokenInternal(token, value); - } - - /// - /// Writes the token. - /// - /// The to write. - 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) + switch (token) { case JsonToken.None: // read to next @@ -670,10 +613,62 @@ namespace Newtonsoft.Json } break; default: - throw MiscellaneousUtils.CreateArgumentOutOfRangeException("TokenType", tokenType, "Unexpected token type."); + throw MiscellaneousUtils.CreateArgumentOutOfRangeException(nameof(token), token, "Unexpected token type."); } } + /// + /// Writes the token. + /// + /// The to write. + 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) { if (!reader.Read())