public void SetOutput_AcceptsSameVersionedDocuments() { // Arrange var services = TestWorkspace.Create().Services; var hostProject = new HostProject("C:/project.csproj", RazorConfiguration.Default, "project"); var projectState = ProjectState.Create(services, hostProject); var project = new DefaultProjectSnapshot(projectState); var text = SourceText.From("..."); var textAndVersion = TextAndVersion.Create(text, VersionStamp.Default); var hostDocument = new HostDocument("C:/file.cshtml", "C:/file.cshtml"); var documentState = new DocumentState(services, hostDocument, text, VersionStamp.Default, () => Task.FromResult(textAndVersion)); var document = new DefaultDocumentSnapshot(project, documentState); var newDocument = new DefaultDocumentSnapshot(project, documentState); var csharpDocument = RazorCSharpDocument.Create("...", RazorCodeGenerationOptions.CreateDefault(), Enumerable.Empty <RazorDiagnostic>()); var version = VersionStamp.Create(); var container = new GeneratedCodeContainer(); container.SetOutput(document, csharpDocument, version, version); // Act container.SetOutput(newDocument, csharpDocument, version, version); // Assert Assert.Same(newDocument, container.LatestDocument); }
public void TryGetLinePositionSpan_SpanOutsideSourceMapping_ReturnsFalse() { // Arrange var content = @" @{ var x = SomeClass.SomeProperty; } "; var sourceText = SourceText.From(content); var codeDocument = GetCodeDocument(content); var generatedCode = codeDocument.GetCSharpDocument().GeneratedCode; var container = new GeneratedCodeContainer(); container.SetOutput(sourceText, codeDocument); // Position of `ExecuteAsync` in the generated code. var symbol = "ExecuteAsync"; var span = new TextSpan(generatedCode.IndexOf(symbol), symbol.Length); // Act var result = container.TryGetLinePositionSpan(span, out var lineSpan); // Assert Assert.False(result); }
public void TryGetLinePositionSpan_SpanWithinSourceMapping_ReturnsTrue() { // Arrange var content = @" @{ var x = SomeClass.SomeProperty; } "; var sourceText = SourceText.From(content); var codeDocument = GetCodeDocument(content); var generatedCode = codeDocument.GetCSharpDocument().GeneratedCode; var container = new GeneratedCodeContainer(); container.SetOutput(sourceText, codeDocument); // TODO: Make writing these tests a little less manual. // Position of `SomeProperty` in the generated code. var symbol = "SomeProperty"; var span = new TextSpan(generatedCode.IndexOf(symbol), symbol.Length); // Position of `SomeProperty` in the source code. var expectedLineSpan = new LinePositionSpan(new LinePosition(2, 22), new LinePosition(2, 34)); // Act var result = container.TryGetLinePositionSpan(span, out var lineSpan); // Assert Assert.True(result); Assert.Equal(expectedLineSpan, lineSpan); }