diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/__init__.py | 81 | ||||
-rw-r--r-- | test/all_tests.py | 16 | ||||
-rw-r--r-- | test/test_input.py | 157 | ||||
-rw-r--r-- | test/test_load.py | 8 | ||||
-rw-r--r-- | test/test_output.py | 10 | ||||
-rw-r--r-- | test/test_reader.py | 212 | ||||
-rw-r--r-- | test/test_writer.py | 40 |
7 files changed, 222 insertions, 302 deletions
diff --git a/test/__init__.py b/test/__init__.py index 9f3cdc6..1f74147 100644 --- a/test/__init__.py +++ b/test/__init__.py @@ -1,20 +1,61 @@ -def TODO(func): - """unittest test method decorator that ignores - exceptions raised by test - - Used to annotate test methods for code that may - not be written yet. Ignores failures in the - annotated test method; fails if the text - unexpectedly succeeds. - """ - def wrapper(*args, **kw): - try: - func(*args, **kw) - succeeded = True - except: - succeeded = False - assert succeeded is False, \ - "%s marked TODO but passed" % func.__name__ - wrapper.__name__ = func.__name__ - wrapper.__doc__ = func.__doc__ - return wrapper +# -*- coding: utf-8 -*- +from __future__ import absolute_import, print_function, unicode_literals +import logging +from test import test_reader, test_input +import yamlish +import unittest +import yaml +logging.basicConfig(level=logging.DEBUG) + +def generate_test_name(source): + out = source.replace(' ', '_').replace(':', '').lower() + return "test_%s" % out + +def create_test(test_src, tested_function): + def do_test_expected(self): + #self.assertEqual(under_test(pair[0]), pair[1]) + if ('skip' in test_src) and test_src['skip']: + logging.info("test_src skipped!") + return + + # rather keep original tests in lists even though we could + # do multiline strings + source = "\n".join(test_src['in']) + "\n" + logging.debug("source = %s", source) + + got = "" + if 'error' in test_src: + self.assertRaises(test_src['error'], tested_function, test_src['in']) + else: + want = test_src['out'] + got = tested_function(test_src['in']) + logging.debug("test_src['out'] = %s", unicode(test_src['out'])) + self.assertEqual(got, want, """Result matches + expected = %s + + observed = %s + """ % (want, got)) + + return do_test_expected + + +def generate_testsuite(test_data, test_case_shell, test_fce): + for in_test in test_data: + if ('skip' in in_test) and in_test['skip']: + logging.info("test %s skipped!", in_test['name']) + continue + name = generate_test_name(in_test['name']) + test_method = create_test (in_test, test_fce) + test_method.__name__ = str('test_%s' % name) + setattr (test_case_shell, test_method.__name__, test_method) + +class TestInput(unittest.TestCase): + pass + +class TestReader(unittest.TestCase): + pass + +if __name__ == "__main__": + generate_testsuite(test_reader.test_data_list, TestReader, yamlish.load) + generate_testsuite(test_input.test_data_list, TestInput, yamlish.load) + unittest.main() diff --git a/test/all_tests.py b/test/all_tests.py index 4a1a532..66e069b 100644 --- a/test/all_tests.py +++ b/test/all_tests.py @@ -1,16 +1,22 @@ +import sys +import os.path +sys.path.insert(0, os.path.realpath(os.path.dirname(__file__) + "/..")) + +import logging +logging.basicConfig(level=logging.INFO) import unittest import test_load import test_input -#import test_reader +import test_reader import test_output -#import test_writer +import test_writer if __name__ == "__main__": loader = unittest.TestLoader() suite = loader.loadTestsFromModule(test_load) - suite.addTests(loader.loadTestsFromModule(test_input)) - #suite.addTests(loader.loadTestsFromModule(test_reader)) - suite.addTests(loader.loadTestsFromModule(test_output)) + #suite.addTests(loader.loadTestsFromModule(test_input)) + suite.addTests(loader.loadTestsFromModule(test_reader)) + #suite.addTests(loader.loadTestsFromModule(test_output)) #suite.addTests(loader.loadTestsFromModule(test_writer)) runner = unittest.TextTestRunner(verbosity=2) diff --git a/test/test_input.py b/test/test_input.py index d221830..d950f86 100644 --- a/test/test_input.py +++ b/test/test_input.py @@ -1,105 +1,66 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import, print_function, unicode_literals -import logging -import unittest -import yamlish -logging.basicConfig(format='%(levelname)s:%(funcName)s:%(message)s', - level=logging.INFO) - -IN = """ - --- - 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': { - 'given': 'Chris', - 'address': { - 'city': 'Royal Oak', - 'postal': '48046', - 'lines': "458 Walkman Dr.\nSuite #292\n", - 'state': 'MI' - }, - 'family': 'Dumars' - }, - 'invoice': '34843', - 'date': '2001-01-23', - 'tax': '251.42', - 'product': [ +test_data_list = [ { - 'sku': 'BL394D', - 'quantity': '4', - 'price': '450.00', - 'description': 'Basketball' - }, - { - 'sku': 'BL4438H', - 'quantity': '1', - 'price': '2392.00', - 'description': 'Super Hoop' - } - ], - 'comments': - "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338\n", - 'total': '4443.52' -} - -class TestInput(unittest.TestCase): - """FIXME description of this class""" - def test_reader(self): - scalar = IN - source = [ + "name": "Input test", + "in": """--- +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': { + 'given': 'Chris', + 'address': { + 'city': 'Royal Oak', + 'postal': 48046, + 'lines': "458 Walkman Dr.\nSuite #292\n", + 'state': 'MI' + }, + 'family': 'Dumars' + }, + 'invoice': 34843, + 'date': '2001-01-23', + 'tax': 251.42, + 'product': [ { - "name": 'Array reference', - "source": IN.split("\n"), + 'sku': 'BL394D', + 'quantity': 4, + 'price': 450.00, + 'description': 'Basketball' }, -# { -# "name": 'Closure', -# "source": sub { shift @lines }, -# }, { - "name": 'Scalar', - "source": IN, + 'sku': 'BL4438H', + 'quantity': 1, + 'price': 2392.00, + 'description': 'Super Hoop' } - ] - - for src in source: - name = src['name'] - yaml = yamlish.Reader() - self.assert_(True, "%s: Created" % name) - self.assert_(isinstance(yaml, yamlish.Reader)) - - #my $got = eval { $yaml -> read($src -> {source}) }; - try: - got = yaml.read(src['source']) - except IOError: # FIXME not sure which one - raise - self.assertEqual(got, OUT, """%s: Result matches - expected = %s - - observed = %s - """ % (name, OUT, got)) + ], + 'comments': + "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338\n", + 'total': 4443.52 + } + } +] diff --git a/test/test_load.py b/test/test_load.py index 852dd29..a86364b 100644 --- a/test/test_load.py +++ b/test/test_load.py @@ -4,11 +4,9 @@ import unittest class TestBasics(unittest.TestCase): def test_import(self): import yamlish - from yamlish import Reader - self.assert_(True, "Importing Reader.") - from yamlish import Writer - self.assert_(True, "Importing Writer.") - self.assert_(True, + from yamlish import Reader #IGNORE:W0612 + from yamlish import Writer #IGNORE:W0612 + self.assertTrue(yamlish.__version__, "Testing import of yamlish, version %s." % yamlish.__version__) if __name__ == "__main__": diff --git a/test/test_output.py b/test/test_output.py index a8f5a43..ea8d841 100644 --- a/test/test_output.py +++ b/test/test_output.py @@ -3,7 +3,7 @@ from __future__ import absolute_import, print_function, unicode_literals import re import unittest import yamlish -from . import TODO +import yaml OUT = [ "---", @@ -74,7 +74,7 @@ destination = [ { "name": 'Array reference', "destination": buf1, - "normalise": (lambda x: buf1), + "normalise": (lambda : buf1), }, # { # "name": 'Closure', @@ -89,15 +89,11 @@ destination = [ ] class TestOuptut(unittest.TestCase): - @TODO def test_output(self): for dest in destination: name = dest['name'] - yaml = yamlish.Writer() - self.assert_(True, "%s: Created" % name) - self.assert_(isinstance(yaml, yamlish.Writer)) - yaml.write(IN, dest[destination]) + yaml.dump(IN, dest) got = dest['normalise']() self.assertEqual(got, OUT, """%s: Result matches diff --git a/test/test_reader.py b/test/test_reader.py index 1694abe..e8d39d1 100644 --- a/test/test_reader.py +++ b/test/test_reader.py @@ -1,10 +1,7 @@ # -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, unicode_literals -import re -import unittest -import yamlish +import yaml -SCHEDULE = [ +test_data_list = [ { "name": 'Hello World', "in": [ '--- Hello, World', '...', ], @@ -26,45 +23,45 @@ SCHEDULE = [ "out": "Hello, World", }, { - "name": 'Hello World 4', - "in": [ '--- >', ' Hello,', ' World', '...', ], + "name": 'Hello World 5', + "in": [ '--- >', ' Hello,', ' World', '...', ], "out": "Hello, World\n", }, { - "name": 'Hello World 5', - "in": [ '--- >', ' Hello,', ' World', '...', ], - "error": re.compile(r"Missing\s+'[.][.][.]'"), + "name": 'Hello World 6', + "in": [ '--- >', ' Hello,', ' World', '...', ], + "error": yaml.parser.ParserError, }, { "name": 'Simple array', "in": [ '---', '- 1', '- 2', '- 3', '...', ], - "out": [ '1', '2', '3' ], + "out": [ 1, 2, 3 ], }, { "name": 'Mixed array', - "in": [ '---', '- 1', '- \'two\'', '- "three\n"', '...', ], - "out": [ '1', 'two', "three\n" ], + "in": [ '---', '- 1', "- 'two'", r'- "three\n"', '...', ], + "out": [ 1, 'two', "three\n" ], }, { "name": 'Hash in array', - "in": [ '---', '- 1', '- two: 2', '- 3', '...', ], - "out": [ '1', { "two": '2' }, '3' ], + "in": [ '---', ' - 1', ' - two: 2', ' - 3', '...', ], + "out": [ 1, { "two": 2 }, 3 ], }, { "name": 'Hash in array 2', "in": [ '---', '- 1', '- two: 2', ' three: 3', '- 4', '...', ], - "out": [ '1', { "two": '2', "three": '3' }, '4' ], + "out": [ 1, { "two": 2, "three": 3 }, 4 ], }, { "name": 'Nested array', "in": [ '---', '- one', - '-', - ' - two', - ' -', - ' - three', - ' - four', + '- ', + ' - two', + ' - ', + ' - three', + ' - four', '- five', '...', ], @@ -83,8 +80,8 @@ SCHEDULE = [ '...', ], "out": { - "one": { "two": { "three": '3', "four": '4' }, "five": '5' }, - "six": '6' + "one": { "two": { "three": 3, "four": 4 }, "five": 5 }, + "six": 6 }, }, @@ -98,7 +95,7 @@ SCHEDULE = [ ' given : Chris', ' family : Dumars', ' address:', - ' lines: |', + ' lines: | ', ' 458 Walkman Dr.', ' Suite #292', ' city : Royal Oak', @@ -122,36 +119,36 @@ SCHEDULE = [ '...', ], "out": { - "bill - to": { + "bill-to": { "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' + "total": 4443.52 } }, @@ -159,24 +156,25 @@ SCHEDULE = [ { "in": ['...'], "name": 'Regression: empty', - "error": re.compile(r"document\s+header\s+not\s+found") + "error": yaml.parser.ParserError, }, { "in": [ '# comment', '...' ], "name": 'Regression: only_comment', - "error": re.compile(r"document\s+ header\s+ not\s+ found") + "error": yaml.parser.ParserError, }, { + "skip": True, # A corner case, which is apparently not + # clear even from the spec file "out": None, "in": [ '---', '...' ], "name": 'Regression: only_header', - "error": re.compile(r"Premature\s+end", re.I), + "x-error": yaml.parser.ParserError, }, { - "out": None, "in": [ '---', '---', '...' ], "name": 'Regression: two_header', - "error": re.compile(r"Unexpected\s+start", re.I), + "error": yaml.composer.ComposerError, }, { "out": None, @@ -191,7 +189,7 @@ SCHEDULE = [ { "in": [ '--- ~', '---', '...' ], "name": 'Regression: two_undef', - "error": re.compile(r"Missing\s+'[.][.][.]'"), + "error": yaml.composer.ComposerError, }, { "out": 'foo', @@ -206,7 +204,7 @@ SCHEDULE = [ { "in": [ '--- foo', '--- bar', '...' ], "name": 'Regression: two_scalar', - "error": re.compile(r"Missing\s+'[.][.][.]'"), + "error": yaml.composer.ComposerError, }, { "out": ['foo'], @@ -310,49 +308,49 @@ SCHEDULE = [ }, { "name": "Unprintables", + "skip": False, "in": [ - " - - -", - " - \"\\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\"", - "- \" !\\\"#\$%&'()*+,-./\"", - "- 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", - "..." - ], + "---", + "- \"\\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\"", + "- \" !\\\"#\$%&'()*+,-./\"", + "- 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", + "..."], "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", - " !\"#\$%&'()*+,-./", - "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" - ], + "\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", + " !\"#\$%&'()*+,-./", + "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" + ] }, { "name": 'Quoted hash keys', "in": [ - '---', ' "quoted": Magic!', ' "\n\t": newline, tab', '...', + '---', ' "quoted": Magic!', ' "\\n\\t": newline, tab', '...', ], "out": { "quoted": 'Magic!', @@ -360,63 +358,3 @@ SCHEDULE = [ }, }, ] - -# FIXME plan(tests=(len(SCHEDULE) * 5)) - -#sub iter { -# my $ar = shift; -# return sub { -# return shift @$ar; -# }; -#} - -class TestReader(unittest.TestCase): - def test_reader(self): - for test in SCHEDULE: - name = test['name'] - yaml = yamlish.Reader() - self.assert_(True, "%s: Created" % name) - self.assert_(isinstance(yaml, yamlish.Reader)) - - # diag "$name\n"; - - # unless ( $test->{in} ) { - # pass for 1 .. 2; - # use YAML; - # diag "Input for test:\n"; - # diag( Dump( $test->{out} ) ); - # next; - # } - - source = "\n".join([line for line in test['in']]) + "\n" - - try: - got = yaml.read(test['in']) # expecting test['in'] being an iterator - except IOError as exc: # FIXME no idea what - dollar_at = exc - - raw = yaml.get_raw() - - err = test['error'] # RE for testing results - if err: # if we have err, use it - if not err.search(dollar_at): # FIXME $@ (or dollar_at) is described - # in perlvar(1) the error status of the last eval(), which - # means that yaml.read(test['in']) - # if everything is alright, then it is None - self.assertFalse("%s: Error message" % name) - raise Exception(dollar_at) - self.assert_(not got, "%s: No result" % name) - else: - want = test['out'] - self.assert_(not dollar_at, "%s: No error\n%s" % (name, dollar_at)) - - self.assertEqual(got, want, """%s: Result matches - expected = %s - - observed = %s - """ % (name, want, got)) - self.assertEqual(raw, source, """%s: Captured source matches - expected = %s - - observed = %s - """ % (name, raw, source)) diff --git a/test/test_writer.py b/test/test_writer.py index 5b0a8c4..d4ee5ba 100644 --- a/test/test_writer.py +++ b/test/test_writer.py @@ -4,7 +4,7 @@ from __future__ import absolute_import, print_function, unicode_literals import unittest import yamlish -SCHEDULE = [ +test_data_list = [ { "name": 'Simple scalar', "in": 1, @@ -138,50 +138,30 @@ SCHEDULE = [ }, ] -# plan(tests = len(SCHEDULE) * 5) +# plan(tests = len(test_data_list) * 5) class TestWriter(unittest.TestCase): def test_writer(self): - for test in SCHEDULE: + for test in test_data_list: name = test['name'] - yaml = yamlish.Writer() - self.assert_(True, "%s: Created" % name) - self.assert_(isinstance(yaml, yamlish.Writer)) - - got = [] data = test['in'] - try: - yaml.write(data, got) - except Exception as exc: - dollar_at = exc - raise - - # FIXME just to say ... THERE IS NO 'error' key in SCHEDULE!!! - err = test['error'] - if err: - if not err.search(dollar_at): # FIXME $@ (or dollar_at) is described - # in perlvar(1) the error status of the last eval(), which - # means that yaml.read(test['in']) - # if everything is alright, then it is None - self.assertFalse("%s: Error message" % name) - raise Exception(dollar_at) - self.assert_(not got, "%s: No result" % name) + 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.write, test['in']) else: want = test['out'] - self.assert_(not dollar_at, "%s: No error\n%s" % (name, dollar_at)) - + yamlish.write(test['in'], got) self.assertEqual(got, want, """%s: Result matches expected = %s observed = %s """ % (name, want, got)) - yr = yamlish.Reader() - # Now try parsing it - parsed = yr.read(got) # FIXME got has an iterator - + parsed = yamlish.load(got) # FIXME got has an iterator self.assertEqual(parsed, data, """%s: Reparse OK expected = %s |