");
var syntaxTree = GetSyntaxTree(source);
var service = new DefaultRazorIndentationFactsService();
// Act
var indentation = service.GetDesiredIndentation(
syntaxTree,
source,
source.GetLineFromLineNumber(2),
indentSize: 4,
tabSize: 4);
// Assert
Assert.Equal(4, indentation);
}
[Fact]
public void GetDesiredIndentation_ReturnsCorrectIndentation_ForMarkupWithinDirectiveBlock()
{
// Arrange
var customDirective = DirectiveDescriptor.CreateRazorBlockDirective("custom");
var source = new StringTextSnapshot($@"@custom
{{
}}");
var syntaxTree = GetSyntaxTree(source, new[] { customDirective });
var service = new DefaultRazorIndentationFactsService();
// Act
var indentation = service.GetDesiredIndentation(
syntaxTree,
source,
source.GetLineFromLineNumber(3),
indentSize: 4,
tabSize: 4);
// Assert
Assert.Equal(4, indentation);
}
[Fact]
public void GetDesiredIndentation_ReturnsCorrectIndentation_ForNestedMarkupWithinCodeBlock()
{
// Arrange
var source = new StringTextSnapshot($@"
@{{
}}
");
var syntaxTree = GetSyntaxTree(source);
var service = new DefaultRazorIndentationFactsService();
// Act
var indentation = service.GetDesiredIndentation(
syntaxTree,
source,
source.GetLineFromLineNumber(4),
indentSize: 4,
tabSize: 4);
// Assert
Assert.Equal(8, indentation);
}
[Fact]
public void GetDesiredIndentation_ReturnsCorrectIndentation_ForMarkupWithinCodeBlockInADirectiveBlock()
{
// Arrange
var customDirective = DirectiveDescriptor.CreateRazorBlockDirective("custom");
var source = new StringTextSnapshot($@"@custom
{{
@{{
}}
}}");
var syntaxTree = GetSyntaxTree(source, new[] { customDirective });
var service = new DefaultRazorIndentationFactsService();
// Act
var indentation = service.GetDesiredIndentation(
syntaxTree,
source,
source.GetLineFromLineNumber(4),
indentSize: 4,
tabSize: 4);
// Assert
Assert.Equal(8, indentation);
}
private static RazorSyntaxTree GetSyntaxTree(StringTextSnapshot source, IEnumerable directives = null)
{
directives = directives ?? Enumerable.Empty();
var engine = RazorProjectEngine.Create(builder =>
{
foreach (var directive in directives)
{
builder.AddDirective(directive);
}
});
var sourceProjectItem = new TestRazorProjectItem("test.cshtml")
{
Content = source.GetText()
};
var codeDocument = engine.ProcessDesignTime(sourceProjectItem);
return codeDocument.GetSyntaxTree();
}
}
}