Last active
March 26, 2016 19:28
-
-
Save ccdschool/12251080382f74883b4e to your computer and use it in GitHub Desktop.
Refactoring exercise taken from the Kata Word Wrap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
class WordWrap { | |
static string[] Erzeuge_Zeilen(string[] worte, int maxLineLength) { | |
List<string> lines = new List<string>(); | |
var i = 0; | |
var line = ""; | |
while(true) { | |
if (i == worte.Length) { | |
if (line.Length != 0) { | |
lines.Add(line); | |
} | |
break; | |
} | |
String nextWord = worte[i]; | |
if (string.IsNullOrWhiteSpace(line) && nextWord.Length > maxLineLength) { | |
String head = nextWord.Substring(0, maxLineLength); | |
String body = nextWord.Substring(maxLineLength); | |
lines.Add(head); | |
worte[i] = body; | |
} | |
else { | |
int futureLength = line.Length + nextWord.Length + (line.Length > 0 ? 1 : 0); | |
if (futureLength <= maxLineLength) { | |
line += (line.Length > 0 ? " " : "") + nextWord; | |
i++; | |
} | |
else { | |
lines.Add(line); | |
line = ""; | |
} | |
} | |
} | |
return lines.ToArray (); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using NUnit.Framework; | |
using System; | |
using System.Collections.Generic; | |
[TestFixture ()] | |
public class WordWrapTests | |
{ | |
[Test] | |
public void testErzeugeZeilen() { | |
var worte = new[]{ "1", "22", "333", "55555"}; | |
var lineLength = 5; | |
var result = WordWrap.Erzeuge_Zeilen(worte, lineLength); | |
Assert.AreEqual(new String[]{"1 22", "333", "55555"}, result); | |
} | |
[Test] | |
public void testErzeugeZeilenMitZuLangemWort() { | |
var worte = new[]{ "1", "22", "666666", "55555"}; | |
var lineLength = 5; | |
var result = WordWrap.Erzeuge_Zeilen(worte, lineLength); | |
Assert.AreEqual(new String[]{"1 22", "66666", "6", "55555"}, result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment