def test_print_spaces_tabs_in_unicode(self): printer = StringPrinter() sh = SpacingHelper(4) test_string = "\the\tllo world " print_spaces_tabs_in_unicode(printer, test_string, dict(sh.yield_tab_lengths(test_string)), "red") self.assertEqual(printer.string, "--->he->llo•world•••") # Test the case when the bullet can't be printed because of encoding # problems. def hijack_print(text, *args, **kwargs): if "•" in text: raise UnicodeEncodeError("test-codec", "", 0, 1, "") else: return StringPrinter.print(printer, text, *args, **kwargs) printer.print = hijack_print printer.clear() test_string = " he\tllo world " print_spaces_tabs_in_unicode(printer, test_string, dict(sh.yield_tab_lengths(test_string)), "red") self.assertEqual(printer.string, ".he>llo..world.")
def test_print_spaces_tabs_in_unicode(self): printer = StringPrinter() sh = SpacingHelper(4) test_string = "\the\tllo world " print_spaces_tabs_in_unicode( printer, test_string, dict(sh.yield_tab_lengths(test_string)), "red") self.assertEqual(printer.string, "--->he->llo•world•••") # Test the case when the bullet can't be printed because of encoding # problems. def hijack_print(text, *args, **kwargs): if "•" in text: raise UnicodeEncodeError("test-codec", "", 0, 1, "") else: return StringPrinter.print(printer, text, *args, **kwargs) printer.print = hijack_print printer.clear() test_string = " he\tllo world " print_spaces_tabs_in_unicode(printer, test_string, dict(sh.yield_tab_lengths(test_string)), "red") self.assertEqual(printer.string, ".he>llo..world.")
def test_printing(self): uut = StringPrinter() self.assertEqual(uut.string, "") uut.print("ABC") self.assertEqual(uut.string, "ABC\n") uut.print("XYZ") self.assertEqual(uut.string, "ABC\nXYZ\n") uut.print("\nHello World.") self.assertEqual(uut.string, "ABC\nXYZ\n\nHello World.\n")
def setUp(self): self.printer = StringPrinter() self.logger = LogPrinter(self.printer) self.section = Section("t")
def hijack_print(text, *args, **kwargs): if "•" in text: raise UnicodeEncodeError("test-codec", "", 0, 1, "") else: return StringPrinter.print(printer, text, *args, **kwargs)
def test_logging(self): uut = LogPrinter(StringPrinter(), timestamp_format="") uut.log_message(self.log_message, end="") self.assertEqual(uut.printer.string, str(self.log_message)) uut = LogPrinter(StringPrinter(), log_level=LOG_LEVEL.DEBUG) uut.log_message(self.log_message, end="") self.assertEqual( uut.printer.string, "[ERROR][" + self.timestamp.strftime("%X") + "] " + Constants.COMPLEX_TEST_STRING) uut.printer.clear() uut.log(LOG_LEVEL.ERROR, Constants.COMPLEX_TEST_STRING, timestamp=self.timestamp, end="") self.assertEqual( uut.printer.string, "[ERROR][" + self.timestamp.strftime("%X") + "] " + Constants.COMPLEX_TEST_STRING) uut.printer.clear() uut.debug(Constants.COMPLEX_TEST_STRING, "d", timestamp=self.timestamp, end="") self.assertEqual( uut.printer.string, "[DEBUG][" + self.timestamp.strftime("%X") + "] " + Constants.COMPLEX_TEST_STRING + " d") uut.printer.clear() uut.log_level = LOG_LEVEL.INFO uut.debug(Constants.COMPLEX_TEST_STRING, timestamp=self.timestamp, end="") self.assertEqual(uut.printer.string, "") uut.printer.clear() uut.info(Constants.COMPLEX_TEST_STRING, "d", timestamp=self.timestamp, end="") self.assertEqual( uut.printer.string, "[INFO][" + self.timestamp.strftime("%X") + "] " + Constants.COMPLEX_TEST_STRING + " d") uut.log_level = LOG_LEVEL.WARNING uut.printer.clear() uut.debug(Constants.COMPLEX_TEST_STRING, timestamp=self.timestamp, end="") self.assertEqual(uut.printer.string, "") uut.printer.clear() uut.warn(Constants.COMPLEX_TEST_STRING, "d", timestamp=self.timestamp, end="") self.assertEqual( uut.printer.string, "[WARNING][" + self.timestamp.strftime("%X") + "] " + Constants.COMPLEX_TEST_STRING + " d") uut.printer.clear() uut.err(Constants.COMPLEX_TEST_STRING, "d", timestamp=self.timestamp, end="") self.assertEqual( uut.printer.string, "[ERROR][" + self.timestamp.strftime("%X") + "] " + Constants.COMPLEX_TEST_STRING + " d") uut.log_level = LOG_LEVEL.DEBUG uut.printer.clear() uut.log_exception("Something failed.", NotImplementedError(Constants.COMPLEX_TEST_STRING), timestamp=self.timestamp) self.assertTrue( uut.printer.string.startswith("[ERROR][" + self.timestamp.strftime("%X") + "] Something failed.\n" + "[DEBUG][" + self.timestamp.strftime("%X") + "] Exception was:")) uut.log_level = LOG_LEVEL.INFO uut.printer.clear() logged = uut.log_exception("Something failed.", NotImplementedError( Constants.COMPLEX_TEST_STRING), timestamp=self.timestamp, end="") self.assertTrue( uut.printer.string.startswith("[ERROR][" + self.timestamp.strftime("%X") + "] Something failed."))
def test_clear(self): uut = StringPrinter() self.assertEqual(uut.string, "") uut.clear() self.assertEqual(uut.string, "") uut.print("1") self.assertEqual(uut.string, "1\n") uut.clear() self.assertEqual(uut.string, "") uut.print(1000 * "#") self.assertEqual(uut.string, 1000 * "#" + "\n") uut.clear() self.assertEqual(uut.string, "")