diff options
| author | Robert Collins <robertc@robertcollins.net> | 2009-10-04 21:23:16 +1100 |
|---|---|---|
| committer | Robert Collins <robertc@robertcollins.net> | 2009-10-04 21:23:16 +1100 |
| commit | 5bf0f62627c4fecdcac1c5044f4e3aba03ad1e30 (patch) | |
| tree | cdb5ecd4d31a24f9d270e92333db3fcb39c9fe4b /python | |
| parent | 47dc87aec4dc886ae625074c7d03410422453a31 (diff) | |
| download | subunit-git-5bf0f62627c4fecdcac1c5044f4e3aba03ad1e30.tar.gz | |
Add subunit.Content python class for serialisation.
Diffstat (limited to 'python')
| -rw-r--r-- | python/subunit/content.py | 41 | ||||
| -rw-r--r-- | python/subunit/tests/__init__.py | 2 | ||||
| -rw-r--r-- | python/subunit/tests/test_content.py | 41 | ||||
| -rw-r--r-- | python/subunit/tests/test_content_type.py | 6 |
4 files changed, 84 insertions, 6 deletions
diff --git a/python/subunit/content.py b/python/subunit/content.py new file mode 100644 index 0000000..517862a --- /dev/null +++ b/python/subunit/content.py @@ -0,0 +1,41 @@ +# +# subunit: extensions to Python unittest to get test results from subprocesses. +# Copyright (C) 2009 Robert Collins <robertc@robertcollins.net> +# +# Licensed under either the Apache License, Version 2.0 or the BSD 3-clause +# license at the users choice. A copy of both licenses are available in the +# project source as Apache-2.0 and BSD. You may not use this file except in +# compliance with one of these two licences. +# +# Unless required by applicable law or agreed to in writing, software +# distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# license you chose for the specific language governing permissions and +# limitations under that license. +# + +"""Content - a MIME-like Content object.""" + +class Content(object): + """A MIME-like Content object. + + Content objects can be serialised to bytes using the iter_bytes method. + If the Content-Type is recognised by other code, they are welcome to + look for richer contents that mere byte serialisation - for example in + memory object graphs etc. However, such code MUST be prepared to receive + a generic Content object that has been reconstructed from a byte stream. + + :ivar content_type: The content type of this Content. + """ + + def __init__(self, content_type, get_bytes): + """Create a ContentType.""" + if None in (content_type, get_bytes): + raise ValueError("None not permitted in %r, %r" % ( + content_type, get_bytes)) + self.content_type = content_type + self._get_bytes = get_bytes + + def iter_bytes(self): + """Iterate over bytestrings of the serialised content.""" + return self._get_bytes() diff --git a/python/subunit/tests/__init__.py b/python/subunit/tests/__init__.py index d0648f2..d842c7e 100644 --- a/python/subunit/tests/__init__.py +++ b/python/subunit/tests/__init__.py @@ -17,6 +17,7 @@ from subunit.tests import ( TestUtil, test_content_type, + test_content, test_progress_model, test_subunit_filter, test_subunit_stats, @@ -29,6 +30,7 @@ from subunit.tests import ( def test_suite(): result = TestUtil.TestSuite() result.addTest(test_content_type.test_suite()) + result.addTest(test_content.test_suite()) result.addTest(test_progress_model.test_suite()) result.addTest(test_test_results.test_suite()) result.addTest(test_test_protocol.test_suite()) diff --git a/python/subunit/tests/test_content.py b/python/subunit/tests/test_content.py new file mode 100644 index 0000000..869717e --- /dev/null +++ b/python/subunit/tests/test_content.py @@ -0,0 +1,41 @@ +# +# subunit: extensions to Python unittest to get test results from subprocesses. +# Copyright (C) 2009 Robert Collins <robertc@robertcollins.net> +# +# Licensed under either the Apache License, Version 2.0 or the BSD 3-clause +# license at the users choice. A copy of both licenses are available in the +# project source as Apache-2.0 and BSD. You may not use this file except in +# compliance with one of these two licences. +# +# Unless required by applicable law or agreed to in writing, software +# distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# license you chose for the specific language governing permissions and +# limitations under that license. +# + +import unittest +import subunit +from subunit.content import Content +from subunit.content_type import ContentType + + +def test_suite(): + loader = subunit.tests.TestUtil.TestLoader() + result = loader.loadTestsFromName(__name__) + return result + + +class TestContent(unittest.TestCase): + + def test___init___None_errors(self): + self.assertRaises(ValueError, Content, None, None) + self.assertRaises(ValueError, Content, None, lambda:["traceback"]) + self.assertRaises(ValueError, Content, + ContentType("text", "traceback"), None) + + def test___init___sets_ivars(self): + content_type = ContentType("foo", "bar") + content = Content(content_type, lambda:["bytes"]) + self.assertEqual(content_type, content.content_type) + self.assertEqual(["bytes"], list(content.iter_bytes())) diff --git a/python/subunit/tests/test_content_type.py b/python/subunit/tests/test_content_type.py index fea9a9b..7e24316 100644 --- a/python/subunit/tests/test_content_type.py +++ b/python/subunit/tests/test_content_type.py @@ -14,15 +14,9 @@ # limitations under that license. # -import datetime import unittest -from StringIO import StringIO -import os import subunit from subunit.content_type import ContentType -import sys - -import subunit.iso8601 as iso8601 def test_suite(): |
