コード例 #1
0
        public void TestFlushFromASCII()
        {
            BaseMock mockBuffer = MockFactories.CreateBufferWithMarker();

            mockBuffer.AddMethodReturnValues(string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLastLineIndex"),
                                             new object[] { 0, 11, 42 });
            mockBuffer["Text"] = "";
            mockBuffer.AddMethodCallback(string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "ReplaceLines"),
                                         new EventHandler <CallbackArgs>(ReplaceLinesCallback));
            using (Stream stream = CreateTextBufferStream((IVsTextLines)mockBuffer))
            {
                string test = "� Test �";
                using (StreamWriter writer = new StreamWriter(stream, System.Text.Encoding.ASCII))
                {
                    writer.Write(test);
                    writer.Flush();
                    // There is no ASCII translation for �, so the standard replacement is used.
                    Assert.IsTrue((string)mockBuffer["Text"] == "? Test ?");
                }
            }
            int lockCount   = mockBuffer.FunctionCalls(string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "LockBuffer"));
            int unlockCount = mockBuffer.FunctionCalls(string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "UnlockBuffer"));

            Assert.IsTrue(lockCount == unlockCount);
        }
コード例 #2
0
        public void TestSetLength()
        {
            IVsTextLines textLines = (IVsTextLines)MockFactories.CreateBufferWithMarker();

            using (Stream stream = CreateTextBufferStream(textLines))
            {
                bool exceptionThrown = false;
                try
                {
                    stream.SetLength(56);
                }
                catch (NotImplementedException)
                {
                    exceptionThrown = true;
                }
                catch (System.Reflection.TargetInvocationException e)
                {
                    NotImplementedException inner = e.InnerException as NotImplementedException;
                    if (null != inner)
                    {
                        exceptionThrown = true;
                    }
                }
                Assert.IsTrue(exceptionThrown);
            }
        }
コード例 #3
0
        public void WriteSmallBuffer()
        {
            BaseMock mockBuffer          = MockFactories.CreateBufferWithMarker();
            string   bufferWriteFunction = string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "ReplaceLines");

            using (Stream stream = CreateTextBufferStream((IVsTextLines)mockBuffer))
            {
                // Verify that writing a small buffer will not cause a change in the
                // buffer (so Flush is not called).
                int    writeSize = 10;
                byte[] buffer    = new byte[writeSize];
                stream.Write(buffer, 0, writeSize);
                Assert.IsTrue(0 == mockBuffer.FunctionCalls(bufferWriteFunction));

                // Now write anothor buffer big enough to leave only 1 not used spot in the
                // internal buffer of the stream.
                writeSize = BufferSize - writeSize - 1;
                buffer    = new byte[writeSize];
                stream.Write(buffer, 0, writeSize);
                Assert.IsTrue(0 == mockBuffer.FunctionCalls(bufferWriteFunction));

                // Verify that writing another byte will cause the data to be written on the
                // text buffer.
                stream.Write(buffer, 0, 1);
                Assert.IsTrue(1 == mockBuffer.FunctionCalls(bufferWriteFunction));
            }
            int lockCount   = mockBuffer.FunctionCalls(string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "LockBuffer"));
            int unlockCount = mockBuffer.FunctionCalls(string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "UnlockBuffer"));

            Assert.IsTrue(lockCount == unlockCount);
        }
コード例 #4
0
        public void WriteNullBuffer()
        {
            IVsTextLines textLines = (IVsTextLines)MockFactories.CreateBufferWithMarker();

            using (Stream stream = CreateTextBufferStream(textLines))
            {
                bool exceptionThrown = false;
                try
                {
                    stream.Write(null, 0, 1);
                }
                catch (ArgumentNullException)
                {
                    exceptionThrown = true;
                }
                catch (System.Reflection.TargetInvocationException e)
                {
                    ArgumentNullException inner = e.InnerException as ArgumentNullException;
                    if (null != inner)
                    {
                        exceptionThrown = true;
                    }
                }
                Assert.IsTrue(exceptionThrown);
            }
        }
コード例 #5
0
        public void StreamProperties()
        {
            BaseMock mockBuffer = MockFactories.CreateBufferWithMarker();

            mockBuffer.AddMethodReturnValues(string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetSize"),
                                             new object[] { 0, 42 });
            mockBuffer.AddMethodReturnValues(string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetPositionOfLineIndex"),
                                             new object[] { 0, 0, 0, 21 });
            using (Stream stream = CreateTextBufferStream((IVsTextLines)mockBuffer))
            {
                Assert.IsFalse(stream.CanRead);
                Assert.IsFalse(stream.CanSeek);
                Assert.IsTrue(stream.CanWrite);
                Assert.IsTrue(42 == stream.Length);
                Assert.IsTrue(21 == stream.Position);
                bool exceptionThrown = false;
                try
                {
                    stream.Position = 11;
                }
                catch (NotImplementedException)
                {
                    exceptionThrown = true;
                }
                catch (System.Reflection.TargetInvocationException e)
                {
                    NotImplementedException inner = e.InnerException as NotImplementedException;
                    if (null != inner)
                    {
                        exceptionThrown = true;
                    }
                }
                Assert.IsTrue(exceptionThrown);
            }
        }
コード例 #6
0
        public void EngineInitialization()
        {
            using (OleServiceProvider provider = new OleServiceProvider())
            {
                // Create a mock text buffer for the console.
                BaseMock          textLinesMock     = MockFactories.CreateBufferWithMarker();
                LocalRegistryMock mockLocalRegistry = new LocalRegistryMock();
                mockLocalRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock);
                provider.AddService(typeof(SLocalRegistry), mockLocalRegistry, false);

                // Create a mock engine provider.
                BaseMock mockEngineProvider = MockFactories.EngineProviderFactory.GetInstance();
                // Create a mock engine.
                BaseMock mockEngine = MockFactories.CreateStandardEngine();
                // Set this engine as the one returned from the GetSharedEngine of the engine provider.
                mockEngineProvider.AddMethodReturnValues(
                    string.Format("{0}.{1}", typeof(IPythonEngineProvider), "GetSharedEngine"),
                    new object[] { (IEngine)mockEngine });
                // Add the engine provider to the list of the services.
                provider.AddService(typeof(IPythonEngineProvider), mockEngineProvider, false);

                // Create the console window
                using (IDisposable disposableObject = CommandWindowHelper.CreateConsoleWindow(provider) as IDisposable)
                {
                    IVsWindowPane windowPane = disposableObject as IVsWindowPane;
                    Assert.IsNotNull(windowPane);

                    // Verify that the shared engine was get.
                    Assert.IsTrue(1 == mockEngineProvider.FunctionCalls(string.Format("{0}.{1}", typeof(IPythonEngineProvider), "GetSharedEngine")));
                    Assert.IsTrue(1 == mockEngine.FunctionCalls(string.Format("{0}.{1}", typeof(IEngine), "set_StdErr")));
                    Assert.IsTrue(1 == mockEngine.FunctionCalls(string.Format("{0}.{1}", typeof(IEngine), "set_StdOut")));
                }
            }
        }
コード例 #7
0
        public void ReadOnlyRegionAfterWrite()
        {
            using (OleServiceProvider provider = new OleServiceProvider())
            {
                // Create a mock text buffer for the console.
                BaseMock textLinesMock = MockFactories.CreateBufferWithMarker();

                // Add the buffer to the local registry.
                LocalRegistryMock mockLocalRegistry = new LocalRegistryMock();
                mockLocalRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock);

                // Add the local registry to the list of services.
                provider.AddService(typeof(SLocalRegistry), mockLocalRegistry, false);

                // Create the console window.
                using (ToolWindowPane windowPane = CommandWindowHelper.CreateConsoleWindow(provider) as ToolWindowPane)
                {
                    // Get the stream from the window pane.
                    System.IO.Stream consoleStream = CommandWindowHelper.ConsoleStream(windowPane);
                    Assert.IsNotNull(consoleStream);

                    // Set a return value for GetLastLineIndex
                    textLinesMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLastLineIndex"),
                        new object[] { 0, 12, 35 });

                    // Write some text on the stream.
                    System.IO.StreamWriter writer = new System.IO.StreamWriter(consoleStream);
                    writer.Write("");
                    writer.Flush();

                    // Verify that the ResetSpan method for the text marker was called and that
                    // the span is set to cover all the current buffer.
                    BaseMock markerMock = (BaseMock)textLinesMock["LineMarker"];
                    Assert.IsTrue(1 == markerMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsTextLineMarker).FullName, "ResetSpan")));
                    TextSpan span = (TextSpan)markerMock["Span"];
                    Assert.IsTrue(0 == span.iStartLine);
                    Assert.IsTrue(0 == span.iStartIndex);
                    Assert.IsTrue(12 == span.iEndLine);
                    Assert.IsTrue(35 == span.iEndIndex);

                    // Change the end point of the buffer and try again.
                    textLinesMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLastLineIndex"),
                        new object[] { 0, 15, 3 });
                    writer.Write("abc");
                    writer.Flush();
                    Assert.IsTrue(2 == markerMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsTextLineMarker).FullName, "ResetSpan")));
                    span = (TextSpan)markerMock["Span"];
                    Assert.IsTrue(0 == span.iStartLine);
                    Assert.IsTrue(0 == span.iStartIndex);
                    Assert.IsTrue(15 == span.iEndLine);
                    Assert.IsTrue(3 == span.iEndIndex);
                }
            }
        }
コード例 #8
0
        public void WriteInvalidArguments()
        {
            IVsTextLines textLines = (IVsTextLines)MockFactories.CreateBufferWithMarker();

            using (Stream stream = CreateTextBufferStream(textLines))
            {
                byte[] buffer = new byte[2];
                Assert.IsTrue(IsWriteOutOfRange(stream, buffer, -1, 1));
                Assert.IsTrue(IsWriteOutOfRange(stream, buffer, 2, 1));
                Assert.IsTrue(IsWriteOutOfRange(stream, buffer, 0, -1));
                Assert.IsTrue(IsWriteOutOfRange(stream, buffer, 1, 2));
                Assert.IsFalse(IsWriteOutOfRange(stream, buffer, 1, 1));
            }
        }
コード例 #9
0
        public void FlushEmptyBuffer()
        {
            BaseMock mockBuffer          = MockFactories.CreateBufferWithMarker();
            string   bufferWriteFunction = string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "ReplaceLines");

            using (Stream stream = CreateTextBufferStream((IVsTextLines)mockBuffer))
            {
                stream.Flush();
                Assert.IsTrue(0 == mockBuffer.FunctionCalls(bufferWriteFunction));
            }
            int lockCount   = mockBuffer.FunctionCalls(string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "LockBuffer"));
            int unlockCount = mockBuffer.FunctionCalls(string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "UnlockBuffer"));

            Assert.IsTrue(lockCount == unlockCount);
        }
コード例 #10
0
        public void WriteBigBuffer()
        {
            BaseMock mockBuffer          = MockFactories.CreateBufferWithMarker();
            string   bufferWriteFunction = string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "ReplaceLines");

            using (Stream stream = CreateTextBufferStream((IVsTextLines)mockBuffer))
            {
                int    writeSize = BufferSize + BufferSize / 2;
                byte[] buffer    = new byte[writeSize];
                stream.Write(buffer, 0, writeSize);
                Assert.IsTrue(1 == mockBuffer.FunctionCalls(bufferWriteFunction));
                stream.Write(buffer, 0, writeSize);
                Assert.IsTrue(3 == mockBuffer.FunctionCalls(bufferWriteFunction));
            }
            int lockCount   = mockBuffer.FunctionCalls(string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "LockBuffer"));
            int unlockCount = mockBuffer.FunctionCalls(string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "UnlockBuffer"));

            Assert.IsTrue(lockCount == unlockCount);
        }
コード例 #11
0
        public void TestFlushWithException()
        {
            BaseMock mockBuffer = MockFactories.CreateBufferWithMarker();

            mockBuffer.AddMethodCallback(string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "ReplaceLines"),
                                         new EventHandler <CallbackArgs>(ReplaceLinesThrow));
            bool exceptionThrown = false;

            try
            {
                using (Stream stream = CreateTextBufferStream((IVsTextLines)mockBuffer))
                {
                    string test = "Test Line";
                    using (StreamWriter writer = new StreamWriter(stream))
                    {
                        writer.Write(test);
                        writer.Flush();
                    }
                }
            }
            catch (TestStreamException)
            {
                exceptionThrown = true;
            }
            catch (System.Reflection.TargetInvocationException e)
            {
                TestStreamException inner = e.InnerException as TestStreamException;
                if (null != inner)
                {
                    exceptionThrown = true;
                }
            }
            Assert.IsTrue(exceptionThrown);
            int lockCount   = mockBuffer.FunctionCalls(string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "LockBuffer"));
            int unlockCount = mockBuffer.FunctionCalls(string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "UnlockBuffer"));

            Assert.IsTrue(lockCount == unlockCount);
        }
コード例 #12
0
        public void ConsoleTextOfLineWithMarker()
        {
            string testString1 = "Test 1";
            string testString2 = "Test 2";

            using (OleServiceProvider provider = new OleServiceProvider())
            {
                BaseMock textLinesMock = MockFactories.CreateBufferWithMarker();
                textLinesMock.AddMethodCallback(
                    string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLineText"),
                    new EventHandler <CallbackArgs>(GetLineTextCallbackForConsoleTextOfLine));

                // Create a new local registry class.
                LocalRegistryMock mockRegistry = new LocalRegistryMock();
                // Add the text buffer to the list of the classes that local registry can create.
                mockRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock);

                // Add the local registry to the service provider.
                provider.AddService(typeof(SLocalRegistry), mockRegistry, false);

                // Create the console.
                using (ToolWindowPane windowPane = CommandWindowHelper.CreateConsoleWindow(provider) as ToolWindowPane)
                {
                    // Make sure that the text marker is created.
                    CommandWindowHelper.EnsureConsoleTextMarker(windowPane);

                    // Set the span of the marker.
                    TextSpan span = new TextSpan();
                    span.iStartLine  = 0;
                    span.iStartIndex = 0;
                    span.iEndLine    = 3;
                    span.iEndIndex   = 5;
                    BaseMock markerMock = (BaseMock)textLinesMock["LineMarker"];
                    markerMock["Span"] = span;

                    IConsoleText consoleText = windowPane as IConsoleText;

                    // Verify the case that the requested line is all inside the
                    // read only region.
                    textLinesMock["LineText"]      = testString1;
                    textLinesMock["ExpectedLine"]  = 1;
                    textLinesMock["ExpectedStart"] = 0;
                    textLinesMock["ExpectedEnd"]   = 10;
                    Assert.IsNull(consoleText.TextOfLine(1, 10, true));
                    string text = consoleText.TextOfLine(1, 10, false);
                    Assert.IsTrue(text == testString1);

                    // Now ask for some text inside the read-only region, but on its last line.
                    textLinesMock["LineText"]      = testString2;
                    textLinesMock["ExpectedLine"]  = 3;
                    textLinesMock["ExpectedStart"] = 0;
                    textLinesMock["ExpectedEnd"]   = 4;
                    Assert.IsNull(consoleText.TextOfLine(3, 4, true));
                    text = consoleText.TextOfLine(3, 4, false);
                    Assert.IsTrue(text == testString2);

                    // Now the text is part inside and part outside the read-only region.
                    textLinesMock["LineText"]      = testString1;
                    textLinesMock["ExpectedLine"]  = 3;
                    textLinesMock["ExpectedStart"] = 5;
                    textLinesMock["ExpectedEnd"]   = 10;
                    text = consoleText.TextOfLine(3, 10, true);
                    Assert.IsTrue(testString1 == text);
                    textLinesMock["LineText"]      = testString2;
                    textLinesMock["ExpectedLine"]  = 3;
                    textLinesMock["ExpectedStart"] = 0;
                    textLinesMock["ExpectedEnd"]   = 10;
                    text = consoleText.TextOfLine(3, 10, false);
                    Assert.IsTrue(text == testString2);

                    // Now the line has no intersection with the read-only region.
                    textLinesMock["LineText"]      = testString1;
                    textLinesMock["ExpectedLine"]  = 4;
                    textLinesMock["ExpectedStart"] = 0;
                    textLinesMock["ExpectedEnd"]   = 10;
                    text = consoleText.TextOfLine(4, 10, true);
                    Assert.IsTrue(testString1 == text);
                    textLinesMock["LineText"]      = testString2;
                    textLinesMock["ExpectedLine"]  = 4;
                    textLinesMock["ExpectedStart"] = 0;
                    textLinesMock["ExpectedEnd"]   = 10;
                    text = consoleText.TextOfLine(4, 10, false);
                    Assert.IsTrue(text == testString2);
                }
            }
        }
コード例 #13
0
        public void EngineStreams()
        {
            using (OleServiceProvider provider = new OleServiceProvider())
            {
                // Create a mock text buffer for the console.
                BaseMock textLinesMock = MockFactories.CreateBufferWithMarker();
                textLinesMock["Text"] = "";
                LocalRegistryMock mockLocalRegistry = new LocalRegistryMock();
                mockLocalRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock);
                provider.AddService(typeof(SLocalRegistry), mockLocalRegistry, false);

                // Create a mock engine provider.
                BaseMock mockEngineProvider = MockFactories.EngineProviderFactory.GetInstance();
                // Create a mock engine.
                BaseMock mockEngine = MockFactories.CreateStandardEngine();
                // Add the callbacks for the setter methods of stderr and stdout
                mockEngine.AddMethodCallback(
                    string.Format("{0}.{1}", typeof(IEngine).FullName, "set_StdErr"),
                    new EventHandler <CallbackArgs>(SetEngineStdErr));
                mockEngine.AddMethodCallback(
                    string.Format("{0}.{1}", typeof(IEngine).FullName, "set_StdOut"),
                    new EventHandler <CallbackArgs>(SetEngineStdOut));
                // Set this engine as the one returned from the GetSharedEngine of the engine provider.
                mockEngineProvider.AddMethodReturnValues(
                    string.Format("{0}.{1}", typeof(IPythonEngineProvider), "GetSharedEngine"),
                    new object[] { (IEngine)mockEngine });
                // Add the engine provider to the list of the services.
                provider.AddService(typeof(IPythonEngineProvider), mockEngineProvider, false);

                // Create the console window.
                using (IDisposable disposableObject = CommandWindowHelper.CreateConsoleWindow(provider) as IDisposable)
                {
                    IVsWindowPane windowPane = disposableObject as IVsWindowPane;
                    Assert.IsNotNull(windowPane);
                    Assert.IsNotNull(mockEngine["StdErr"]);
                    Assert.IsNotNull(mockEngine["StdOut"]);

                    // Set the callback for the text buffer.
                    textLinesMock.AddMethodCallback(
                        string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "ReplaceLines"),
                        new EventHandler <CallbackArgs>(ReplaceLinesCallback));

                    // Verify that the standard error stream is associated with the text buffer.
                    System.IO.Stream stream = (System.IO.Stream)mockEngine["StdErr"];
                    using (System.IO.StreamWriter writer = new System.IO.StreamWriter(stream))
                    {
                        writer.Write("Test String");
                        writer.Flush();
                        Assert.IsTrue((string)textLinesMock["Text"] == "Test String");
                        textLinesMock["Text"] = "";
                    }

                    // Verify the standard output.
                    stream = (System.IO.Stream)mockEngine["StdOut"];
                    using (System.IO.StreamWriter writer = new System.IO.StreamWriter(stream))
                    {
                        writer.Write("Test String");
                        writer.Flush();
                        Assert.IsTrue((string)textLinesMock["Text"] == "Test String");
                        textLinesMock["Text"] = "";
                    }
                }
            }
        }