Skip to content

Instantly share code, notes, and snippets.

@YoniTsafir
Created January 30, 2012 17:10
Show Gist options
  • Save YoniTsafir/1705477 to your computer and use it in GitHub Desktop.
Save YoniTsafir/1705477 to your computer and use it in GitHub Desktop.
Code from TDD Workshop with Corey Haines
'''
Created on Jan 30, 2012
@author: yonits
'''
import unittest
def flip_indices(new_list, first_idx, second_idx):
new_list[first_idx], new_list[second_idx] = new_list[second_idx], new_list[first_idx]
def sorted_list(my_list):
new_list = list(my_list)
last_index = len(new_list) - 1
for i in xrange(last_index - 1, -1, -1):
for j in xrange(i, last_index):
first = new_list[j]
second = new_list[j + 1]
if first > second:
flip_indices(new_list, j, j + 1)
return new_list
class SortTests(unittest.TestCase):
def check_sort(self, input, expected_output):
return self.assertEquals(sorted_list(input), expected_output)
def test_sort_empty_list_gives_empty_list(self):
self.check_sort([], [])
def test_sort_single_element_doesnt_change_the_list(self):
self.check_sort([1], [1])
def test_sorted_list_of_length_two(self):
self.check_sort([1, 2], [1, 2])
def test_flipped_two_elements_list(self):
self.check_sort([5, 1], [1, 5])
def test_sorted_list_of_length_three(self):
self.check_sort([1, 2, 5], [1, 2, 5])
def test_flipped_first_two_elements_in_list_of_length_three(self):
self.check_sort([4, 1, 6], [1, 4, 6])
def test_flipped_last_two_elements_in_list_of_length_three(self):
self.check_sort([1, 7, 4], [1, 4, 7])
def test_reversed_list_of_length_three(self):
self.check_sort([9, 8, 7], [7, 8, 9])
'''
Created on Jan 30, 2012
@author: yonits
'''
import unittest
def _replace_last(string, substr, new_substr):
return new_substr.join(string.rsplit(substr, 1))
def _split_at_width(string, width):
if string[width] == " ":
split_index = width + 1
else:
split_index = width
return string[:split_index], string[split_index:]
def wrap(string, width):
if string is None:
return ""
if len(string) < width + 1:
return string
before, after = _split_at_width(string, width)
after = wrap(after, width)
if " " in before:
return _replace_last(before, " ", "\n") + after
else:
return "\n".join([before, after])
class WrapTests(unittest.TestCase):
def test_wrap_none_returns_empty_string(self):
self._assert_wraps(None, 6, "")
def test_wrap_empty_string_returns_empty_string(self):
self._assert_wraps("", 4, "")
def test_wrap_small_single_word_returns_it_in_one_line(self):
self._assert_wraps("word", 5, "word")
def test_wraps_two_small_words_that_dont_fit_in_width_breaks_line(self):
self._assert_wraps("word word", 6, "word\nword")
def test_wraps_three_words_first_two_fits_in_width_breaks_after_second_word(self):
self._assert_wraps("word word word", 9, "word word\nword")
def test_2_words_in_2_lines_split_in_the_middle(self):
self._assert_wraps("word word word word", 11, "word word\nword word")
def test_2_words_in_2_lines_split_in_the_middle_different_width(self):
self._assert_wraps("word word word word", 10, "word word\nword word")
def test_long_word_breaks_in_middle(self):
self._assert_wraps("longword", 4, "long\nword")
def test_multiple_lines(self):
self._assert_wraps("word word word word", 5, "word\nword\nword\nword")
def test_short_word_and_then_long_word(self):
self._assert_wraps("word longword", 8, "word\nlongword")
def test_short_word_and_then_long_word_small_width(self):
self._assert_wraps("word longword", 5, "word\nlongw\nord")
def _assert_wraps(self, input_string, width, expected_output):
return self.assertEquals(wrap(input_string, width), expected_output)
'''
Created on Jan 30, 2012
@author: abyx, johnny
'''
import unittest
def wrap(string, width):
if string is None: return ""
if len(string) <= width: return string
if " " in string:
space_at = string[:width + 1].rfind(" ")
return string[:space_at] + "\n" + wrap(string[space_at + 1:], width)
else:
return string[:width] + "\n" + wrap(string[width:], width)
class WrapTests(unittest.TestCase):
# test: {} -> nil
# code: nil -> constant
def test_none_returns_empty_string(self):
assert wrap(None, 6) == ""
# test: nil -> constant
# code: nothing...
def test_empty_string_returns_itself(self):
assert wrap("", 5) == ""
# test: constant -> constant+
# code: 1. constant -> scalar
# 2. unconditional -> if
def test_word_shorter_than_width_returns_itself(self):
assert wrap("word", 5) == "word"
# 1. test: constant -> scalar
# 2. code: unconditional -> if
# 3. code: nil -> constant
# 4. test: statement -> statements
# 5. code: constant -> scalar
def test_word_longer_than_width_wraps_at_width(self):
assert wrap("longword", 4) == "long\nword"
assert wrap("longerword", 6) == "longer\nword"
# 1. test: scalar -> array
# 2. code: statement -> recursion
def test_word_longer_than_twice_the_width_wraps_twice(self):
assert wrap("verylongword", 4) == "very\nlong\nword"
# 1. test: constant -> scalar
# 2. code: unconditional -> if
# 3. code: nil -> constant
# 4. test: statement -> statements
# 5. code: constant -> scalar
def test_two_words_break_at_space(self):
assert wrap("word word", 6) == "word\nword"
assert wrap("wrap here", 6) == "wrap\nhere"
# 1. test: scalar -> array
# 2. code: statement -> recursion
# --- after broken by other test: ---
# 3. code: ??? (string -> string[:width])
def test_multiple_words_break_at_space(self):
assert wrap("word word word", 6) == "word\nword\nword"
# 1. test: constant -> constant+ (changed the width)
# 2. code: ??? (find -> rfind)
# 3. breaks previous test -> see continue there
def test_doesnt_break_words_before_width(self):
assert wrap("word word word", 11) == "word word\nword"
# 1. test: constant -> constant+ (changed the width)
# 2. code: ??? (width -> width + 1)
def test_wraps_even_if_fits_exactly_to_width(self):
assert wrap("word word", 4) == "word\nword"
# 1. test: scalar -> array (1 word in a line, 2 words in a line)
# 2. code: nothing
def test_wraps_in_the_middle(self):
assert wrap("word word word word", 9) == "word word\nword word"
# 1. test: constant -> constant+ (width changed)
# 2. code: nothing...
def test_wraps_in_the_middle_when_space_is_last_char(self):
assert wrap("word word word word", 10) == "word word\nword word"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment