Commits

Christoph Schindler committed bd936f6

Ergebnis der Dojo-Session WordWrap

  • Participants
  • Parent commits c0cba5a

Comments (0)

Files changed (4)

WordWrap/TEST LIST.txt

+TEST LIST for WordWrap Kata
+===========================
+
+ + Empty string
+ + Line that is shorter than column number
+ - Short line with blank at beginning
+ + Line is exactly column long
+ + Single word that is longer than column number
+ - Line that needs word wrapping once
+ - Line that needs word wrapping twice
+ - Multiple lines
+ - Line with punctuation at end
+ - Line with punctuation in the middle
+ - Word with hyphen in the middle
+
+

WordWrap/setup.cfg

+[nosy]
+base_path = ./
+glob_pattern = *.py
+extra_paths = setup.cfg
+options = -x --with-yanc

WordWrap/test_wordwrap.py

+from nose.tools import *
+
+import wordwrap
+
+def test_empty_string():
+    assert_equal(wordwrap.wordwrap("", 80), "")
+
+def test_short_line():
+    assert_equal(wordwrap.wordwrap("lorem ipsum", 80), "lorem ipsum")
+    
+def test_too_long_word():
+    assert_equal(wordwrap.wordwrap("loremipsum", 6), "lorem\nipsum")
+    
+def test_word_exactly_columns():
+    assert_equal(wordwrap.wordwrap("lorem", 6), "lorem")
+
+def test_break_at_hyphen():
+    assert_equal(wordwrap.wordwrap("lorem-ipsum", 8), "lorem-\nipsum")

WordWrap/wordwrap.py

+#!/usr/bin/env python
+# encoding: utf-8
+"""
+wordwrap.py
+
+Created by Coding Dojo on 2013-03-13.
+Copyright (c) 2013 PyUGAT. All rights reserved.
+"""
+import unittest
+import re
+WORD_RE = re.compile("[^ -]*.")
+
+
+def wordwrap(string, columns):
+    """Wrap input string at column."""
+    output = []
+    if len(string) < columns:
+        return string
+    words = WORD_RE.findall(string)
+    remaining = columns
+    for word in words:
+        #if len(word) >= columns
+
+        if len(word) < remaining:
+            output.append(word)
+        else:
+            output.append("\n")
+            remaining = columns
+            output.append(word)
+    return output
+
+if __name__ == '__main__':
+    unittest.main()