Exemplo n.º 1
0
        public async Task SourceRScriptTest(bool echo, string encoding)
        {
            await _workflow.RSessions.TrySwitchBrokerAsync(nameof(RInteractiveWorkflowCommandTest));

            await _workflow.RSession.EnsureHostStartedAsync(new RHostStartupInfo(
                                                                cranMirrorName : _settings.CranMirror,
                                                                codePage : _settings.RCodePage
                                                                ), null, 50000);

            var session = _workflow.RSession;
            await session.ExecuteAsync("sourced <- FALSE");

            var tracker = Substitute.For <IActiveWpfTextViewTracker>();

            tracker.GetLastActiveTextView(RContentTypeDefinition.ContentType).Returns((IWpfTextView)null);

            var command = new SourceRScriptCommand(_workflow, tracker, echo);

            command.Should().BeSupported()
            .And.BeInvisibleAndDisabled();

            using (await _workflow.GetOrCreateVisualComponentAsync()) {
                const string code       = "sourced <- TRUE";
                var          textBuffer = new TextBufferMock(code, RContentTypeDefinition.ContentType);
                var          textView   = new WpfTextViewMock(textBuffer);

                tracker.GetLastActiveTextView(RContentTypeDefinition.ContentType).Returns(textView);
                tracker.LastActiveTextView.Returns(textView);

                command.Should().BeSupported()
                .And.BeVisibleAndDisabled();

                using (var sf = new SourceFile(code)) {
                    var document = new TextDocumentMock(textBuffer, sf.FilePath)
                    {
                        Encoding = Encoding.GetEncoding(encoding)
                    };

                    textBuffer.Properties[typeof(ITextDocument)] = document;

                    command.Should().BeSupported()
                    .And.BeVisibleAndEnabled();

                    var mutatedTask = EventTaskSources.IRSession.Mutated.Create(session);

                    await command.InvokeAsync().Should().BeCompletedAsync();

                    await mutatedTask.Should().BeCompletedAsync();

                    (await session.EvaluateAsync <bool>("sourced", REvaluationKind.Normal)).Should().BeTrue();
                }
            }
        }
Exemplo n.º 2
0
        public async Task EvaluatorTest02()
        {
            using (new VsRHostScript()) {
                var session = _sessionProvider.GetInteractiveWindowRSession();
                using (var eval = new RInteractiveEvaluator(session, RHistoryStubFactory.CreateDefault(), VsAppShell.Current, RToolsSettings.Current)) {
                    var tb = new TextBufferMock(string.Empty, RContentTypeDefinition.ContentType);
                    var tv = new WpfTextViewMock(tb);

                    var iwm = new InteractiveWindowMock(tv);
                    eval.CurrentWindow = iwm;

                    var result = await eval.InitializeAsync();

                    result.Should().Be(ExecutionResult.Success);
                    session.IsHostRunning.Should().BeTrue();

                    VsRHostScript.DoIdle(1000);
                    tb.Clear();

                    result = await eval.ExecuteCodeAsync("w <- dQuote('text')" + Environment.NewLine);

                    result.Should().Be(ExecutionResult.Success);
                    var text = tb.CurrentSnapshot.GetText();
                    text.Should().Be(string.Empty);
                    tb.Clear();

                    result = await eval.ExecuteCodeAsync("w" + Environment.NewLine);

                    result.Should().Be(ExecutionResult.Success);
                    text = tb.CurrentSnapshot.GetText();
                    text.TrimEnd().Should().Be("[1] \"“text”\"");
                    tb.Clear();

                    result = await eval.ExecuteCodeAsync("e <- dQuote('абвг')" + Environment.NewLine);

                    result.Should().Be(ExecutionResult.Success);
                    text = tb.CurrentSnapshot.GetText();
                    text.Should().Be(string.Empty);
                    tb.Clear();

                    result = await eval.ExecuteCodeAsync("e" + Environment.NewLine);

                    result.Should().Be(ExecutionResult.Success);
                    text = tb.CurrentSnapshot.GetText();
                    text.TrimEnd().Should().Be("[1] \"“абвг”\"");
                    tb.Clear();
                }
            }
        }
Exemplo n.º 3
0
        private static ICoreShell SetupServices(out IWpfTextViewHost host, out IWpfTextViewMargin container, out IRMarkdownEditorSettings settings)
        {
            var shell = TestCoreShell.CreateSubstitute();

            settings = shell.SetupSettingsSubstitute();
            shell.SetupSessionSubstitute();

            host = Substitute.For <IWpfTextViewHost>();
            var tb = new TextBufferMock("", MdContentTypeDefinition.ContentType);
            var tv = new WpfTextViewMock(tb);

            host.TextView.Returns(tv);

            container = Substitute.For <IWpfTextViewMargin>();
            return(shell);
        }
        public async Task SourceRScriptTest(bool echo, string encoding)
        {
            var session = _workflow.RSession;
            await session.ExecuteAsync("sourced <- FALSE");

            var tracker = Substitute.For <IActiveWpfTextViewTracker>();

            tracker.GetLastActiveTextView(RContentTypeDefinition.ContentType).Returns((IWpfTextView)null);

            var command = new SourceRScriptCommand(_workflow, tracker, echo);

            command.Should().BeSupported()
            .And.BeInvisibleAndDisabled();

            using (await _workflow.GetOrCreateVisualComponent(_componentContainerFactory)) {
                command.Should().BeSupported()
                .And.BeVisibleAndDisabled();

                const string code       = "sourced <- TRUE";
                var          textBuffer = new TextBufferMock(code, RContentTypeDefinition.ContentType);
                var          textView   = new WpfTextViewMock(textBuffer);

                tracker.GetLastActiveTextView(RContentTypeDefinition.ContentType).Returns(textView);

                command.Should().BeSupported()
                .And.BeVisibleAndDisabled();

                using (var sf = new SourceFile(code)) {
                    var document = new TextDocumentMock(textBuffer, sf.FilePath)
                    {
                        Encoding = Encoding.GetEncoding(encoding)
                    };

                    textBuffer.Properties[typeof(ITextDocument)] = document;

                    command.Should().BeSupported()
                    .And.BeVisibleAndEnabled();

                    var mutatedTask = EventTaskSources.IRSession.Mutated.Create(session);

                    await command.InvokeAsync();

                    await mutatedTask;
                    (await session.EvaluateAsync <bool>("sourced", REvaluationKind.Normal)).Should().BeTrue();
                }
            }
        }
Exemplo n.º 5
0
        public ActiveTextViewTrackerMock(string content, string contentTypeName)
        {
            var tb = new TextBufferMock(content, contentTypeName);

            _textView = new WpfTextViewMock(tb);
        }
Exemplo n.º 6
0
        public async Task EvaluatorTest01()
        {
            using (new VsRHostScript()) {
                var session = _sessionProvider.GetInteractiveWindowRSession();
                using (var eval = new RInteractiveEvaluator(session, RHistoryStubFactory.CreateDefault(), VsAppShell.Current, RToolsSettings.Current)) {
                    var tb = new TextBufferMock(string.Empty, RContentTypeDefinition.ContentType);
                    var tv = new WpfTextViewMock(tb);

                    var iwm = new InteractiveWindowMock(tv);
                    eval.CurrentWindow = iwm;

                    var result = await eval.InitializeAsync();

                    result.Should().Be(ExecutionResult.Success);
                    session.IsHostRunning.Should().BeTrue();

                    eval.CanExecuteCode("x <-").Should().BeFalse();
                    eval.CanExecuteCode("(()").Should().BeFalse();
                    eval.CanExecuteCode("a *(b+c)").Should().BeTrue();

                    VsRHostScript.DoIdle(300);

                    result = await eval.ExecuteCodeAsync(new string(new char[10000]) + "\r\n");

                    result.Should().Be(ExecutionResult.Failure);
                    string text = tb.CurrentSnapshot.GetText();
                    text.Should().Contain(string.Format(Microsoft.R.Components.Resources.InputIsTooLong, 4096));
                    tb.Clear();

                    result = await eval.ExecuteCodeAsync("z <- '電話帳 全米のお'\n");

                    result.Should().Be(ExecutionResult.Success);
                    tb.Clear();

                    result = await eval.ExecuteCodeAsync("z" + Environment.NewLine);

                    result.Should().Be(ExecutionResult.Success);
                    text = tb.CurrentSnapshot.GetText();
                    text.TrimEnd().Should().Be("[1] \"電話帳 全米のお\"");
                    tb.Clear();

                    result = await eval.ExecuteCodeAsync("Encoding(z)\n");

                    result.Should().Be(ExecutionResult.Success);
                    text = tb.CurrentSnapshot.GetText();
                    text.TrimEnd().Should().Be("[1] \"UTF-8\"");
                    tb.Clear();

                    result = await eval.ExecuteCodeAsync("x <- c(1:10)\n");

                    result.Should().Be(ExecutionResult.Success);
                    text = tb.CurrentSnapshot.GetText();
                    text.Should().Be(string.Empty);
                    tb.Clear();

                    await eval.ResetAsync(initialize : false);

                    text = tb.CurrentSnapshot.GetText();
                    text.Should().StartWith(Microsoft.R.Components.Resources.MicrosoftRHostStopping);
                }
            }
        }