def testBasic(self): cm = TestContextManager() def body(var): cm.log.append("body(%r)" % var) _py_context_manager.test_py_context_manager(cm, body) self.assertEqual("\n".join(cm.log), NO_EXCEPTION_LOG)
def testExitRaisesException(self): cm = TestContextManager("raise_from_exit") def body(var): cm.log.append("body(%r)" % var) # Note: this does *not* raise an exception (but does log a warning): _py_context_manager.test_py_context_manager(cm, body) self.assertEqual("\n".join(cm.log), NO_EXCEPTION_LOG)
def testEnterRaisesException(self): cm = TestContextManager("raise_from_enter") def body(var): cm.log.append("body(%r)" % var) with self.assertRaisesRegexp(ValueError, "exception in __enter__"): _py_context_manager.test_py_context_manager(cm, body) self.assertEqual("\n".join(cm.log), "__enter__()")
def testBodyRaisesException(self): cm = TestContextManager() def body(var): cm.log.append("body(%r)" % var) raise ValueError("Foo") with self.assertRaisesRegexp(ValueError, "Foo"): _py_context_manager.test_py_context_manager(cm, body) self.assertRegex("\n".join(cm.log), EXCEPTION_LOG)
def testExitSuppressesException(self): cm = TestContextManager("suppress_exception") def body(var): cm.log.append("body(%r)" % var) raise ValueError("Foo") with self.assertRaisesRegexp( ValueError, "tensorflow::PyContextManager::Enter does not support " "context managers that suppress exception"): _py_context_manager.test_py_context_manager(cm, body) self.assertRegex("\n".join(cm.log), EXCEPTION_LOG)