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); }
public void CommandBufferAddMultiLine() { BaseMock mockEngine = CreateDefaultEngine(); CommandBuffer buffer = new CommandBuffer(mockEngine as IEngine); mockEngine.AddMethodReturnValues(parseFunctionName, new object[] { false }); string[] lines = new string[] { "Line 1", "Line 2", "Line 3" }; const string lastLine = "Last Line"; mockEngine.ResetFunctionCalls(string.Format("{0}.{1}", typeof(IEngine).FullName, "ExecuteToConsole")); string expected = string.Empty; foreach (string line in lines) { expected += line; buffer.Add(line); Assert.AreEqual <string>(expected, buffer.Text); Assert.AreEqual <int>(0, mockEngine.FunctionCalls(string.Format("{0}.{1}", typeof(IEngine).FullName, "ExecuteToConsole"))); expected += System.Environment.NewLine; } // Now change the return value for ParseInteractiveInput so that the execute // function of the engine is called. mockEngine.AddMethodReturnValues(parseFunctionName, new object[] { true }); expected += lastLine; buffer.Add(lastLine); // Now the buffer should be cleared and the text should be passed to the engine. Assert.IsTrue(string.IsNullOrEmpty(buffer.Text)); Assert.AreEqual <int>(1, mockEngine.FunctionCalls(string.Format("{0}.{1}", typeof(IEngine).FullName, "ExecuteToConsole"))); Assert.AreEqual <string>(expected, (string)mockEngine["ExecutedCommand"]); }
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); }
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"))); } } }
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); } } }
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); }
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); }
public void WindowPaneImplementation() { using (OleServiceProvider provider = new OleServiceProvider()) { // Create a mock text buffer for the console. BaseMock textLinesMock = MockFactories.TextBufferFactory.GetInstance(); LocalRegistryMock mockLocalRegistry = new LocalRegistryMock(); mockLocalRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock); BaseMock textViewMock = MockFactories.TextViewFactory.GetInstance(); mockLocalRegistry.AddClass(typeof(VsTextViewClass), textViewMock); provider.AddService(typeof(SLocalRegistry), mockLocalRegistry, false); // Create the tool window. using (IDisposable disposableObject = CommandWindowHelper.CreateConsoleWindow(provider) as IDisposable) { IVsWindowPane windowPane = disposableObject as IVsWindowPane; Assert.IsNotNull(windowPane); // Now call the IVsWindowPane's methods and check that they are redirect to // the implementation provided by the text view. IntPtr newHwnd; Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded( windowPane.CreatePaneWindow(IntPtr.Zero, 0, 0, 0, 0, out newHwnd))); Assert.IsTrue(1 == textViewMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsWindowPane).FullName, "CreatePaneWindow"))); Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded( windowPane.GetDefaultSize(null))); Assert.IsTrue(1 == textViewMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsWindowPane).FullName, "GetDefaultSize"))); Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded( windowPane.LoadViewState(null))); Assert.IsTrue(1 == textViewMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsWindowPane).FullName, "LoadViewState"))); Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded( windowPane.SaveViewState(null))); Assert.IsTrue(1 == textViewMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsWindowPane).FullName, "SaveViewState"))); Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded( windowPane.SetSite(null))); Assert.IsTrue(1 == textViewMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsWindowPane).FullName, "SetSite"))); Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded( windowPane.TranslateAccelerator(null))); Assert.IsTrue(1 == textViewMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsWindowPane).FullName, "TranslateAccelerator"))); Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded( windowPane.ClosePane())); Assert.IsTrue(1 == textViewMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsWindowPane).FullName, "ClosePane"))); } // Verify that the text view is closed after Dispose is called on the window pane. Assert.IsTrue(1 == textViewMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsTextView).FullName, "CloseView"))); } }
public void CommandBufferAddNullLine() { BaseMock mockEngine = CreateDefaultEngine(); CommandBuffer buffer = new CommandBuffer(mockEngine as IEngine); mockEngine.AddMethodReturnValues(parseFunctionName, new object[] { true }); mockEngine.ResetFunctionCalls(string.Format("{0}.{1}", typeof(IEngine).FullName, "ExecuteToConsole")); buffer.Add(null); Assert.AreEqual <int>(1, mockEngine.FunctionCalls(string.Format("{0}.{1}", typeof(IEngine).FullName, "ExecuteToConsole"))); Assert.IsTrue(string.IsNullOrEmpty((string)mockEngine["ExecutedCommand"])); }
private static void TextViewInitializeCallback(object sender, CallbackArgs args) { BaseMock mock = (BaseMock)sender; // Verify that the view is sited and that a text buffer is provided. Assert.IsTrue(1 == mock.FunctionCalls(string.Format("{0}.{1}", typeof(IObjectWithSite), "SetSite"))); IVsTextLines textLines = args.GetParameter(0) as IVsTextLines; Assert.IsNotNull(textLines); // This text view is not supposed to be initialized using a parent window. Assert.IsTrue(IntPtr.Zero == (IntPtr)args.GetParameter(1)); args.ReturnValue = Microsoft.VisualStudio.VSConstants.S_OK; }
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); }
public void TextViewCreation() { using (OleServiceProvider provider = new OleServiceProvider()) { // Create a mock text buffer for the console. BaseMock textLinesMock = MockFactories.TextBufferFactory.GetInstance(); LocalRegistryMock mockLocalRegistry = new LocalRegistryMock(); mockLocalRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock); // Define the mock object for the text view. BaseMock textViewMock = MockFactories.TextViewFactory.GetInstance(); textViewMock.AddMethodCallback(string.Format("{0}.{1}", typeof(IObjectWithSite).FullName, "SetSite"), new EventHandler <CallbackArgs>(TextViewSetSiteCallback)); textViewMock.AddMethodCallback(string.Format("{0}.{1}", typeof(IVsTextView).FullName, "Initialize"), new EventHandler <CallbackArgs>(TextViewInitializeCallback)); mockLocalRegistry.AddClass(typeof(VsTextViewClass), textViewMock); // Add the local registry to the list of services. provider.AddService(typeof(SLocalRegistry), mockLocalRegistry, false); // Create the tool window. using (IDisposable disposableObject = CommandWindowHelper.CreateConsoleWindow(provider) as IDisposable) { IVsWindowPane windowPane = disposableObject as IVsWindowPane; Assert.IsNotNull(windowPane); // Call the CreatePaneWindow method that will force the creation of the text view. IntPtr newHwnd; Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded( windowPane.CreatePaneWindow(IntPtr.Zero, 0, 0, 0, 0, out newHwnd))); // Verify that the text view was used as expected. Assert.IsTrue(1 == textViewMock.FunctionCalls(string.Format("{0}.{1}", typeof(IObjectWithSite), "SetSite"))); Assert.IsTrue(1 == textViewMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsTextView), "Initialize"))); } } }
public void TestIndexComboSetCurValWithInt() { ComboBoxPackage packageObject = new ComboBoxPackage(); Assert.IsNotNull(packageObject, "Failed to create package"); OleServiceProvider serviceProvider = OleServiceProvider.CreateOleServiceProviderWithBasicServices(); // Add site support to create and enumerate tool windows GenericMockFactory mockFactory = new GenericMockFactory("MockUIShell", new Type[] { typeof(IVsUIShell) }); BaseMock uiShell = mockFactory.GetInstance(); serviceProvider.AddService(typeof(SVsUIShell), uiShell, false); // Site the package Assert.AreEqual(0, ((IVsPackage)packageObject).SetSite(serviceProvider), "SetSite did not return S_OK"); MethodInfo method = typeof(ComboBoxPackage).GetMethod("OnMenuMyIndexCombo", BindingFlags.NonPublic | BindingFlags.Instance); int inChoice = 1; object inParam1 = inChoice; IntPtr outParam1 = IntPtr.Zero; object inParam2 = null; IntPtr outParam2 = Marshal.AllocCoTaskMem(64); //64 == size of a variant + a little extra padding try { // Set IndexCombo to 2nd choice in list OleMenuCmdEventArgs eventArgs1 = new OleMenuCmdEventArgs(inParam1, outParam1); object result = method.Invoke(packageObject, new object[] { null, eventArgs1 }); // Retrieve current value of Index and verify it is "Oranges" OleMenuCmdEventArgs eventArgs2 = new OleMenuCmdEventArgs(inParam2, outParam2); result = method.Invoke(packageObject, new object[] { null, eventArgs2 }); string retrieved = (string)Marshal.GetObjectForNativeVariant(outParam2); Assert.AreEqual <string>(expectedIndexComboChoices[inChoice], retrieved); Assert.AreEqual(1, uiShell.FunctionCalls(String.Format("{0}.{1}", typeof(IVsUIShell).FullName, "ShowMessageBox")), "IVsUIShell.ShowMessageBox was not called"); } finally { if (outParam2 != IntPtr.Zero) { Marshal.FreeCoTaskMem(outParam2); } } }
public void GetDeclarationsTwoResults() { using (OleServiceProvider provider = new OleServiceProvider()) { ResetScopeState(); // Create a mock engine provider. BaseMock mockEngineProvider = MockFactories.EngineProviderFactory.GetInstance(); // Create a mock engine. BaseMock mockEngine = MockFactories.EngineFactory.GetInstance(); string evaluateMethodName = string.Format("{0}.{1}", typeof(IEngine).FullName, "Evaluate"); mockEngine.AddMethodCallback( evaluateMethodName, new EventHandler <CallbackArgs>(EvaluateCallback)); // 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 scanner for this test. BaseMock scannerMock = MockFactories.ScannerFactory.GetInstance(); scannerMock["Iteration"] = 0; TokenInfo[] tokens = new TokenInfo[2]; tokens[0] = new TokenInfo(); tokens[0].StartIndex = 0; tokens[0].EndIndex = 7; tokens[0].Trigger = TokenTriggers.None; tokens[1] = new TokenInfo(); tokens[1].StartIndex = 8; tokens[1].EndIndex = 8; tokens[1].Trigger = TokenTriggers.MemberSelect; scannerMock["Tokens"] = tokens; scannerMock.AddMethodCallback( string.Format("{0}.{1}", typeof(IScanner).FullName, "ScanTokenAndProvideInfoAboutIt"), new EventHandler <CallbackArgs>(StandardScannerCallback)); Declarations declarations = ExecuteGetDeclarations("variable.", scannerMock as IScanner, provider); Assert.IsTrue(2 == declarations.GetCount()); Assert.IsTrue(1 == mockEngine.FunctionCalls(evaluateMethodName)); Assert.IsTrue("Method 1" == declarations.GetDisplayText(0)); Assert.IsTrue("Method 2" == declarations.GetDisplayText(1)); } }
public void WindowConstructor() { using (OleServiceProvider provider = new OleServiceProvider()) { // Create a mock object for the text buffer. BaseMock textLinesMock = MockFactories.TextBufferFactory.GetInstance(); // 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); provider.AddService(typeof(SLocalRegistry), mockRegistry, false); // Now create the object and verify that the constructor sets the site for the text buffer. using (IDisposable consoleObject = CommandWindowHelper.CreateConsoleWindow(provider) as IDisposable) { Assert.IsNotNull(consoleObject); Assert.IsTrue(0 < textLinesMock.FunctionCalls(string.Format("{0}.{1}", typeof(IObjectWithSite).FullName, "SetSite"))); } } }
public void StandardConstructor() { using (OleServiceProvider provider = OleServiceProvider.CreateOleServiceProviderWithBasicServices()) { IVsPackage package = null; try { // Create a mock object for the text buffer. BaseMock textLinesMock = MockFactories.TextBufferFactory.GetInstance(); // 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); provider.AddService(typeof(SLocalRegistry), mockRegistry, false); // Now create a package object and site it. package = new PythonConsolePackage() as IVsPackage; package.SetSite(provider); // Create a console window using the standard constructor and verify that the // text buffer is created and sited. using (IDisposable consoleObject = CommandWindowHelper.CreateConsoleWindow() as IDisposable) { Assert.IsTrue(0 < textLinesMock.FunctionCalls(string.Format("{0}.{1}", typeof(IObjectWithSite).FullName, "SetSite"))); } } finally { if (null != package) { package.SetSite(null); package.Close(); } } } }