Commit 109358d8 by Charlie Beattie Committed by Copybara-Service

Internal change

PiperOrigin-RevId: 359606102
parent a2223619
...@@ -104,8 +104,8 @@ void RegisterStatusBindings(module m) { ...@@ -104,8 +104,8 @@ void RegisterStatusBindings(module m) {
"update", "update",
(void (absl::Status::*)(const absl::Status &)) & absl::Status::Update, (void (absl::Status::*)(const absl::Status &)) & absl::Status::Update,
arg("other")) arg("other"))
.def("to_string", &absl::Status::ToString) .def("to_string", [](const absl::Status& s) { return s.ToString(); })
.def("__repr__", &absl::Status::ToString); .def("__repr__", [](const absl::Status& s) { return s.ToString(); });
m.def("is_ok", &IsOk, arg("status_or"), m.def("is_ok", &IsOk, arg("status_or"),
"Returns false only if passed a non-ok status; otherwise returns true. " "Returns false only if passed a non-ok status; otherwise returns true. "
......
...@@ -92,6 +92,18 @@ class StatusTest(absltest.TestCase): ...@@ -92,6 +92,18 @@ class StatusTest(absltest.TestCase):
failure_status = status_example.make_status(status.StatusCode.CANCELLED) failure_status = status_example.make_status(status.StatusCode.CANCELLED)
self.assertFalse(status.is_ok(failure_status)) self.assertFalse(status.is_ok(failure_status))
def test_ok_to_string(self):
ok_status = status_example.make_status(status.StatusCode.OK)
self.assertEqual(ok_status.to_string(), 'OK')
self.assertEqual(repr(ok_status), 'OK')
self.assertEqual(str(ok_status), 'OK')
def test_canonical_error_to_string(self):
test_status = status.aborted_error('test')
self.assertEqual(test_status.to_string(), 'ABORTED: test')
self.assertEqual(repr(test_status), 'ABORTED: test')
self.assertEqual(str(test_status), 'ABORTED: test')
class StatusOrTest(absltest.TestCase): class StatusOrTest(absltest.TestCase):
...@@ -149,6 +161,13 @@ class StatusOrTest(absltest.TestCase): ...@@ -149,6 +161,13 @@ class StatusOrTest(absltest.TestCase):
with self.assertRaises(status.StatusNotOk): with self.assertRaises(status.StatusNotOk):
status_example.return_failure_status_or_pointer() status_example.return_failure_status_or_pointer()
def test_canonical_error_to_string(self):
failure_result = status_example.make_failure_status_or(
status.StatusCode.CANCELLED)
self.assertEqual(failure_result.to_string(), 'CANCELLED: ')
self.assertEqual(repr(failure_result), 'CANCELLED: ')
self.assertEqual(str(failure_result), 'CANCELLED: ')
if __name__ == '__main__': if __name__ == '__main__':
absltest.main() absltest.main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment