def test_unallowed_options(self): with self.assertRaisesRegex(ValueError, "'fmt' is not a valid option"): LegacyPyomoFormatter(fmt='%(message)') with self.assertRaisesRegex(ValueError, "'style' is not a valid option"): LegacyPyomoFormatter(style='%')
def test_numbered_level(self): testname = 'test_numbered_level' self.handler.setFormatter( LegacyPyomoFormatter( base=os.path.dirname(__file__), verbosity=lambda: logger.isEnabledFor(logging.DEBUG), )) logger.setLevel(logging.WARNING) logger.log(45, "(hi)") ans = "Level 45: (hi)\n" self.assertEqual(self.stream.getvalue(), ans) logger.log(45, "") ans += "Level 45:\n" self.assertEqual(self.stream.getvalue(), ans) logger.setLevel(logging.DEBUG) logger.log(45, "(hi)") lineno = getframeinfo(currentframe()).lineno - 1 ans += 'Level 45: "[base]%stest_log.py", %d, %s\n' \ ' (hi)\n' % (os.path.sep, lineno, testname) self.assertEqual(self.stream.getvalue(), ans) logger.log(45, "") lineno = getframeinfo(currentframe()).lineno - 1 ans += 'Level 45: "[base]%stest_log.py", %d, %s\n\n' \ % (os.path.sep, lineno, testname) self.assertEqual(self.stream.getvalue(), ans)
def test_no_base(self): self.handler.setFormatter(LegacyPyomoFormatter()) logger.setLevel(logging.WARNING) logger.info("(info)") self.assertEqual(self.stream.getvalue(), "") logger.warning("(warn)") lineno = getframeinfo(currentframe()).lineno - 1 ans = 'WARNING: "%s", %d, test_no_base\n' \ ' (warn)\n' % (filename, lineno,) self.assertEqual(self.stream.getvalue(), ans)
def test_blank_lines(self): self.handler.setFormatter( LegacyPyomoFormatter( base=os.path.dirname(__file__), verbosity=lambda: logger.isEnabledFor(logging.DEBUG), )) logger.setLevel(logging.WARNING) logger.warning("\n\nthis is a message.\n\n\n") ans = "WARNING: this is a message.\n" self.assertEqual(self.stream.getvalue(), ans) logger.setLevel(logging.DEBUG) logger.warning("\n\nthis is a message.\n\n\n") lineno = getframeinfo(currentframe()).lineno - 1 ans += 'WARNING: "[base]%stest_log.py", %d, test_blank_lines\n' \ " this is a message.\n" % (os.path.sep, lineno) self.assertEqual(self.stream.getvalue(), ans)
def test_simple_log(self): # Testing positional base, configurable verbosity self.handler.setFormatter( LegacyPyomoFormatter( base=os.path.dirname(__file__), verbosity=lambda: logger.isEnabledFor(logging.DEBUG))) logger.setLevel(logging.WARNING) logger.info("(info)") self.assertEqual(self.stream.getvalue(), "") logger.warning("(warn)") ans = "WARNING: (warn)\n" self.assertEqual(self.stream.getvalue(), ans) logger.setLevel(logging.DEBUG) logger.warning("(warn)") lineno = getframeinfo(currentframe()).lineno - 1 ans += 'WARNING: "[base]%stest_log.py", %d, test_simple_log\n' \ ' (warn)\n' % (os.path.sep, lineno,) self.assertEqual(self.stream.getvalue(), ans)
def test_verbatim(self): self.handler.setFormatter( LegacyPyomoFormatter( base=os.path.dirname(__file__), verbosity=lambda: logger.isEnabledFor(logging.DEBUG))) msg = ("This is a long message\n" "\n" " ```\n" "With some \n" "internal\n" "verbatim \n" " - including a\n" " long list\n" " - and a short list \n" " ```\n" "\n" "And some \n" "internal\n" "non-verbatim \n" " - including a\n" " long list\n" " - and a short list \n") logger.setLevel(logging.WARNING) logger.warning(msg) ans = ("WARNING: This is a long message\n" "\n" " With some \n" " internal\n" " verbatim \n" " - including a\n" " long list\n" " - and a short list \n" "\n" " And some internal non-verbatim\n" " - including a long list\n" " - and a short list\n") self.assertEqual(self.stream.getvalue(), ans)
def test_long_messages(self): self.handler.setFormatter( LegacyPyomoFormatter( base=os.path.dirname(__file__), verbosity=lambda: logger.isEnabledFor(logging.DEBUG))) msg = ("This is a long message\n" "\n" "With some kind of internal formatting\n" " - including a bulleted list\n" " - list 2 ") logger.setLevel(logging.WARNING) logger.warning(msg) ans = ("WARNING: This is a long message\n" "\n" " With some kind of internal formatting\n" " - including a bulleted list\n" " - list 2\n") self.assertEqual(self.stream.getvalue(), ans) logger.setLevel(logging.DEBUG) logger.info(msg) lineno = getframeinfo(currentframe()).lineno - 1 ans += ('INFO: "[base]%stest_log.py", %d, test_long_messages\n' " This is a long message\n" "\n" " With some kind of internal formatting\n" " - including a bulleted list\n" " - list 2\n" % ( os.path.sep, lineno, )) self.assertEqual(self.stream.getvalue(), ans) # test trailing newline msg += "\n" logger.setLevel(logging.WARNING) logger.warning(msg) ans += ("WARNING: This is a long message\n" "\n" " With some kind of internal formatting\n" " - including a bulleted list\n" " - list 2\n") self.assertEqual(self.stream.getvalue(), ans) logger.setLevel(logging.DEBUG) logger.info(msg) lineno = getframeinfo(currentframe()).lineno - 1 ans += ('INFO: "[base]%stest_log.py", %d, test_long_messages\n' " This is a long message\n" "\n" " With some kind of internal formatting\n" " - including a bulleted list\n" " - list 2\n" % ( os.path.sep, lineno, )) self.assertEqual(self.stream.getvalue(), ans) # test initial and final blank lines msg = "\n" + msg + "\n\n" logger.setLevel(logging.WARNING) logger.warning(msg) ans += ("WARNING: This is a long message\n" "\n" " With some kind of internal formatting\n" " - including a bulleted list\n" " - list 2\n") self.assertEqual(self.stream.getvalue(), ans) logger.setLevel(logging.DEBUG) logger.info(msg) lineno = getframeinfo(currentframe()).lineno - 1 ans += ('INFO: "[base]%stest_log.py", %d, test_long_messages\n' " This is a long message\n" "\n" " With some kind of internal formatting\n" " - including a bulleted list\n" " - list 2\n" % ( os.path.sep, lineno, )) self.assertEqual(self.stream.getvalue(), ans)