Browse Source

Move IronPython and IronRuby console classes to common Scripting project.

pull/1/head
mrward 15 years ago
parent
commit
4608b9b580
  1. 313
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonConsole.cs
  2. 32
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/DisposedPythonConsoleTestFixture.cs
  3. 87
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/PythonConsoleCommandLineHistoryTestFixture.cs
  4. 47
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/PythonConsoleCurrentLineTestFixture.cs
  5. 119
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/PythonConsoleReadTestFixture.cs
  6. 22
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/PythonConsoleTestsBase.cs
  7. 55
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/PythonConsoleUnreadLinesTestFixture.cs
  8. 102
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/TwoPythonConsoleLinesWaitingTestFixture.cs
  9. 14
      src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj
  10. 316
      src/AddIns/BackendBindings/Ruby/RubyBinding/Project/Src/RubyConsole.cs
  11. 32
      src/AddIns/BackendBindings/Ruby/RubyBinding/Test/Console/DisposedRubyConsoleTestFixture.cs
  12. 55
      src/AddIns/BackendBindings/Ruby/RubyBinding/Test/Console/KeysPressedWhenCompletionWindowOpenTestFixture.cs
  13. 55
      src/AddIns/BackendBindings/Ruby/RubyBinding/Test/Console/RubyConsoleCodeCompletionTestFixture.cs
  14. 60
      src/AddIns/BackendBindings/Ruby/RubyBinding/Test/Console/RubyConsoleEnterKeyTestFixture.cs
  15. 49
      src/AddIns/BackendBindings/Ruby/RubyBinding/Test/Console/RubyConsoleHomeKeyTestFixture.cs
  16. 160
      src/AddIns/BackendBindings/Ruby/RubyBinding/Test/Console/RubyConsoleReadOnlyRegionsTestFixture.cs
  17. 170
      src/AddIns/BackendBindings/Ruby/RubyBinding/Test/Console/RubyConsoleSendLineTests.cs
  18. 22
      src/AddIns/BackendBindings/Ruby/RubyBinding/Test/Console/RubyConsoleTestsBase.cs
  19. 53
      src/AddIns/BackendBindings/Ruby/RubyBinding/Test/Console/RubyConsoleWriteTestFixture.cs
  20. 14
      src/AddIns/BackendBindings/Ruby/RubyBinding/Test/RubyBinding.Tests.csproj
  21. 2
      src/AddIns/BackendBindings/Scripting/Project/ICSharpCode.Scripting.csproj
  22. 334
      src/AddIns/BackendBindings/Scripting/Project/Src/ScriptingConsole.cs
  23. 19
      src/AddIns/BackendBindings/Scripting/Project/Src/ScriptingStyle.cs
  24. 29
      src/AddIns/BackendBindings/Scripting/Test/Console/DisposedScriptingConsoleTests.cs
  25. 25
      src/AddIns/BackendBindings/Scripting/Test/Console/KeysPressedWhenCompletionWindowOpenTests.cs
  26. 12
      src/AddIns/BackendBindings/Scripting/Test/Console/ScriptingConsoleCodeCompletionTests.cs
  27. 25
      src/AddIns/BackendBindings/Scripting/Test/Console/ScriptingConsoleCommandLineHistoryTests.cs
  28. 24
      src/AddIns/BackendBindings/Scripting/Test/Console/ScriptingConsoleCurrentLineTests.cs
  29. 17
      src/AddIns/BackendBindings/Scripting/Test/Console/ScriptingConsoleEnterKeyTests.cs
  30. 13
      src/AddIns/BackendBindings/Scripting/Test/Console/ScriptingConsoleHomeKeyTests.cs
  31. 35
      src/AddIns/BackendBindings/Scripting/Test/Console/ScriptingConsoleReadOnlyRegionsTests.cs
  32. 17
      src/AddIns/BackendBindings/Scripting/Test/Console/ScriptingConsoleReadTests.cs
  33. 53
      src/AddIns/BackendBindings/Scripting/Test/Console/ScriptingConsoleSendLineTests.cs
  34. 21
      src/AddIns/BackendBindings/Scripting/Test/Console/ScriptingConsoleTestsBase.cs
  35. 26
      src/AddIns/BackendBindings/Scripting/Test/Console/ScriptingConsoleUnreadLinesTests.cs
  36. 26
      src/AddIns/BackendBindings/Scripting/Test/Console/ScriptingConsoleWriteTests.cs
  37. 31
      src/AddIns/BackendBindings/Scripting/Test/Console/TwoConsoleLinesWaitingTests.cs
  38. 15
      src/AddIns/BackendBindings/Scripting/Test/ICSharpCode.Scripting.Tests.csproj
  39. 51
      src/AddIns/BackendBindings/Scripting/Test/Utils/TestableScriptingConsole.cs

313
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonConsole.cs

@ -3,47 +3,22 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing;
using System.IO; using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Input;
using ICSharpCode.AvalonEdit.CodeCompletion;
using ICSharpCode.Scripting; using ICSharpCode.Scripting;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting.Shell; using Microsoft.Scripting.Hosting.Shell;
namespace ICSharpCode.PythonBinding namespace ICSharpCode.PythonBinding
{ {
public class PythonConsole : IConsole, IDisposable, IMemberProvider, IScriptingConsole public class PythonConsole : ScriptingConsole, IConsole
{ {
IScriptingConsoleTextEditor textEditor;
int lineReceivedEventIndex = 0; // The index into the waitHandles array where the lineReceivedEvent is stored.
ManualResetEvent lineReceivedEvent = new ManualResetEvent(false);
ManualResetEvent disposedEvent = new ManualResetEvent(false);
WaitHandle[] waitHandles;
int promptLength;
bool firstPromptDisplayed;
string savedSendLineText;
CommandLineHistory commandLineHistory = new CommandLineHistory();
protected List<string> unreadLines = new List<string>();
public PythonConsole(IScriptingConsoleTextEditor textEditor) public PythonConsole(IScriptingConsoleTextEditor textEditor)
: base(textEditor)
{ {
waitHandles = new WaitHandle[] {lineReceivedEvent, disposedEvent};
this.textEditor = textEditor;
textEditor.PreviewKeyDown += ProcessPreviewKeyDown;
} }
public CommandLine CommandLine { get; set; } public CommandLine CommandLine { get; set; }
public void Dispose()
{
disposedEvent.Set();
}
public TextWriter Output { public TextWriter Output {
get { return null; } get { return null; }
set { } set { }
@ -57,298 +32,24 @@ namespace ICSharpCode.PythonBinding
/// <summary> /// <summary>
/// Gets the member names of the specified item. /// Gets the member names of the specified item.
/// </summary> /// </summary>
public IList<string> GetMemberNames(string name) public override IList<string> GetMemberNames(string name)
{ {
return CommandLine.GetMemberNames(name); return CommandLine.GetMemberNames(name);
} }
public IList<string> GetGlobals(string name) public override IList<string> GetGlobals(string name)
{ {
return CommandLine.GetGlobals(name); return CommandLine.GetGlobals(name);
} }
/// <summary>
/// Returns the next line typed in by the console user. If no line is available this method
/// will block.
/// </summary>
public string ReadLine(int autoIndentSize)
{
string indent = GetIndent(autoIndentSize);
if (autoIndentSize > 0) {
Write(indent, Style.Prompt);
}
string line = ReadLineFromTextEditor();
if (line != null) {
return indent + line;
}
return null;
}
string GetIndent(int autoIndentSize)
{
return String.Empty.PadLeft(autoIndentSize);
}
/// <summary>
/// Writes text to the console.
/// </summary>
public void Write(string text, Style style) public void Write(string text, Style style)
{ {
textEditor.Write(text); base.Write(text, (ScriptingStyle)style);
if (style == Style.Prompt) {
WriteSavedLineTextAfterFirstPrompt(text);
promptLength = text.Length;
textEditor.MakeCurrentContentReadOnly();
}
} }
void WriteSavedLineTextAfterFirstPrompt(string promptText)
{
firstPromptDisplayed = true;
if (savedSendLineText != null) {
textEditor.Write(savedSendLineText + "\r\n");
savedSendLineText = null;
}
}
/// <summary>
/// Writes text followed by a newline to the console.
/// </summary>
public void WriteLine(string text, Style style) public void WriteLine(string text, Style style)
{ {
Write(text + Environment.NewLine, style); base.WriteLine(text, (ScriptingStyle)style);
}
/// <summary>
/// Writes an empty line to the console.
/// </summary>
public void WriteLine()
{
Write(Environment.NewLine, Style.Out);
}
/// <summary>
/// Indicates whether there is a line already read by the console and waiting to be processed.
/// </summary>
public bool IsLineAvailable {
get {
lock (unreadLines) {
return (unreadLines.Count > 0);
}
}
}
/// <summary>
/// Gets the text that is yet to be processed from the console. This is the text that is being
/// typed in by the user who has not yet pressed the enter key.
/// </summary>
public string GetCurrentLine()
{
string fullLine = GetLastTextEditorLine();
return fullLine.Substring(promptLength);
}
string GetLastTextEditorLine()
{
return textEditor.GetLine(textEditor.TotalLines - 1);
}
string ReadLineFromTextEditor()
{
int result = WaitHandle.WaitAny(waitHandles);
if (result == lineReceivedEventIndex) {
lock (unreadLines) {
string line = unreadLines[0];
unreadLines.RemoveAt(0);
if (unreadLines.Count == 0) {
lineReceivedEvent.Reset();
}
return line;
}
}
return null;
}
/// <summary>
/// Processes characters entered into the text editor by the user.
/// </summary>
void ProcessPreviewKeyDown(object source, ConsoleTextEditorKeyEventArgs e)
{
Key keyPressed = e.Key;
e.Handled = HandleKeyDown(keyPressed);
}
bool HandleKeyDown(Key keyPressed)
{
if (textEditor.IsCompletionWindowDisplayed) {
return false;
}
if (IsInReadOnlyRegion) {
switch (keyPressed) {
case Key.Left:
case Key.Right:
case Key.Up:
case Key.Down:
return false;
default:
return true;
}
}
switch (keyPressed) {
case Key.Back:
return !CanBackspace;
case Key.Home:
MoveToHomePosition();
return true;
case Key.Down:
MoveToNextCommandLine();
return true;
case Key.Up:
MoveToPreviousCommandLine();
return true;
}
if (keyPressed == Key.Return) {
OnEnterKeyPressed();
}
if (keyPressed == Key.OemPeriod) {
ShowCompletionWindow();
}
return false;
}
void OnEnterKeyPressed()
{
lock (unreadLines) {
MoveCursorToEndOfLastTextEditorLine();
SaveLastTextEditorLine();
lineReceivedEvent.Set();
}
}
void MoveCursorToEndOfLastTextEditorLine()
{
textEditor.Line = textEditor.TotalLines - 1;
textEditor.Column = GetLastTextEditorLine().Length;
}
void SaveLastTextEditorLine()
{
string currentLine = GetCurrentLine();
unreadLines.Add(currentLine);
commandLineHistory.Add(currentLine);
}
/// <summary>
/// Returns true if the cursor is in a readonly text editor region.
/// </summary>
bool IsInReadOnlyRegion {
get { return IsCurrentLineReadOnly || IsInPrompt; }
}
/// <summary>
/// Only the last line in the text editor is not read only.
/// </summary>
bool IsCurrentLineReadOnly {
get { return textEditor.Line < (textEditor.TotalLines - 1); }
}
/// <summary>
/// Determines whether the current cursor position is in a prompt.
/// </summary>
bool IsInPrompt {
get { return (textEditor.Column - promptLength) < 0; }
}
/// <summary>
/// Returns true if the user can backspace at the current cursor position.
/// </summary>
bool CanBackspace {
get {
int cursorIndex = textEditor.Column - promptLength;
int selectionStartIndex = textEditor.SelectionStart - promptLength;
return (cursorIndex > 0) && (selectionStartIndex > 0);
}
}
void ShowCompletionWindow()
{
ScriptingConsoleCompletionDataProvider completionProvider = new ScriptingConsoleCompletionDataProvider(this);
textEditor.ShowCompletionWindow(completionProvider);
}
/// <summary>
/// The home position is at the start of the line after the prompt.
/// </summary>
void MoveToHomePosition()
{
textEditor.Column = promptLength;
}
/// <summary>
/// Shows the previous command line in the command line history.
/// </summary>
void MoveToPreviousCommandLine()
{
if (commandLineHistory.MovePrevious()) {
ReplaceCurrentLineTextAfterPrompt(commandLineHistory.Current);
}
}
/// <summary>
/// Shows the next command line in the command line history.
/// </summary>
void MoveToNextCommandLine()
{
if (commandLineHistory.MoveNext()) {
ReplaceCurrentLineTextAfterPrompt(commandLineHistory.Current);
}
}
/// <summary>
/// Replaces the current line text after the prompt with the specified text.
/// </summary>
void ReplaceCurrentLineTextAfterPrompt(string text)
{
string currentLine = GetCurrentLine();
textEditor.Replace(promptLength, currentLine.Length, text);
// Put cursor at end.
textEditor.Column = promptLength + text.Length;
}
public void SendLine(string text)
{
using (ILock linesLock = CreateLock(unreadLines)) {
unreadLines.Add(text);
}
FireLineReceivedEvent();
MoveCursorToEndOfLastTextEditorLine();
WriteLineIfFirstPromptHasBeenDisplayed(text);
}
protected virtual ILock CreateLock(List<string> lines)
{
return new StringListLock(lines);
}
protected virtual void FireLineReceivedEvent()
{
lineReceivedEvent.Set();
}
void WriteLineIfFirstPromptHasBeenDisplayed(string text)
{
if (firstPromptDisplayed) {
WriteLine(text, Style.Out);
} else {
savedSendLineText = text;
}
} }
} }
} }

32
src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/DisposedPythonConsoleTestFixture.cs

@ -1,32 +0,0 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using ICSharpCode.PythonBinding;
using NUnit.Framework;
using PythonBinding.Tests.Utils;
namespace PythonBinding.Tests.Console
{
/// <summary>
/// Tests the disposing of the PythonConsole.
/// </summary>
[TestFixture]
public class DisposedPythonConsoleTestFixture : PythonConsoleTestsBase
{
[Test]
public void PythonConsoleImplementsIDisposable()
{
base.CreatePythonConsole();
Assert.IsNotNull(TestablePythonConsole as IDisposable);
}
[Test]
public void ReadLineReturnsNullWhenConsoleDisposed()
{
base.CreatePythonConsole();
TestablePythonConsole.Dispose();
Assert.IsNull(TestablePythonConsole.ReadLine(0));
}
}
}

87
src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/PythonConsoleCommandLineHistoryTestFixture.cs

@ -1,87 +0,0 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Windows.Input;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using Microsoft.Scripting.Hosting.Shell;
using ICSharpCode.PythonBinding;
using NUnit.Framework;
using PythonBinding.Tests.Utils;
namespace PythonBinding.Tests.Console
{
/// <summary>
/// Tests the PythonConsole's command line history.
/// </summary>
[TestFixture]
public class PythonConsoleCommandLineHistoryTestFixture : PythonConsoleTestsBase
{
string prompt = ">>> ";
[SetUp]
public void Init()
{
base.CreatePythonConsole();
TestablePythonConsole.Write(prompt, Style.Prompt);
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.A);
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter);
TestablePythonConsole.Write(prompt, Style.Prompt);
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.B);
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.C);
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter);
TestablePythonConsole.Write(prompt, Style.Prompt);
}
[Test]
public void UpArrowKeyPressed()
{
Assert.IsTrue(MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Up));
}
[Test]
public void CurrentLineAfterUpArrowKeyPressed()
{
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Up);
Assert.AreEqual("BC", TestablePythonConsole.GetCurrentLine());
}
[Test]
public void TextEditorCursorIsAtEndOfLineAfterUpArrowKeyPressed()
{
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Up);
Assert.AreEqual(prompt.Length + 2, MockConsoleTextEditor.Column);
}
[Test]
public void TextAfterUpArrowKeyPressedTwiceThenDownArrowKey()
{
UpArrowKeyPressedTwiceThenDownArrowKey();
Assert.AreEqual("BC", TestablePythonConsole.GetCurrentLine());
}
[Test]
public void TextEditorCursorAfterUpArrowKeyPressedTwice()
{
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Up);
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Up);
Assert.AreEqual(prompt.Length + 1, MockConsoleTextEditor.Column);
}
[Test]
public void DownArrowKeyHandled()
{
Assert.IsTrue(MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Down));
}
void UpArrowKeyPressedTwiceThenDownArrowKey()
{
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Up);
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Up);
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Down);
}
}
}

47
src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/PythonConsoleCurrentLineTestFixture.cs

@ -1,47 +0,0 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Windows.Input;
using ICSharpCode.PythonBinding;
using ICSharpCode.Scripting.Tests.Utils;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using Microsoft.Scripting.Hosting.Shell;
using NUnit.Framework;
using PythonBinding.Tests.Utils;
namespace PythonBinding.Tests.Console
{
/// <summary>
/// Tests the PythonConsole's GetCurrentLine method.
/// </summary>
[TestFixture]
public class PythonConsoleCurrentLineTestFixture
{
TestablePythonConsole pythonConsole;
MockConsoleTextEditor textEditor;
string prompt = ">>> ";
[SetUp]
public void Init()
{
pythonConsole = new TestablePythonConsole();
pythonConsole.Write(prompt, Style.Prompt);
textEditor = pythonConsole.MockConsoleTextEditor;
}
[Test]
public void CurrentLineIsEmpty()
{
Assert.AreEqual(String.Empty, pythonConsole.GetCurrentLine());
}
[Test]
public void SingleCharacterAddedToTextEditor()
{
textEditor.RaisePreviewKeyDownEvent(Key.A);
Assert.AreEqual("A", pythonConsole.GetCurrentLine());
}
}
}

119
src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/PythonConsoleReadTestFixture.cs

@ -1,119 +0,0 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Threading;
using System.Windows.Input;
using ICSharpCode.PythonBinding;
using ICSharpCode.Scripting.Tests.Utils;
using NUnit.Framework;
using PythonBinding.Tests.Utils;
namespace PythonBinding.Tests.Console
{
/// <summary>
/// Tests the PythonConsole ReadLine method.
/// </summary>
[TestFixture]
public class PythonConsoleReadTestFixture : PythonConsoleTestsBase
{
int initialAutoIndentSize = 4;
string readLine;
int autoIndentSize;
bool raiseKeyPressEvent;
bool raiseDialogKeyPressEvent;
[TestFixtureSetUp]
public void Init()
{
base.CreatePythonConsole();
autoIndentSize = initialAutoIndentSize;
Thread thread = new Thread(ReadLineFromConsoleOnDifferentThread);
thread.Start();
int sleepInterval = 10;
int maxWait = 2000;
int currentWait = 0;
while ((MockConsoleTextEditor.Text.Length < autoIndentSize) && (currentWait < maxWait)) {
Thread.Sleep(sleepInterval);
currentWait += sleepInterval;
}
raiseKeyPressEvent = MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.A);
raiseDialogKeyPressEvent = MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter);
currentWait = 0;
while ((MockConsoleTextEditor.Text.Length < autoIndentSize + 2) && (currentWait < maxWait)) {
Thread.Sleep(10);
currentWait += sleepInterval;
}
thread.Join(2000);
}
[TestFixtureTearDown]
public void TearDown()
{
TestablePythonConsole.Dispose();
}
[Test]
public void ReadLineFromConsole()
{
string expectedString = String.Empty.PadLeft(initialAutoIndentSize) + "A";
Assert.AreEqual(expectedString, readLine);
}
[Test]
public void ReadLineWithNonZeroAutoIndentSizeWritesSpacesToTextEditor()
{
string expectedString = String.Empty.PadLeft(initialAutoIndentSize) + "A\r\n";
Assert.AreEqual(expectedString, MockConsoleTextEditor.Text);
}
[Test]
public void DocumentInsertCalledWhenAutoIndentIsNonZero()
{
Assert.IsTrue(MockConsoleTextEditor.IsWriteCalled);
}
[Test]
public void NoTextWrittenWhenAutoIndentSizeIsZero()
{
TestablePythonConsole pythonConsole = new TestablePythonConsole();
MockConsoleTextEditor textEditor = pythonConsole.MockConsoleTextEditor;
textEditor.RaisePreviewKeyDownEvent(Key.A);
textEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter);
textEditor.IsWriteCalled = false;
pythonConsole.ReadLine(0);
Assert.IsFalse(textEditor.IsWriteCalled);
}
/// <summary>
/// Should return false for any character that should be handled by the text editor itself.
/// </summary>
[Test]
public void RaiseKeyPressEventReturnedFalse()
{
Assert.IsFalse(raiseKeyPressEvent);
}
/// <summary>
/// Should return false for any character that should be handled by the text editor itself.
/// </summary>
[Test]
public void RaiseDialogKeyPressEventReturnedFalse()
{
Assert.IsFalse(raiseDialogKeyPressEvent);
}
void ReadLineFromConsoleOnDifferentThread()
{
System.Console.WriteLine("Reading on different thread");
readLine = TestablePythonConsole.ReadLine(autoIndentSize);
System.Console.WriteLine("Finished reading on different thread");
}
}
}

22
src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/PythonConsoleTestsBase.cs

@ -1,22 +0,0 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using ICSharpCode.PythonBinding;
using ICSharpCode.Scripting.Tests.Utils;
using PythonBinding.Tests.Utils;
namespace PythonBinding.Tests.Console
{
public class PythonConsoleTestsBase
{
public MockConsoleTextEditor MockConsoleTextEditor;
public TestablePythonConsole TestablePythonConsole;
public void CreatePythonConsole()
{
TestablePythonConsole = new TestablePythonConsole();
MockConsoleTextEditor = TestablePythonConsole.MockConsoleTextEditor;
}
}
}

55
src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/PythonConsoleUnreadLinesTestFixture.cs

@ -1,55 +0,0 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Windows.Input;
using ICSharpCode.PythonBinding;
using IronPython.Hosting;
using IronPython.Runtime;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using Microsoft.Scripting.Hosting.Shell;
using NUnit.Framework;
using PythonBinding.Tests.Utils;
namespace PythonBinding.Tests.Console
{
/// <summary>
/// Tests the PythonConsole's GetUnreadLines method.
/// </summary>
[TestFixture]
public class PythonConsoleUnreadLinesTestFixture : PythonConsoleTestsBase
{
[SetUp]
public void Init()
{
base.CreatePythonConsole();
}
[Test]
public void NoUnreadLinesAtStart()
{
Assert.AreEqual(0, TestablePythonConsole.GetUnreadLines().Length);
}
[Test]
public void HasUnreadLines()
{
Assert.IsFalse(TestablePythonConsole.IsLineAvailable);
}
[Test]
public void AddOneLine()
{
MockConsoleTextEditor.RaisePreviewKeyDownEvent(System.Windows.Input.Key.A);
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(System.Windows.Input.Key.Enter);
string[] lines = TestablePythonConsole.GetUnreadLines();
string[] expectedLines = new string[] {"A"};
Assert.AreEqual(expectedLines, lines);
Assert.IsTrue(TestablePythonConsole.IsLineAvailable);
}
}
}

102
src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/TwoPythonConsoleLinesWaitingTestFixture.cs

@ -1,102 +0,0 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Threading;
using ICSharpCode.PythonBinding;
using ICSharpCode.Scripting.Tests.Utils;
using IronPython.Hosting;
using IronPython.Runtime;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using Microsoft.Scripting.Hosting.Shell;
using NUnit.Framework;
using PythonBinding.Tests.Utils;
using Input = System.Windows.Input;
namespace PythonBinding.Tests.Console
{
/// <summary>
/// Ensures that both lines of text can be read from the python console if they are written
/// before ReadLine is called.
/// </summary>
[TestFixture]
public class TwoPythonConsoleLinesWaitingTestFixture
{
string line1;
string line2;
TestablePythonConsole pythonConsole;
bool lineAvailableBeforeFirstEnterKey;
bool lineAvailableAfterFirstEnterKey;
bool lineAvailableAtEnd;
[TestFixtureSetUp]
public void SetUpFixture()
{
using (pythonConsole = new TestablePythonConsole()) {
MockConsoleTextEditor textEditor = pythonConsole.MockConsoleTextEditor;
textEditor.RaisePreviewKeyDownEvent(Input.Key.A);
textEditor.RaisePreviewKeyDownEvent(Input.Key.B);
textEditor.RaisePreviewKeyDownEvent(Input.Key.C);
lineAvailableBeforeFirstEnterKey = pythonConsole.IsLineAvailable;
textEditor.RaisePreviewKeyDownEventForDialogKey(Input.Key.Enter);
lineAvailableAfterFirstEnterKey = pythonConsole.IsLineAvailable;
textEditor.RaisePreviewKeyDownEvent(Input.Key.D);
textEditor.RaisePreviewKeyDownEvent(Input.Key.E);
textEditor.RaisePreviewKeyDownEvent(Input.Key.F);
textEditor.RaisePreviewKeyDownEventForDialogKey(Input.Key.Enter);
Thread t = new Thread(ReadLinesOnSeparateThread);
t.Start();
int sleepInterval = 20;
int currentWait = 0;
int maxWait = 2000;
while (line2 == null && currentWait < maxWait) {
Thread.Sleep(sleepInterval);
currentWait += sleepInterval;
}
lineAvailableAtEnd = pythonConsole.IsLineAvailable;
}
}
[Test]
public void FirstLineRead()
{
Assert.AreEqual("ABC", line1);
}
[Test]
public void SecondLineRead()
{
Assert.AreEqual("DEF", line2);
}
[Test]
public void LineAvailableBeforeEnterKeyPressed()
{
Assert.IsFalse(lineAvailableBeforeFirstEnterKey);
}
[Test]
public void LineAvailableAfterEnterKeyPressed()
{
Assert.IsTrue(lineAvailableAfterFirstEnterKey);
}
[Test]
public void LineAvailableAfterAllLinesRead()
{
Assert.IsFalse(lineAvailableAtEnd);
}
void ReadLinesOnSeparateThread()
{
line1 = pythonConsole.ReadLine(0);
line2 = pythonConsole.ReadLine(0);
}
}
}

14
src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj

@ -102,21 +102,7 @@
<Compile Include="Completion\SysModuleMembersInPythonContextTestFixture.cs" /> <Compile Include="Completion\SysModuleMembersInPythonContextTestFixture.cs" />
<Compile Include="Configuration\AddInOptionsTestFixture.cs" /> <Compile Include="Configuration\AddInOptionsTestFixture.cs" />
<Compile Include="Console\BuiltinCodeCompletionTestFixture.cs" /> <Compile Include="Console\BuiltinCodeCompletionTestFixture.cs" />
<Compile Include="Console\PythonConsoleSendLineTests.cs" />
<Compile Include="Console\PythonConsoleTestsBase.cs" />
<Compile Include="Console\KeysPressedWhenCompletionWindowOpenTestFixture.cs" />
<Compile Include="Console\DisposedPythonConsoleTestFixture.cs" />
<Compile Include="Console\PythonConsoleCommandLineHistoryTestFixture.cs" />
<Compile Include="Console\PythonConsoleHomeKeyTestFixture.cs" />
<Compile Include="Console\PythonConsoleCodeCompletionTestFixture.cs" />
<Compile Include="Console\PythonConsoleCurrentLineTestFixture.cs" />
<Compile Include="Console\PythonConsoleEnterKeyTestFixture.cs" />
<Compile Include="Console\PythonConsoleHostTests.cs" /> <Compile Include="Console\PythonConsoleHostTests.cs" />
<Compile Include="Console\PythonConsoleReadOnlyRegionsTestFixture.cs" />
<Compile Include="Console\PythonConsoleReadTestFixture.cs" />
<Compile Include="Console\PythonConsoleUnreadLinesTestFixture.cs" />
<Compile Include="Console\PythonConsoleWriteTestFixture.cs" />
<Compile Include="Console\TwoPythonConsoleLinesWaitingTestFixture.cs" />
<Compile Include="Converter\AddHandlerConversionTestFixture.cs" /> <Compile Include="Converter\AddHandlerConversionTestFixture.cs" />
<Compile Include="Converter\ArrayCastConversionTestFixture.cs" /> <Compile Include="Converter\ArrayCastConversionTestFixture.cs" />
<Compile Include="Converter\ArrayConversionTestFixture.cs" /> <Compile Include="Converter\ArrayConversionTestFixture.cs" />

316
src/AddIns/BackendBindings/Ruby/RubyBinding/Project/Src/RubyConsole.cs

@ -2,56 +2,21 @@
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) // This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Threading;
using System.Windows.Input;
using ICSharpCode.AvalonEdit.CodeCompletion;
using ICSharpCode.Scripting; using ICSharpCode.Scripting;
using Microsoft.Scripting.Hosting;
using Microsoft.Scripting.Hosting.Shell; using Microsoft.Scripting.Hosting.Shell;
namespace ICSharpCode.RubyBinding namespace ICSharpCode.RubyBinding
{ {
public class RubyConsole : IConsole, IDisposable, IMemberProvider, IScriptingConsole public class RubyConsole : ScriptingConsole, IConsole
{ {
IScriptingConsoleTextEditor textEditor;
int lineReceivedEventIndex = 0; // The index into the waitHandles array where the lineReceivedEvent is stored.
ManualResetEvent lineReceivedEvent = new ManualResetEvent(false);
ManualResetEvent disposedEvent = new ManualResetEvent(false);
WaitHandle[] waitHandles;
int promptLength;
bool firstPromptDisplayed;
string savedSendLineText;
CommandLineHistory commandLineHistory = new CommandLineHistory();
protected List<string> unreadLines = new List<string>();
public RubyConsole(IScriptingConsoleTextEditor textEditor) public RubyConsole(IScriptingConsoleTextEditor textEditor)
: base(textEditor)
{ {
waitHandles = new WaitHandle[] {lineReceivedEvent, disposedEvent};
this.textEditor = textEditor;
textEditor.PreviewKeyDown += ProcessPreviewKeyDown;
} }
public CommandLine CommandLine { get; set; } public CommandLine CommandLine { get; set; }
public void Dispose()
{
disposedEvent.Set();
}
/// <summary>
/// Gets the text that is yet to be processed from the console. This is the text that is being
/// typed in by the user who has not yet pressed the enter key.
/// </summary>
public string GetCurrentLine()
{
string fullLine = GetLastTextEditorLine();
return fullLine.Substring(promptLength);
}
public TextWriter Output { public TextWriter Output {
get { return null; } get { return null; }
set { } set { }
@ -62,285 +27,14 @@ namespace ICSharpCode.RubyBinding
set { } set { }
} }
public string ReadLine(int autoIndentSize)
{
string indent = GetIndent(autoIndentSize);
if (autoIndentSize > 0) {
Write(indent, Style.Prompt);
}
string line = ReadLineFromTextEditor();
if (line != null) {
return indent + line;
}
return null;
}
string GetIndent(int autoIndentSize)
{
return String.Empty.PadLeft(autoIndentSize);
}
public void Write(string text, Style style) public void Write(string text, Style style)
{ {
textEditor.Write(text); base.Write(text, (ScriptingStyle)style);
if (style == Style.Prompt) {
WriteSavedLineTextAfterFirstPrompt(text);
promptLength = text.Length;
textEditor.MakeCurrentContentReadOnly();
}
}
void WriteSavedLineTextAfterFirstPrompt(string promptText)
{
firstPromptDisplayed = true;
if (savedSendLineText != null) {
textEditor.Write(savedSendLineText + "\r\n");
savedSendLineText = null;
}
} }
public void WriteLine(string text, Style style) public void WriteLine(string text, Style style)
{ {
Write(text + Environment.NewLine, style); base.WriteLine(text, (ScriptingStyle)style);
}
/// <summary>
/// Writes an empty line to the console.
/// </summary>
public void WriteLine()
{
Write(Environment.NewLine, Style.Out);
}
/// <summary>
/// Indicates whether there is a line already read by the console and waiting to be processed.
/// </summary>
public bool IsLineAvailable {
get {
lock (unreadLines) {
return (unreadLines.Count > 0);
}
}
}
/// <summary>
/// Gets the member names of the specified item.
/// </summary>
public IList<string> GetMemberNames(string name)
{
return CommandLine.GetMemberNames(name);
}
public IList<string> GetGlobals(string name)
{
return CommandLine.GetGlobals(name);
}
/// <summary>
/// Processes characters entered into the text editor by the user.
/// </summary>
void ProcessPreviewKeyDown(object source, ConsoleTextEditorKeyEventArgs e)
{
Key keyPressed = e.Key;
e.Handled = HandleKeyDown(keyPressed);
}
bool HandleKeyDown(Key keyPressed)
{
if (textEditor.IsCompletionWindowDisplayed) {
return false;
}
if (IsInReadOnlyRegion) {
switch (keyPressed) {
case Key.Left:
case Key.Right:
case Key.Up:
case Key.Down:
return false;
default:
return true;
}
}
switch (keyPressed) {
case Key.Back:
return !CanBackspace;
case Key.Home:
MoveToHomePosition();
return true;
case Key.Down:
MoveToNextCommandLine();
return true;
case Key.Up:
MoveToPreviousCommandLine();
return true;
}
if (keyPressed == Key.Return) {
OnEnterKeyPressed();
}
if (keyPressed == Key.OemPeriod) {
ShowCompletionWindow();
}
return false;
}
string ReadLineFromTextEditor()
{
int result = WaitHandle.WaitAny(waitHandles);
if (result == lineReceivedEventIndex) {
lock (unreadLines) {
string line = unreadLines[0];
unreadLines.RemoveAt(0);
if (unreadLines.Count == 0) {
lineReceivedEvent.Reset();
}
return line;
}
}
return null;
}
/// <summary>
/// Move cursor to the end of the line before retrieving the line.
/// </summary>
void OnEnterKeyPressed()
{
lock (unreadLines) {
MoveCursorToEndOfLastTextEditorLine();
SaveLastTextEditorLine();
lineReceivedEvent.Set();
}
}
void MoveCursorToEndOfLastTextEditorLine()
{
textEditor.Line = textEditor.TotalLines - 1;
textEditor.Column = GetLastTextEditorLine().Length;
}
void SaveLastTextEditorLine()
{
string currentLine = GetCurrentLine();
unreadLines.Add(currentLine);
commandLineHistory.Add(currentLine);
}
string GetLastTextEditorLine()
{
return textEditor.GetLine(textEditor.TotalLines - 1);
}
/// <summary>
/// Returns true if the cursor is in a readonly text editor region.
/// </summary>
bool IsInReadOnlyRegion {
get { return IsCurrentLineReadOnly || IsInPrompt; }
}
/// <summary>
/// Only the last line in the text editor is not read only.
/// </summary>
bool IsCurrentLineReadOnly {
get { return textEditor.Line < (textEditor.TotalLines - 1); }
}
/// <summary>
/// Determines whether the current cursor position is in a prompt.
/// </summary>
bool IsInPrompt {
get { return (textEditor.Column - promptLength) < 0; }
}
/// <summary>
/// Returns true if the user can backspace at the current cursor position.
/// </summary>
bool CanBackspace {
get {
int cursorIndex = textEditor.Column - promptLength;
int selectionStartIndex = textEditor.SelectionStart - promptLength;
return (cursorIndex > 0) && (selectionStartIndex > 0);
}
}
/// <summary>
/// The home position is at the start of the line after the prompt.
/// </summary>
void MoveToHomePosition()
{
textEditor.Column = promptLength;
}
/// <summary>
/// Shows the previous command line in the command line history.
/// </summary>
void MoveToPreviousCommandLine()
{
if (commandLineHistory.MovePrevious()) {
ReplaceCurrentLineTextAfterPrompt(commandLineHistory.Current);
}
}
/// <summary>
/// Shows the next command line in the command line history.
/// </summary>
void MoveToNextCommandLine()
{
if (commandLineHistory.MoveNext()) {
ReplaceCurrentLineTextAfterPrompt(commandLineHistory.Current);
}
}
/// <summary>
/// Replaces the current line text after the prompt with the specified text.
/// </summary>
void ReplaceCurrentLineTextAfterPrompt(string text)
{
string currentLine = GetCurrentLine();
textEditor.Replace(promptLength, currentLine.Length, text);
// Put cursor at end.
textEditor.Column = promptLength + text.Length;
}
void ShowCompletionWindow()
{
ScriptingConsoleCompletionDataProvider completionProvider = new ScriptingConsoleCompletionDataProvider(this);
textEditor.ShowCompletionWindow(completionProvider);
}
public void SendLine(string text)
{
using (ILock linesLock = CreateLock(unreadLines)) {
unreadLines.Add(text);
}
FireLineReceivedEvent();
MoveCursorToEndOfLastTextEditorLine();
WriteLineIfFirstPromptHasBeenDisplayed(text);
}
protected virtual ILock CreateLock(List<string> lines)
{
return new StringListLock(lines);
}
protected virtual void FireLineReceivedEvent()
{
lineReceivedEvent.Set();
}
void WriteLineIfFirstPromptHasBeenDisplayed(string text)
{
if (firstPromptDisplayed) {
WriteLine(text, Style.Out);
} else {
savedSendLineText = text;
}
} }
} }
} }

32
src/AddIns/BackendBindings/Ruby/RubyBinding/Test/Console/DisposedRubyConsoleTestFixture.cs

@ -1,32 +0,0 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using ICSharpCode.RubyBinding;
using NUnit.Framework;
using RubyBinding.Tests.Utils;
namespace RubyBinding.Tests.Console
{
/// <summary>
/// Tests the disposing of the RubyConsole.
/// </summary>
[TestFixture]
public class DisposedRubyConsoleTestFixture : RubyConsoleTestsBase
{
[Test]
public void RubyConsoleImplementsIDisposable()
{
base.CreateRubyConsole();
Assert.IsNotNull(TestableRubyConsole as IDisposable);
}
[Test]
public void ReadLineReturnsNullWhenConsoleDisposed()
{
base.CreateRubyConsole();
TestableRubyConsole.Dispose();
Assert.IsNull(TestableRubyConsole.ReadLine(0));
}
}
}

55
src/AddIns/BackendBindings/Ruby/RubyBinding/Test/Console/KeysPressedWhenCompletionWindowOpenTestFixture.cs

@ -1,55 +0,0 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Windows.Input;
using ICSharpCode.RubyBinding;
using ICSharpCode.SharpDevelop;
using Microsoft.Scripting.Hosting.Shell;
using NUnit.Framework;
using RubyBinding.Tests.Utils;
namespace RubyBinding.Tests.Console
{
/// <summary>
/// Pressing the up key closes the completion window when it should not. This fixture tests
/// that the up, down, home and end key do not close this window.
/// </summary>
[TestFixture]
public class KeysPressedWhenCompletionWindowOpenTestFixture : RubyConsoleTestsBase
{
string prompt = ">>> ";
[SetUp]
public void Init()
{
base.CreateRubyConsole();
TestableRubyConsole.Write(prompt, Style.Prompt);
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.A);
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter);
TestableRubyConsole.Write(prompt, Style.Prompt);
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.B);
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.OemPeriod);
}
[Test]
public void ShowCompletionWindowIsCalled()
{
Assert.IsTrue(MockConsoleTextEditor.IsShowCompletionWindowCalled);
}
[Test]
public void UpKeyDoesNothing()
{
Assert.IsFalse(MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Up));
}
[Test]
public void DownKeyDoesNothing()
{
Assert.IsFalse(MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Down));
}
}
}

55
src/AddIns/BackendBindings/Ruby/RubyBinding/Test/Console/RubyConsoleCodeCompletionTestFixture.cs

@ -1,55 +0,0 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Input;
using ICSharpCode.RubyBinding;
using ICSharpCode.Scripting;
using Microsoft.Scripting.Hosting.Shell;
using NUnit.Framework;
using RubyBinding.Tests.Utils;
namespace RubyBinding.Tests.Console
{
/// <summary>
/// When the dot character is typed in after an object the code completion window should appear.
/// </summary>
[TestFixture]
public class RubyConsoleCodeCompletionTestFixture : RubyConsoleTestsBase
{
string prompt = ">>> ";
bool showCompletionWindowCalledBeforeDotTypedIn;
[TestFixtureSetUp]
public void SetUpFixture()
{
base.CreateRubyConsole();
TestableRubyConsole.WriteLine(prompt, Style.Prompt);
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.A);
showCompletionWindowCalledBeforeDotTypedIn = MockConsoleTextEditor.IsShowCompletionWindowCalled;
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.OemPeriod);
}
[Test]
public void ShowCompletionWindowCalled()
{
Assert.IsTrue(MockConsoleTextEditor.IsShowCompletionWindowCalled);
}
[Test]
public void ShowCompletionWindowNotCalledBeforeDotTypedIn()
{
Assert.IsFalse(showCompletionWindowCalledBeforeDotTypedIn);
}
[Test]
public void ScriptingConsoleCompletionDataProviderPassedToShowCompletionWindowMethod()
{
Assert.IsInstanceOf(typeof(ScriptingConsoleCompletionDataProvider), MockConsoleTextEditor.CompletionProviderPassedToShowCompletionWindow);
}
}
}

60
src/AddIns/BackendBindings/Ruby/RubyBinding/Test/Console/RubyConsoleEnterKeyTestFixture.cs

@ -1,60 +0,0 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Windows.Input;
using Microsoft.Scripting.Hosting.Shell;
using ICSharpCode.RubyBinding;
using NUnit.Framework;
using RubyBinding.Tests.Utils;
namespace RubyBinding.Tests.Console
{
/// <summary>
/// Tests that pressing the enter key in the middle of a typed in line in the Ruby console
/// leaves the line alone and moves the cursor to the next line. By default the text editor
/// will break the line and move the last part to the second line.
/// </summary>
[TestFixture]
public class RubyConsoleEnterKeyTestFixture : RubyConsoleTestsBase
{
string prompt = ">>> ";
[SetUp]
public void Init()
{
base.CreateRubyConsole();
TestableRubyConsole.Write(prompt, Style.Prompt);
}
[Test]
public void EnterKeyDoesNotBreakUpExistingLine()
{
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.A);
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.B);
MockConsoleTextEditor.SelectionStart = 1 + prompt.Length;
MockConsoleTextEditor.Column = 1 + prompt.Length;
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter);
Assert.AreEqual(">>> AB\r\n", MockConsoleTextEditor.Text);
}
[Test]
public void PreviousLineIsReadOnlyAfterEnterPressed()
{
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.A);
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.B);
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter);
TestableRubyConsole.Write(prompt, Style.Prompt);
// Move up a line with cursor.
MockConsoleTextEditor.Line = 0;
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.C);
string expectedText =
">>> AB\r\n" +
">>> ";
Assert.AreEqual(expectedText, MockConsoleTextEditor.Text);
}
}
}

49
src/AddIns/BackendBindings/Ruby/RubyBinding/Test/Console/RubyConsoleHomeKeyTestFixture.cs

@ -1,49 +0,0 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.Windows.Input;
using System.Threading;
using Microsoft.Scripting.Hosting.Shell;
using ICSharpCode.RubyBinding;
using NUnit.Framework;
using RubyBinding.Tests.Utils;
namespace RubyBinding.Tests.Console
{
/// <summary>
/// The Home Key should return the user to the start of the line after the prompt.
/// </summary>
[TestFixture]
public class RubyConsoleHomeKeyTestFixture : RubyConsoleTestsBase
{
string prompt = ">>> ";
[SetUp]
public void Init()
{
base.CreateRubyConsole();
TestableRubyConsole.Write(prompt, Style.Prompt);
}
[Test]
public void HomeKeyPressedWhenNoUserTextInConsole()
{
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Home);
int expectedColumn = prompt.Length;
Assert.AreEqual(expectedColumn, MockConsoleTextEditor.Column);
}
[Test]
public void HomeKeyPressedWhenTextInConsole()
{
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.A);
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Home);
int expectedColumn = prompt.Length;
Assert.AreEqual(expectedColumn, MockConsoleTextEditor.Column);
}
}
}

160
src/AddIns/BackendBindings/Ruby/RubyBinding/Test/Console/RubyConsoleReadOnlyRegionsTestFixture.cs

@ -1,160 +0,0 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Windows.Input;
using ICSharpCode.RubyBinding;
using Microsoft.Scripting.Hosting.Shell;
using NUnit.Framework;
using RubyBinding.Tests.Utils;
namespace RubyBinding.Tests.Console
{
/// <summary>
/// Tests that the user cannot type into read-only regions of the text editor. The
/// RubyConsole itself restricts typing itself by handling key press events.
/// </summary>
[TestFixture]
public class RubyConsoleReadOnlyRegionsTestFixture : RubyConsoleTestsBase
{
string prompt = ">>> ";
[SetUp]
public void Init()
{
base.CreateRubyConsole();
TestableRubyConsole.Write(prompt, Style.Prompt);
}
[Test]
public void MakeCurrentContentReadOnlyIsCalled()
{
Assert.IsTrue(MockConsoleTextEditor.IsMakeCurrentContentReadOnlyCalled);
}
[Test]
public void LeftArrowThenInsertNewCharacterInsertsText()
{
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.A);
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.B);
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Left);
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.C);
Assert.AreEqual("ACB", TestableRubyConsole.GetCurrentLine());
}
[Test]
public void MoveOneCharacterIntoPromptTypingShouldBePrevented()
{
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Left);
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.A);
Assert.AreEqual(String.Empty, TestableRubyConsole.GetCurrentLine());
}
[Test]
public void MoveOneCharacterIntoPromptAndBackspaceKeyShouldNotRemoveAnything()
{
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.A);
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Left);
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Back);
Assert.AreEqual("A", TestableRubyConsole.GetCurrentLine());
Assert.AreEqual(prompt + "A", MockConsoleTextEditor.Text);
}
[Test]
public void MoveTwoCharactersIntoPromptAndBackspaceKeyShouldNotRemoveAnything()
{
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.A);
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Left);
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Left);
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Back);
Assert.AreEqual("A", TestableRubyConsole.GetCurrentLine());
Assert.AreEqual(prompt + "A", MockConsoleTextEditor.Text);
}
[Test]
public void SelectLastCharacterOfPromptThenPressingTheBackspaceKeyShouldNotRemoveAnything()
{
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.A);
MockConsoleTextEditor.SelectionStart = prompt.Length - 1;
MockConsoleTextEditor.SelectionLength = 2;
MockConsoleTextEditor.Column += 2;
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Back);
Assert.AreEqual("A", TestableRubyConsole.GetCurrentLine());
Assert.AreEqual(prompt + "A", MockConsoleTextEditor.Text);
}
[Test]
public void CanMoveIntoPromptRegionWithLeftCursorKey()
{
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Left);
Assert.IsFalse(MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Left));
}
[Test]
public void CanMoveOutOfPromptRegionWithRightCursorKey()
{
MockConsoleTextEditor.Column = 0;
Assert.IsFalse(MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Right));
}
[Test]
public void CanMoveOutOfPromptRegionWithUpCursorKey()
{
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter);
TestableRubyConsole.Write(prompt, Style.Prompt);
MockConsoleTextEditor.Column = 0;
Assert.IsFalse(MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Up));
}
[Test]
public void CanMoveInReadOnlyRegionWithDownCursorKey()
{
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter);
TestableRubyConsole.Write(prompt, Style.Prompt);
MockConsoleTextEditor.Column = 0;
MockConsoleTextEditor.Line = 0;
Assert.IsFalse(MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Down));
}
[Test]
public void BackspaceKeyPressedIgnoredIfLineIsEmpty()
{
Assert.IsTrue(MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Back));
}
[Test]
public void BackspaceOnPreviousLine()
{
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.A);
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.B);
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter);
TestableRubyConsole.Write(prompt, Style.Prompt);
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.C);
// Move up a line with cursor.
MockConsoleTextEditor.Line = 0;
Assert.IsTrue(MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Back));
Assert.AreEqual("C", TestableRubyConsole.GetCurrentLine());
}
[Test]
public void CanBackspaceFirstCharacterOnLine()
{
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.A);
MockConsoleTextEditor.Column = 5;
MockConsoleTextEditor.SelectionStart = 5;
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Back);
Assert.AreEqual(String.Empty, TestableRubyConsole.GetCurrentLine());
}
}
}

170
src/AddIns/BackendBindings/Ruby/RubyBinding/Test/Console/RubyConsoleSendLineTests.cs

@ -1,170 +0,0 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using ICSharpCode.NRefactory;
using ICSharpCode.RubyBinding;
using Microsoft.Scripting.Hosting.Shell;
using NUnit.Framework;
using RubyBinding.Tests.Utils;
namespace RubyBinding.Tests.Console
{
[TestFixture]
public class RubyConsoleSendLineTests : RubyConsoleTestsBase
{
[Test]
public void SendLine_NoUnreadLines_AddsLineToUnreadLines()
{
SendLineToConsole("test");
string[] unreadLines = TestableRubyConsole.GetUnreadLines();
string[] expectedUnreadlines = new string[] {"test"};
Assert.AreEqual(expectedUnreadlines, unreadLines);
}
void SendLineToConsole(string text)
{
base.CreateRubyConsole();
WritePrompt();
TestableRubyConsole.SendLine(text);
}
void WritePrompt()
{
TestableRubyConsole.Write(">>> ", Style.Prompt);
}
[Test]
public void SendLine_NoUnreadLines_CreatesLockForPreviousLines()
{
SendLineToConsole("test");
List<string> lines = TestableRubyConsole.LockCreated.Lines;
List<string> expectedLines = TestableRubyConsole.GetUnreadLinesList();
Assert.AreEqual(expectedLines, lines);
}
[Test]
public void SendLine_NoUnreadLines_LockForPreviousLinesIsDisposed()
{
SendLineToConsole("test");
bool disposed = TestableRubyConsole.LockCreated.IsDisposed;
Assert.IsTrue(disposed);
}
[Test]
public void SendLine_NoUnreadLines_LineNotAddedBeforeLockCreated()
{
SendLineToConsole("test");
int count = TestableRubyConsole.LockCreated.UnreadLineCountWhenLockCreated;
int expectedCount = 0;
Assert.AreEqual(expectedCount, count);
}
[Test]
public void SendLine_NoUnreadLines_LineAddedBeforeLockDisposed()
{
SendLineToConsole("test");
int count = TestableRubyConsole.LockCreated.UnreadLineCountWhenLockDisposed;
int expectedCount = 1;
Assert.AreEqual(expectedCount, count);
}
[Test]
public void SendLine_NoUnreadLines_LineReceivedEventIsFired()
{
SendLineToConsole("test");
bool fired = TestableRubyConsole.IsLineReceivedEventFired;
Assert.IsTrue(fired);
}
[Test]
public void SendLine_NoUnreadLines_LineReceivedEventAfterLineAddedToUnreadLines()
{
SendLineToConsole("test");
int count = TestableRubyConsole.UnreadLineCountWhenLineReceivedEventFired;
int expectedCount = 1;
Assert.AreEqual(expectedCount, count);
}
[Test]
public void SendLine_NoUnreadLines_LineWrittenToConsoleTextEditor()
{
SendLineToConsole("test");
string text = MockConsoleTextEditor.TextPassedToWrite;
string expectedTextWritten = "test\r\n";
Assert.AreEqual(expectedTextWritten, text);
}
[Test]
public void SendLine_TwoLinesInConsoleTextEditorCursorOnFirstLine_CursorMovedToEndOfLastLineBeforeTextWritten()
{
base.CreateRubyConsole();
WritePrompt();
MockConsoleTextEditor.Text =
">>> first\r\n" +
">>> second\r\n" +
">>> ";
MockConsoleTextEditor.Line = 0;
MockConsoleTextEditor.Column = 0;
TestableRubyConsole.SendLine("test");
int expectedLine = 2;
int expectedColumn = 4;
Location expectedLocation = new Location(expectedColumn, expectedLine);
Location location = MockConsoleTextEditor.CursorLocationWhenWriteTextCalled;
Assert.AreEqual(expectedLocation, location);
}
[Test]
public void SendLine_NoUnreadLines_NoTextWrittenToConsoleTextEditorBeforeFirstPromptIsWritten()
{
base.CreateRubyConsole();
TestableRubyConsole.SendLine("test");
string text = MockConsoleTextEditor.TextPassedToWrite;
Assert.IsNull(text);
}
[Test]
public void Write_SendLineCalledButNoPromptWritten_WritesOutSavedSendLineText()
{
base.CreateRubyConsole();
TestableRubyConsole.SendLine("test");
TestableRubyConsole.Write(">>> ", Style.Prompt);
string text = MockConsoleTextEditor.Text;
string expectedText =
">>> test\r\n";
Assert.AreEqual(expectedText, text);
}
[Test]
public void Write_CalledTwiceAfterSendLineCalledButNoPromptWritten_WritesOutSavedSendLineTextOnlyOnce()
{
base.CreateRubyConsole();
TestableRubyConsole.SendLine("test");
TestableRubyConsole.Write(">>> ", Style.Prompt);
TestableRubyConsole.Write(">>> ", Style.Prompt);
string text = MockConsoleTextEditor.Text;
string expectedText =
">>> test\r\n" +
">>> ";
Assert.AreEqual(expectedText, text);
}
}
}

22
src/AddIns/BackendBindings/Ruby/RubyBinding/Test/Console/RubyConsoleTestsBase.cs

@ -1,22 +0,0 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using ICSharpCode.RubyBinding;
using ICSharpCode.Scripting.Tests.Utils;
using RubyBinding.Tests.Utils;
namespace RubyBinding.Tests.Console
{
public class RubyConsoleTestsBase
{
public MockConsoleTextEditor MockConsoleTextEditor;
public TestableRubyConsole TestableRubyConsole;
public void CreateRubyConsole()
{
TestableRubyConsole = new TestableRubyConsole();
MockConsoleTextEditor = TestableRubyConsole.MockConsoleTextEditor;
}
}
}

53
src/AddIns/BackendBindings/Ruby/RubyBinding/Test/Console/RubyConsoleWriteTestFixture.cs

@ -1,53 +0,0 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Drawing;
using ICSharpCode.RubyBinding;
using Microsoft.Scripting.Hosting.Shell;
using NUnit.Framework;
using RubyBinding.Tests.Utils;
namespace RubyBinding.Tests.Console
{
/// <summary>
/// Tests that the RubyConsole Write method correctly update the text editor.
/// </summary>
[TestFixture]
public class RubyConsoleWriteTestFixture : RubyConsoleTestsBase
{
[SetUp]
public void Init()
{
base.CreateRubyConsole();
}
[Test]
public void WriteLine()
{
TestableRubyConsole.WriteLine();
Assert.AreEqual(Environment.NewLine, MockConsoleTextEditor.Text);
}
[Test]
public void WriteLineWithText()
{
TestableRubyConsole.WriteLine("test", Style.Out);
Assert.AreEqual("test" + Environment.NewLine, MockConsoleTextEditor.Text);
}
[Test]
public void TwoWrites()
{
TestableRubyConsole.Write("a", Style.Out);
TestableRubyConsole.Write("b", Style.Out);
Assert.AreEqual("ab", MockConsoleTextEditor.Text);
}
[Test]
public void DoesNotHasLinesWaitingToBeRead()
{
Assert.IsFalse(TestableRubyConsole.IsLineAvailable);
}
}
}

14
src/AddIns/BackendBindings/Ruby/RubyBinding/Test/RubyBinding.Tests.csproj

@ -72,21 +72,7 @@
<ItemGroup> <ItemGroup>
<Compile Include="AssemblyInfo.cs" /> <Compile Include="AssemblyInfo.cs" />
<Compile Include="Configuration\RubyAddInOptionsTestFixture.cs" /> <Compile Include="Configuration\RubyAddInOptionsTestFixture.cs" />
<Compile Include="Console\DisposedRubyConsoleTestFixture.cs" />
<Compile Include="Console\KeysPressedWhenCompletionWindowOpenTestFixture.cs" />
<Compile Include="Console\RubyConsoleCodeCompletionTestFixture.cs" />
<Compile Include="Console\RubyConsoleCommandLineHistoryTestFixture.cs" />
<Compile Include="Console\RubyConsoleCurrentLineTestFixture.cs" />
<Compile Include="Console\RubyConsoleEnterKeyTestFixture.cs" />
<Compile Include="Console\RubyConsoleHomeKeyTestFixture.cs" />
<Compile Include="Console\RubyConsoleHostTests.cs" /> <Compile Include="Console\RubyConsoleHostTests.cs" />
<Compile Include="Console\RubyConsoleReadOnlyRegionsTestFixture.cs" />
<Compile Include="Console\RubyConsoleReadTestFixture.cs" />
<Compile Include="Console\RubyConsoleSendLineTests.cs" />
<Compile Include="Console\RubyConsoleTestsBase.cs" />
<Compile Include="Console\RubyConsoleUnreadLinesTestFixture.cs" />
<Compile Include="Console\RubyConsoleWriteTestFixture.cs" />
<Compile Include="Console\TwoRubyConsoleLinesWaitingTestFixture.cs" />
<Compile Include="Converter\AddHandlerConversionTestFixture.cs" /> <Compile Include="Converter\AddHandlerConversionTestFixture.cs" />
<Compile Include="Converter\ArrayCastConversionTestFixture.cs" /> <Compile Include="Converter\ArrayCastConversionTestFixture.cs" />
<Compile Include="Converter\ArrayConversionTestFixture.cs" /> <Compile Include="Converter\ArrayConversionTestFixture.cs" />

2
src/AddIns/BackendBindings/Scripting/Project/ICSharpCode.Scripting.csproj

@ -79,6 +79,7 @@
<Compile Include="Src\IScriptingFileService.cs" /> <Compile Include="Src\IScriptingFileService.cs" />
<Compile Include="Src\IScriptingWorkbench.cs" /> <Compile Include="Src\IScriptingWorkbench.cs" />
<Compile Include="Src\ScriptingCodeBuilder.cs" /> <Compile Include="Src\ScriptingCodeBuilder.cs" />
<Compile Include="Src\ScriptingConsole.cs" />
<Compile Include="Src\ScriptingConsoleCompletionData.cs" /> <Compile Include="Src\ScriptingConsoleCompletionData.cs" />
<Compile Include="Src\ScriptingConsoleCompletionDataProvider.cs" /> <Compile Include="Src\ScriptingConsoleCompletionDataProvider.cs" />
<Compile Include="Src\ScriptingConsoleOutputStream.cs" /> <Compile Include="Src\ScriptingConsoleOutputStream.cs" />
@ -88,6 +89,7 @@
<Compile Include="Src\ScriptingDesignerLoader.cs" /> <Compile Include="Src\ScriptingDesignerLoader.cs" />
<Compile Include="Src\ScriptingFileService.cs" /> <Compile Include="Src\ScriptingFileService.cs" />
<Compile Include="Src\ScriptingNameCreationService.cs" /> <Compile Include="Src\ScriptingNameCreationService.cs" />
<Compile Include="Src\ScriptingStyle.cs" />
<Compile Include="Src\ScriptingTextEditorViewContent.cs" /> <Compile Include="Src\ScriptingTextEditorViewContent.cs" />
<Compile Include="Src\ScriptingWorkbench.cs" /> <Compile Include="Src\ScriptingWorkbench.cs" />
<Compile Include="Src\StringListLock.cs" /> <Compile Include="Src\StringListLock.cs" />

334
src/AddIns/BackendBindings/Scripting/Project/Src/ScriptingConsole.cs

@ -0,0 +1,334 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Input;
using ICSharpCode.Scripting;
namespace ICSharpCode.Scripting
{
public class ScriptingConsole : IDisposable, IMemberProvider, IScriptingConsole
{
IScriptingConsoleTextEditor textEditor;
int lineReceivedEventIndex = 0; // The index into the waitHandles array where the lineReceivedEvent is stored.
ManualResetEvent lineReceivedEvent = new ManualResetEvent(false);
ManualResetEvent disposedEvent = new ManualResetEvent(false);
WaitHandle[] waitHandles;
int promptLength;
bool firstPromptDisplayed;
string savedSendLineText;
CommandLineHistory commandLineHistory = new CommandLineHistory();
protected List<string> unreadLines = new List<string>();
public ScriptingConsole(IScriptingConsoleTextEditor textEditor)
{
waitHandles = new WaitHandle[] {lineReceivedEvent, disposedEvent};
this.textEditor = textEditor;
textEditor.PreviewKeyDown += ProcessPreviewKeyDown;
}
public void Dispose()
{
disposedEvent.Set();
}
/// <summary>
/// Returns the next line typed in by the console user. If no line is available this method
/// will block.
/// </summary>
public string ReadLine(int autoIndentSize)
{
string indent = GetIndent(autoIndentSize);
if (autoIndentSize > 0) {
Write(indent, ScriptingStyle.Prompt);
}
string line = ReadLineFromTextEditor();
if (line != null) {
return indent + line;
}
return null;
}
string GetIndent(int autoIndentSize)
{
return String.Empty.PadLeft(autoIndentSize);
}
/// <summary>
/// Writes text to the console.
/// </summary>
public void Write(string text, ScriptingStyle style)
{
textEditor.Write(text);
if (style == ScriptingStyle.Prompt) {
WriteSavedLineTextAfterFirstPrompt(text);
promptLength = text.Length;
textEditor.MakeCurrentContentReadOnly();
}
}
void WriteSavedLineTextAfterFirstPrompt(string promptText)
{
firstPromptDisplayed = true;
if (savedSendLineText != null) {
textEditor.Write(savedSendLineText + "\r\n");
savedSendLineText = null;
}
}
/// <summary>
/// Writes text followed by a newline to the console.
/// </summary>
public void WriteLine(string text, ScriptingStyle style)
{
Write(text + Environment.NewLine, style);
}
/// <summary>
/// Writes an empty line to the console.
/// </summary>
public void WriteLine()
{
Write(Environment.NewLine, ScriptingStyle.Out);
}
/// <summary>
/// Indicates whether there is a line already read by the console and waiting to be processed.
/// </summary>
public bool IsLineAvailable {
get {
lock (unreadLines) {
return (unreadLines.Count > 0);
}
}
}
/// <summary>
/// Gets the text that is yet to be processed from the console. This is the text that is being
/// typed in by the user who has not yet pressed the enter key.
/// </summary>
public string GetCurrentLine()
{
string fullLine = GetLastTextEditorLine();
return fullLine.Substring(promptLength);
}
string GetLastTextEditorLine()
{
return textEditor.GetLine(textEditor.TotalLines - 1);
}
string ReadLineFromTextEditor()
{
int result = WaitHandle.WaitAny(waitHandles);
if (result == lineReceivedEventIndex) {
lock (unreadLines) {
string line = unreadLines[0];
unreadLines.RemoveAt(0);
if (unreadLines.Count == 0) {
lineReceivedEvent.Reset();
}
return line;
}
}
return null;
}
/// <summary>
/// Processes characters entered into the text editor by the user.
/// </summary>
void ProcessPreviewKeyDown(object source, ConsoleTextEditorKeyEventArgs e)
{
Key keyPressed = e.Key;
e.Handled = HandleKeyDown(keyPressed);
}
bool HandleKeyDown(Key keyPressed)
{
if (textEditor.IsCompletionWindowDisplayed) {
return false;
}
if (IsInReadOnlyRegion) {
switch (keyPressed) {
case Key.Left:
case Key.Right:
case Key.Up:
case Key.Down:
return false;
default:
return true;
}
}
switch (keyPressed) {
case Key.Back:
return !CanBackspace;
case Key.Home:
MoveToHomePosition();
return true;
case Key.Down:
MoveToNextCommandLine();
return true;
case Key.Up:
MoveToPreviousCommandLine();
return true;
}
if (keyPressed == Key.Return) {
OnEnterKeyPressed();
}
if (keyPressed == Key.OemPeriod) {
ShowCompletionWindow();
}
return false;
}
void OnEnterKeyPressed()
{
lock (unreadLines) {
MoveCursorToEndOfLastTextEditorLine();
SaveLastTextEditorLine();
lineReceivedEvent.Set();
}
}
void MoveCursorToEndOfLastTextEditorLine()
{
textEditor.Line = textEditor.TotalLines - 1;
textEditor.Column = GetLastTextEditorLine().Length;
}
void SaveLastTextEditorLine()
{
string currentLine = GetCurrentLine();
unreadLines.Add(currentLine);
commandLineHistory.Add(currentLine);
}
/// <summary>
/// Returns true if the cursor is in a readonly text editor region.
/// </summary>
bool IsInReadOnlyRegion {
get { return IsCurrentLineReadOnly || IsInPrompt; }
}
/// <summary>
/// Only the last line in the text editor is not read only.
/// </summary>
bool IsCurrentLineReadOnly {
get { return textEditor.Line < (textEditor.TotalLines - 1); }
}
/// <summary>
/// Determines whether the current cursor position is in a prompt.
/// </summary>
bool IsInPrompt {
get { return (textEditor.Column - promptLength) < 0; }
}
/// <summary>
/// Returns true if the user can backspace at the current cursor position.
/// </summary>
bool CanBackspace {
get {
int cursorIndex = textEditor.Column - promptLength;
int selectionStartIndex = textEditor.SelectionStart - promptLength;
return (cursorIndex > 0) && (selectionStartIndex > 0);
}
}
void ShowCompletionWindow()
{
ScriptingConsoleCompletionDataProvider completionProvider = new ScriptingConsoleCompletionDataProvider(this);
textEditor.ShowCompletionWindow(completionProvider);
}
/// <summary>
/// The home position is at the start of the line after the prompt.
/// </summary>
void MoveToHomePosition()
{
textEditor.Column = promptLength;
}
/// <summary>
/// Shows the previous command line in the command line history.
/// </summary>
void MoveToPreviousCommandLine()
{
if (commandLineHistory.MovePrevious()) {
ReplaceCurrentLineTextAfterPrompt(commandLineHistory.Current);
}
}
/// <summary>
/// Shows the next command line in the command line history.
/// </summary>
void MoveToNextCommandLine()
{
if (commandLineHistory.MoveNext()) {
ReplaceCurrentLineTextAfterPrompt(commandLineHistory.Current);
}
}
/// <summary>
/// Replaces the current line text after the prompt with the specified text.
/// </summary>
void ReplaceCurrentLineTextAfterPrompt(string text)
{
string currentLine = GetCurrentLine();
textEditor.Replace(promptLength, currentLine.Length, text);
// Put cursor at end.
textEditor.Column = promptLength + text.Length;
}
public void SendLine(string text)
{
using (ILock linesLock = CreateLock(unreadLines)) {
unreadLines.Add(text);
}
FireLineReceivedEvent();
MoveCursorToEndOfLastTextEditorLine();
WriteLineIfFirstPromptHasBeenDisplayed(text);
}
protected virtual ILock CreateLock(List<string> lines)
{
return new StringListLock(lines);
}
protected virtual void FireLineReceivedEvent()
{
lineReceivedEvent.Set();
}
void WriteLineIfFirstPromptHasBeenDisplayed(string text)
{
if (firstPromptDisplayed) {
WriteLine(text, ScriptingStyle.Out);
} else {
savedSendLineText = text;
}
}
public virtual IList<string> GetMemberNames(string name)
{
return new string[0];
}
public virtual IList<string> GetGlobals(string name)
{
return new string[0];
}
}
}

19
src/AddIns/BackendBindings/Scripting/Project/Src/ScriptingStyle.cs

@ -0,0 +1,19 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
namespace ICSharpCode.Scripting
{
public enum ScriptingStyle
{
Prompt,
Out,
Error,
Warning
}
}

29
src/AddIns/BackendBindings/Scripting/Test/Console/DisposedScriptingConsoleTests.cs

@ -0,0 +1,29 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using ICSharpCode.Scripting;
using NUnit.Framework;
using ICSharpCode.Scripting.Tests.Utils;
namespace ICSharpCode.Scripting.Tests.Console
{
[TestFixture]
public class DisposedScriptingConsoleTests : ScriptingConsoleTestsBase
{
[Test]
public void ConsoleImplementsIDisposable()
{
base.CreateConsole();
Assert.IsNotNull(TestableScriptingConsole as IDisposable);
}
[Test]
public void ReadLineReturnsNullWhenConsoleDisposed()
{
base.CreateConsole();
TestableScriptingConsole.Dispose();
Assert.IsNull(TestableScriptingConsole.ReadLine(0));
}
}
}

25
src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/KeysPressedWhenCompletionWindowOpenTestFixture.cs → src/AddIns/BackendBindings/Scripting/Test/Console/KeysPressedWhenCompletionWindowOpenTests.cs

@ -1,35 +1,38 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) // <file>
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) // <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System; using System;
using System.Windows.Input; using System.Windows.Input;
using ICSharpCode.PythonBinding; using ICSharpCode.Scripting;
using ICSharpCode.Scripting.Tests.Utils;
using ICSharpCode.SharpDevelop; using ICSharpCode.SharpDevelop;
using Microsoft.Scripting.Hosting.Shell;
using NUnit.Framework; using NUnit.Framework;
using PythonBinding.Tests.Utils;
namespace PythonBinding.Tests.Console namespace ICSharpCode.Scripting.Tests.Console
{ {
/// <summary> /// <summary>
/// Pressing the up key closes the completion window when it should not. This fixture tests /// Pressing the up key closes the completion window when it should not. This fixture tests
/// that the up, down, home and end key do not close this window. /// that the up, down, home and end key do not close this window.
/// </summary> /// </summary>
[TestFixture] [TestFixture]
public class KeysPressedWhenCompletionWindowOpenTestFixture : PythonConsoleTestsBase public class KeysPressedWhenCompletionWindowOpenTests : ScriptingConsoleTestsBase
{ {
string prompt = ">>> "; string prompt = ">>> ";
[SetUp] [SetUp]
public void Init() public void Init()
{ {
base.CreatePythonConsole(); base.CreateConsole();
TestablePythonConsole.Write(prompt, Style.Prompt); TestableScriptingConsole.Write(prompt, ScriptingStyle.Prompt);
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.A); MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.A);
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter); MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter);
TestablePythonConsole.Write(prompt, Style.Prompt); TestableScriptingConsole.Write(prompt, ScriptingStyle.Prompt);
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.B); MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.B);
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.OemPeriod); MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.OemPeriod);
} }
@ -44,6 +47,6 @@ namespace PythonBinding.Tests.Console
public void DownKeyDoesNothing() public void DownKeyDoesNothing()
{ {
Assert.IsFalse(MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Down)); Assert.IsFalse(MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Down));
} }
} }
} }

12
src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/PythonConsoleCodeCompletionTestFixture.cs → src/AddIns/BackendBindings/Scripting/Test/Console/ScriptingConsoleCodeCompletionTests.cs

@ -6,19 +6,17 @@ using System.Collections.Generic;
using System.Threading; using System.Threading;
using System.Windows.Input; using System.Windows.Input;
using ICSharpCode.PythonBinding;
using ICSharpCode.Scripting; using ICSharpCode.Scripting;
using Microsoft.Scripting.Hosting.Shell; using ICSharpCode.Scripting.Tests.Utils;
using NUnit.Framework; using NUnit.Framework;
using PythonBinding.Tests.Utils;
namespace PythonBinding.Tests.Console namespace ICSharpCode.Scripting.Tests.Console
{ {
/// <summary> /// <summary>
/// When the dot character is typed in after an object the code completion window should appear. /// When the dot character is typed in after an object the code completion window should appear.
/// </summary> /// </summary>
[TestFixture] [TestFixture]
public class PythonConsoleCodeCompletionTestFixture : PythonConsoleTestsBase public class ScriptingConsoleCodeCompletionTests : ScriptingConsoleTestsBase
{ {
string prompt = ">>> "; string prompt = ">>> ";
bool showCompletionWindowCalledBeforeDotTypedIn; bool showCompletionWindowCalledBeforeDotTypedIn;
@ -26,8 +24,8 @@ namespace PythonBinding.Tests.Console
[TestFixtureSetUp] [TestFixtureSetUp]
public void SetUpFixture() public void SetUpFixture()
{ {
base.CreatePythonConsole(); base.CreateConsole();
TestablePythonConsole.WriteLine(prompt, Style.Prompt); TestableScriptingConsole.WriteLine(prompt, ScriptingStyle.Prompt);
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.A); MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.A);
showCompletionWindowCalledBeforeDotTypedIn = MockConsoleTextEditor.IsShowCompletionWindowCalled; showCompletionWindowCalledBeforeDotTypedIn = MockConsoleTextEditor.IsShowCompletionWindowCalled;

25
src/AddIns/BackendBindings/Ruby/RubyBinding/Test/Console/RubyConsoleCommandLineHistoryTestFixture.cs → src/AddIns/BackendBindings/Scripting/Test/Console/ScriptingConsoleCommandLineHistoryTests.cs

@ -4,36 +4,33 @@
using System; using System;
using System.Windows.Input; using System.Windows.Input;
using Microsoft.Scripting; using ICSharpCode.Scripting;
using Microsoft.Scripting.Hosting; using ICSharpCode.Scripting.Tests.Utils;
using Microsoft.Scripting.Hosting.Shell;
using ICSharpCode.RubyBinding;
using NUnit.Framework; using NUnit.Framework;
using RubyBinding.Tests.Utils;
namespace RubyBinding.Tests.Console namespace ICSharpCode.Scripting.Tests.Console
{ {
/// <summary> /// <summary>
/// Tests the RubyConsole's command line history. /// Tests the ScriptingConsole's command line history.
/// </summary> /// </summary>
[TestFixture] [TestFixture]
public class RubyConsoleCommandLineHistoryTestFixture : RubyConsoleTestsBase public class ScriptingConsoleCommandLineHistoryTestFixture : ScriptingConsoleTestsBase
{ {
string prompt = ">>> "; string prompt = ">>> ";
[SetUp] [SetUp]
public void Init() public void Init()
{ {
base.CreateRubyConsole(); base.CreateConsole();
TestableRubyConsole.Write(prompt, Style.Prompt); TestableScriptingConsole.Write(prompt, ScriptingStyle.Prompt);
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.A); MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.A);
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter); MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter);
TestableRubyConsole.Write(prompt, Style.Prompt); TestableScriptingConsole.Write(prompt, ScriptingStyle.Prompt);
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.B); MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.B);
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.C); MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.C);
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter); MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter);
TestableRubyConsole.Write(prompt, Style.Prompt); TestableScriptingConsole.Write(prompt, ScriptingStyle.Prompt);
} }
[Test] [Test]
@ -46,7 +43,7 @@ namespace RubyBinding.Tests.Console
public void CurrentLineAfterUpArrowKeyPressed() public void CurrentLineAfterUpArrowKeyPressed()
{ {
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Up); MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Up);
Assert.AreEqual("BC", TestableRubyConsole.GetCurrentLine()); Assert.AreEqual("BC", TestableScriptingConsole.GetCurrentLine());
} }
[Test] [Test]
@ -60,7 +57,7 @@ namespace RubyBinding.Tests.Console
public void TextAfterUpArrowKeyPressedTwiceThenDownArrowKey() public void TextAfterUpArrowKeyPressedTwiceThenDownArrowKey()
{ {
UpArrowKeyPressedTwiceThenDownArrowKey(); UpArrowKeyPressedTwiceThenDownArrowKey();
Assert.AreEqual("BC", TestableRubyConsole.GetCurrentLine()); Assert.AreEqual("BC", TestableScriptingConsole.GetCurrentLine());
} }
[Test] [Test]

24
src/AddIns/BackendBindings/Ruby/RubyBinding/Test/Console/RubyConsoleCurrentLineTestFixture.cs → src/AddIns/BackendBindings/Scripting/Test/Console/ScriptingConsoleCurrentLineTests.cs

@ -3,45 +3,41 @@
using System; using System;
using System.Windows.Input; using System.Windows.Input;
using ICSharpCode.RubyBinding; using ICSharpCode.Scripting;
using ICSharpCode.Scripting.Tests.Utils; using ICSharpCode.Scripting.Tests.Utils;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using Microsoft.Scripting.Hosting.Shell;
using NUnit.Framework; using NUnit.Framework;
using RubyBinding.Tests.Utils;
namespace RubyBinding.Tests.Console namespace ICSharpCode.Scripting.Tests.Console
{ {
/// <summary> /// <summary>
/// Tests the RubyConsole's GetCurrentLine method. /// Tests the ScriptingConsole's GetCurrentLine method.
/// </summary> /// </summary>
[TestFixture] [TestFixture]
public class RubyConsoleCurrentLineTestFixture public class ScriptingConsoleCurrentLineTests
{ {
TestableRubyConsole rubyConsole; TestableScriptingConsole console;
MockConsoleTextEditor textEditor; MockConsoleTextEditor textEditor;
string prompt = ">>> "; string prompt = ">>> ";
[SetUp] [SetUp]
public void Init() public void Init()
{ {
rubyConsole = new TestableRubyConsole(); console = new TestableScriptingConsole();
rubyConsole.Write(prompt, Style.Prompt); console.Write(prompt, ScriptingStyle.Prompt);
textEditor = rubyConsole.MockConsoleTextEditor; textEditor = console.MockConsoleTextEditor;
} }
[Test] [Test]
public void CurrentLineIsEmpty() public void CurrentLineIsEmpty()
{ {
Assert.AreEqual(String.Empty, rubyConsole.GetCurrentLine()); Assert.AreEqual(String.Empty, console.GetCurrentLine());
} }
[Test] [Test]
public void SingleCharacterAddedToTextEditor() public void SingleCharacterAddedToTextEditor()
{ {
textEditor.RaisePreviewKeyDownEvent(Key.A); textEditor.RaisePreviewKeyDownEvent(Key.A);
Assert.AreEqual("A", rubyConsole.GetCurrentLine()); Assert.AreEqual("A", console.GetCurrentLine());
} }
} }
} }

17
src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/PythonConsoleEnterKeyTestFixture.cs → src/AddIns/BackendBindings/Scripting/Test/Console/ScriptingConsoleEnterKeyTests.cs

@ -3,28 +3,27 @@
using System; using System;
using System.Windows.Input; using System.Windows.Input;
using Microsoft.Scripting.Hosting.Shell; using ICSharpCode.Scripting;
using ICSharpCode.PythonBinding;
using NUnit.Framework; using NUnit.Framework;
using PythonBinding.Tests.Utils; using ICSharpCode.Scripting.Tests.Utils;
namespace PythonBinding.Tests.Console namespace ICSharpCode.Scripting.Tests.Console
{ {
/// <summary> /// <summary>
/// Tests that pressing the enter key in the middle of a typed in line in the python console /// Tests that pressing the enter key in the middle of a typed in line in the console
/// leaves the line alone and moves the cursor to the next line. By default the text editor /// leaves the line alone and moves the cursor to the next line. By default the text editor
/// will break the line and move the last part to the second line. /// will break the line and move the last part to the second line.
/// </summary> /// </summary>
[TestFixture] [TestFixture]
public class PythonConsoleEnterKeyTestFixture : PythonConsoleTestsBase public class ScriptingConsoleEnterKeyTests : ScriptingConsoleTestsBase
{ {
string prompt = ">>> "; string prompt = ">>> ";
[SetUp] [SetUp]
public void Init() public void Init()
{ {
base.CreatePythonConsole(); base.CreateConsole();
TestablePythonConsole.Write(prompt, Style.Prompt); TestableScriptingConsole.Write(prompt, ScriptingStyle.Prompt);
} }
public void EnterKeyDoesNotBreakUpExistingLine() public void EnterKeyDoesNotBreakUpExistingLine()
@ -44,7 +43,7 @@ namespace PythonBinding.Tests.Console
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.A); MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.A);
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.B); MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.B);
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter); MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter);
TestablePythonConsole.Write(prompt, Style.Prompt); TestableScriptingConsole.Write(prompt, ScriptingStyle.Prompt);
// Move up a line with cursor. // Move up a line with cursor.
MockConsoleTextEditor.Line = 0; MockConsoleTextEditor.Line = 0;

13
src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/PythonConsoleHomeKeyTestFixture.cs → src/AddIns/BackendBindings/Scripting/Test/Console/ScriptingConsoleHomeKeyTests.cs

@ -5,26 +5,25 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Windows.Input; using System.Windows.Input;
using System.Threading; using System.Threading;
using Microsoft.Scripting.Hosting.Shell; using ICSharpCode.Scripting;
using ICSharpCode.PythonBinding;
using NUnit.Framework; using NUnit.Framework;
using PythonBinding.Tests.Utils; using ICSharpCode.Scripting.Tests.Utils;
namespace PythonBinding.Tests.Console namespace ICSharpCode.Scripting.Tests.Console
{ {
/// <summary> /// <summary>
/// The Home Key should return the user to the start of the line after the prompt. /// The Home Key should return the user to the start of the line after the prompt.
/// </summary> /// </summary>
[TestFixture] [TestFixture]
public class PythonConsoleHomeKeyTestFixture : PythonConsoleTestsBase public class ScriptingConsoleHomeKeyTests : ScriptingConsoleTestsBase
{ {
string prompt = ">>> "; string prompt = ">>> ";
[SetUp] [SetUp]
public void Init() public void Init()
{ {
base.CreatePythonConsole(); base.CreateConsole();
TestablePythonConsole.Write(prompt, Style.Prompt); TestableScriptingConsole.Write(prompt, ScriptingStyle.Prompt);
} }
[Test] [Test]

35
src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/PythonConsoleReadOnlyRegionsTestFixture.cs → src/AddIns/BackendBindings/Scripting/Test/Console/ScriptingConsoleReadOnlyRegionsTests.cs

@ -4,27 +4,26 @@
using System; using System;
using System.Windows.Input; using System.Windows.Input;
using ICSharpCode.PythonBinding; using ICSharpCode.Scripting;
using Microsoft.Scripting.Hosting.Shell; using ICSharpCode.Scripting.Tests.Utils;
using NUnit.Framework; using NUnit.Framework;
using PythonBinding.Tests.Utils;
namespace PythonBinding.Tests.Console namespace ICSharpCode.Scripting.Tests.Console
{ {
/// <summary> /// <summary>
/// Tests that the user cannot type into read-only regions of the text editor. The /// Tests that the user cannot type into read-only regions of the text editor. The
/// PythonConsole itself restricts typing itself by handling key press events. /// ScriptingConsole itself restricts typing itself by handling key press events.
/// </summary> /// </summary>
[TestFixture] [TestFixture]
public class PythonConsoleReadOnlyRegionsTestFixture : PythonConsoleTestsBase public class ScriptingConsoleReadOnlyRegionsTests : ScriptingConsoleTestsBase
{ {
string prompt = ">>> "; string prompt = ">>> ";
[SetUp] [SetUp]
public void Init() public void Init()
{ {
base.CreatePythonConsole(); base.CreateConsole();
TestablePythonConsole.Write(prompt, Style.Prompt); TestableScriptingConsole.Write(prompt, ScriptingStyle.Prompt);
} }
[Test] [Test]
@ -41,7 +40,7 @@ namespace PythonBinding.Tests.Console
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Left); MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Left);
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.C); MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.C);
Assert.AreEqual("ACB", TestablePythonConsole.GetCurrentLine()); Assert.AreEqual("ACB", TestableScriptingConsole.GetCurrentLine());
} }
[Test] [Test]
@ -50,7 +49,7 @@ namespace PythonBinding.Tests.Console
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Left); MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Left);
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.A); MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.A);
Assert.AreEqual(String.Empty, TestablePythonConsole.GetCurrentLine()); Assert.AreEqual(String.Empty, TestableScriptingConsole.GetCurrentLine());
} }
[Test] [Test]
@ -60,7 +59,7 @@ namespace PythonBinding.Tests.Console
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Left); MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Left);
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Back); MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Back);
Assert.AreEqual("A", TestablePythonConsole.GetCurrentLine()); Assert.AreEqual("A", TestableScriptingConsole.GetCurrentLine());
Assert.AreEqual(prompt + "A", MockConsoleTextEditor.Text); Assert.AreEqual(prompt + "A", MockConsoleTextEditor.Text);
} }
@ -72,7 +71,7 @@ namespace PythonBinding.Tests.Console
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Left); MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Left);
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Back); MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Back);
Assert.AreEqual("A", TestablePythonConsole.GetCurrentLine()); Assert.AreEqual("A", TestableScriptingConsole.GetCurrentLine());
Assert.AreEqual(prompt + "A", MockConsoleTextEditor.Text); Assert.AreEqual(prompt + "A", MockConsoleTextEditor.Text);
} }
@ -85,7 +84,7 @@ namespace PythonBinding.Tests.Console
MockConsoleTextEditor.Column += 2; MockConsoleTextEditor.Column += 2;
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Back); MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Back);
Assert.AreEqual("A", TestablePythonConsole.GetCurrentLine()); Assert.AreEqual("A", TestableScriptingConsole.GetCurrentLine());
Assert.AreEqual(prompt + "A", MockConsoleTextEditor.Text); Assert.AreEqual(prompt + "A", MockConsoleTextEditor.Text);
} }
@ -107,7 +106,7 @@ namespace PythonBinding.Tests.Console
public void CanMoveOutOfPromptRegionWithUpCursorKey() public void CanMoveOutOfPromptRegionWithUpCursorKey()
{ {
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter); MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter);
TestablePythonConsole.Write(prompt, Style.Prompt); TestableScriptingConsole.Write(prompt, ScriptingStyle.Prompt);
MockConsoleTextEditor.Column = 0; MockConsoleTextEditor.Column = 0;
Assert.IsFalse(MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Up)); Assert.IsFalse(MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Up));
} }
@ -116,7 +115,7 @@ namespace PythonBinding.Tests.Console
public void CanMoveInReadOnlyRegionWithDownCursorKey() public void CanMoveInReadOnlyRegionWithDownCursorKey()
{ {
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter); MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter);
TestablePythonConsole.Write(prompt, Style.Prompt); TestableScriptingConsole.Write(prompt, ScriptingStyle.Prompt);
MockConsoleTextEditor.Column = 0; MockConsoleTextEditor.Column = 0;
MockConsoleTextEditor.Line = 0; MockConsoleTextEditor.Line = 0;
Assert.IsFalse(MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Down)); Assert.IsFalse(MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Down));
@ -135,7 +134,7 @@ namespace PythonBinding.Tests.Console
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.B); MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.B);
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter); MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter);
TestablePythonConsole.Write(prompt, Style.Prompt); TestableScriptingConsole.Write(prompt, ScriptingStyle.Prompt);
MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.C); MockConsoleTextEditor.RaisePreviewKeyDownEvent(Key.C);
@ -143,7 +142,7 @@ namespace PythonBinding.Tests.Console
MockConsoleTextEditor.Line = 0; MockConsoleTextEditor.Line = 0;
Assert.IsTrue(MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Back)); Assert.IsTrue(MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Back));
Assert.AreEqual("C", TestablePythonConsole.GetCurrentLine()); Assert.AreEqual("C", TestableScriptingConsole.GetCurrentLine());
} }
[Test] [Test]
@ -154,7 +153,7 @@ namespace PythonBinding.Tests.Console
MockConsoleTextEditor.SelectionStart = 5; MockConsoleTextEditor.SelectionStart = 5;
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Back); MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(Key.Back);
Assert.AreEqual(String.Empty, TestablePythonConsole.GetCurrentLine()); Assert.AreEqual(String.Empty, TestableScriptingConsole.GetCurrentLine());
} }
} }
} }

17
src/AddIns/BackendBindings/Ruby/RubyBinding/Test/Console/RubyConsoleReadTestFixture.cs → src/AddIns/BackendBindings/Scripting/Test/Console/ScriptingConsoleReadTests.cs

@ -5,18 +5,17 @@ using System;
using System.Threading; using System.Threading;
using System.Windows.Input; using System.Windows.Input;
using ICSharpCode.RubyBinding; using ICSharpCode.Scripting;
using ICSharpCode.Scripting.Tests.Utils; using ICSharpCode.Scripting.Tests.Utils;
using NUnit.Framework; using NUnit.Framework;
using RubyBinding.Tests.Utils;
namespace RubyBinding.Tests.Console namespace ICSharpCode.Scripting.Tests.Console
{ {
/// <summary> /// <summary>
/// Tests the RubyConsole ReadLine method. /// Tests the ScriptingConsole ReadLine method.
/// </summary> /// </summary>
[TestFixture] [TestFixture]
public class RubyConsoleReadTestFixture : RubyConsoleTestsBase public class ScriptingConsoleReadTests : ScriptingConsoleTestsBase
{ {
int initialAutoIndentSize = 4; int initialAutoIndentSize = 4;
string readLine; string readLine;
@ -27,7 +26,7 @@ namespace RubyBinding.Tests.Console
[TestFixtureSetUp] [TestFixtureSetUp]
public void Init() public void Init()
{ {
base.CreateRubyConsole(); base.CreateConsole();
autoIndentSize = initialAutoIndentSize; autoIndentSize = initialAutoIndentSize;
Thread thread = new Thread(ReadLineFromConsoleOnDifferentThread); Thread thread = new Thread(ReadLineFromConsoleOnDifferentThread);
@ -55,7 +54,7 @@ namespace RubyBinding.Tests.Console
[TestFixtureTearDown] [TestFixtureTearDown]
public void TearDown() public void TearDown()
{ {
TestableRubyConsole.Dispose(); TestableScriptingConsole.Dispose();
} }
[Test] [Test]
@ -81,7 +80,7 @@ namespace RubyBinding.Tests.Console
[Test] [Test]
public void NoTextWrittenWhenAutoIndentSizeIsZero() public void NoTextWrittenWhenAutoIndentSizeIsZero()
{ {
TestableRubyConsole console = new TestableRubyConsole(); TestableScriptingConsole console = new TestableScriptingConsole();
MockConsoleTextEditor textEditor = console.MockConsoleTextEditor; MockConsoleTextEditor textEditor = console.MockConsoleTextEditor;
textEditor.RaisePreviewKeyDownEvent(Key.A); textEditor.RaisePreviewKeyDownEvent(Key.A);
textEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter); textEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter);
@ -112,7 +111,7 @@ namespace RubyBinding.Tests.Console
void ReadLineFromConsoleOnDifferentThread() void ReadLineFromConsoleOnDifferentThread()
{ {
System.Console.WriteLine("Reading on different thread"); System.Console.WriteLine("Reading on different thread");
readLine = TestableRubyConsole.ReadLine(autoIndentSize); readLine = TestableScriptingConsole.ReadLine(autoIndentSize);
System.Console.WriteLine("Finished reading on different thread"); System.Console.WriteLine("Finished reading on different thread");
} }
} }

53
src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/PythonConsoleSendLineTests.cs → src/AddIns/BackendBindings/Scripting/Test/Console/ScriptingConsoleSendLineTests.cs

@ -4,21 +4,20 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using ICSharpCode.NRefactory; using ICSharpCode.NRefactory;
using ICSharpCode.PythonBinding; using ICSharpCode.Scripting;
using Microsoft.Scripting.Hosting.Shell; using ICSharpCode.Scripting.Tests.Utils;
using NUnit.Framework; using NUnit.Framework;
using PythonBinding.Tests.Utils;
namespace PythonBinding.Tests.Console namespace ICSharpCode.Scripting.Tests.Console
{ {
[TestFixture] [TestFixture]
public class PythonConsoleSendLineTests : PythonConsoleTestsBase public class ScriptingConsoleSendLineTests : ScriptingConsoleTestsBase
{ {
[Test] [Test]
public void SendLine_NoUnreadLines_AddsLineToUnreadLines() public void SendLine_NoUnreadLines_AddsLineToUnreadLines()
{ {
SendLineToConsole("test"); SendLineToConsole("test");
string[] unreadLines = TestablePythonConsole.GetUnreadLines(); string[] unreadLines = TestableScriptingConsole.GetUnreadLines();
string[] expectedUnreadlines = new string[] {"test"}; string[] expectedUnreadlines = new string[] {"test"};
@ -27,22 +26,22 @@ namespace PythonBinding.Tests.Console
void SendLineToConsole(string text) void SendLineToConsole(string text)
{ {
base.CreatePythonConsole(); base.CreateConsole();
WritePrompt(); WritePrompt();
TestablePythonConsole.SendLine(text); TestableScriptingConsole.SendLine(text);
} }
void WritePrompt() void WritePrompt()
{ {
TestablePythonConsole.Write(">>> ", Style.Prompt); TestableScriptingConsole.Write(">>> ", ScriptingStyle.Prompt);
} }
[Test] [Test]
public void SendLine_NoUnreadLines_CreatesLockForPreviousLines() public void SendLine_NoUnreadLines_CreatesLockForPreviousLines()
{ {
SendLineToConsole("test"); SendLineToConsole("test");
List<string> lines = TestablePythonConsole.LockCreated.Lines; List<string> lines = TestableScriptingConsole.LockCreated.Lines;
List<string> expectedLines = TestablePythonConsole.GetUnreadLinesList(); List<string> expectedLines = TestableScriptingConsole.GetUnreadLinesList();
Assert.AreEqual(expectedLines, lines); Assert.AreEqual(expectedLines, lines);
} }
@ -51,7 +50,7 @@ namespace PythonBinding.Tests.Console
public void SendLine_NoUnreadLines_LockForPreviousLinesIsDisposed() public void SendLine_NoUnreadLines_LockForPreviousLinesIsDisposed()
{ {
SendLineToConsole("test"); SendLineToConsole("test");
bool disposed = TestablePythonConsole.LockCreated.IsDisposed; bool disposed = TestableScriptingConsole.LockCreated.IsDisposed;
Assert.IsTrue(disposed); Assert.IsTrue(disposed);
} }
@ -60,7 +59,7 @@ namespace PythonBinding.Tests.Console
public void SendLine_NoUnreadLines_LineNotAddedBeforeLockCreated() public void SendLine_NoUnreadLines_LineNotAddedBeforeLockCreated()
{ {
SendLineToConsole("test"); SendLineToConsole("test");
int count = TestablePythonConsole.LockCreated.UnreadLineCountWhenLockCreated; int count = TestableScriptingConsole.LockCreated.UnreadLineCountWhenLockCreated;
int expectedCount = 0; int expectedCount = 0;
Assert.AreEqual(expectedCount, count); Assert.AreEqual(expectedCount, count);
@ -70,7 +69,7 @@ namespace PythonBinding.Tests.Console
public void SendLine_NoUnreadLines_LineAddedBeforeLockDisposed() public void SendLine_NoUnreadLines_LineAddedBeforeLockDisposed()
{ {
SendLineToConsole("test"); SendLineToConsole("test");
int count = TestablePythonConsole.LockCreated.UnreadLineCountWhenLockDisposed; int count = TestableScriptingConsole.LockCreated.UnreadLineCountWhenLockDisposed;
int expectedCount = 1; int expectedCount = 1;
Assert.AreEqual(expectedCount, count); Assert.AreEqual(expectedCount, count);
@ -80,7 +79,7 @@ namespace PythonBinding.Tests.Console
public void SendLine_NoUnreadLines_LineReceivedEventIsFired() public void SendLine_NoUnreadLines_LineReceivedEventIsFired()
{ {
SendLineToConsole("test"); SendLineToConsole("test");
bool fired = TestablePythonConsole.IsLineReceivedEventFired; bool fired = TestableScriptingConsole.IsLineReceivedEventFired;
Assert.IsTrue(fired); Assert.IsTrue(fired);
} }
@ -88,7 +87,7 @@ namespace PythonBinding.Tests.Console
public void SendLine_NoUnreadLines_LineReceivedEventAfterLineAddedToUnreadLines() public void SendLine_NoUnreadLines_LineReceivedEventAfterLineAddedToUnreadLines()
{ {
SendLineToConsole("test"); SendLineToConsole("test");
int count = TestablePythonConsole.UnreadLineCountWhenLineReceivedEventFired; int count = TestableScriptingConsole.UnreadLineCountWhenLineReceivedEventFired;
int expectedCount = 1; int expectedCount = 1;
Assert.AreEqual(expectedCount, count); Assert.AreEqual(expectedCount, count);
} }
@ -106,7 +105,7 @@ namespace PythonBinding.Tests.Console
[Test] [Test]
public void SendLine_TwoLinesInConsoleTextEditorCursorOnFirstLine_CursorMovedToEndOfLastLineBeforeTextWritten() public void SendLine_TwoLinesInConsoleTextEditorCursorOnFirstLine_CursorMovedToEndOfLastLineBeforeTextWritten()
{ {
base.CreatePythonConsole(); base.CreateConsole();
WritePrompt(); WritePrompt();
MockConsoleTextEditor.Text = MockConsoleTextEditor.Text =
">>> first\r\n" + ">>> first\r\n" +
@ -115,7 +114,7 @@ namespace PythonBinding.Tests.Console
MockConsoleTextEditor.Line = 0; MockConsoleTextEditor.Line = 0;
MockConsoleTextEditor.Column = 0; MockConsoleTextEditor.Column = 0;
TestablePythonConsole.SendLine("test"); TestableScriptingConsole.SendLine("test");
int expectedLine = 2; int expectedLine = 2;
int expectedColumn = 4; int expectedColumn = 4;
@ -129,8 +128,8 @@ namespace PythonBinding.Tests.Console
[Test] [Test]
public void SendLine_NoUnreadLines_NoTextWrittenToConsoleTextEditorBeforeFirstPromptIsWritten() public void SendLine_NoUnreadLines_NoTextWrittenToConsoleTextEditorBeforeFirstPromptIsWritten()
{ {
base.CreatePythonConsole(); base.CreateConsole();
TestablePythonConsole.SendLine("test"); TestableScriptingConsole.SendLine("test");
string text = MockConsoleTextEditor.TextPassedToWrite; string text = MockConsoleTextEditor.TextPassedToWrite;
Assert.IsNull(text); Assert.IsNull(text);
@ -139,10 +138,10 @@ namespace PythonBinding.Tests.Console
[Test] [Test]
public void Write_SendLineCalledButNoPromptWritten_WritesOutSavedSendLineText() public void Write_SendLineCalledButNoPromptWritten_WritesOutSavedSendLineText()
{ {
base.CreatePythonConsole(); base.CreateConsole();
TestablePythonConsole.SendLine("test"); TestableScriptingConsole.SendLine("test");
TestablePythonConsole.Write(">>> ", Style.Prompt); TestableScriptingConsole.Write(">>> ", ScriptingStyle.Prompt);
string text = MockConsoleTextEditor.Text; string text = MockConsoleTextEditor.Text;
string expectedText = string expectedText =
@ -153,11 +152,11 @@ namespace PythonBinding.Tests.Console
[Test] [Test]
public void Write_CalledTwiceAfterSendLineCalledButNoPromptWritten_WritesOutSavedSendLineTextOnlyOnce() public void Write_CalledTwiceAfterSendLineCalledButNoPromptWritten_WritesOutSavedSendLineTextOnlyOnce()
{ {
base.CreatePythonConsole(); base.CreateConsole();
TestablePythonConsole.SendLine("test"); TestableScriptingConsole.SendLine("test");
TestablePythonConsole.Write(">>> ", Style.Prompt); TestableScriptingConsole.Write(">>> ", ScriptingStyle.Prompt);
TestablePythonConsole.Write(">>> ", Style.Prompt); TestableScriptingConsole.Write(">>> ", ScriptingStyle.Prompt);
string text = MockConsoleTextEditor.Text; string text = MockConsoleTextEditor.Text;

21
src/AddIns/BackendBindings/Scripting/Test/Console/ScriptingConsoleTestsBase.cs

@ -0,0 +1,21 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using ICSharpCode.Scripting;
using ICSharpCode.Scripting.Tests.Utils;
namespace ICSharpCode.Scripting.Tests.Console
{
public class ScriptingConsoleTestsBase
{
public MockConsoleTextEditor MockConsoleTextEditor;
public TestableScriptingConsole TestableScriptingConsole;
public void CreateConsole()
{
TestableScriptingConsole = new TestableScriptingConsole();
MockConsoleTextEditor = TestableScriptingConsole.MockConsoleTextEditor;
}
}
}

26
src/AddIns/BackendBindings/Ruby/RubyBinding/Test/Console/RubyConsoleUnreadLinesTestFixture.cs → src/AddIns/BackendBindings/Scripting/Test/Console/ScriptingConsoleUnreadLinesTests.cs

@ -4,39 +4,35 @@
using System; using System;
using System.Windows.Input; using System.Windows.Input;
using ICSharpCode.RubyBinding; using ICSharpCode.Scripting;
using IronRuby.Hosting; using ICSharpCode.Scripting.Tests.Utils;
using IronRuby.Runtime;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using Microsoft.Scripting.Hosting.Shell;
using NUnit.Framework; using NUnit.Framework;
using RubyBinding.Tests.Utils;
namespace RubyBinding.Tests.Console namespace ICSharpCode.Scripting.Tests.Console
{ {
/// <summary> /// <summary>
/// Tests the RubyConsole's GetUnreadLines method. /// Tests the ScriptingConsole's GetUnreadLines method.
/// </summary> /// </summary>
[TestFixture] [TestFixture]
public class RubyConsoleUnreadLinesTestFixture : RubyConsoleTestsBase public class ScriptingConsoleUnreadLinesTests : ScriptingConsoleTestsBase
{ {
[SetUp] [SetUp]
public void Init() public void Init()
{ {
base.CreateRubyConsole(); base.CreateConsole();
} }
[Test] [Test]
public void NoUnreadLinesAtStart() public void NoUnreadLinesAtStart()
{ {
Assert.AreEqual(0, TestableRubyConsole.GetUnreadLines().Length); int length = TestableScriptingConsole.GetUnreadLines().Length;
Assert.AreEqual(0, length);
} }
[Test] [Test]
public void HasUnreadLines() public void HasUnreadLines()
{ {
Assert.IsFalse(TestableRubyConsole.IsLineAvailable); Assert.IsFalse(TestableScriptingConsole.IsLineAvailable);
} }
[Test] [Test]
@ -45,11 +41,11 @@ namespace RubyBinding.Tests.Console
MockConsoleTextEditor.RaisePreviewKeyDownEvent(System.Windows.Input.Key.A); MockConsoleTextEditor.RaisePreviewKeyDownEvent(System.Windows.Input.Key.A);
MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(System.Windows.Input.Key.Enter); MockConsoleTextEditor.RaisePreviewKeyDownEventForDialogKey(System.Windows.Input.Key.Enter);
string[] lines = TestableRubyConsole.GetUnreadLines(); string[] lines = TestableScriptingConsole.GetUnreadLines();
string[] expectedLines = new string[] {"A"}; string[] expectedLines = new string[] {"A"};
Assert.AreEqual(expectedLines, lines); Assert.AreEqual(expectedLines, lines);
Assert.IsTrue(TestableRubyConsole.IsLineAvailable); Assert.IsTrue(TestableScriptingConsole.IsLineAvailable);
} }
} }
} }

26
src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/PythonConsoleWriteTestFixture.cs → src/AddIns/BackendBindings/Scripting/Test/Console/ScriptingConsoleWriteTests.cs

@ -3,52 +3,52 @@
using System; using System;
using System.Drawing; using System.Drawing;
using ICSharpCode.PythonBinding; using ICSharpCode.Scripting;
using Microsoft.Scripting.Hosting.Shell; using ICSharpCode.Scripting.Tests.Utils;
using NUnit.Framework; using NUnit.Framework;
using PythonBinding.Tests.Utils;
namespace PythonBinding.Tests.Console namespace ICSharpCode.Scripting.Tests.Console
{ {
/// <summary> /// <summary>
/// Tests that the PythonConsole Write method correctly update the text editor. /// Tests that the ScriptingConsole Write method correctly update the text editor.
/// </summary> /// </summary>
[TestFixture] [TestFixture]
public class PythonConsoleWriteTestFixture : PythonConsoleTestsBase public class ScriptingConsoleWriteTestFixture : ScriptingConsoleTestsBase
{ {
[SetUp] [SetUp]
public void Init() public void Init()
{ {
base.CreatePythonConsole(); base.CreateConsole();
MockConsoleTextEditor.Text = String.Empty; MockConsoleTextEditor.Text = String.Empty;
} }
[Test] [Test]
public void WriteLine() public void WriteLine()
{ {
TestablePythonConsole.WriteLine(); TestableScriptingConsole.WriteLine();
Assert.AreEqual(Environment.NewLine, MockConsoleTextEditor.Text); Assert.AreEqual(Environment.NewLine, MockConsoleTextEditor.Text);
} }
[Test] [Test]
public void WriteLineWithText() public void WriteLineWithText()
{ {
TestablePythonConsole.WriteLine("test", Style.Out); TestableScriptingConsole.WriteLine("test", ScriptingStyle.Out);
Assert.AreEqual("test" + Environment.NewLine, MockConsoleTextEditor.Text); string expectedText = "test" + Environment.NewLine;
Assert.AreEqual(expectedText, MockConsoleTextEditor.Text);
} }
[Test] [Test]
public void TwoWrites() public void TwoWrites()
{ {
TestablePythonConsole.Write("a", Style.Out); TestableScriptingConsole.Write("a", ScriptingStyle.Out);
TestablePythonConsole.Write("b", Style.Out); TestableScriptingConsole.Write("b", ScriptingStyle.Out);
Assert.AreEqual("ab", MockConsoleTextEditor.Text); Assert.AreEqual("ab", MockConsoleTextEditor.Text);
} }
[Test] [Test]
public void DoesNotHasLinesWaitingToBeRead() public void DoesNotHasLinesWaitingToBeRead()
{ {
Assert.IsFalse(TestablePythonConsole.IsLineAvailable); Assert.IsFalse(TestableScriptingConsole.IsLineAvailable);
} }
} }
} }

31
src/AddIns/BackendBindings/Ruby/RubyBinding/Test/Console/TwoRubyConsoleLinesWaitingTestFixture.cs → src/AddIns/BackendBindings/Scripting/Test/Console/TwoConsoleLinesWaitingTests.cs

@ -3,29 +3,22 @@
using System; using System;
using System.Threading; using System.Threading;
using ICSharpCode.RubyBinding;
using ICSharpCode.Scripting.Tests.Utils; using ICSharpCode.Scripting.Tests.Utils;
using IronRuby.Hosting;
using IronRuby.Runtime;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using Microsoft.Scripting.Hosting.Shell;
using NUnit.Framework; using NUnit.Framework;
using RubyBinding.Tests.Utils;
using Input = System.Windows.Input; using Input = System.Windows.Input;
namespace RubyBinding.Tests.Console namespace ICSharpCode.Scripting.Tests.Console
{ {
/// <summary> /// <summary>
/// Ensures that both lines of text can be read from the Ruby console if they are written /// Ensures that both lines of text can be read from the console if they are written
/// before ReadLine is called. /// before ReadLine is called.
/// </summary> /// </summary>
[TestFixture] [TestFixture]
public class TwoRubyConsoleLinesWaitingTestFixture public class TwoConsoleLinesWaitingTests
{ {
string line1; string line1;
string line2; string line2;
TestableRubyConsole rubyConsole; TestableScriptingConsole scriptingConsole;
bool lineAvailableBeforeFirstEnterKey; bool lineAvailableBeforeFirstEnterKey;
bool lineAvailableAfterFirstEnterKey; bool lineAvailableAfterFirstEnterKey;
bool lineAvailableAtEnd; bool lineAvailableAtEnd;
@ -33,15 +26,15 @@ namespace RubyBinding.Tests.Console
[TestFixtureSetUp] [TestFixtureSetUp]
public void SetUpFixture() public void SetUpFixture()
{ {
using (rubyConsole = new TestableRubyConsole()) { using (scriptingConsole = new TestableScriptingConsole()) {
MockConsoleTextEditor textEditor = rubyConsole.MockConsoleTextEditor; MockConsoleTextEditor textEditor = scriptingConsole.MockConsoleTextEditor;
textEditor.RaisePreviewKeyDownEvent(Input.Key.A); textEditor.RaisePreviewKeyDownEvent(Input.Key.A);
textEditor.RaisePreviewKeyDownEvent(Input.Key.B); textEditor.RaisePreviewKeyDownEvent(Input.Key.B);
textEditor.RaisePreviewKeyDownEvent(Input.Key.C); textEditor.RaisePreviewKeyDownEvent(Input.Key.C);
lineAvailableBeforeFirstEnterKey = rubyConsole.IsLineAvailable; lineAvailableBeforeFirstEnterKey = scriptingConsole.IsLineAvailable;
textEditor.RaisePreviewKeyDownEventForDialogKey(Input.Key.Enter); textEditor.RaisePreviewKeyDownEventForDialogKey(Input.Key.Enter);
lineAvailableAfterFirstEnterKey = rubyConsole.IsLineAvailable; lineAvailableAfterFirstEnterKey = scriptingConsole.IsLineAvailable;
textEditor.RaisePreviewKeyDownEvent(Input.Key.D); textEditor.RaisePreviewKeyDownEvent(Input.Key.D);
textEditor.RaisePreviewKeyDownEvent(Input.Key.E); textEditor.RaisePreviewKeyDownEvent(Input.Key.E);
textEditor.RaisePreviewKeyDownEvent(Input.Key.F); textEditor.RaisePreviewKeyDownEvent(Input.Key.F);
@ -59,7 +52,7 @@ namespace RubyBinding.Tests.Console
currentWait += sleepInterval; currentWait += sleepInterval;
} }
lineAvailableAtEnd = rubyConsole.IsLineAvailable; lineAvailableAtEnd = scriptingConsole.IsLineAvailable;
} }
} }
@ -95,8 +88,8 @@ namespace RubyBinding.Tests.Console
void ReadLinesOnSeparateThread() void ReadLinesOnSeparateThread()
{ {
line1 = rubyConsole.ReadLine(0); line1 = scriptingConsole.ReadLine(0);
line2 = rubyConsole.ReadLine(0); line2 = scriptingConsole.ReadLine(0);
} }
} }
} }

15
src/AddIns/BackendBindings/Scripting/Test/ICSharpCode.Scripting.Tests.csproj

@ -69,13 +69,27 @@
<ItemGroup> <ItemGroup>
<Compile Include="Configuration\AssemblyInfo.cs" /> <Compile Include="Configuration\AssemblyInfo.cs" />
<Compile Include="Console\CommandLineHistoryTestFixture.cs" /> <Compile Include="Console\CommandLineHistoryTestFixture.cs" />
<Compile Include="Console\DisposedScriptingConsoleTests.cs" />
<Compile Include="Console\EmptyCommandLineHistoryTestFixture.cs" /> <Compile Include="Console\EmptyCommandLineHistoryTestFixture.cs" />
<Compile Include="Console\EmptyStringCodeCompletionTests.cs" /> <Compile Include="Console\EmptyStringCodeCompletionTests.cs" />
<Compile Include="Console\InsertConsoleCompletionDataTestFixture.cs" /> <Compile Include="Console\InsertConsoleCompletionDataTestFixture.cs" />
<Compile Include="Console\KeysPressedWhenCompletionWindowOpenTests.cs" />
<Compile Include="Console\OneItemCommandLineHistoryTestFixture.cs" /> <Compile Include="Console\OneItemCommandLineHistoryTestFixture.cs" />
<Compile Include="Console\ScriptingConsoleCodeCompletionTests.cs" />
<Compile Include="Console\ScriptingConsoleCommandLineHistoryTests.cs" />
<Compile Include="Console\ScriptingConsoleCurrentLineTests.cs" />
<Compile Include="Console\ScriptingConsoleEnterKeyTests.cs" />
<Compile Include="Console\ScriptingConsoleHomeKeyTests.cs" />
<Compile Include="Console\ScriptingConsoleOutputStreamTests.cs" /> <Compile Include="Console\ScriptingConsoleOutputStreamTests.cs" />
<Compile Include="Console\ScriptingConsoleReadOnlyRegionsTests.cs" />
<Compile Include="Console\ScriptingConsoleReadTests.cs" />
<Compile Include="Console\ScriptingConsoleSendLineTests.cs" />
<Compile Include="Console\ScriptingConsoleTestsBase.cs" />
<Compile Include="Console\ScriptingConsoleTextEditorTests.cs" /> <Compile Include="Console\ScriptingConsoleTextEditorTests.cs" />
<Compile Include="Console\ScriptingConsoleUnreadLinesTests.cs" />
<Compile Include="Console\ScriptingConsoleWriteTests.cs" />
<Compile Include="Console\ThreadSafeScriptingConsoleTextEditorTests.cs" /> <Compile Include="Console\ThreadSafeScriptingConsoleTextEditorTests.cs" />
<Compile Include="Console\TwoConsoleLinesWaitingTests.cs" />
<Compile Include="Designer\CallBeginInitOnLoadTests.cs" /> <Compile Include="Designer\CallBeginInitOnLoadTests.cs" />
<Compile Include="Designer\GenerateAcceptButtonFormTestsBase.cs" /> <Compile Include="Designer\GenerateAcceptButtonFormTestsBase.cs" />
<Compile Include="Designer\GenerateDesignerCodeTestsBase.cs" /> <Compile Include="Designer\GenerateDesignerCodeTestsBase.cs" />
@ -141,6 +155,7 @@
<Compile Include="Utils\MSBuildEngineHelper.cs" /> <Compile Include="Utils\MSBuildEngineHelper.cs" />
<Compile Include="Utils\NullPropertyUserControl.cs" /> <Compile Include="Utils\NullPropertyUserControl.cs" />
<Compile Include="Utils\SupportInitCustomControl.cs" /> <Compile Include="Utils\SupportInitCustomControl.cs" />
<Compile Include="Utils\TestableScriptingConsole.cs" />
<Compile Include="Utils\TestableScriptingDesignerGenerator.cs" /> <Compile Include="Utils\TestableScriptingDesignerGenerator.cs" />
<Compile Include="Utils\TestableScriptingDesignerLoader.cs" /> <Compile Include="Utils\TestableScriptingDesignerLoader.cs" />
<Compile Include="Utils\Tests\AddInPathHelperTests.cs" /> <Compile Include="Utils\Tests\AddInPathHelperTests.cs" />

51
src/AddIns/BackendBindings/Scripting/Test/Utils/TestableScriptingConsole.cs

@ -0,0 +1,51 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using ICSharpCode.Scripting;
using ICSharpCode.Scripting.Tests.Utils;
namespace ICSharpCode.Scripting.Tests.Utils
{
public class TestableScriptingConsole : ScriptingConsole
{
public MockConsoleTextEditor MockConsoleTextEditor;
public FakeLock LockCreated;
public bool IsLineReceivedEventFired;
public int UnreadLineCountWhenLineReceivedEventFired = -1;
public TestableScriptingConsole()
: this(new MockConsoleTextEditor())
{
}
TestableScriptingConsole(IScriptingConsoleTextEditor consoleTextEditor)
: base(consoleTextEditor)
{
MockConsoleTextEditor = (MockConsoleTextEditor)consoleTextEditor;
}
public List<string> GetUnreadLinesList()
{
return base.unreadLines;
}
public string[] GetUnreadLines()
{
return base.unreadLines.ToArray();
}
protected override ILock CreateLock(List<string> lines)
{
LockCreated = new FakeLock(lines);
return LockCreated;
}
protected override void FireLineReceivedEvent()
{
IsLineReceivedEventFired = true;
UnreadLineCountWhenLineReceivedEventFired = LockCreated.Lines.Count;
}
}
}
Loading…
Cancel
Save