private void RunPartialParseRejectionTest(TestEdit edit, PartialParseResultInternal additionalFlags = 0) { var templateEngine = CreateTemplateEngine(); var document = TestRazorCodeDocument.Create(edit.OldSnapshot.GetText()); templateEngine.Engine.Process(document); var syntaxTree = document.GetSyntaxTree(); var parser = new RazorSyntaxTreePartialParser(syntaxTree); var result = parser.Parse(edit.Change); Assert.Equal(PartialParseResultInternal.Rejected | additionalFlags, result); }
public void SetRazorSyntaxTree_SetsSyntaxTree() { // Arrange var codeDocument = TestRazorCodeDocument.CreateEmpty(); var expected = RazorSyntaxTree.Parse(codeDocument.Source); // Act codeDocument.SetSyntaxTree(expected); // Assert Assert.Same(expected, codeDocument.Items[typeof(RazorSyntaxTree)]); }
private static VisualStudioRazorParser CreateParser(string text, params DirectiveDescriptor[] directives) { var syntaxTree = CreateSyntaxTree(text, directives); var codeDocument = TestRazorCodeDocument.Create(text); codeDocument.SetSyntaxTree(syntaxTree); var parser = new Mock <VisualStudioRazorParser>(); parser.Setup(p => p.GetLatestCodeDocumentAsync()) .Returns(Task.FromResult(codeDocument)); return(parser.Object); }
public void SetCodeGenerationOptions_SetsSuccessfully() { // Arrange var codeDocument = TestRazorCodeDocument.CreateEmpty(); var expected = RazorCodeGenerationOptions.CreateDefault(); // Act codeDocument.SetCodeGenerationOptions(expected); // Assert Assert.Same(expected, codeDocument.Items[typeof(RazorCodeGenerationOptions)]); }
public void SetTagHelperContext_SetsTagHelperContext() { // Arrange var codeDocument = TestRazorCodeDocument.CreateEmpty(); var expected = TagHelperDocumentContext.Create(null, new TagHelperDescriptor[0]); // Act codeDocument.SetTagHelperContext(expected); // Assert Assert.Same(expected, codeDocument.Items[typeof(TagHelperDocumentContext)]); }
public void SetCSharpDocument_SetsCSharpDocument() { // Arrange var codeDocument = TestRazorCodeDocument.CreateEmpty(); var expected = RazorCSharpDocument.Create("", RazorCodeGenerationOptions.CreateDefault(), Array.Empty <RazorDiagnostic>()); // Act codeDocument.SetCSharpDocument(expected); // Assert Assert.Same(expected, codeDocument.Items[typeof(RazorCSharpDocument)]); }
public void IsUnsupported_Set_ReturnsTrue() { // Arrange var codeDocument = TestRazorCodeDocument.CreateEmpty(); codeDocument.SetUnsupported(); // Act var result = codeDocument.IsUnsupported(); // Assert Assert.True(result); }
public void SetIRDocument_SetsIRDocument() { // Arrange var codeDocument = TestRazorCodeDocument.CreateEmpty(); var expected = new DocumentIntermediateNode(); // Act codeDocument.SetDocumentIntermediateNode((DocumentIntermediateNode)expected); // Assert Assert.Same(expected, codeDocument.Items[typeof(DocumentIntermediateNode)]); }
public void InstrumentationPass_SkipsCSharpExpression_InsideTagHelperProperty() { // Arrange var document = new DocumentIntermediateNode() { Options = RazorCodeGenerationOptions.CreateDefault(), }; var builder = IntermediateNodeBuilder.Create(document); builder.Push(new TagHelperIntermediateNode()); builder.Push(new TagHelperPropertyIntermediateNode()); builder.Push(new CSharpExpressionIntermediateNode() { Source = CreateSource(5) }); builder.Add(new IntermediateToken() { Content = "Hi", Kind = TokenKind.CSharp, }); var pass = new InstrumentationPass() { Engine = RazorProjectEngine.CreateEmpty().Engine, }; // Act pass.Execute(TestRazorCodeDocument.CreateEmpty(), document); // Assert Children( document, n => { Assert.IsType <TagHelperIntermediateNode>(n); Children( n, c => { Assert.IsType <TagHelperPropertyIntermediateNode>(c); Children( c, s => CSharpExpression("Hi", s)); }); }); }
private static void RunPartialParseTest(TestEdit edit, Block expectedTree, PartialParseResultInternal additionalFlags = 0) { var templateEngine = CreateProjectEngine(); var document = TestRazorCodeDocument.Create(edit.OldSnapshot.GetText()); templateEngine.Engine.Process(document); var syntaxTree = document.GetSyntaxTree(); var parser = new RazorSyntaxTreePartialParser(syntaxTree); var result = parser.Parse(edit.Change); Assert.Equal(PartialParseResultInternal.Accepted | additionalFlags, result); ParserTestBase.EvaluateParseTree(parser.SyntaxTreeRoot, expectedTree); }
public void Lower_SetsOptions_Defaults() { // Arrange var codeDocument = TestRazorCodeDocument.CreateEmpty(); // Act var documentNode = Lower(codeDocument); // Assert Assert.NotNull(documentNode.Options); Assert.False(documentNode.Options.DesignTime); Assert.Equal(4, documentNode.Options.IndentSize); Assert.False(documentNode.Options.IndentWithTabs); }
public void GetCodeGenerationOptions_ReturnsSuccessfully() { // Arrange var codeDocument = TestRazorCodeDocument.CreateEmpty(); var expected = RazorCodeGenerationOptions.CreateDefault(); codeDocument.Items[typeof(RazorCodeGenerationOptions)] = expected; // Act var actual = codeDocument.GetCodeGenerationOptions(); // Assert Assert.Same(expected, actual); }
public void GetIRDocument_ReturnsIRDocument() { // Arrange var codeDocument = TestRazorCodeDocument.CreateEmpty(); var expected = new DocumentIntermediateNode(); codeDocument.Items[typeof(DocumentIntermediateNode)] = expected; // Act var actual = codeDocument.GetDocumentIntermediateNode(); // Assert Assert.Same(expected, actual); }
public void GetCSharpDocument_ReturnsCSharpDocument() { // Arrange var codeDocument = TestRazorCodeDocument.CreateEmpty(); var expected = RazorCSharpDocument.Create("", RazorCodeGenerationOptions.CreateDefault(), Array.Empty <RazorDiagnostic>()); codeDocument.Items[typeof(RazorCSharpDocument)] = expected; // Act var actual = codeDocument.GetCSharpDocument(); // Assert Assert.Same(expected, actual); }
public void Execute_NoOps_IfNamespaceNodeIsMissing() { // Arrange var irDocument = new DocumentIntermediateNode(); var pass = new AssemblyAttributeInjectionPass { Engine = RazorEngine.Create(), }; // Act pass.Execute(TestRazorCodeDocument.CreateEmpty(), irDocument); // Assert Assert.Empty(irDocument.Children); }
public void GetAndSetTagHelpers_ReturnsTagHelpers() { // Arrange var codeDocument = TestRazorCodeDocument.CreateEmpty(); var expected = new[] { TagHelperDescriptorBuilder.Create("TestTagHelper", "TestAssembly").Build() }; codeDocument.SetTagHelpers(expected); // Act var actual = codeDocument.GetTagHelpers(); // Assert Assert.Same(expected, actual); }
public void GetRazorSyntaxTree_ReturnsSyntaxTree() { // Arrange var codeDocument = TestRazorCodeDocument.CreateEmpty(); var expected = RazorSyntaxTree.Parse(codeDocument.Source); codeDocument.Items[typeof(RazorSyntaxTree)] = expected; // Act var actual = codeDocument.GetSyntaxTree(); // Assert Assert.Same(expected, actual); }
public void GetTagHelperContext_ReturnsTagHelperContext() { // Arrange var codeDocument = TestRazorCodeDocument.CreateEmpty(); var expected = TagHelperDocumentContext.Create(null, new TagHelperDescriptor[0]); codeDocument.Items[typeof(TagHelperDocumentContext)] = expected; // Act var actual = codeDocument.GetTagHelperContext(); // Assert Assert.Same(expected, actual); }
public void GetAndSetImportSyntaxTrees_ReturnsSyntaxTrees() { // Arrange var codeDocument = TestRazorCodeDocument.CreateEmpty(); var expected = new[] { RazorSyntaxTree.Parse(codeDocument.Source), }; codeDocument.SetImportSyntaxTrees(expected); // Act var actual = codeDocument.GetImportSyntaxTrees(); // Assert Assert.Same(expected, actual); }
public void Lower_WithFunctions() { // Arrange var codeDocument = TestRazorCodeDocument.Create(@"@functions { public int Foo { get; set; }}"); // Act var documentNode = Lower(codeDocument); // Assert Children(documentNode, n => Directive( "functions", n, c => Assert.IsType <CSharpCodeIntermediateNode>(c))); }
public void Execute_ThrowsForMissingDependency() { // Arrange var phase = new DefaultRazorOptimizationPhase(); var engine = RazorProjectEngine.CreateEmpty(b => b.Phases.Add(phase)); var codeDocument = TestRazorCodeDocument.CreateEmpty(); // Act & Assert ExceptionAssert.Throws <InvalidOperationException>( () => phase.Execute(codeDocument), $"The '{nameof(DefaultRazorOptimizationPhase)}' phase requires a '{nameof(DocumentIntermediateNode)}' " + $"provided by the '{nameof(RazorCodeDocument)}'."); }
public async Task ProvideCompletionAsync_DoesNotProvideCompletionsForCodeDocumentWithoutSyntaxTree() { // Arrange var codeDocumentProvider = new Mock <RazorCodeDocumentProvider>(); var codeDocument = TestRazorCodeDocument.CreateEmpty(); codeDocumentProvider.Setup(provider => provider.TryGetFromDocument(It.IsAny <TextDocument>(), out codeDocument)) .Returns(true); var completionProvider = new FailOnGetCompletionsProvider(new Lazy <RazorCodeDocumentProvider>(() => codeDocumentProvider.Object)); var document = CreateDocument(); var context = CreateContext(2, completionProvider, document); // Act & Assert await completionProvider.ProvideCompletionsAsync(context); }
public void CreateDefault_CallsDelegate() { // Arrange var wasCalled = false; Action <CodeTargetBuilder> @delegate = (b) => { wasCalled = true; }; var codeDocument = TestRazorCodeDocument.CreateEmpty(); var options = RazorCodeGenerationOptions.CreateDefault(); // Act CodeTarget.CreateDefault(codeDocument, options, @delegate); // Assert Assert.True(wasCalled); }
public void Execute_ExecutesPhasesInOrder() { // Arrange var codeDocument = TestRazorCodeDocument.CreateEmpty(); // We're going to set up mocks to simulate a sequence of passes. We don't care about // what's in the nodes, we're just going to look at the identity via strict mocks. var originalNode = new DocumentIntermediateNode(); var firstPassNode = new DocumentIntermediateNode(); var secondPassNode = new DocumentIntermediateNode(); codeDocument.SetDocumentIntermediateNode(originalNode); var firstPass = new Mock <IRazorOptimizationPass>(MockBehavior.Strict); firstPass.SetupGet(m => m.Order).Returns(0); firstPass.SetupProperty(m => m.Engine); firstPass.Setup(m => m.Execute(codeDocument, originalNode)).Callback(() => { originalNode.Children.Add(firstPassNode); }); var secondPass = new Mock <IRazorOptimizationPass>(MockBehavior.Strict); secondPass.SetupGet(m => m.Order).Returns(1); secondPass.SetupProperty(m => m.Engine); secondPass.Setup(m => m.Execute(codeDocument, originalNode)).Callback(() => { // Works only when the first pass has run before this. originalNode.Children[0].Children.Add(secondPassNode); }); var phase = new DefaultRazorOptimizationPhase(); var engine = RazorProjectEngine.CreateEmpty(b => { b.Phases.Add(phase); b.Features.Add(firstPass.Object); b.Features.Add(secondPass.Object); }); // Act phase.Execute(codeDocument); // Assert Assert.Same(secondPassNode, codeDocument.GetDocumentIntermediateNode().Children[0].Children[0]); }
public void TryGetFromBuffer_SucceedsIfParserFromProviderHasCodeDocument() { // Arrange var expectedCodeDocument = TestRazorCodeDocument.Create("Hello World"); VisualStudioRazorParser parser = new DefaultVisualStudioRazorParser(expectedCodeDocument); var parserProvider = Mock.Of <RazorEditorFactoryService>(p => p.TryGetParser(It.IsAny <ITextBuffer>(), out parser) == true); var textBuffer = Mock.Of <ITextBuffer>(); var provider = new DefaultTextBufferCodeDocumentProvider(parserProvider); // Act var result = provider.TryGetFromBuffer(textBuffer, out var codeDocument); // Assert Assert.True(result); Assert.Same(expectedCodeDocument, codeDocument); }
private void RunPartialParseTest(TestEdit edit, PartialParseResultInternal additionalFlags = 0) { var templateEngine = CreateProjectEngine(); var document = TestRazorCodeDocument.Create(edit.OldSnapshot.GetText()); templateEngine.Engine.Process(document); var syntaxTree = document.GetSyntaxTree(); var parser = new RazorSyntaxTreePartialParser(syntaxTree); var(result, _) = parser.Parse(edit.Change); Assert.Equal(PartialParseResultInternal.Accepted | additionalFlags, result); var newSource = TestRazorSourceDocument.Create(edit.NewSnapshot.GetText()); var newSyntaxTree = RazorSyntaxTree.Create(parser.ModifiedSyntaxTreeRoot, newSource, parser.OriginalSyntaxTree.Diagnostics, parser.OriginalSyntaxTree.Options); BaselineTest(newSyntaxTree); }
public void TryComputeNamespace_ComputesNamespace() { // Arrange var sourceDocument = TestRazorSourceDocument.Create(filePath: "C:\\Hello\\Components\\Test.cshtml", relativePath: "\\Components\\Test.cshtml"); var codeDocument = TestRazorCodeDocument.Create(sourceDocument, Array.Empty <RazorSourceDocument>()); codeDocument.SetCodeGenerationOptions(RazorCodeGenerationOptions.Create(c => { c.RootNamespace = "Hello"; })); // Act codeDocument.TryComputeNamespace(fallbackToRootNamespace: true, out var @namespace); // Assert Assert.Equal("Hello.Components", @namespace); }
public void Lower_WithUsing() { // Arrange var codeDocument = TestRazorCodeDocument.Create(@"@using System"); var expectedSourceLocation = new SourceSpan(codeDocument.Source.FilePath, 1, 0, 1, 12); // Act var documentNode = Lower(codeDocument); // Assert Children(documentNode, n => { Using("System", n); Assert.Equal(expectedSourceLocation, n.Source); }); }
public void Execute_AddsSyntaxTree() { // Arrange var phase = new DefaultRazorParsingPhase(); var engine = RazorProjectEngine.CreateEmpty(builder => { builder.Phases.Add(phase); builder.Features.Add(new DefaultRazorParserOptionsFeature(designTime: false, version: RazorLanguageVersion.Latest, fileKind: null)); }); var codeDocument = TestRazorCodeDocument.CreateEmpty(); // Act phase.Execute(codeDocument); // Assert Assert.NotNull(codeDocument.GetSyntaxTree()); }
public void WriteDocument_WritesMethod() { // Arrange var document = new DocumentIntermediateNode(); var builder = IntermediateNodeBuilder.Create(document); builder.Add(new MethodDeclarationIntermediateNode() { Modifiers = { "internal", "virtual", "async", }, MethodName = "TestMethod", ReturnType = "string", }); var codeDocument = TestRazorCodeDocument.CreateEmpty(); var options = RazorCodeGenerationOptions.CreateDefault(); var target = CodeTarget.CreateDefault(codeDocument, options); var writer = new DefaultDocumentWriter(target, options); // Act var result = writer.WriteDocument(codeDocument, document); // Assert var csharp = result.GeneratedCode; Assert.Equal( @"#pragma checksum ""test.cshtml"" ""{ff1816ec-aa5e-4d10-87f7-6f4963833460}"" ""da39a3ee5e6b4b0d3255bfef95601890afd80709"" // <auto-generated/> #pragma warning disable 1591 #pragma warning disable 1998 internal virtual async string TestMethod() { } #pragma warning restore 1998 #pragma warning restore 1591 ", csharp, ignoreLineEndingDifferences: true); }