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);
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        public HostDocument(HostDocument other)
        {
            if (other == null)
            {
                throw new ArgumentNullException(nameof(other));
            }

            FileKind   = other.FileKind;
            FilePath   = other.FilePath;
            TargetPath = other.TargetPath;

            GeneratedCodeContainer = new GeneratedCodeContainer();
        }
Exemplo n.º 5
0
        public HostDocument(string filePath, string targetPath)
        {
            if (filePath == null)
            {
                throw new ArgumentNullException(nameof(filePath));
            }

            if (targetPath == null)
            {
                throw new ArgumentNullException(nameof(targetPath));
            }

            FilePath               = filePath;
            TargetPath             = targetPath;
            GeneratedCodeContainer = new GeneratedCodeContainer();
        }