# This test can be run stand-alone with something like: # > PYTHONPATH=. python2 openerp/tests/test_misc.py import unittest2 from ..tools import misc class append_content_to_html(unittest2.TestCase): """ Test some of our generic utility functions """ def test_append_to_html(self): test_samples = [ ('some content', '--\nYours truly', True, 'some content\n
--\nYours truly\n'), ('some content', '\n\n
--
\nYours truly
\n\n', False, 'some content\n\n\n--
\nYours truly
\n\n\n'), ] for html, content, flag, expected in test_samples: self.assertEqual(misc.append_content_to_html(html,content,flag), expected, 'append_content_to_html is broken') class test_countingstream(unittest2.TestCase): def test_empty_stream(self): s = misc.CountingStream(iter([])) self.assertEqual(s.index, -1) self.assertIsNone(next(s, None)) self.assertEqual(s.index, 0) def test_single(self): s = misc.CountingStream(xrange(1)) self.assertEqual(s.index, -1) self.assertEqual(next(s, None), 0) self.assertIsNone(next(s, None)) self.assertEqual(s.index, 1) def test_full(self): s = misc.CountingStream(xrange(42)) for _ in s: pass self.assertEqual(s.index, 42) def test_repeated(self): """ Once the CountingStream has stopped iterating, the index should not increase anymore (the internal state should not be allowed to change) """ s = misc.CountingStream(iter([])) self.assertIsNone(next(s, None)) self.assertEqual(s.index, 0) self.assertIsNone(next(s, None)) self.assertEqual(s.index, 0) if __name__ == '__main__': unittest2.main()