Skip to content

Commit 6d0effb

Browse files
added test suite; vdf clean up
1 parent 3d88866 commit 6d0effb

File tree

8 files changed

+214
-140
lines changed

8 files changed

+214
-140
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
dist
22
*.egg-info
33
*.pyc
4+
.coverage
5+
*.swp

.travis.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,10 @@ python:
55
- "3.2"
66
- "3.3"
77
- "3.4"
8+
install:
9+
- make init
10+
- pip install coveralls
811
script:
9-
python -c "from __future__ import print_function; import vdf; print(vdf.test())"
12+
make test
13+
after_success:
14+
coveralls

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ init:
1919
pip install -r requirements.txt
2020

2121
test:
22-
rm -f vdf/*.pyc
22+
rm -f vdf/*.pyc tests/*.pyc
2323
nosetests --verbosity 2 --with-coverage --cover-package=vdf
2424

2525
pylint:

README.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
|pypi| |license|
1+
|pypi| |license| |coverage| |master_build|
22

33
VDF is Valve's KeyValue text file format
44

@@ -51,5 +51,12 @@ Usage
5151

5252
.. |license| image:: https://img.shields.io/pypi/l/vdf.svg?style=flat&label=license
5353
:target: https://pypi.python.org/pypi/vdf
54-
:alt: Latest version released on PyPi
54+
:alt: MIT License
55+
56+
.. |coverage| image:: https://img.shields.io/coveralls/rossengeorgiev/vdf-python/master.svg?style=flat
57+
:target: https://coveralls.io/r/rossengeorgiev/vdf-python?branch=master
58+
:alt: Test coverage
5559

60+
.. |master_build| image:: https://img.shields.io/travis/rossengeorgiev/vdf-python/master.svg?style=flat&label=master%20build
61+
:target: http://travis-ci.org/rossengeorgiev/vdf-python
62+
:alt: Build status of master branch

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
nose
2+
coverage
3+
mock

tests/__init__.py

Whitespace-only changes.

tests/test_vdf.py

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
import unittest
2+
import mock
3+
import sys
4+
5+
try:
6+
from StringIO import StringIO
7+
except ImportError:
8+
from io import StringIO
9+
10+
import vdf
11+
12+
13+
class testcase_helpers_load(unittest.TestCase):
14+
def setUp(self):
15+
self.f = StringIO()
16+
17+
def tearDown(self):
18+
self.f.close()
19+
20+
@mock.patch("vdf.parse")
21+
def test_routine_loads(self, mock_parse):
22+
vdf.loads("")
23+
mock_parse.assert_called_with("")
24+
25+
def test_routine_loads_assert(self):
26+
for t in [5, 5.5, 1.0j, None, [], (), {}, lambda: 0, sys.stdin, self.f]:
27+
self.assertRaises(AssertionError, vdf.loads, t)
28+
29+
@mock.patch("vdf.parse")
30+
def test_routine_load(self, mock_parse):
31+
vdf.load(sys.stdin)
32+
mock_parse.assert_called_with(sys.stdin)
33+
34+
vdf.load(self.f)
35+
mock_parse.assert_called_with(self.f)
36+
37+
def test_routine_load_assert(self):
38+
for t in [5, 5.5, 1.0j, None, [], (), {}, lambda: 0, '']:
39+
self.assertRaises(AssertionError, vdf.load, t)
40+
41+
42+
class testcase_helpers_dump(unittest.TestCase):
43+
def setUp(self):
44+
self.f = StringIO()
45+
46+
def tearDown(self):
47+
self.f.close()
48+
49+
def test_routine_dumps_asserts(self):
50+
for x in [5, 5.5, 1.0j, True, None, (), {}, lambda: 0, sys.stdin, self.f]:
51+
for y in [5, 5.5, 1.0j, None, [], (), {}, lambda: 0, sys.stdin, self.f]:
52+
self.assertRaises(ValueError, vdf.dumps, x, y)
53+
54+
def test_routine_dump_asserts(self):
55+
for x in [5, 5.5, 1.0j, True, None, (), {}, lambda: 0, sys.stdin, self.f]:
56+
for y in [5, 5.5, 1.0j, True, None, [], (), {}, lambda: 0]:
57+
self.assertRaises(ValueError, vdf.dump, x, y)
58+
59+
def test_routine_dump_writing(self):
60+
src = {"asd": "123"}
61+
expected = vdf.dumps(src)
62+
63+
vdf.dump(src, self.f)
64+
self.f.seek(0)
65+
66+
self.assertEqual(expected, self.f.read())
67+
68+
69+
class testcase_parse_routine(unittest.TestCase):
70+
def test_parse_bom_removal(self):
71+
for mark in vdf.BOMS:
72+
result = vdf.loads(mark + "")
73+
self.assertEqual(result, {})
74+
75+
def test_parse_asserts(self):
76+
for t in [5, 5.5, 1.0j, True, None, (), {}, lambda: 0]:
77+
self.assertRaises(ValueError, vdf.parse, t)
78+
79+
def test_parse_file_source(self):
80+
self.assertEqual(vdf.parse(StringIO(" ")), {})
81+
82+
83+
class testcase_VDF(unittest.TestCase):
84+
def test_format(self):
85+
tests = [
86+
# empty test
87+
['', {}],
88+
89+
# simple key and values
90+
[
91+
{'1': '1'},
92+
'"1" "1"\n'
93+
],
94+
[
95+
{"a": "1", "b": "2"},
96+
'"a" "1"\n"b" "2"\n'
97+
],
98+
99+
# nesting
100+
[
101+
{"a": {"b": {"c": {"d": "1", "e": "2"}}}},
102+
'"a"\n{\n"b"\n{\n"c"\n{\n"e" "2"\n"d" "1"\n}\n}\n}\n'
103+
],
104+
[
105+
'"a"\n{\n"b"\n{\n"c"\n{\n"e" "2"\n"d" "1"\n}\n}\n}\n"b" "2"\n',
106+
{"a": {"b": {"c": {"d": "1", "e": "2"}}}, "b": "2"}
107+
],
108+
109+
# ignoring comment lines
110+
[
111+
"//comment text\n//comment",
112+
{}
113+
],
114+
[
115+
"//comment text\n//comment",
116+
{}
117+
],
118+
[
119+
'"a" "b" //comment text',
120+
{"a": "b"}],
121+
[
122+
'//comment\n"a" "1"\n"b" "2" //comment',
123+
{"a": "1", "b": "2"}
124+
],
125+
[
126+
'"a"\n{//comment\n}//comment',
127+
{"a": {}}
128+
],
129+
[
130+
'"a" //comment\n{\n}',
131+
{"a": {}}
132+
],
133+
134+
135+
# new lines in value
136+
[
137+
r'"a" "xx\"xxx"',
138+
{"a": r'xx\"xxx'}
139+
],
140+
[
141+
'"a" "xx\\"\nxxx"',
142+
{"a": 'xx\\"\nxxx'}
143+
],
144+
[
145+
'"a" "\n\n\n\n"',
146+
{"a": '\n\n\n\n'}
147+
],
148+
]
149+
150+
for test, expected in tests:
151+
if isinstance(test, dict):
152+
self.assertEqual(vdf.dumps(test), expected)
153+
else:
154+
self.assertEqual(vdf.loads(test), expected)
155+
156+
def test_parse_exceptions(self):
157+
tests = [
158+
159+
# expect bracket - invalid syntax
160+
'"asd"\n"zxc" "333"\n"',
161+
162+
# invalid syntax
163+
'"asd" "123"\n"zxc" "333"\n"',
164+
165+
# unclosed parenthasis
166+
'"asd"\n{\n"zxc" "333"\n'
167+
]
168+
169+
for test in tests:
170+
self.assertRaises(SyntaxError, vdf.parse, test)

0 commit comments

Comments
 (0)