Created
June 27, 2025 15:19
-
-
Save rmariano/5a889c2a5cc8197955cab9012e2a122f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
def coalesce(*args): | |
for a in args: | |
if a is not None: | |
return a | |
else: | |
raise ValueError("No values found to return") | |
class TestCoalesce(unittest.TestCase): | |
def test_empty(self): | |
with self.assertRaises(ValueError): | |
coalesce() | |
def test_all_failed(self): | |
with self.assertRaises(ValueError): | |
coalesce(None, None) | |
def test_find_valid(self): | |
self.assertEqual(coalesce(None, 42, None, "asdfa", None), 42) | |
self.assertEqual(coalesce(1, 2, 3), 1) | |
self.assertEqual(coalesce(None, 0, "last value"), 0) | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment