Thursday, April 21, 2011

Writing a ToString Method for Board

Today's task is to create a toString method for the Board class that will display the whole board in a readable manner.

Let's start by looking at the board again. Here's the diagram I have on my desktop to refer to whenever I write up my tests:

.01 .02 .03 .04 .05 .

06 07 08 09 10 11

.12 .13 .14 .15 .16 .

17 18 19 20 21 22

.23 .24 .25 .26 .27 .

28 29 30 31 32 33

.34 .35 .36 .37 .38 .

39 40 41 42 43 44

.45 .46 .47 .48 .49 .

50 51 52 53 54 55

.56.57 .58 .59 .60 .


There are two patterns:

.-.-.-.-.-.\r (odd rows)

| | | | | |\r (even rows)

So, I'm going to write two private methods that take the number of the first location and return a string that represents that row.

Why don't I go do that right now. Here's my ToString:


public override string ToString()

{

StringBuilder display = new StringBuilder(132);

display.Append(displayOddRow(1));

display.Append(displayEvenRow(6));

display.Append(displayOddRow(12));

display.Append(displayEvenRow(17));

display.Append(displayOddRow(23));

display.Append(displayEvenRow(28));

display.Append(displayOddRow(34));

display.Append(displayEvenRow(39));

display.Append(displayOddRow(45));

display.Append(displayEvenRow(50));

display.Append(displayOddRow(56));

return display.ToString();

}


Here are my private methods:

private string displayOddRow(int start)

{

return "s";

}

private string displayEvenRow(int start)

{

return "s";

}

It's not much, but it's enough to write a test.


# Testing ToString override Method


$testToString = new Board(60)


$testToString.ToString()


# END Testing ToString override Method


The test worked. I did some extra coding and came up with the following:


public override string ToString()

{

StringBuilder display = new StringBuilder(144);

display.Append(displayOddRow(1));

display.Append(displayEvenRow(6));

display.Append(displayOddRow(12));

display.Append(displayEvenRow(17));

display.Append(displayOddRow(23));

display.Append(displayEvenRow(28));

display.Append(displayOddRow(34));

display.Append(displayEvenRow(39));

display.Append(displayOddRow(45));

display.Append(displayEvenRow(50));

display.Append(displayOddRow(56));

return display.ToString();

}


private string displayOddRow(int start)

{

StringBuilder displayOdd = new StringBuilder("o o o o o o\r\n");

return displayOdd.ToString();

}

private string displayEvenRow(int start)

{

StringBuilder displayOdd = new StringBuilder(" \r\n");

return displayOdd.ToString();

}


This is enough to get the following result:


o o o o o o

o o o o o o

o o o o o o

o o o o o o

o o o o o o

o o o o o o


Now, all I have to do is place sticks inside. OK.


[Break in time]

I usually do these posts at a sitting. I found that if I tried to do more than one per day or to write a post that wasn't connected to a session of coding, it didn't work as well, but I bent that rule here.

I did “put sticks inside” as I said. Here's the C# code:

private string displayOddRow(int start)

{

StringBuilder displayOdd = new StringBuilder("o");

for (int i = start; i LESS THAN SYMBOL start + 5; i++)

{

if (getPositionValue(i)) {displayOdd.Append("-");}

else {displayOdd.Append(" ");}

displayOdd.Append("o");

}

displayOdd.Append("\r\n");

return displayOdd.ToString();

}

private string displayEvenRow(int start)

{

StringBuilder displayEven = new StringBuilder();

for (int i = start; i LESS THAN SYMBOL start + 5; i++)

{

if (getPositionValue(i)) {displayEven.Append("|");}

else {displayEven.Append(" ");}

displayEven.Append(" ");

}

if (getPositionValue(start + 5)) {displayEven.Append("|");}

else {displayEven.Append(" ");}

displayEven.Append("\r\n");

return displayEven.ToString();

}


And here's the test code I wrote for the ToString method:


# Testing ToString override Method



$testToString = new Board(60)


for ($a = 1; $a -lt 61; $a++)

{

$temp = $testToString.addStick($a);

}


$testToStringTestObjectFilled = import-clixml -path testToStringTestObjectFilled

if (!($testToString.ToString() -eq $testToStringTestObjectFilled)) {"FAIL: Should match saved object."}



$testToString = new Board(60)


$temp = $testToString.addStick(1)

$temp = $testToString.addStick(7)

$temp = $testToString.addStick(13)

$temp = $testToString.addStick(19)

$temp = $testToString.addStick(25)

$temp = $testToString.addStick(31)

$temp = $testToString.addStick(37)


$temp = $testToString.addStick(43)

$temp = $testToString.addStick(44)

$temp = $testToString.addStick(49)

$temp = $testToString.addStick(54)


$temp = $testToString.addStick(60)


$testToString.ToString()


$testToStringTestObject = Import-Clixml -path testToStringTestObject

if (!($testToString.ToString() -eq $testToStringTestObject)) { "FAIL: ToString output should match saved test obect."}


# END Testing ToString override Method


PowerShell wouldn't accept the output of ToString as equal to the output of the display function I wrote earlier – either directly or through cut and paste. To get around this, I made and saved copies of the output from the current ToString method using export-clixml. The script imports these and uses them for the comparison.

This is much nicer than my display testing method, but it works about the same.

No comments:

Post a Comment