# Path: mom/optional.py
# class Optional(object):
#     """A class that should make it easier to handle "missing" return values."""
# 
#     def __init__(self, value, set=True):
#         self._value = value
#         self._set = set
# 
#     @classmethod
#     def missing(cls):
#         return cls(None, set=False)
# 
#     @property
#     def value(self):
#         return self._value
# 
#     @property
#     def present(self):
#         return self._set
# 
#     def orNone(self):
#         return self.value if self._set else None
# 
#     def orElse(self, default):
#         return self.value if self._set else default
# 
#     def orRaise(self, exception, *args, **kwargs):
#         if not self._set:
#             raise exception(*args, **kwargs)
# 
#         return self.value
# 
#     def map(self, func):
#         return Optional(func(self.value)) if self._set else Optional.missing()
# 
#     def get(self, key, default=None):
#         return Optional(self.value.get(key, default)) if self._set else Optional.missing()
# 
#     def __getitem__(self, key):
#         if not self._set:
#             return Optional.missing()
#         try:
#             return Optional(self.value[key])
#         except KeyError:
#             return Optional.missing()
# 
#     def __iter__(self):
#         return iter(self.orElse(()))


import unittest
import sys
from mom.optional import Optional

    def test_missing_item(self):
        self.assertEqual(False, Optional.missing()["key"].present)

    def test_missing_iter(self):
        if sys.version_info[0] >= 3:
            self.assertRaises(StopIteration, iter(Optional.missing()).__next__)
        else:
            self.assertRaises(StopIteration, iter(Optional.missing()).next)

    def test_value(self):
        self.assertEqual(True, Optional("val").present)
        self.assertEqual("val", Optional("val").value)

    def test_raise(self):
        self.assertEqual(Optional("val").orRaise(RuntimeError, "error"), "val")

    def test_map(self):
        self.assertEqual("VAL", Optional("val").map(str.upper).value)

    def test_get(self):
        self.assertEqual("val", Optional({"key": "val"}).get("key").value)

    def test_get_default(self):
        self.assertEqual("default", Optional({"key": "val"}).get("key2", "default").value)

    def test_item(self):
        self.assertEqual("val", Optional({"key": "val"})["key"].value)

    def test_get_default_item(self):

# Path: mom/optional.py
# class Optional(object):
#     """A class that should make it easier to handle "missing" return values."""
# 
#     def __init__(self, value, set=True):
#         self._value = value
#         self._set = set
# 
#     @classmethod
#     def missing(cls):
#         return cls(None, set=False)
# 
#     @property
#     def value(self):
#         return self._value
# 
#     @property
#     def present(self):
#         return self._set
# 
#     def orNone(self):
#         return self.value if self._set else None
# 
#     def orElse(self, default):
#         return self.value if self._set else default
# 
#     def orRaise(self, exception, *args, **kwargs):
#         if not self._set:
#             raise exception(*args, **kwargs)
# 
#         return self.value
# 
#     def map(self, func):
#         return Optional(func(self.value)) if self._set else Optional.missing()
# 
#     def get(self, key, default=None):
#         return Optional(self.value.get(key, default)) if self._set else Optional.missing()
# 
#     def __getitem__(self, key):
#         if not self._set:
#             return Optional.missing()
#         try:
#             return Optional(self.value[key])
#         except KeyError:
#             return Optional.missing()
# 
#     def __iter__(self):
#         return iter(self.orElse(()))


# Path: tests/OptionalTests.py
import unittest
import sys
from mom.optional import Optional


    def test_missing_item(self):
        self.assertEqual(False, Optional.missing()["key"].present)

    def test_missing_iter(self):
        if sys.version_info[0] >= 3:
            self.assertRaises(StopIteration, iter(Optional.missing()).__next__)
        else:
            self.assertRaises(StopIteration, iter(Optional.missing()).next)

    def test_value(self):
        self.assertEqual(True, Optional("val").present)
        self.assertEqual("val", Optional("val").value)

    def test_raise(self):
        self.assertEqual(Optional("val").orRaise(RuntimeError, "error"), "val")

    def test_map(self):
        self.assertEqual("VAL", Optional("val").map(str.upper).value)

    def test_get(self):
        self.assertEqual("val", Optional({"key": "val"}).get("key").value)

    def test_get_default(self):
        self.assertEqual("default", Optional({"key": "val"}).get("key2", "default").value)

    def test_item(self):
        self.assertEqual("val", Optional({"key": "val"})["key"].value)

    def test_get_default_item(self):

        self.assertEqual(False, Optional({"key": "val"})["key2"].present)