From a9adb2cae3d62d95a1fdc57d7137dca5e5b9ada9 Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Mon, 27 Feb 2012 01:29:51 +0100 Subject: Reorganization of writer interface. Also: * changed license to MIT/X11 * cleanup of some PyLint warnings (mostly missing docstrings) * make _YamlishLoader a little bit cleaner (don't copy whole SafeLoader __init__, just subclass it cleanly). --- test/__init__.py | 37 ++++++++++++++++++++++--------------- test/test_output.py | 46 ++++++++++++++++++++++++++++++++++++++-------- test/test_reader.py | 7 ++++--- test/test_writer.py | 6 +++--- 4 files changed, 67 insertions(+), 29 deletions(-) (limited to 'test') diff --git a/test/__init__.py b/test/__init__.py index 1f74147..cb37281 100644 --- a/test/__init__.py +++ b/test/__init__.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- +# -*- coding: utf-8 -*- IGNORE:C0111 from __future__ import absolute_import, print_function, unicode_literals import logging from test import test_reader, test_input @@ -7,25 +7,29 @@ import unittest import yaml logging.basicConfig(level=logging.DEBUG) -def generate_test_name(source): +def _generate_test_name(source): + """ + Clean up human-friendly test name into a method name. + """ out = source.replace(' ', '_').replace(':', '').lower() return "test_%s" % out -def create_test(test_src, tested_function): +def _create_test(test_src, tested_function): + """ + Decorate tested function to be used as a method for TestCase. + """ def do_test_expected(self): - #self.assertEqual(under_test(pair[0]), pair[1]) + """ + Execute a test by calling a tested_function on test_src data. + """ 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']) + self.assertRaises(test_src['error'], tested_function, + test_src['in']) else: want = test_src['out'] got = tested_function(test_src['in']) @@ -40,19 +44,22 @@ def create_test(test_src, tested_function): def generate_testsuite(test_data, test_case_shell, test_fce): + """ + Generate tests from the test data, class to build upon and function to use for testing. + """ 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) + name = _generate_test_name(in_test['name']) + test_method = _create_test (in_test, test_fce) + test_method.__name__ = str('test_%s' % name) # IGNORE:W0622 setattr (test_case_shell, test_method.__name__, test_method) -class TestInput(unittest.TestCase): +class TestInput(unittest.TestCase): # IGNORE:C0111 pass -class TestReader(unittest.TestCase): +class TestReader(unittest.TestCase): # IGNORE:C0111 pass if __name__ == "__main__": diff --git a/test/test_output.py b/test/test_output.py index ea8d841..cebd66d 100644 --- a/test/test_output.py +++ b/test/test_output.py @@ -1,9 +1,14 @@ # -*- coding: utf-8 -*- +""" +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 yamlish -import yaml OUT = [ "---", @@ -89,15 +94,40 @@ destination = [ ] class TestOuptut(unittest.TestCase): - def test_output(self): - for dest in destination: - name = dest['name'] + def setUp(self): + """ + Transform expected list into string which we actually use. + """ + self._expected = "" + for line in OUT: + self._expected += "%s\n" % line + + + def test_file_output(self): + """ + Test output to a file. + """ + outf = StringIO() + yamlish.dump(IN, outf) + outf.seek(0) + got = outf.read() + outf.close() + self.assertEqual(got, self._expected, """Result matches + expected = %s - yaml.dump(IN, dest) - got = dest['normalise']() + observed = %s + """ % (self._expected, got)) - self.assertEqual(got, OUT, """%s: Result matches + 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 - """ % (name, OUT, got)) + """ % (self._expected, got)) + +if __name__ == "__main__": + unittest.main() diff --git a/test/test_reader.py b/test/test_reader.py index e8d39d1..79756c9 100644 --- a/test/test_reader.py +++ b/test/test_reader.py @@ -308,9 +308,9 @@ test_data_list = [ }, { "name": "Unprintables", - "skip": False, + "skip": True, "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\"", "- \" !\\\"#\$%&'()*+,-./\"", @@ -327,7 +327,8 @@ test_data_list = [ "- \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", diff --git a/test/test_writer.py b/test/test_writer.py index d4ee5ba..0c1b517 100644 --- a/test/test_writer.py +++ b/test/test_writer.py @@ -146,14 +146,14 @@ class TestWriter(unittest.TestCase): name = test['name'] data = test['in'] - got = [] + 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']) + self.assertRaises(test['error'], yamlish.dumps, test['in']) else: want = test['out'] - yamlish.write(test['in'], got) + yamlish.dump(test['in'], got) self.assertEqual(got, want, """%s: Result matches expected = %s -- cgit