Skip to content

Instantly share code, notes, and snippets.

@rmariano
Created June 27, 2025 15:19
Show Gist options
  • Save rmariano/5a889c2a5cc8197955cab9012e2a122f to your computer and use it in GitHub Desktop.
Save rmariano/5a889c2a5cc8197955cab9012e2a122f to your computer and use it in GitHub Desktop.
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