Prevent RazorErrorExtensions.ToDiagnostics from throwing when it

encounters SourceLocation.Undefined \ negative error lengths
This commit is contained in:
Pranav K 2015-03-24 16:43:15 -07:00
parent c6a1af97b0
commit a4f3b86865
2 changed files with 79 additions and 8 deletions

View File

@ -1,7 +1,9 @@
// 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.Parser.SyntaxTree;
using Microsoft.AspNet.Razor.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using Microsoft.Framework.Internal;
@ -20,15 +22,18 @@ namespace Microsoft.AspNet.Mvc.Razor.Precompilation
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);
var textSpan = new TextSpan(error.Location.AbsoluteIndex, error.Length);
var linePositionStart = new LinePosition(error.Location.LineIndex, error.Location.CharacterIndex);
var linePositionEnd = new LinePosition(error.Location.LineIndex,
error.Location.CharacterIndex + error.Length);
var location = error.Location;
if (location == SourceLocation.Undefined)
{
location = SourceLocation.Zero;
}
var length = Math.Max(0, error.Length);
var textSpan = new TextSpan(location.AbsoluteIndex, length);
var linePositionStart = new LinePosition(location.LineIndex, location.CharacterIndex);
var linePositionEnd = new LinePosition(location.LineIndex, location.CharacterIndex + length);
var linePositionSpan = new LinePositionSpan(linePositionStart, linePositionEnd);
var location = Location.Create(filePath, textSpan, linePositionSpan);
return Diagnostic.Create(descriptor, location);
return Diagnostic.Create(descriptor, Location.Create(filePath, textSpan, linePositionSpan));
}
}
}

View File

@ -0,0 +1,66 @@
// 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 Microsoft.AspNet.Razor.Parser.SyntaxTree;
using Microsoft.AspNet.Razor.Text;
using Xunit;
namespace Microsoft.AspNet.Mvc.Razor.Precompilation
{
public class RazorErrorExtensionsTest
{
public static TheoryData ToDiagnostic_SucceedsWhenRazorErrorLocationIsZeroOrUndefinedData
{
get
{
return new TheoryData<SourceLocation, int>
{
{ SourceLocation.Undefined, -1 },
{ SourceLocation.Undefined, 0 },
{ SourceLocation.Zero, -1 },
{ SourceLocation.Zero, 0 },
};
}
}
[Theory]
[MemberData(nameof(ToDiagnostic_SucceedsWhenRazorErrorLocationIsZeroOrUndefinedData))]
public void ToDiagnostic_SucceedsWhenRazorErrorLocationIsZeroOrUndefined(
SourceLocation location,
int length)
{
// Arrange
var error = new RazorError("some message", location, length);
// Act
var diagnostics = error.ToDiagnostics("/some-path");
// Assert
var span = diagnostics.Location.GetMappedLineSpan();
Assert.Equal("/some-path", span.Path);
Assert.Equal(0, span.StartLinePosition.Line);
Assert.Equal(0, span.StartLinePosition.Character);
Assert.Equal(0, span.EndLinePosition.Line);
Assert.Equal(0, span.EndLinePosition.Character);
}
[Fact]
public void ToDiagnostic_ConvertsRazorErrorLocation_ToSourceLineMappings()
{
// Arrange
var sourceLocation = new SourceLocation(absoluteIndex: 30, lineIndex: 10, characterIndex: 1);
var error = new RazorError("some message", sourceLocation, length: 5);
// Act
var diagnostics = error.ToDiagnostics("/some-path");
// Assert
var span = diagnostics.Location.GetMappedLineSpan();
Assert.Equal("/some-path", span.Path);
Assert.Equal(10, span.StartLinePosition.Line);
Assert.Equal(1, span.StartLinePosition.Character);
Assert.Equal(10, span.EndLinePosition.Line);
Assert.Equal(6, span.EndLinePosition.Character);
}
}
}