Update RazorError to be deserialized.

- Added tests to ensure RazorError can be serialized/deserialized.

#330
This commit is contained in:
N. Taylor Mullen 2015-03-23 15:24:33 -07:00
parent 444ea25b44
commit 05eb00b258
2 changed files with 62 additions and 3 deletions

View File

@ -9,6 +9,11 @@ namespace Microsoft.AspNet.Razor.Parser.SyntaxTree
{
public class RazorError : IEquatable<RazorError>
{
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()
{

View File

@ -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<RazorError>(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);
}
}
}