From 05eb00b25828f6f7c3f04f69081aed2d56fe0c01 Mon Sep 17 00:00:00 2001 From: "N. Taylor Mullen" Date: Mon, 23 Mar 2015 15:24:33 -0700 Subject: [PATCH] Update RazorError to be deserialized. - Added tests to ensure RazorError can be serialized/deserialized. #330 --- .../Parser/SyntaxTree/RazorError.cs | 11 ++-- .../Parser/RazorErrorTest.cs | 54 +++++++++++++++++++ 2 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 test/Microsoft.AspNet.Razor.Test/Parser/RazorErrorTest.cs diff --git a/src/Microsoft.AspNet.Razor/Parser/SyntaxTree/RazorError.cs b/src/Microsoft.AspNet.Razor/Parser/SyntaxTree/RazorError.cs index 8ba6998c20..aeb3d89df9 100644 --- a/src/Microsoft.AspNet.Razor/Parser/SyntaxTree/RazorError.cs +++ b/src/Microsoft.AspNet.Razor/Parser/SyntaxTree/RazorError.cs @@ -9,6 +9,11 @@ namespace Microsoft.AspNet.Razor.Parser.SyntaxTree { public class RazorError : IEquatable { + public RazorError() + : this(message: string.Empty, location: SourceLocation.Undefined) + { + } + public RazorError(string message, SourceLocation location) : this(message, location, 1) { @@ -31,9 +36,9 @@ namespace Microsoft.AspNet.Razor.Parser.SyntaxTree { } - public string Message { get; private set; } - public SourceLocation Location { get; private set; } - public int Length { get; private set; } + public string Message { get; set; } + public SourceLocation Location { get; set; } + public int Length { get; set; } public override string ToString() { diff --git a/test/Microsoft.AspNet.Razor.Test/Parser/RazorErrorTest.cs b/test/Microsoft.AspNet.Razor.Test/Parser/RazorErrorTest.cs new file mode 100644 index 0000000000..bb7705e000 --- /dev/null +++ b/test/Microsoft.AspNet.Razor.Test/Parser/RazorErrorTest.cs @@ -0,0 +1,54 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using Microsoft.AspNet.Razor.Text; +using Newtonsoft.Json; +using Xunit; + +namespace Microsoft.AspNet.Razor.Parser.SyntaxTree +{ + public class RazorErrorTest + { + [Fact] + public void RazorError_CanBeSerialized() + { + // Arrange + var error = new RazorError( + message: "Testing", + location: new SourceLocation(absoluteIndex: 1, lineIndex: 2, characterIndex: 3), + length: 456); + var expectedSerializedError = + $"{{\"{nameof(RazorError.Message)}\":\"Testing\",\"{nameof(RazorError.Location)}\":{{\"" + + $"{nameof(SourceLocation.AbsoluteIndex)}\":1,\"{nameof(SourceLocation.LineIndex)}\":2,\"" + + $"{nameof(SourceLocation.CharacterIndex)}\":3}},\"{nameof(RazorError.Length)}\":456}}"; + + // Act + var serializedError = JsonConvert.SerializeObject(error); + + // Assert + Assert.Equal(expectedSerializedError, serializedError, StringComparer.Ordinal); + } + + [Fact] + public void RazorError_CanBeDeserialized() + { + // Arrange + var error = new RazorError( + message: "Testing", + location: new SourceLocation(absoluteIndex: 1, lineIndex: 2, characterIndex: 3), + length: 456); + var serializedError = JsonConvert.SerializeObject(error); + + // Act + var deserializedError = JsonConvert.DeserializeObject(serializedError); + + // Assert + Assert.Equal("Testing", deserializedError.Message, StringComparer.Ordinal); + Assert.Equal(1, deserializedError.Location.AbsoluteIndex); + Assert.Equal(2, deserializedError.Location.LineIndex); + Assert.Equal(3, deserializedError.Location.CharacterIndex); + Assert.Equal(456, deserializedError.Length); + } + } +} \ No newline at end of file