Пример #1
0
        // Internal for testing
        internal IAsyncCompletionSource CreateCompletionSource(ITextBuffer razorBuffer)
        {
            if (!razorBuffer.Properties.TryGetProperty(typeof(VisualStudioRazorParser), out VisualStudioRazorParser parser))
            {
                // Parser hasn't been associated with the text buffer yet.
                return(null);
            }

            var completionSource = new RazorDirectiveCompletionSource(parser, _completionFactsService);

            return(completionSource);
        }
        public async Task GetDescriptionAsync_DoesNotAddDescriptionWhenPropertyAbsent()
        {
            // Arrange
            var completionItem   = new CompletionItem("TestDirective", Mock.Of <IAsyncCompletionSource>());
            var completionSource = new RazorDirectiveCompletionSource(Dispatcher, Mock.Of <VisualStudioRazorParser>(), CompletionFactsService);

            // Act
            var descriptionObject = await completionSource.GetDescriptionAsync(null, completionItem, CancellationToken.None);

            // Assert
            var description = Assert.IsType <string>(descriptionObject);

            Assert.Equal(string.Empty, description);
        }
        public async Task GetCompletionContextAsync_DoesNotProvideCompletionsWhenNotAtCompletionPoint()
        {
            // Arrange
            var text             = "@(NotValidCompletionLocation)";
            var parser           = CreateParser(text);
            var completionSource = new RazorDirectiveCompletionSource(Dispatcher, parser, CompletionFactsService);
            var documentSnapshot = new StringTextSnapshot(text);
            var triggerLocation  = new SnapshotPoint(documentSnapshot, 4);
            var applicableSpan   = new SnapshotSpan(documentSnapshot, new Span(2, text.Length - 3 /* @() */));

            // Act
            var completionContext = await Task.Run(
                async() => await completionSource.GetCompletionContextAsync(null, new CompletionTrigger(CompletionTriggerReason.Invoke, triggerLocation.Snapshot), triggerLocation, applicableSpan, CancellationToken.None));

            // Assert
            Assert.Empty(completionContext.Items);
        }
        public async Task GetDescriptionAsync_AddsDirectiveDescriptionIfPropertyExists()
        {
            // Arrange
            var completionItem      = new CompletionItem("TestDirective", Mock.Of <IAsyncCompletionSource>());
            var expectedDescription = new DirectiveCompletionDescription("The expected description");

            completionItem.Properties.AddProperty(RazorDirectiveCompletionSource.DescriptionKey, expectedDescription);
            var completionSource = new RazorDirectiveCompletionSource(Dispatcher, Mock.Of <VisualStudioRazorParser>(), CompletionFactsService);

            // Act
            var descriptionObject = await completionSource.GetDescriptionAsync(null, completionItem, CancellationToken.None);

            // Assert
            var description = Assert.IsType <string>(descriptionObject);

            Assert.Equal(expectedDescription.Description, descriptionObject);
        }
        public async Task GetCompletionContextAsync_DoesNotProvideCompletionsPriorToParseResults()
        {
            // Arrange
            var text   = "@validCompletion";
            var parser = new Mock <VisualStudioRazorParser>();

            parser.Setup(p => p.GetLatestCodeDocumentAsync(It.IsAny <ITextSnapshot>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult <RazorCodeDocument>(null));    // CodeDocument will be null faking a parser without a parse.
            var completionSource = new RazorDirectiveCompletionSource(Dispatcher, parser.Object, CompletionFactsService);
            var documentSnapshot = new StringTextSnapshot(text);
            var triggerLocation  = new SnapshotPoint(documentSnapshot, 4);
            var applicableSpan   = new SnapshotSpan(documentSnapshot, new Span(1, text.Length - 1 /* validCompletion */));

            // Act
            var completionContext = await Task.Run(
                async() => await completionSource.GetCompletionContextAsync(null, new CompletionTrigger(CompletionTriggerReason.Invoke, triggerLocation.Snapshot), triggerLocation, applicableSpan, CancellationToken.None));

            // Assert
            Assert.Empty(completionContext.Items);
        }
        public async Task GetCompletionContextAsync_ProvidesCompletionsWhenAtCompletionPoint()
        {
            // Arrange
            var text             = "@addTag";
            var parser           = CreateParser(text, SectionDirective.Directive);
            var completionSource = new RazorDirectiveCompletionSource(Dispatcher, parser, CompletionFactsService);
            var documentSnapshot = new StringTextSnapshot(text);
            var triggerLocation  = new SnapshotPoint(documentSnapshot, 4);
            var applicableSpan   = new SnapshotSpan(documentSnapshot, new Span(1, 6 /* addTag */));

            // Act
            var completionContext = await Task.Run(
                async() => await completionSource.GetCompletionContextAsync(null, new CompletionTrigger(CompletionTriggerReason.Invoke, triggerLocation.Snapshot), triggerLocation, applicableSpan, CancellationToken.None));

            // Assert
            Assert.Collection(
                completionContext.Items,
                item => AssertRazorCompletionItem(SectionDirective.Directive, item, completionSource),
                item => AssertRazorCompletionItem(DefaultDirectives[0], item, completionSource),
                item => AssertRazorCompletionItem(DefaultDirectives[1], item, completionSource),
                item => AssertRazorCompletionItem(DefaultDirectives[2], item, completionSource));
        }