# Path: flask_dance/utils.py
# class FakeCache:
#     """
#     An object that mimics just enough of Flask-Caching's API to be compatible
#     with our needs, but does nothing.
#     """
# 
#     def get(self, key):
#         return None
# 
#     def set(self, key, value):
#         return None
# 
#     def delete(self, key):
#         return None
# 
# def first(iterable, default=None, key=None):
#     """
#     Return the first truthy value of an iterable.
#     Shamelessly stolen from https://github.com/hynek/first
#     """
#     if key is None:
#         for el in iterable:
#             if el:
#                 return el
#     else:
#         for el in iterable:
#             if key(el):
#                 return el
#     return default
# 
# def getattrd(obj, name, default=sentinel):
#     """
#     Same as getattr(), but allows dot notation lookup
#     Source: http://stackoverflow.com/a/14324459
#     """
#     try:
#         return functools.reduce(getattr, name.split("."), obj)
#     except AttributeError as e:
#         if default is not sentinel:
#             return default
#         raise


import pytest
from flask_dance.utils import FakeCache, first, getattrd


def test_first():
    assert first([1, 2, 3]) == 1
    assert first([None, 2, 3]) == 2
    assert first([None, 0, False, [], {}]) == None
    assert first([None, 0, False, [], {}], default=42) == 42
    first([1, 1, 3, 4, 5], key=lambda x: x % 2 == 0) == 4


class C:
    d = "foo"


class B:
    C = C


class A:
    B = B


def test_getattrd():
    assert A.B.C.d == "foo"
    assert getattrd(A, "B.C.d") == "foo"

# Path: flask_dance/utils.py
# class FakeCache:
#     """
#     An object that mimics just enough of Flask-Caching's API to be compatible
#     with our needs, but does nothing.
#     """
# 
#     def get(self, key):
#         return None
# 
#     def set(self, key, value):
#         return None
# 
#     def delete(self, key):
#         return None
# 
# def first(iterable, default=None, key=None):
#     """
#     Return the first truthy value of an iterable.
#     Shamelessly stolen from https://github.com/hynek/first
#     """
#     if key is None:
#         for el in iterable:
#             if el:
#                 return el
#     else:
#         for el in iterable:
#             if key(el):
#                 return el
#     return default
# 
# def getattrd(obj, name, default=sentinel):
#     """
#     Same as getattr(), but allows dot notation lookup
#     Source: http://stackoverflow.com/a/14324459
#     """
#     try:
#         return functools.reduce(getattr, name.split("."), obj)
#     except AttributeError as e:
#         if default is not sentinel:
#             return default
#         raise


# Path: tests/test_utils.py
import pytest
from flask_dance.utils import FakeCache, first, getattrd



def test_first():
    assert first([1, 2, 3]) == 1
    assert first([None, 2, 3]) == 2
    assert first([None, 0, False, [], {}]) == None
    assert first([None, 0, False, [], {}], default=42) == 42
    first([1, 1, 3, 4, 5], key=lambda x: x % 2 == 0) == 4


class C:
    d = "foo"


class B:
    C = C


class A:
    B = B


def test_getattrd():
    assert A.B.C.d == "foo"
    assert getattrd(A, "B.C.d") == "foo"

    assert getattrd(A, "B") == B