Merge branch 'release' into dev

This commit is contained in:
Pranav K 2014-09-17 12:51:17 -07:00
commit 70521e1fa6
4 changed files with 117 additions and 17 deletions

View File

@ -38,10 +38,10 @@ namespace Microsoft.AspNet.Mvc.Razor
if (Context.Host.DesignTimeMode && chunk.Association != null)
{
Writer.WriteLine("public");
var code = string.Format(CultureInfo.InvariantCulture,
"{0} {1}",
chunk.TypeName,
chunk.MemberName);
var code = string.IsNullOrEmpty(chunk.MemberName) ?
chunk.TypeName :
chunk.TypeName + ' ' + chunk.MemberName;
var csharpVisitor = new CSharpCodeVisitor(Writer, Context);
csharpVisitor.CreateExpressionCodeMapping(code, chunk);
Writer.WriteLine("{ get; private set; }");

View File

@ -107,7 +107,7 @@ namespace Microsoft.AspNet.Mvc.Razor
var propertyStartLocation = CurrentLocation;
AcceptWhile(IsSpacingToken(includeNewLines: false, includeComments: true));
if (!hasTypeError && At(CSharpSymbolType.NewLine))
if (!hasTypeError && (EndOfFile || At(CSharpSymbolType.NewLine)))
{
// Add an error for the property name only if we successfully read the type name
Context.OnError(propertyStartLocation,
@ -131,6 +131,7 @@ namespace Microsoft.AspNet.Mvc.Razor
propertyName.Trim());
// Output the span and finish the block
CompleteBlock();
Output(SpanKind.Code);
}

View File

@ -1,6 +1,7 @@
// 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 System.Collections.Generic;
using System.IO;
using Microsoft.AspNet.Razor;
@ -69,20 +70,56 @@ public MyType2 @MyPropertyName2 { get; private set; }
[Fact]
public void Visit_WithDesignTimeHost_GeneratesPropertiesAndLinePragmas_ForInjectChunks()
{
// Arrange
var expected = string.Join(Environment.NewLine,
@"[Microsoft.AspNet.Mvc.ActivateAttribute]",
@"public",
@"#line 1 """"",
@"MyType1 MyPropertyName1",
"",
@"#line default",
@"#line hidden",
@"{ get; private set; }",
@"[Microsoft.AspNet.Mvc.ActivateAttribute]",
@"public",
@"#line 1 """"",
@"MyType2 @MyPropertyName2",
"",
@"#line default",
@"#line hidden",
@"{ get; private set; }",
"");
var writer = new CSharpCodeWriter();
var context = CreateContext();
context.Host.DesignTimeMode = true;
var visitor = new InjectChunkVisitor(writer, context, "Microsoft.AspNet.Mvc.ActivateAttribute");
var factory = SpanFactory.CreateCsHtml();
var node = (Span)factory.Code("Some code")
.As(new InjectParameterGenerator("MyType", "MyPropertyName"));
// Act
visitor.Accept(new Chunk[]
{
new LiteralChunk(),
new InjectChunk("MyType1", "MyPropertyName1") { Association = node },
new InjectChunk("MyType2", "@MyPropertyName2") { Association = node }
});
var code = writer.GenerateCode();
// Assert
Assert.Equal(expected, code);
}
[Fact]
public void Visit_WithDesignTimeHost_GeneratesPropertiesAndLinePragmas_ForPartialInjectChunks()
{
// Arrange
var expected = @"[Microsoft.AspNet.Mvc.ActivateAttribute]
public
#line 1 """"
MyType1 MyPropertyName1
#line default
#line hidden
{ get; private set; }
[Microsoft.AspNet.Mvc.ActivateAttribute]
public
#line 1 """"
MyType2 @MyPropertyName2
MyType1
#line default
#line hidden
@ -101,8 +138,7 @@ MyType2 @MyPropertyName2
visitor.Accept(new Chunk[]
{
new LiteralChunk(),
new InjectChunk("MyType1", "MyPropertyName1") { Association = node },
new InjectChunk("MyType2", "@MyPropertyName2") { Association = node }
new InjectChunk("MyType1", string.Empty) { Association = node },
});
var code = writer.GenerateCode();

View File

@ -309,7 +309,7 @@ namespace Microsoft.AspNet.Mvc.Razor
{
// Arrange
var errors = new List<RazorError>();
var documentContent = "@inject \r\nBar";
var documentContent = "@inject " + Environment.NewLine + "Bar";
var factory = SpanFactory.CreateCsHtml();
var expectedSpans = new Span[]
{
@ -337,6 +337,37 @@ namespace Microsoft.AspNet.Mvc.Razor
Assert.Equal(expectedErrors, errors);
}
[Fact]
public void ParseInjectKeyword_ErrorOnMissingTypeName_WhenTypeNameEndsWithEOF()
{
// Arrange
var errors = new List<RazorError>();
var documentContent = "@inject ";
var factory = SpanFactory.CreateCsHtml();
var expectedSpans = new Span[]
{
factory.EmptyHtml(),
factory.CodeTransition(SyntaxConstants.TransitionString)
.Accepts(AcceptedCharacters.None),
factory.MetaCode("inject ")
.Accepts(AcceptedCharacters.None),
factory.Code(" ")
.As(new InjectParameterGenerator(string.Empty, string.Empty)),
};
var expectedErrors = new[]
{
new RazorError("The 'inject' keyword must be followed by a type name on the same line.",
new SourceLocation(11, 0, 11), 1)
};
// Act
var spans = ParseDocument(documentContent, errors);
// Assert
Assert.Equal(expectedSpans, spans);
Assert.Equal(expectedErrors, errors);
}
[Fact]
public void ParseInjectKeyword_ErrorOnMissingPropertyName()
{
@ -371,6 +402,38 @@ namespace Microsoft.AspNet.Mvc.Razor
Assert.Equal(expectedErrors, errors);
}
[Fact]
public void ParseInjectKeyword_ErrorOnMissingPropertyName_WhenTypeNameEndsWithEOF()
{
// Arrange
var errors = new List<RazorError>();
var documentContent = "@inject IMyServi";
var factory = SpanFactory.CreateCsHtml();
var expectedSpans = new Span[]
{
factory.EmptyHtml(),
factory.CodeTransition(SyntaxConstants.TransitionString)
.Accepts(AcceptedCharacters.None),
factory.MetaCode("inject ")
.Accepts(AcceptedCharacters.None),
factory.Code(" IMyServi")
.As(new InjectParameterGenerator("IMyServi", string.Empty)),
};
var expectedErrors = new[]
{
new RazorError("A property name must be specified when using the 'inject' statement. " +
"Format for a 'inject' statement is '@inject <Type Name> <Property Name>'.",
new SourceLocation(19, 0, 19), 1)
};
// Act
var spans = ParseDocument(documentContent, errors);
// Assert
Assert.Equal(expectedSpans, spans);
Assert.Equal(expectedErrors, errors);
}
private static List<Span> ParseDocument(string documentContents,
List<RazorError> errors = null,
List<LineMapping> lineMappings = null)