Skip to content

Instantly share code, notes, and snippets.

@notionparallax
Last active July 6, 2019 06:26
Show Gist options
  • Save notionparallax/12fb14f4fbab33fa19239d69d8046c5c to your computer and use it in GitHub Desktop.
Save notionparallax/12fb14f4fbab33fa19239d69d8046c5c to your computer and use it in GitHub Desktop.
import io, sys
book_of_counts = [
{
"expect": """let's get ready to rumble 8
let's get ready to rumble 7
let's get ready to rumble 6
let's get ready to rumble 5
let's get ready to rumble 4
let's get ready to rumble 3
let's get ready to rumble 2
let's get ready to rumble 1
*rumbling sound*
""",
"input": {
"message": "let's get ready to rumble",
"start": 8,
"stop": 1,
"completion_message": "*rumbling sound*",
},
},
{
"expect": """prepare to die in this many ways: 4
prepare to die in this many ways: 3
prepare to die in this many ways: 2
prepare to die in this many ways: 1
or not, I guess
""",
"input": {
"message": "prepare to die in this many ways:",
"start": 5,
"stop": 2,
"completion_message": "or not, I guess",
},
},
{
"expect": """Getting ready to start in 9
Getting ready to start in 8
Getting ready to start in 7
Getting ready to start in 6
Getting ready to start in 5
Getting ready to start in 4
Getting ready to start in 3
Getting ready to start in 2
Getting ready to start in 1
Let's go!
""",
"input": {
"message": "Getting ready to start in",
"start": 9,
"stop": 1,
"completion_message": "Let's go!",
},
},
]
def countdown(message, start, stop, completion_message):
for i in range(stop - 1, start):
print("{m} {i}".format(m=message, i=start - i))
print(completion_message)
for c in book_of_counts:
capturedOutput = io.StringIO() # Create StringIO object
sys.stdout = capturedOutput # and redirect stdout.
countdown(**c["input"])
sys.stdout = sys.__stdout__ # Reset redirect.
test = c["expect"]
if capturedOutput.getvalue() != test:
print("Captured:\n" + capturedOutput.getvalue() + "|") # Now works as before.
print("Expected:\n" + test + "|")
print(capturedOutput.getvalue() == test)
@notionparallax
Copy link
Author

This is how I'm going to test the countdown function. It's a surprisingly tricky function, so I'm going to write some kind of explainer for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment