Пример #1
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")));
                }
            }
        }
Пример #2
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"] = "";
                    }
                }
            }
        }