Skip to content
Snippets Groups Projects
Commit d18c730f authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Initial Commit

parents
No related branches found
No related tags found
No related merge requests found
# Mac file finder metadata
.DS_Store
# Windows file metadata
._*
# Thumbnail image caches
Thumbs.db
ethumbs.db
# MS Office temporary file
~*
# Emacs backup file
*~
# Common
bin
# Python files
*.pyc
*.pyo
__pycache__/
# JetBrains (IntelliJ IDEA, PyCharm, etc) files
.idea/
cmake-build-*/
*.iml
*.iws
*.ipr
venv/
# 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