def test_unwrap_or(self): res_var = Ok(10) self.assertEqual(res_var.unwrap_or(0), 10) res_var = Ok(True) self.assertEqual(res_var.unwrap_or(False), True) res_var = Err("error") self.assertEqual(res_var.unwrap_or(0), 0)
def test_map_or_else(self): res_var = Ok(["1", "2", "3"]) length = res_var.map_or_else(len, lambda: 0) self.assertEqual(length, Ok(3)) res_var = Err("error") length = res_var.map_or_else(len, lambda: 0) self.assertEqual(length, Ok(0))
def load_message(message_file_path: Path) -> Result[str, Exception]: try: if not message_file_path.is_file(): raise FileNotFoundError( f"{message_file_path} is not a valid file path") with message_file_path.open('r') as f: line = f.readline() return Ok(str(line)) except Exception as e: return Err(e)
def test_map(self): res_var = Ok(["1", "2", "3"]) length = res_var.map(len) self.assertEqual(length, Ok(3)) res_var = Err("error") length = res_var.map(len) self.assertEqual(length, Err("error"))
def test_ok(self): res_var = Ok(["1", "2", "3"]) self.assertEqual(res_var.ok(), Some(["1", "2", "3"])) res_var = Err("error") self.assertEqual(res_var.ok(), Nothing())
def test_unwrap(self): res_var = Ok(10) self.assertEqual(res_var.unwrap(), 10) res_var = Ok("10") self.assertEqual(res_var.unwrap(), "10") res_var = Ok(None) self.assertEqual(res_var.unwrap(), None) res_var = Ok(Err(RuntimeError("error"))) self.assertEqual(res_var.unwrap(), Err(RuntimeError("error"))) res_var = Err("error") self.assertRaises(SystemExit, res_var.unwrap)
def test_is_err(self): res_var = Ok(10) self.assertEqual(res_var.is_err(), False) res_var = Err("error") self.assertEqual(res_var.is_err(), True)
def test_unwrap_or_else(self): res_var = Ok(10) self.assertEqual(res_var.unwrap_or_else(lambda: 0), 10) res_var = Err("error") self.assertEqual(res_var.unwrap_or_else(lambda: 0), 0)
def test_unwrap_err(self): res_var = Ok(10) self.assertRaises(SystemExit, res_var.unwrap_err) res_var = Err("error") self.assertEqual(res_var.unwrap_err(), "error")
def test_ok_or_else(self): some_var = Some(10) self.assertEqual(some_var.ok_or_else(lambda: 0), Ok(10)) some_var = Nothing() self.assertEqual(some_var.ok_or_else(lambda: 0), Err(0))