Current Code


 using System; 
using System.Collections;
using System.Text;

namespace Notepad {
class TextModel {
private ArrayList lines;
private int selectionStart;
public TextModel() {
lines = new ArrayList();
}

public void InsertControlPText() {
}

private int LineContainingCursor() {
int length = 0;
int lineNr = 0;
int cr = Environment.NewLine.Length;
foreach ( String s in lines) {
if (length <= selectionStart
&& selectionStart < length+s.Length + cr )
break;
length += s.Length + cr;
lineNr++;
}
return lineNr;
}
public void Enter() {
InsertParagraphTag();
}
public void InsertParagraphTag() {
//
// On Enter, we change the TextModel lines to insert, after the line
// containing the cursor, a blank line, and a line with < P >< /P > . We set
// the new cursor location to be between the P tags: < P > < /P > .
//
// handle empty array special case (yucch)
if ( lines.Count == 0 ) {
lines.Add( "<P></P>" );
selectionStart = 3;
return;
}

lines.InsertRange(LineContainingCursor()+1, NewParagraph());

// set cursor location
selectionStart = NewSelectionStart(LineContainingCursor() + 2);
}

private int NewSelectionStart(int cursorLine) {
int length = 0;
for (int i = 0; i < cursorLine; i++)
length += ((String)lines[i]).Length + Environment.NewLine.Length;
return length + "<p>".Length;
}

public ArrayList LinesThroughCursor() {
return lines.GetRange(0,LineContainingCursor()+1);
}

public ArrayList NewParagraph() {
ArrayList temp = new ArrayList();
temp.Add("");
temp.Add("<P></P>");
return temp;
}

public ArrayList LinesAfterCursor() {
int cursorLine = LineContainingCursor();
return lines.GetRange(cursorLine+1,lines.Count - cursorLine - 1);
}
public String[] Lines {
get {
return (String[]) lines.ToArray(typeof(String));
}
set {
lines = new ArrayList(value);
}
}
public String TestText {
get {
StringBuilder b = new StringBuilder();
foreach(String s in lines) {
b.Append(s);
b.Append(System.Environment.NewLine);
}
b.Insert(SelectionStart,"");
return b.ToString();
}
}

public int SelectionStart {
get {
return selectionStart;
}
set {
selectionStart = value;
}
}
}
}

using System;
using Nunit.Framework;

namespace Notepad {

[TestFixture] public class TestTextModel : Assertion {
private TextModel model;

[SetUp] public void CreateModel() {
model = new TextModel();
}

[Test] public void TestNoLines() {
model.Lines = new String[0];
AssertEquals(0, model.Lines.Length);
}

[Test] public void TestNoProcessing() {
model.Lines = new String[3] { "hi", "there", "chet"};
AssertEquals(3, model.Lines.Length);
}

[Test] public void TestOneEnter() {
model.Lines = new String[1] {"hello world" };
model.SelectionStart = 5;
model.InsertParagraphTag();
AssertEquals(3, model.Lines.Length);
AssertEquals(18, model.SelectionStart);
}
[Test] public void TestEmptyText() {
model.Lines = new String[0];
model.InsertParagraphTag();
AssertEquals(1, model.Lines.Length);
AssertEquals(3, model.SelectionStart);
}

// Chet comments that he hates the comments

[Test] public void InsertWithCursorAtLineStart () {
model.Lines = new String[3] { "<P>one</P>", "", "<P>two</P>"};
model.SelectionStart = 14;
model.InsertParagraphTag();
AssertEquals("<P>two</P>", model.Lines[2]);
}

[Test] public void TestLineContainingCursorDirectly() {
// todo?
}
}
}



Extreme Programming Adventures in C#
Javaв„ў EE 5 Tutorial, The (3rd Edition)
ISBN: 735619492
EAN: 2147483647
Year: 2006
Pages: 291

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net