Skip to content
Snippets Groups Projects
Commit d9e7c0d9 authored by Brady James Garvin's avatar Brady James Garvin
Browse files

Initial commit.

parents
No related branches found
No related tags found
No related merge requests found
.idea
*.pyc
*.pyo
*.apk
# A function with associated doctests.
def parse(pair):
"""
Converts a key/value pair represented as a string to a key/value pair represented as a tuple.
Key/value pairs are expected to be separated by a colon and a space:
>>> parse('a key: a value')
('a key', 'a value')
If there is no colon and space, parse raises a ValueError:
>>> parse('invalid')
Traceback (most recent call last):
...
ValueError: need more than 1 value to unpack
Likewise if there is more than one separator:
>>> parse('first: second: third')
Traceback (most recent call last):
...
ValueError: too many values to unpack (expected 2)
"""
key, value = pair.split(': ')
return key, value
from unittest import TestCase
import examples
# The same tests in a separate unit-testing class.
class TestParse(TestCase):
# assertEqual is like a reversed version of assertEquals from JUnit.
def test_parse(self):
self.assertEqual(examples.parse('a key: a value'), ('a key', 'a value'))
# It is also possible to expect exceptions like in the doctests, but the syntax is a little different.
def test_parse_single(self):
self.assertRaises(ValueError, examples.parse, 'invalid')
def test_parse_triple(self):
self.assertRaises(ValueError, examples.parse, 'first: second: third')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment