60 changed files with 185 additions and 995 deletions
@ -1,205 +0,0 @@ |
|||||||
// <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; |
|
||||||
using System.Collections.Generic; |
|
||||||
using System.Runtime.InteropServices; |
|
||||||
using System.Text; |
|
||||||
using System.Windows.Input; |
|
||||||
|
|
||||||
using ICSharpCode.NRefactory; |
|
||||||
using ICSharpCode.PythonBinding; |
|
||||||
using ICSharpCode.Scripting; |
|
||||||
using ICSharpCode.Scripting.Tests.Utils; |
|
||||||
|
|
||||||
namespace PythonBinding.Tests.Utils |
|
||||||
{ |
|
||||||
public class MockConsoleTextEditor : IConsoleTextEditor |
|
||||||
{ |
|
||||||
public bool IsDisposed; |
|
||||||
public bool IsWriteCalled; |
|
||||||
|
|
||||||
public bool IsShowCompletionWindowCalled; |
|
||||||
public bool IsMakeCurrentContentReadOnlyCalled; |
|
||||||
public PythonConsoleCompletionDataProvider CompletionProviderPassedToShowCompletionWindow; |
|
||||||
public string TextPassedToWrite; |
|
||||||
public string TextPassedToReplace; |
|
||||||
public int LengthPassedToReplace = -1; |
|
||||||
public int IndexPassedToReplace = -1; |
|
||||||
public Location CursorLocationWhenWriteTextCalled; |
|
||||||
public bool IsColumnChangedBeforeTextWritten; |
|
||||||
|
|
||||||
public StringBuilder PreviousLines = new StringBuilder(); |
|
||||||
public StringBuilder LineBuilder = new StringBuilder(); |
|
||||||
|
|
||||||
public event ConsoleTextEditorKeyEventHandler PreviewKeyDown; |
|
||||||
|
|
||||||
public MockConsoleTextEditor() |
|
||||||
{ |
|
||||||
TotalLines = 1; |
|
||||||
} |
|
||||||
|
|
||||||
public void Dispose() |
|
||||||
{ |
|
||||||
IsDisposed = true; |
|
||||||
} |
|
||||||
|
|
||||||
public void Write(string text) |
|
||||||
{ |
|
||||||
TextPassedToWrite = text; |
|
||||||
CursorLocationWhenWriteTextCalled = new Location(Column, Line); |
|
||||||
IsWriteCalled = true; |
|
||||||
LineBuilder.Append(text); |
|
||||||
Column += text.Length; |
|
||||||
} |
|
||||||
|
|
||||||
public string Text { |
|
||||||
get { return PreviousLines.ToString() + LineBuilder.ToString(); } |
|
||||||
set { |
|
||||||
PreviousLines = new StringBuilder(); |
|
||||||
LineBuilder = new StringBuilder(); |
|
||||||
TotalLines = 1; |
|
||||||
foreach (char ch in value) { |
|
||||||
LineBuilder.Append(ch); |
|
||||||
if (ch == '\n') { |
|
||||||
TotalLines++; |
|
||||||
PreviousLines.Append(LineBuilder.ToString()); |
|
||||||
LineBuilder = new StringBuilder(); |
|
||||||
} |
|
||||||
} |
|
||||||
Column = LineBuilder.Length; |
|
||||||
SelectionStart = Column; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public bool RaisePreviewKeyDownEvent(Key key) |
|
||||||
{ |
|
||||||
MockConsoleTextEditorKeyEventArgs e = new MockConsoleTextEditorKeyEventArgs(key); |
|
||||||
OnPreviewKeyDown(e); |
|
||||||
if (!e.Handled) { |
|
||||||
KeyConverter converter = new KeyConverter(); |
|
||||||
string text = converter.ConvertToString(key); |
|
||||||
if (IsCursorAtEnd) { |
|
||||||
LineBuilder.Append(text); |
|
||||||
} else { |
|
||||||
LineBuilder.Insert(Column, text); |
|
||||||
} |
|
||||||
Column++; |
|
||||||
SelectionStart = Column; |
|
||||||
} |
|
||||||
return e.Handled; |
|
||||||
} |
|
||||||
|
|
||||||
void OnPreviewKeyDown(MockConsoleTextEditorKeyEventArgs e) |
|
||||||
{ |
|
||||||
if (PreviewKeyDown != null) { |
|
||||||
PreviewKeyDown(this, e); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public void RaisePreviewKeyDownEvent(MockConsoleTextEditorKeyEventArgs e) |
|
||||||
{ |
|
||||||
OnPreviewKeyDown(e); |
|
||||||
} |
|
||||||
|
|
||||||
public bool RaisePreviewKeyDownEventForDialogKey(Key key) |
|
||||||
{ |
|
||||||
MockConsoleTextEditorKeyEventArgs e = new MockConsoleTextEditorKeyEventArgs(key); |
|
||||||
OnPreviewKeyDown(e); |
|
||||||
if (!e.Handled) { |
|
||||||
switch (key) { |
|
||||||
case Key.Enter: { |
|
||||||
if (IsCursorAtEnd) { |
|
||||||
LineBuilder.Append(Environment.NewLine); |
|
||||||
PreviousLines.Append(LineBuilder.ToString()); |
|
||||||
LineBuilder = new StringBuilder(); |
|
||||||
Column = 0; |
|
||||||
SelectionStart = Column; |
|
||||||
} else { |
|
||||||
int length = LineBuilder.Length; |
|
||||||
string currentLine = LineBuilder.ToString(); |
|
||||||
PreviousLines.Append(currentLine.Substring(0, Column) + Environment.NewLine); |
|
||||||
LineBuilder = new StringBuilder(); |
|
||||||
LineBuilder.Append(currentLine.Substring(Column)); |
|
||||||
Column = length - Column; |
|
||||||
SelectionStart = Column; |
|
||||||
} |
|
||||||
TotalLines++; |
|
||||||
Line++; |
|
||||||
} |
|
||||||
break; |
|
||||||
case Key.Back: { |
|
||||||
OnBackspaceKeyPressed(); |
|
||||||
} |
|
||||||
break; |
|
||||||
case Key.Left: { |
|
||||||
Column--; |
|
||||||
SelectionStart = Column; |
|
||||||
} |
|
||||||
break; |
|
||||||
case Key.Right: { |
|
||||||
Column++; |
|
||||||
SelectionStart = Column; |
|
||||||
} |
|
||||||
break; |
|
||||||
} |
|
||||||
} |
|
||||||
return e.Handled; |
|
||||||
} |
|
||||||
|
|
||||||
public bool IsCompletionWindowDisplayed { get; set; } |
|
||||||
public int Column { get; set; } |
|
||||||
public int SelectionStart { get; set; } |
|
||||||
public int SelectionLength { get; set; } |
|
||||||
public int Line { get; set; } |
|
||||||
public int TotalLines { get; set; } |
|
||||||
|
|
||||||
public string GetLine(int index) |
|
||||||
{ |
|
||||||
if (index == TotalLines - 1) { |
|
||||||
return LineBuilder.ToString(); |
|
||||||
} |
|
||||||
return "aaaa"; |
|
||||||
} |
|
||||||
|
|
||||||
public void Replace(int index, int length, string text) |
|
||||||
{ |
|
||||||
TextPassedToReplace = text; |
|
||||||
IndexPassedToReplace = index; |
|
||||||
LengthPassedToReplace = length; |
|
||||||
|
|
||||||
LineBuilder.Remove(index, length); |
|
||||||
LineBuilder.Insert(index, text); |
|
||||||
} |
|
||||||
|
|
||||||
public void ShowCompletionWindow(PythonConsoleCompletionDataProvider completionDataProvider) |
|
||||||
{ |
|
||||||
IsShowCompletionWindowCalled = true; |
|
||||||
IsCompletionWindowDisplayed = true; |
|
||||||
this.CompletionProviderPassedToShowCompletionWindow = completionDataProvider; |
|
||||||
} |
|
||||||
|
|
||||||
public void MakeCurrentContentReadOnly() |
|
||||||
{ |
|
||||||
IsMakeCurrentContentReadOnlyCalled = true; |
|
||||||
} |
|
||||||
|
|
||||||
bool IsCursorAtEnd { |
|
||||||
get { return Column == LineBuilder.ToString().Length; } |
|
||||||
} |
|
||||||
|
|
||||||
void OnBackspaceKeyPressed() |
|
||||||
{ |
|
||||||
if (SelectionLength == 0) { |
|
||||||
// Remove a single character to the left of the cursor position.
|
|
||||||
LineBuilder.Remove(Column - 1, 1); |
|
||||||
} else { |
|
||||||
LineBuilder.Remove(SelectionStart, SelectionLength); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -1,84 +0,0 @@ |
|||||||
// <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; |
|
||||||
using ICSharpCode.Scripting; |
|
||||||
|
|
||||||
namespace ICSharpCode.RubyBinding |
|
||||||
{ |
|
||||||
/// <summary>
|
|
||||||
/// The interface that the text editor used by the RubyConsole needs to be implement. Note that
|
|
||||||
/// all the methods will be called on another thread not the main UI thread and will therefore need to
|
|
||||||
/// be invoked.
|
|
||||||
/// </summary>
|
|
||||||
public interface IConsoleTextEditor : IDisposable |
|
||||||
{ |
|
||||||
/// <summary>
|
|
||||||
/// Fired when a key is pressed but before any text has been added to the text editor.
|
|
||||||
/// </summary>
|
|
||||||
/// <remarks>
|
|
||||||
/// The handler should set the ConsoleTextEditorKeyEventArgs.Handled to true if the text editor should not
|
|
||||||
/// process the key and not insert any text.
|
|
||||||
/// </remarks>
|
|
||||||
event ConsoleTextEditorKeyEventHandler PreviewKeyDown; |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Inserts text at the current cursor location.
|
|
||||||
/// </summary>
|
|
||||||
void Write(string text); |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Replaces the text at the specified index on the current line with the specified text.
|
|
||||||
/// </summary>
|
|
||||||
void Replace(int index, int length, string text); |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the current column position of the cursor on the current line. This is zero based.
|
|
||||||
/// </summary>
|
|
||||||
int Column {get; set;} |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the length of the currently selected text.
|
|
||||||
/// </summary>
|
|
||||||
int SelectionLength {get;} |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the start position of the currently selected text.
|
|
||||||
/// </summary>
|
|
||||||
int SelectionStart {get;} |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the current line the cursor is on. This is zero based.
|
|
||||||
/// </summary>
|
|
||||||
int Line {get; set;} |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the total number of lines in the text editor.
|
|
||||||
/// </summary>
|
|
||||||
int TotalLines {get;} |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the text for the specified line.
|
|
||||||
/// </summary>
|
|
||||||
string GetLine(int index); |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Shows the code completion window.
|
|
||||||
/// </summary>
|
|
||||||
void ShowCompletionWindow(RubyConsoleCompletionDataProvider completionDataProvider); |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Indicates whether the completion window is currently being displayed.
|
|
||||||
/// </summary>
|
|
||||||
bool IsCompletionWindowDisplayed {get;} |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Makes the current text content read only. Text can be entered at the end.
|
|
||||||
/// </summary>
|
|
||||||
void MakeCurrentContentReadOnly(); |
|
||||||
} |
|
||||||
} |
|
@ -1,50 +0,0 @@ |
|||||||
// <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; |
|
||||||
using System.Windows.Media; |
|
||||||
using ICSharpCode.AvalonEdit.CodeCompletion; |
|
||||||
using ICSharpCode.AvalonEdit.Document; |
|
||||||
using ICSharpCode.AvalonEdit.Editing; |
|
||||||
|
|
||||||
namespace ICSharpCode.RubyBinding |
|
||||||
{ |
|
||||||
public class RubyConsoleCompletionData : ICompletionData |
|
||||||
{ |
|
||||||
string text = String.Empty; |
|
||||||
|
|
||||||
public RubyConsoleCompletionData(string text) |
|
||||||
{ |
|
||||||
this.text = text; |
|
||||||
} |
|
||||||
|
|
||||||
public ImageSource Image { |
|
||||||
get { return null; } |
|
||||||
} |
|
||||||
|
|
||||||
public string Text { |
|
||||||
get { return text; } |
|
||||||
} |
|
||||||
|
|
||||||
public object Content { |
|
||||||
get { return text; } |
|
||||||
} |
|
||||||
|
|
||||||
public object Description { |
|
||||||
get { return null; } |
|
||||||
} |
|
||||||
|
|
||||||
public double Priority { |
|
||||||
get { return 0; } |
|
||||||
} |
|
||||||
|
|
||||||
public void Complete(TextArea textArea, ISegment completionSegment, EventArgs insertionRequestEventArgs) |
|
||||||
{ |
|
||||||
textArea.Document.Replace(completionSegment, text); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -1,65 +0,0 @@ |
|||||||
// <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; |
|
||||||
using System.Collections.Generic; |
|
||||||
using ICSharpCode.AvalonEdit.CodeCompletion; |
|
||||||
using ICSharpCode.Core; |
|
||||||
using ICSharpCode.Scripting; |
|
||||||
using ICSharpCode.SharpDevelop.Editor; |
|
||||||
using ICSharpCode.SharpDevelop.Editor.CodeCompletion; |
|
||||||
|
|
||||||
namespace ICSharpCode.RubyBinding |
|
||||||
{ |
|
||||||
/// <summary>
|
|
||||||
/// Provides code completion for the Ruby Console window.
|
|
||||||
/// </summary>
|
|
||||||
public class RubyConsoleCompletionDataProvider |
|
||||||
{ |
|
||||||
IMemberProvider memberProvider; |
|
||||||
|
|
||||||
public RubyConsoleCompletionDataProvider(IMemberProvider memberProvider) |
|
||||||
{ |
|
||||||
this.memberProvider = memberProvider; |
|
||||||
//DefaultIndex = 0;
|
|
||||||
} |
|
||||||
|
|
||||||
public ICompletionData[] GenerateCompletionData(IConsoleTextEditor textEditor) |
|
||||||
{ |
|
||||||
string line = textEditor.GetLine(textEditor.TotalLines - 1); |
|
||||||
return GenerateCompletionData(line); |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Generates completion data for the specified text. The text should be everything before
|
|
||||||
/// the dot character that triggered the completion. The text can contain the command line prompt
|
|
||||||
/// '>>>' as this will be ignored.
|
|
||||||
/// </summary>
|
|
||||||
public ICompletionData[] GenerateCompletionData(string line) |
|
||||||
{ |
|
||||||
List<RubyConsoleCompletionData> items = new List<RubyConsoleCompletionData>(); |
|
||||||
|
|
||||||
string name = GetName(line); |
|
||||||
if (!String.IsNullOrEmpty(name)) { |
|
||||||
try { |
|
||||||
foreach (string member in memberProvider.GetMemberNames(name)) { |
|
||||||
items.Add(new RubyConsoleCompletionData(member)); |
|
||||||
} |
|
||||||
} catch { |
|
||||||
// Do nothing.
|
|
||||||
} |
|
||||||
} |
|
||||||
return items.ToArray(); |
|
||||||
} |
|
||||||
|
|
||||||
string GetName(string text) |
|
||||||
{ |
|
||||||
int startIndex = text.LastIndexOf(' '); |
|
||||||
return text.Substring(startIndex + 1); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -1,66 +0,0 @@ |
|||||||
// <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; |
|
||||||
|
|
||||||
using ICSharpCode.AvalonEdit.CodeCompletion; |
|
||||||
using ICSharpCode.RubyBinding; |
|
||||||
using ICSharpCode.Scripting.Tests.Console; |
|
||||||
using ICSharpCode.SharpDevelop; |
|
||||||
using NUnit.Framework; |
|
||||||
|
|
||||||
namespace RubyBinding.Tests.Console |
|
||||||
{ |
|
||||||
[TestFixture] |
|
||||||
public class EmptyStringCodeCompletionTestFixture |
|
||||||
{ |
|
||||||
MockMemberProvider memberProvider; |
|
||||||
RubyConsoleCompletionDataProvider completionProvider; |
|
||||||
|
|
||||||
[SetUp] |
|
||||||
public void Init() |
|
||||||
{ |
|
||||||
memberProvider = new MockMemberProvider(); |
|
||||||
completionProvider = new RubyConsoleCompletionDataProvider(memberProvider); |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// If the user presses the dot character without having any text in the command line then
|
|
||||||
/// a SyntaxException occurs if the code calls IronRuby's CommandLine.GetMemberNames. So this
|
|
||||||
/// tests ensures that if the string is empty then this method is not called.
|
|
||||||
/// </summary>
|
|
||||||
[Test] |
|
||||||
public void NoCompletionItemsGeneratedForEmptyString() |
|
||||||
{ |
|
||||||
memberProvider.SetMemberNames(new string[] {"a"}); |
|
||||||
memberProvider.SetGlobals(new string[] {"a"}); |
|
||||||
|
|
||||||
Assert.AreEqual(0, completionProvider.GenerateCompletionData(">>> ").Length); |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Checks that the GenerateCompletionData method catches any exceptions thrown by the
|
|
||||||
/// IMemberProvider implementation. This can occur when an invalid name is passed to
|
|
||||||
/// IronRuby's CommandLine.GetMemberNames or GetGlobals. For example, an UnboundNameException is
|
|
||||||
/// thrown if an unknown name is used.
|
|
||||||
/// </summary>
|
|
||||||
[Test] |
|
||||||
public void NoCompletionItemsGeneratedWhenExceptionThrown() |
|
||||||
{ |
|
||||||
memberProvider.ExceptionToThrow = new ApplicationException("Should not be thrown"); |
|
||||||
|
|
||||||
Assert.AreEqual(0, completionProvider.GenerateCompletionData(">>> a").Length); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void UnderscoresPassedToGetMemberNames() |
|
||||||
{ |
|
||||||
completionProvider.GenerateCompletionData(">>> __builtins__"); |
|
||||||
Assert.AreEqual("__builtins__", memberProvider.GetMemberNamesParameter); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -1,47 +0,0 @@ |
|||||||
// <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; |
|
||||||
using ICSharpCode.AvalonEdit; |
|
||||||
using ICSharpCode.AvalonEdit.Editing; |
|
||||||
using ICSharpCode.RubyBinding; |
|
||||||
using NUnit.Framework; |
|
||||||
|
|
||||||
namespace RubyBinding.Tests.Console |
|
||||||
{ |
|
||||||
[TestFixture] |
|
||||||
public class InsertConsoleCompletionDataTestFixture |
|
||||||
{ |
|
||||||
RubyConsoleCompletionData completionData; |
|
||||||
TextEditor textEditor; |
|
||||||
|
|
||||||
[SetUp] |
|
||||||
public void Init() |
|
||||||
{ |
|
||||||
textEditor = new TextEditor(); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void TextInsertedAtCursor() |
|
||||||
{ |
|
||||||
textEditor.Text = "abc.n"; |
|
||||||
textEditor.CaretOffset = 4; |
|
||||||
|
|
||||||
int startOffset = 4; |
|
||||||
int endOffset = 5; |
|
||||||
SimpleSelection selection = new SimpleSelection(startOffset, endOffset); |
|
||||||
|
|
||||||
completionData = new RubyConsoleCompletionData("new"); |
|
||||||
completionData.Complete(textEditor.TextArea, selection, null); |
|
||||||
|
|
||||||
string expectedText = |
|
||||||
"abc.new"; |
|
||||||
|
|
||||||
Assert.AreEqual(expectedText, textEditor.Text); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -1,351 +0,0 @@ |
|||||||
// <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; |
|
||||||
using System.Windows.Input; |
|
||||||
using ICSharpCode.RubyBinding; |
|
||||||
using ICSharpCode.Scripting; |
|
||||||
using NUnit.Framework; |
|
||||||
using RubyBinding.Tests.Console; |
|
||||||
using RubyBinding.Tests.Utils; |
|
||||||
|
|
||||||
namespace RubyBinding.Tests.Utils.Tests |
|
||||||
{ |
|
||||||
[TestFixture] |
|
||||||
public class MockConsoleTextEditorTestFixture |
|
||||||
{ |
|
||||||
MockConsoleTextEditor textEditor; |
|
||||||
|
|
||||||
[SetUp] |
|
||||||
public void Init() |
|
||||||
{ |
|
||||||
textEditor = new MockConsoleTextEditor(); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void TextReturnsEmptyStringByDefault() |
|
||||||
{ |
|
||||||
Assert.AreEqual(String.Empty, textEditor.Text); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void TextReturnsTextWritten() |
|
||||||
{ |
|
||||||
textEditor.Write("abc"); |
|
||||||
Assert.AreEqual("abc", textEditor.Text); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void ColumnReturnsPositionAfterTextWritten() |
|
||||||
{ |
|
||||||
textEditor.Write("ab"); |
|
||||||
Assert.AreEqual(2, textEditor.Column); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void TextReturnsAllTextWritten() |
|
||||||
{ |
|
||||||
textEditor.Write("a"); |
|
||||||
textEditor.Write("b"); |
|
||||||
Assert.AreEqual("ab", textEditor.Text); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void ColumnReturnsPositionAfterTextWhenWriteCalledTwice() |
|
||||||
{ |
|
||||||
textEditor.Write("a"); |
|
||||||
textEditor.Write("bb"); |
|
||||||
Assert.AreEqual(3, textEditor.Column); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void IsWriteCalledReturnsTrueAfterWriteMethodCalled() |
|
||||||
{ |
|
||||||
textEditor.Write("a"); |
|
||||||
Assert.IsTrue(textEditor.IsWriteCalled); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void SettingTextSetsColumnToPositionAfterText() |
|
||||||
{ |
|
||||||
textEditor.Text = "abc"; |
|
||||||
Assert.AreEqual(3, textEditor.Column); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void SettingTextSetsSelectionStartToPositionAfterText() |
|
||||||
{ |
|
||||||
textEditor.Text = "abc"; |
|
||||||
Assert.AreEqual(3, textEditor.SelectionStart); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void TotalLinesEqualsOneByDefault() |
|
||||||
{ |
|
||||||
Assert.AreEqual(1, textEditor.TotalLines); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void TotalLinesReturnsTwoWhenTextSetWithTwoLines() |
|
||||||
{ |
|
||||||
textEditor.Text = |
|
||||||
"ab\n" + |
|
||||||
"cd"; |
|
||||||
Assert.AreEqual(2, textEditor.TotalLines); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void ColumnReturnsPositionAfterLastCharacterOnLastLineWhenTextSetWithTwoLines() |
|
||||||
{ |
|
||||||
textEditor.Text = |
|
||||||
"ab\n" + |
|
||||||
"c"; |
|
||||||
Assert.AreEqual(1, textEditor.Column); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void SelectionStartReturnsPositionAfterLastCharacterOnLastLineWhenTextSetWithTwoLines() |
|
||||||
{ |
|
||||||
textEditor.Text = |
|
||||||
"ab\n" + |
|
||||||
"c"; |
|
||||||
Assert.AreEqual(1, textEditor.SelectionStart); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void NoTextAddedWhenPreviewKeyDownEventHandlerReturnsTrue() |
|
||||||
{ |
|
||||||
textEditor.PreviewKeyDown += delegate(object source, ConsoleTextEditorKeyEventArgs e) { |
|
||||||
e.Handled = true; |
|
||||||
}; |
|
||||||
textEditor.RaisePreviewKeyDownEvent(Key.A); |
|
||||||
Assert.AreEqual(String.Empty, textEditor.Text); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void RaisePreviewKeyDownEventReturnsTrueWhenPreviewDownHandlerReturnsTrue() |
|
||||||
{ |
|
||||||
textEditor.PreviewKeyDown += delegate(object source, ConsoleTextEditorKeyEventArgs e) { |
|
||||||
e.Handled = true; |
|
||||||
}; |
|
||||||
Assert.IsTrue(textEditor.RaisePreviewKeyDownEvent(System.Windows.Input.Key.A)); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void KeyPressWhenCursorInsideTextInsertsTextAtCursor() |
|
||||||
{ |
|
||||||
textEditor.Text = "abcdef"; |
|
||||||
textEditor.Column = 3; |
|
||||||
textEditor.RaisePreviewKeyDownEvent(Key.X); |
|
||||||
Assert.AreEqual("abcXdef", textEditor.Text); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void GetLineWithIndex2ReturnsLastLineForThreeLinesOfText() |
|
||||||
{ |
|
||||||
textEditor.Text = |
|
||||||
"abc\n" + |
|
||||||
"def\n" + |
|
||||||
"ghi"; |
|
||||||
Assert.AreEqual("ghi", textEditor.GetLine(2)); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void ReplaceMethodReplacesTextOnLastLine() |
|
||||||
{ |
|
||||||
textEditor.Text = |
|
||||||
"1\n" + |
|
||||||
"2\n" + |
|
||||||
"abcdef"; |
|
||||||
|
|
||||||
int index = 1; |
|
||||||
int lengthToRemove = 4; |
|
||||||
string newText = "123"; |
|
||||||
textEditor.Replace(index, lengthToRemove, newText); |
|
||||||
|
|
||||||
string expectedText = |
|
||||||
"1\n" + |
|
||||||
"2\n" + |
|
||||||
"a123f"; |
|
||||||
|
|
||||||
Assert.AreEqual(expectedText, textEditor.Text); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void LeftCursorDialogKeyPressMovesSelectionStartToLeft() |
|
||||||
{ |
|
||||||
textEditor.Text = "abc"; |
|
||||||
textEditor.SelectionStart = 3; |
|
||||||
textEditor.RaisePreviewKeyDownEventForDialogKey(Key.Left); |
|
||||||
Assert.AreEqual(2, textEditor.SelectionStart); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void LeftCursorDialogKeyPressMovesCursorToLeft() |
|
||||||
{ |
|
||||||
textEditor.Text = "abc"; |
|
||||||
textEditor.SelectionStart = 3; |
|
||||||
textEditor.RaisePreviewKeyDownEventForDialogKey(Key.Left); |
|
||||||
Assert.AreEqual(2, textEditor.Column); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void CursorPositionUnchangedWhenDialogKeyPressEventHandlerReturnsTrue() |
|
||||||
{ |
|
||||||
textEditor.Text = "abc"; |
|
||||||
textEditor.Column = 3; |
|
||||||
textEditor.PreviewKeyDown += delegate(object source, ConsoleTextEditorKeyEventArgs e) { |
|
||||||
e.Handled = true; |
|
||||||
}; |
|
||||||
textEditor.RaisePreviewKeyDownEventForDialogKey(Key.Left); |
|
||||||
Assert.AreEqual(3, textEditor.Column); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void RaiseDialogKeyPressEventReturnsTruedWhenDialogKeyPressEventHandlerReturnsTrue() |
|
||||||
{ |
|
||||||
textEditor.Text = "abc"; |
|
||||||
textEditor.Column = 3; |
|
||||||
textEditor.PreviewKeyDown += delegate(object source, ConsoleTextEditorKeyEventArgs e) { |
|
||||||
e.Handled = true; |
|
||||||
}; |
|
||||||
Assert.IsTrue(textEditor.RaisePreviewKeyDownEventForDialogKey(Key.Left)); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void RightCursorDialogKeyPressMovesCursorToRight() |
|
||||||
{ |
|
||||||
textEditor.Text = "abc"; |
|
||||||
textEditor.Column = 0; |
|
||||||
textEditor.SelectionStart = 0; |
|
||||||
textEditor.RaisePreviewKeyDownEventForDialogKey(Key.Right); |
|
||||||
Assert.AreEqual(1, textEditor.Column); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void RightCursorDialogKeyPressMovesSelectionStartToRight() |
|
||||||
{ |
|
||||||
textEditor.Text = "abc"; |
|
||||||
textEditor.Column = 0; |
|
||||||
textEditor.SelectionStart = 0; |
|
||||||
textEditor.RaisePreviewKeyDownEventForDialogKey(Key.Right); |
|
||||||
Assert.AreEqual(1, textEditor.SelectionStart); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void BackspaceDialogKeyPressRemovesLastCharacterFromLine() |
|
||||||
{ |
|
||||||
textEditor.Text = "abc"; |
|
||||||
textEditor.RaisePreviewKeyDownEventForDialogKey(Key.Back); |
|
||||||
Assert.AreEqual("ab", textEditor.Text); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void BackspaceDialogKeyPressRemovesSelectedTextFromLine() |
|
||||||
{ |
|
||||||
textEditor.Text = "abcd"; |
|
||||||
textEditor.SelectionStart = 1; |
|
||||||
textEditor.SelectionLength = 2; |
|
||||||
textEditor.RaisePreviewKeyDownEventForDialogKey(Key.Back); |
|
||||||
Assert.AreEqual("ad", textEditor.Text); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void EnterDialogKeyPressCreatesNewLine() |
|
||||||
{ |
|
||||||
textEditor.Text = "abc"; |
|
||||||
textEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter); |
|
||||||
|
|
||||||
string expectedText = "abc\r\n"; |
|
||||||
Assert.AreEqual(expectedText, textEditor.Text); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void ColumnResetToZeroAfterEnterDialogKeyPress() |
|
||||||
{ |
|
||||||
textEditor.Text = "abc"; |
|
||||||
textEditor.Column = 3; |
|
||||||
textEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter); |
|
||||||
|
|
||||||
Assert.AreEqual(0, textEditor.Column); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void SelectionStartResetToZeroAfterEnterDialogKeyPress() |
|
||||||
{ |
|
||||||
textEditor.Text = "abc"; |
|
||||||
textEditor.SelectionStart = 3; |
|
||||||
textEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter); |
|
||||||
|
|
||||||
Assert.AreEqual(0, textEditor.Column); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void EnterDialogKeyPressDoesNothingWhenKeyPressEventHandlerReturnsTrue() |
|
||||||
{ |
|
||||||
textEditor.Text = "abc"; |
|
||||||
textEditor.PreviewKeyDown += delegate(object source, ConsoleTextEditorKeyEventArgs e) { |
|
||||||
if (e.Key == Key.Enter) { |
|
||||||
e.Handled = true; |
|
||||||
} else { |
|
||||||
e.Handled = false; |
|
||||||
} |
|
||||||
}; |
|
||||||
textEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter); |
|
||||||
Assert.AreEqual("abc", textEditor.Text); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void EnterDialogKeyPressIncreasesTotalLineCountByOne() |
|
||||||
{ |
|
||||||
textEditor.Text = "abc"; |
|
||||||
textEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter); |
|
||||||
|
|
||||||
Assert.AreEqual(2, textEditor.TotalLines); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void EnterDialogKeyPressIncreasesCurrentLineByOne() |
|
||||||
{ |
|
||||||
textEditor.Text = "abc"; |
|
||||||
textEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter); |
|
||||||
|
|
||||||
Assert.AreEqual(1, textEditor.Line); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void EnterDialogKeyPressInMiddleOfLinePutsTextAfterCursorOnNewLine() |
|
||||||
{ |
|
||||||
textEditor.Text = "abcd"; |
|
||||||
textEditor.Column = 2; |
|
||||||
textEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter); |
|
||||||
|
|
||||||
string expectedText = |
|
||||||
"ab\r\n" + |
|
||||||
"cd"; |
|
||||||
Assert.AreEqual(expectedText, textEditor.Text); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void EnterDialogKeyPressInMiddleOfLinePutsSelectionStartAtEndOfNewLineGenerated() |
|
||||||
{ |
|
||||||
textEditor.Text = "abcd"; |
|
||||||
textEditor.Column = 3; |
|
||||||
textEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter); |
|
||||||
Assert.AreEqual(1, textEditor.SelectionStart); |
|
||||||
} |
|
||||||
|
|
||||||
[Test] |
|
||||||
public void EnterDialogKeyPressInMiddleOfLinePutsCursorAtEndOfNewLineGenerated() |
|
||||||
{ |
|
||||||
textEditor.Text = "abcd"; |
|
||||||
textEditor.Column = 3; |
|
||||||
textEditor.RaisePreviewKeyDownEventForDialogKey(Key.Enter); |
|
||||||
Assert.AreEqual(1, textEditor.Column); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,14 @@ |
|||||||
|
<configuration> |
||||||
|
<configSections> |
||||||
|
<sectionGroup name="NUnit"> |
||||||
|
<section name="TestRunner" |
||||||
|
type="System.Configuration.NameValueSectionHandler" /> |
||||||
|
</sectionGroup> |
||||||
|
</configSections> |
||||||
|
<NUnit> |
||||||
|
<TestRunner> |
||||||
|
<!-- Valid values are STA,MTA. Others ignored. --> |
||||||
|
<add key="ApartmentState" value="STA" /> |
||||||
|
</TestRunner> |
||||||
|
</NUnit> |
||||||
|
</configuration> |
Loading…
Reference in new issue