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
from __future__ import absolute_import, print_function, unicode_literals
import logging
from test import test_reader, test_input
import yamlish
import unittest
import unittest2 as unittest
import yaml
import tempfile
import textwrap
from textwrap import dedent
logging.basicConfig(level=logging.INFO)
INPUT = 1
OUTPUT = 2
def _generate_test_name(source):
"""
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
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.
"""
......@@ -22,6 +27,7 @@ def _create_test(test_src, tested_function):
"""
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
......@@ -43,7 +49,41 @@ def _create_test(test_src, tested_function):
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.
"""
......@@ -52,6 +92,9 @@ def generate_testsuite(test_data, test_case_shell, test_fce):
logging.info("test %s skipped!", in_test['name'])
continue
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
setattr (test_case_shell, test_method.__name__, test_method)
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals
import test
import unittest
import unittest2 as unittest
import yamlish
test_data_list = [
{
"name": "Input test",
"in": """---
"in": r"""---
bill-to:
address:
city: "Royal Oak"
lines: "458 Walkman Dr.\\nSuite #292\\n"
lines: "458 Walkman Dr.\nSuite #292\n"
postal: 48046
state: MI
family: Dumars
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
invoice: 34843
product:
......@@ -72,6 +72,7 @@ total: 4443.52
class TestInput(unittest.TestCase): # IGNORE:C0111
pass
test.generate_testsuite(test_data_list, TestInput, yamlish.load)
if __name__ == "__main__":
test.generate_testsuite(test_data_list, TestInput, yamlish.load)
unittest.main()
# -*- coding: utf-8 -*-
import unittest
from __future__ import absolute_import, print_function, unicode_literals
import unittest2 as unittest
class TestBasics(unittest.TestCase):
def test_import(self):
import yamlish
from yamlish import Reader #IGNORE:W0612
from yamlish import Writer #IGNORE:W0612
self.assertTrue(yamlish.__version__,
"Testing import of yamlish, version %s." % yamlish.__version__)
......
......@@ -5,39 +5,39 @@ Test general output functionality.
Without much stress on the format itself.
"""
from __future__ import absolute_import, print_function, unicode_literals
from StringIO import StringIO
import re
import unittest
import unittest2 as unittest
import yamlish
import yaml
import logging
import tempfile
OUT = [
"---",
"bill-to:",
" address:",
" city: 'Royal Oak'",
" lines: \"458 Walkman Dr.\\nSuite #292\\n\"",
" postal: 48046",
" state: MI",
" family: Dumars",
" given: Chris",
"comments: \"Late afternoon is best. Backup contact is Nancy Billsmer \@ 338-4338\\n\"",
"date: 2001-01-23",
"invoice: 34843",
"product:",
" -",
" description: Basketball",
" price: 450.00",
" quantity: 4",
" sku: BL394D",
" -",
" description: 'Super Hoop'",
" price: 2392.00",
" quantity: 1",
" sku: BL4438H",
"tax: 251.42",
"total: 4443.52",
"...",
]
OUT = """---
bill-to:
address:
city: Royal Oak
lines: "458 Walkman Dr.\\nSuite #292\\n"
postal: 48046
state: MI
family: Dumars
given: Chris
comments: Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338
date: 2001-01-23
invoice: 34843
product:
-
description: Basketball
price: 450.00
quantity: 4
sku: BL394D
-
description: Super Hoop
price: 2392.00
quantity: 1
sku: BL4438H
tax: 251.42
total: 4443.52
...
"""
IN = {
'bill-to': {
......@@ -45,7 +45,7 @@ IN = {
'address': {
'city': 'Royal Oak',
'postal': 48046,
'lines': "458 Walkman Dr.\\nSuite #292\\n",
'lines': "458 Walkman Dr.\nSuite #292\n",
'state': 'MI'
},
'family': 'Dumars'
......@@ -67,67 +67,39 @@ IN = {
'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
}
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):
def setUp(self):
"""
Transform expected list into string which we actually use.
"""
self._expected = ""
for line in OUT:
self._expected += line + "\n"
self._expected = yaml.safe_load(OUT)
def notest_file_output(self):
def test_file_output(self):
"""
Test output to a file.
"""
outf = StringIO()
yamlish.dump(IN, outf)
outf = tempfile.TemporaryFile()
yaml.safe_dump(IN, outf)
outf.seek(0)
got = outf.read()
got_str = outf.read()
outf.close()
self.assertEqual(got, self._expected, """Result matches
expected = %s
logging.debug("got_str = %s", got_str)
got = yaml.safe_load(got_str)
self.assertEqual(got, self._expected, "Result matches")
observed = %s
""" % (self._expected, got))
def test_string_output(self):
"""
Test output to a string.
"""
got = yamlish.dumps(IN)
self.assertEqual(got, self._expected, """Result matches
expected = %s
observed = %s
""" % (self._expected, got))
got_str = yamlish.dumps(IN)
got = yaml.load(got_str)
self.assertEqual(got, self._expected, "Result matches")
if __name__ == "__main__":
unittest.main()
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals
import yaml
import yamlish
import test
import unittest
import unittest2 as unittest
test_data_list = [
{
......@@ -42,7 +43,7 @@ test_data_list = [
},
{
"name": 'Mixed array',
"in": [ '---', '- 1', "- 'two'", '- "three\n"', '...', ],
"in": [ '---', '- 1', "- 'two'", r'- "three\n"', '...', ],
"out": [ 1, 'two', "three\n" ],
},
{
......@@ -311,44 +312,42 @@ test_data_list = [
},
{
"name": "Unprintables",
"skip": True,
"in": [
"---",
"- \"\\z\\x01\\x02\\x03\\x04\\x05\\x06\\a\\x08\\t\\n\\v\\f\\r\\x0e\\x0f\"",
"- \"\\x00\\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\"",
"- \" !\\\"#\$%&'()*+,-./\"",
"- \" !\\\"#$%&'()*+,-./\"",
"- 0123456789:;<=>?",
"- '\@ABCDEFGHIJKLMNO'",
"- 'PQRSTUVWXYZ[\\]^_'",
"- '`abcdefghijklmno'",
"- 'pqrstuvwxyz{|}~\177'",
"- \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",
"- \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",
"- \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",
"- \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"- 'pqrstuvwxyz{|}~\177'",
"- \\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",
"- \\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",
"- \\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",
"- \\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",
"..."
],
"out": [
"\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:;<=>?",
"\@ABCDEFGHIJKLMNO",
"PQRSTUVWXYZ[\\]^_",
"`abcdefghijklmno",
"pqrstuvwxyz{|}~\177",
"\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",
"\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",
"\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",
"\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"pqrstuvwxyz{|}~\177",
r"\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217",
r"\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237",
r"\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257",
r"\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277",
r"\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317",
r"\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337",
r"\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357",
r"\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377"
]
},
{
......@@ -366,6 +365,7 @@ test_data_list = [
class TestReader(unittest.TestCase): # IGNORE:C0111
pass
test.generate_testsuite(test_data_list, TestReader, yamlish.load)
if __name__ == "__main__":
test.generate_testsuite(test_data_list, TestReader, yamlish.load)
unittest.main()
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals
import unittest
import unittest2 as unittest
import test
import yamlish
test_data_list = [
{
"name": 'Simple scalar',
"in": 1,
"out": [ '--- 1', '...', ],
"out": """\
--- 1
...
""",
},
{
"name": 'Undef',
"in": None,
"out": [ '--- ~', '...', ],
"out": """\
--- ~
...
""",
},
{
"name": 'Unprintable',
"in": "\x01\n\t",
"out": [ '--- "\x01\n\t"', '...', ],
"out": """\
---
"\\x01\\n\\t"
...
""",
},
{
"name": 'Simple array',
"in": [ 1, 2, 3 ],
"out": [ '---', '- 1', '- 2', '- 3', '...', ],
"out": """\
---
- 1
- 2
- 3
...
""",
},
{
"name": 'Array, two elements, None',
"in": [ None, None ],
"out": [ '---', '- ~', '- ~', '...', ],
"out": """\
---
- ~
- ~
...
""",
},
{
"name": 'Nested array',
"in": [ 1, 2, [ 3, 4 ], 5 ],
"out":
[ '---', '- 1', '- 2', '-', ' - 3', ' - 4', '- 5', '...', ],
"out": """\
---
- 1
- 2
-
- 3
- 4
- 5
...
""",
},
{
"name": 'Simple hash',
"in": { "one": '1', "two": '2', "three": '3' },
"out": [ '---', 'one: 1', 'three: 3', 'two: 2', '...', ],
"in": { "one": 1, "two": 2, "three": 3 },
"out": """\
---
one: 1
three: 3
two: 2
...
""",
},
{
"name": 'Nested hash',
"in": {
"one": '1',
"two": '2',
"more": { "three": '3', "four": '4' }
},
"out": [
'---',
'more:',
' four: 4',
' three: 3',
'one: 1',
'two: 2',
'...',
],
"one": 1,
"two": 2,
"more": { "three": 3, "four": 4 }
},
"out": """\
---
more:
four: 4
three: 3
one: 1
two: 2
...
""",
},
{
"name": 'Unprintable key',
"in": { "one": '1', "\x02": '2', "three": '3' },
"out": [ '---', '"\x02": 2', 'one: 1', 'three: 3', '...', ],
"in": { "one": 1, "\x02": 2, "three": 3 },
"out": """\
---
"\\x02": 2
one: 1
three: 3
...
""",
},
{
"name": 'Empty key',
"in": { '': 'empty' },
"out": [ '---', "'': empty", '...', ],
"out": """\
---
'': empty
...
""",
},
{
"name": 'Empty value',
"in": { '': '' },
"out": [ '---', "'': ''", '...', ],
"out": """\
---
'': ''
...
""",
},
{
"name": 'Complex',
......@@ -80,90 +130,68 @@ test_data_list = [
'given': 'Chris',
'address': {
'city': 'Royal Oak',
'postal': '48046',
'postal': 48046,
'lines': "458 Walkman Dr.\nSuite #292\n",
'state': 'MI'
},
'family': 'Dumars'
},
'invoice': '34843',
'invoice': 34843,
'date': '2001-01-23',
'tax': '251.42',
'tax': 251.42,
'product': [
{
'sku': 'BL394D',
'quantity': '4',
'price': '450.00',
'quantity': 4,
'price': 450.00,
'description': 'Basketball'
},
{
'sku': 'BL4438H',
'quantity': '1',
'price': '2392.00',
'quantity': 1,
'price': 2392.00,
'description': 'Super Hoop'
}
],
'comments':
"Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338\n",
'total': '4443.52'
},
"out": [
"---",
"bill-to:",
" address:",
" city: 'Royal Oak'",
" lines: \"458 Walkman Dr.\\nSuite #292\\n\"",
" postal: 48046",
" state: MI",
" family: Dumars",
" given: Chris",
"comments: \"Late afternoon is best. Backup contact is Nancy Billsmer \@ 338-4338\\n\"",
"date: 2001-01-23",
"invoice: 34843",
"product:",
" -",
" description: Basketball",
" price: 450.00",
" quantity: 4",
" sku: BL394D",
" -",
" description: 'Super Hoop'",
" price: 2392.00",
" quantity: 1",
" sku: BL4438H",
"tax: 251.42",
"total: 4443.52",
"...",
],
'total': 4443.52
},
"out": r"""
---
bill-to:
address:
city: 'Royal Oak'
lines: "458 Walkman Dr.\nSuite #292\n"
postal: 48046
state: MI
family: Dumars
given: Chris
comments: "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338\n"
date: 2001-01-23
invoice: 34843
product:
-
description: Basketball
price: 450.00
quantity: 4
sku: BL394D
-
description: 'Super Hoop'
price: 2392.00
quantity: 1
sku: BL4438H
tax: 251.42
total: 4443.52
...
""",
},
]
# plan(tests = len(test_data_list) * 5)
class TestWriter(unittest.TestCase):
def test_writer(self):
for test in test_data_list:
name = test['name']
data = test['in']
got = ""
# We currently don't throw any exceptions in Writer, so this
# this is always false
if 'error' in test:
self.assertRaises(test['error'], yamlish.dumps, test['in'])
else:
want = test['out']
yamlish.dump(test['in'], got)
self.assertEqual(got, want, """%s: Result matches
expected = %s
observed = %s
""" % (name, want, got))
class TestWriter(unittest.TestCase): # IGNORE:C0111
pass
# Now try parsing it
parsed = yamlish.load(got) # FIXME got has an iterator
self.assertEqual(parsed, data, """%s: Reparse OK
expected = %s
test.generate_testsuite(test_data_list, TestWriter, yamlish.dump, direction=test.OUTPUT)
observed = %s
""" % (name, data, parsed))
if __name__ == "__main__":
unittest.main()
......@@ -172,10 +172,12 @@ def dump(source, destination):
if isinstance(destination, (str, unicode)):
with open(destination, "w") as outf:
dump(source, outf)
elif isinstance(destination, file):
elif getattr(destination, "file"):
yaml.dump(source, destination, encoding="utf-8",
default_flow_style=False, canonical=False,
Dumper=yaml.SafeDumper)
else:
raise NameError
def dumps(source):
"""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment