Commit 8ac37474 authored by Matěj Cepl's avatar Matěj Cepl

All tests succeeding!!!

parent d9f2aa1f
#!/usr/bin/python
from __future__ import absolute_import, print_function, unicode_literals
import yaml
inobj = {
"date": "2001-01-23",
"state": "MI",
"quantity": 4
}
print("type inobj = %s" % type(inobj))
print(inobj)
res = yaml.dump(inobj, encoding="utf-8", default_flow_style=False,
default_style=False, canonical=False, Dumper=yaml.SafeDumper)
print("'%s'" % res)
# -*- coding: utf-8 -*- IGNORE:C0111 # -*- coding: utf-8 -*- IGNORE:C0111
from __future__ import absolute_import, print_function, unicode_literals from __future__ import absolute_import, print_function, unicode_literals
import logging import logging
from test import test_reader, test_input
import yamlish import yamlish
import unittest import unittest2 as unittest
import yaml import yaml
import tempfile
import textwrap
from textwrap import dedent
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
INPUT = 1
OUTPUT = 2
def _generate_test_name(source): def _generate_test_name(source):
""" """
Clean up human-friendly test name into a method name. Clean up human-friendly test name into a method name.
""" """
out = source.replace(' ', '_').replace(':', '').lower() out = source.replace(' ', '_').replace(':', '').replace(',', '').lower()
return "test_%s" % out return "test_%s" % out
def _create_test(test_src, tested_function): def _create_input_test(test_src, tested_function):
""" """
Decorate tested function to be used as a method for TestCase. Decorate tested function to be used as a method for TestCase.
""" """
...@@ -22,6 +27,7 @@ def _create_test(test_src, tested_function): ...@@ -22,6 +27,7 @@ def _create_test(test_src, tested_function):
""" """
Execute a test by calling a tested_function on test_src data. Execute a test by calling a tested_function on test_src data.
""" """
self.maxDiff = None
if ('skip' in test_src) and test_src['skip']: if ('skip' in test_src) and test_src['skip']:
logging.info("test_src skipped!") logging.info("test_src skipped!")
return return
...@@ -43,7 +49,41 @@ def _create_test(test_src, tested_function): ...@@ -43,7 +49,41 @@ def _create_test(test_src, tested_function):
return do_test_expected return do_test_expected
def generate_testsuite(test_data, test_case_shell, test_fce): def _create_output_test(test_src, tested_function):
"""
Decorate tested function to be used as a method for TestCase.
"""
def do_test_expected(self):
"""
Execute a test by calling a tested_function on test_src data.
"""
self.maxDiff = None
if ('skip' in test_src) and test_src['skip']:
logging.info("test_src skipped!")
return
# We currently don't throw any exceptions in Writer, so this
# this is always false
if 'error' in test_src:
self.assertRaises(test_src['error'], yamlish.dumps, test_src['in'])
else:
logging.debug("out:\n%s", textwrap.dedent(test_src['out']))
want = yaml.load(textwrap.dedent(test_src['out']))
logging.debug("want:\n%s", want)
with tempfile.NamedTemporaryFile(delete=False) as test_file:
tested_function(test_src['in'], test_file)
test_file.seek(0)
got_str = test_file.read()
logging.debug("got_str = %s", got_str)
got = yaml.load(got_str)
self.assertEqual(got, want, "Result matches")
return do_test_expected
def generate_testsuite(test_data, test_case_shell, test_fce, direction=INPUT):
""" """
Generate tests from the test data, class to build upon and function to use for testing. Generate tests from the test data, class to build upon and function to use for testing.
""" """
...@@ -52,6 +92,9 @@ def generate_testsuite(test_data, test_case_shell, test_fce): ...@@ -52,6 +92,9 @@ def generate_testsuite(test_data, test_case_shell, test_fce):
logging.info("test %s skipped!", in_test['name']) logging.info("test %s skipped!", in_test['name'])
continue continue
name = _generate_test_name(in_test['name']) name = _generate_test_name(in_test['name'])
test_method = _create_test (in_test, test_fce) if direction == INPUT:
test_method = _create_input_test (in_test, test_fce)
elif direction == OUTPUT:
test_method = _create_output_test(in_test, test_fce)
test_method.__name__ = str('test_%s' % name) # IGNORE:W0622 test_method.__name__ = str('test_%s' % name) # IGNORE:W0622
setattr (test_case_shell, test_method.__name__, test_method) setattr (test_case_shell, test_method.__name__, test_method)
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals from __future__ import absolute_import, print_function, unicode_literals
import test import test
import unittest import unittest2 as unittest
import yamlish import yamlish
test_data_list = [ test_data_list = [
{ {
"name": "Input test", "name": "Input test",
"in": """--- "in": r"""---
bill-to: bill-to:
address: address:
city: "Royal Oak" city: "Royal Oak"
lines: "458 Walkman Dr.\\nSuite #292\\n" lines: "458 Walkman Dr.\nSuite #292\n"
postal: 48046 postal: 48046
state: MI state: MI
family: Dumars family: Dumars
given: Chris given: Chris
comments: "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338\\n" comments: "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338\n"
date: 2001-01-23 date: 2001-01-23
invoice: 34843 invoice: 34843
product: product:
...@@ -72,6 +72,7 @@ total: 4443.52 ...@@ -72,6 +72,7 @@ total: 4443.52
class TestInput(unittest.TestCase): # IGNORE:C0111 class TestInput(unittest.TestCase): # IGNORE:C0111
pass pass
test.generate_testsuite(test_data_list, TestInput, yamlish.load)
if __name__ == "__main__": if __name__ == "__main__":
test.generate_testsuite(test_data_list, TestInput, yamlish.load)
unittest.main() unittest.main()
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import unittest from __future__ import absolute_import, print_function, unicode_literals
import unittest2 as unittest
class TestBasics(unittest.TestCase): class TestBasics(unittest.TestCase):
def test_import(self): def test_import(self):
import yamlish import yamlish
from yamlish import Reader #IGNORE:W0612
from yamlish import Writer #IGNORE:W0612
self.assertTrue(yamlish.__version__, self.assertTrue(yamlish.__version__,
"Testing import of yamlish, version %s." % yamlish.__version__) "Testing import of yamlish, version %s." % yamlish.__version__)
......
...@@ -5,39 +5,39 @@ Test general output functionality. ...@@ -5,39 +5,39 @@ Test general output functionality.
Without much stress on the format itself. Without much stress on the format itself.
""" """
from __future__ import absolute_import, print_function, unicode_literals from __future__ import absolute_import, print_function, unicode_literals
from StringIO import StringIO import unittest2 as unittest
import re
import unittest
import yamlish import yamlish
import yaml
import logging
import tempfile
OUT = [ OUT = """---
"---", bill-to:
"bill-to:", address:
" address:", city: Royal Oak
" city: 'Royal Oak'", lines: "458 Walkman Dr.\\nSuite #292\\n"
" lines: \"458 Walkman Dr.\\nSuite #292\\n\"", postal: 48046
" postal: 48046", state: MI
" state: MI", family: Dumars
" family: Dumars", given: Chris
" given: Chris", comments: Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338
"comments: \"Late afternoon is best. Backup contact is Nancy Billsmer \@ 338-4338\\n\"", date: 2001-01-23
"date: 2001-01-23", invoice: 34843
"invoice: 34843", product:
"product:", -
" -", description: Basketball
" description: Basketball", price: 450.00
" price: 450.00", quantity: 4
" quantity: 4", sku: BL394D
" sku: BL394D", -
" -", description: Super Hoop
" description: 'Super Hoop'", price: 2392.00
" price: 2392.00", quantity: 1
" quantity: 1", sku: BL4438H
" sku: BL4438H", tax: 251.42
"tax: 251.42", total: 4443.52
"total: 4443.52", ...
"...", """
]
IN = { IN = {
'bill-to': { 'bill-to': {
...@@ -45,7 +45,7 @@ IN = { ...@@ -45,7 +45,7 @@ IN = {
'address': { 'address': {
'city': 'Royal Oak', 'city': 'Royal Oak',
'postal': 48046, 'postal': 48046,
'lines': "458 Walkman Dr.\\nSuite #292\\n", 'lines': "458 Walkman Dr.\nSuite #292\n",
'state': 'MI' 'state': 'MI'
}, },
'family': 'Dumars' 'family': 'Dumars'
...@@ -67,67 +67,39 @@ IN = { ...@@ -67,67 +67,39 @@ IN = {
'description': 'Super Hoop' 'description': 'Super Hoop'
} }
], ],
'comments': "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338\\n", 'comments': "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338",
'total': 4443.52 'total': 4443.52
} }
buf1 = []
buf2 = []
buf3 = ""
destination = [
{
"name": 'Array reference',
"destination": buf1,
"normalise": (lambda : buf1),
},
# {
# "name": 'Closure',
# "destination": sub { push @buf2, shift },
# "normalise": sub { return \@buf2 },
# },
{
"name": 'Scalar',
"destination": buf3,
"normalise": (lambda : re.split(r" \n ", buf3))
}
]
class TestOuptut(unittest.TestCase): class TestOuptut(unittest.TestCase):
def setUp(self): def setUp(self):
""" """
Transform expected list into string which we actually use. Transform expected list into string which we actually use.
""" """
self._expected = "" self._expected = yaml.safe_load(OUT)
for line in OUT:
self._expected += line + "\n"
def notest_file_output(self): def test_file_output(self):
""" """
Test output to a file. Test output to a file.
""" """
outf = StringIO() outf = tempfile.TemporaryFile()
yamlish.dump(IN, outf) yaml.safe_dump(IN, outf)
outf.seek(0) outf.seek(0)
got = outf.read() got_str = outf.read()
outf.close() outf.close()
self.assertEqual(got, self._expected, """Result matches logging.debug("got_str = %s", got_str)
expected = %s got = yaml.safe_load(got_str)
self.assertEqual(got, self._expected, "Result matches")
observed = %s
""" % (self._expected, got))
def test_string_output(self): def test_string_output(self):
""" """
Test output to a string. Test output to a string.
""" """
got = yamlish.dumps(IN) got_str = yamlish.dumps(IN)
self.assertEqual(got, self._expected, """Result matches got = yaml.load(got_str)
expected = %s self.assertEqual(got, self._expected, "Result matches")
observed = %s
""" % (self._expected, got))
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals
import yaml import yaml
import yamlish import yamlish
import test import test
import unittest import unittest2 as unittest
test_data_list = [ test_data_list = [
{ {
...@@ -42,7 +43,7 @@ test_data_list = [ ...@@ -42,7 +43,7 @@ test_data_list = [
}, },
{ {
"name": 'Mixed array', "name": 'Mixed array',
"in": [ '---', '- 1', "- 'two'", '- "three\n"', '...', ], "in": [ '---', '- 1', "- 'two'", r'- "three\n"', '...', ],
"out": [ 1, 'two', "three\n" ], "out": [ 1, 'two', "three\n" ],
}, },
{ {
...@@ -311,44 +312,42 @@ test_data_list = [ ...@@ -311,44 +312,42 @@ test_data_list = [
}, },
{ {
"name": "Unprintables", "name": "Unprintables",
"skip": True,
"in": [ "in": [
"---", "- \"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\a\\x08\\t\\n\\v\\f\\r\\x0e\\x0f\"",
"- \"\\z\\x01\\x02\\x03\\x04\\x05\\x06\\a\\x08\\t\\n\\v\\f\\r\\x0e\\x0f\"",
"- \"\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a\\e\\x1c\\x1d\\x1e\\x1f\"", "- \"\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a\\e\\x1c\\x1d\\x1e\\x1f\"",
"- \" !\\\"#\$%&'()*+,-./\"", "- \" !\\\"#$%&'()*+,-./\"",
"- 0123456789:;<=>?", "- 0123456789:;<=>?",
"- '\@ABCDEFGHIJKLMNO'", "- '\@ABCDEFGHIJKLMNO'",
"- 'PQRSTUVWXYZ[\\]^_'", "- 'PQRSTUVWXYZ[\\]^_'",
"- '`abcdefghijklmno'", "- '`abcdefghijklmno'",
"- 'pqrstuvwxyz{|}~\177'", r"- 'pqrstuvwxyz{|}~\177'",
"- \200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217", "- \\200\\201\\202\\203\\204\\205\\206\\207\\210\\211\\212\\213\\214\\215\\216\\217",
"- \220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237", "- \\220\\221\\222\\223\\224\\225\\226\\227\\230\\231\\232\\233\\234\\235\\236\\237",
"- \240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257", "- \\240\\241\\242\\243\\244\\245\\246\\247\\250\\251\\252\\253\\254\\255\\256\\257",
"- \260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277", "- \\260\\261\\262\\263\\264\\265\\266\\267\\270\\271\\272\\273\\274\\275\\276\\277",
"- \300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317", "- \\300\\301\\302\\303\\304\\305\\306\\307\\310\\311\\312\\313\\314\\315\\316\\317",
"- \320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337", "- \\320\\321\\322\\323\\324\\325\\326\\327\\330\\331\\332\\333\\334\\335\\336\\337",
"- \340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357", "- \\340\\341\\342\\343\\344\\345\\346\\347\\350\\351\\352\\353\\354\\355\\356\\357",
"- \360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377", "- \\360\\361\\362\\363\\364\\365\\366\\367\\370\\371\\372\\373\\374\\375\\376\\377",
"..." "..."
], ],
"out": [ "out": [
"\0\1\2\3\4\5\6\a\b\t\n\13\f\r\16\17", "\0\1\2\3\4\5\6\a\b\t\n\13\f\r\16\17",
"\20\21\22\23\24\25\26\27\30\31\32\e\34\35\36\37", "\20\21\22\23\24\25\26\27\30\31\32\33\34\35\36\37",
" !\"#\$%&'()*+,-./", " !\"#$%&'()*+,-./",
"0123456789:;<=>?", "0123456789:;<=>?",
"\@ABCDEFGHIJKLMNO", "\@ABCDEFGHIJKLMNO",
"PQRSTUVWXYZ[\\]^_", "PQRSTUVWXYZ[\\]^_",
"`abcdefghijklmno", "`abcdefghijklmno",
"pqrstuvwxyz{|}~\177", r"pqrstuvwxyz{|}~\177",
"\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217", r"\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217",
"\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237", r"\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237",
"\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257", r"\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257",
"\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277", r"\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277",
"\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317", r"\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317",
"\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337", r"\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337",
"\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357", r"\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357",
"\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377" r"\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377"
] ]
}, },
{ {
...@@ -366,6 +365,7 @@ test_data_list = [ ...@@ -366,6 +365,7 @@ test_data_list = [
class TestReader(unittest.TestCase): # IGNORE:C0111 class TestReader(unittest.TestCase): # IGNORE:C0111
pass pass
test.generate_testsuite(test_data_list, TestReader, yamlish.load)
if __name__ == "__main__": if __name__ == "__main__":
test.generate_testsuite(test_data_list, TestReader, yamlish.load)
unittest.main() unittest.main()
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals from __future__ import absolute_import, print_function, unicode_literals
import unittest import unittest2 as unittest
import test
import yamlish import yamlish
test_data_list = [ test_data_list = [
{ {
"name": 'Simple scalar', "name": 'Simple scalar',
"in": 1, "in": 1,
"out": [ '--- 1', '...', ], "out": """\
--- 1
...
""",
}, },
{ {
"name": 'Undef', "name": 'Undef',
"in": None, "in": None,
"out": [ '--- ~', '...', ], "out": """\
--- ~
...
""",
}, },
{ {
"name": 'Unprintable', "name": 'Unprintable',
"in": "\x01\n\t", "in": "\x01\n\t",
"out": [ '--- "\x01\n\t"', '...', ], "out": """\
---
"\\x01\\n\\t"
...
""",
}, },
{ {
"name": 'Simple array', "name": 'Simple array',
"in": [ 1, 2, 3 ], "in": [ 1, 2, 3 ],
"out": [ '---', '- 1', '- 2', '- 3', '...', ], "out": """\
---
- 1
- 2
- 3
...
""",
}, },
{ {
"name": 'Array, two elements, None', "name": 'Array, two elements, None',
"in": [ None, None ], "in": [ None, None ],
"out": [ '---', '- ~', '- ~', '...', ], "out": """\
---
- ~
- ~
...
""",
}, },
{ {
"name": 'Nested array', "name": 'Nested array',
"in": [ 1, 2, [ 3, 4 ], 5 ], "in": [ 1, 2, [ 3, 4 ], 5 ],
"out": "out": """\
[ '---', '- 1', '- 2', '-', ' - 3', ' - 4', '- 5', '...', ], ---
- 1
- 2
-
- 3
- 4
- 5
...
""",
}, },
{ {
"name": 'Simple hash', "name": 'Simple hash',
"in": { "one": '1', "two": '2', "three": '3' }, "in": { "one": 1, "two": 2, "three": 3 },
"out": [ '---', 'one: 1', 'three: 3', 'two: 2', '...', ], "out": """\
---
one: 1
three: 3
two: 2
...
""",
}, },
{ {
"name": 'Nested hash', "name": 'Nested hash',
"in": { "in": {
"one": '1', "one": 1,
"two": '2', "two": 2,
"more": { "three": '3', "four": '4' } "more": { "three": 3, "four": 4 }
}, },
"out": [ "out": """\
'---', ---
'more:', more:
' four: 4', four: 4
' three: 3', three: 3
'one: 1', one: 1
'two: 2', two: 2
'...', ...
], """,
}, },
{ {
"name": 'Unprintable key', "name": 'Unprintable key',
"in": { "one": '1', "\x02": '2', "three": '3' }, "in": { "one": 1, "\x02": 2, "three": 3 },
"out": [ '---', '"\x02": 2', 'one: 1', 'three: 3', '...', ], "out": """\
---
"\\x02": 2
one: 1
three: 3
...
""",
}, },
{ {
"name": 'Empty key', "name": 'Empty key',
"in": { '': 'empty' }, "in": { '': 'empty' },
"out": [ '---', "'': empty", '...', ], "out": """\
---
'': empty
...
""",
}, },
{ {
"name": 'Empty value', "name": 'Empty value',
"in": { '': '' }, "in": { '': '' },
"out": [ '---', "'': ''", '...', ], "out": """\
---
'': ''
...
""",
}, },
{ {
"name": 'Complex', "name": 'Complex',
...@@ -80,90 +130,68 @@ test_data_list = [ ...@@ -80,90 +130,68 @@ test_data_list = [
'given': 'Chris', 'given': 'Chris',
'address': { 'address': {
'city': 'Royal Oak', 'city': 'Royal Oak',
'postal': '48046', 'postal': 48046,
'lines': "458 Walkman Dr.\nSuite #292\n", 'lines': "458 Walkman Dr.\nSuite #292\n",
'state': 'MI' 'state': 'MI'
}, },
'family': 'Dumars' 'family': 'Dumars'
}, },
'invoice': '34843', 'invoice': 34843,
'date': '2001-01-23', 'date': '2001-01-23',
'tax': '251.42', 'tax': 251.42,
'product': [ 'product': [
{ {
'sku': 'BL394D', 'sku': 'BL394D',
'quantity': '4', 'quantity': 4,
'price': '450.00', 'price': 450.00,
'description': 'Basketball' 'description': 'Basketball'
}, },
{ {
'sku': 'BL4438H', 'sku': 'BL4438H',
'quantity': '1', 'quantity': 1,
'price': '2392.00', 'price': 2392.00,
'description': 'Super Hoop' 'description': 'Super Hoop'
} }
], ],