43 lines
913 B
Python
43 lines
913 B
Python
import unittest
|
|
from pwo import Try
|
|
|
|
|
|
class TestException(Exception):
|
|
|
|
def __init__(self, msg: str):
|
|
super().__init__(msg)
|
|
|
|
|
|
class TryTest(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
pass
|
|
|
|
def test_try(self):
|
|
|
|
with self.subTest("Test failure"):
|
|
def throw_test_exception():
|
|
raise TestException("error")
|
|
|
|
t = Try.of(throw_test_exception)
|
|
|
|
with self.assertRaises(TestException):
|
|
t.get()
|
|
|
|
t = Try.failure(TestException("error"))
|
|
|
|
with self.assertRaises(TestException):
|
|
t.get()
|
|
|
|
with self.subTest("Test success"):
|
|
def complete_successfully():
|
|
return 42
|
|
|
|
t = Try.of(complete_successfully)
|
|
|
|
self.assertEqual(42, t.get())
|
|
|
|
t2 = t.handle(lambda value, err: value * 2)
|
|
self.assertEqual(84, t2.get())
|
|
|