# Path: core/util.py
# def nonone(value, replace_value):
#     """Returns ``value`` if ``value`` is not ``None``. Returns ``replace_value`` otherwise.
#     """
#     if value is None:
#         return replace_value
#     else:
#         return value
# 
# def tryint(value, default=0):
#     """Tries to convert ``value`` to in ``int`` and returns ``default`` if it fails.
#     """
#     try:
#         return int(value)
#     except (TypeError, ValueError):
#         return default
# 
# def minmax(value, min_value, max_value):
#     """Returns `value` or one of the min/max bounds if `value` is not between them.
#     """
#     return min(max(value, min_value), max_value)
# 
# def first(iterable):
#     """Returns the first item of ``iterable``.
#     """
#     try:
#         return next(iter(iterable))
#     except StopIteration:
#         return None
# 
# def flatten(iterables, start_with=None):
#     """Takes a list of lists ``iterables`` and returns a list containing elements of every list.
# 
#     If ``start_with`` is not ``None``, the result will start with ``start_with`` items, exactly as
#     if ``start_with`` would be the first item of lists.
#     """
#     result = []
#     if start_with:
#         result.extend(start_with)
#     for iterable in iterables:
#         result.extend(iterable)
#     return result
# 
# def dedupe(iterable):
#     """Returns a list of elements in ``iterable`` with all dupes removed.
# 
#     The order of the elements is preserved.
#     """
#     result = []
#     seen = {}
#     for item in iterable:
#         if item in seen:
#             continue
#         seen[item] = 1
#         result.append(item)
#     return result
# 
# def stripfalse(seq):
#     """Returns a sequence with all false elements stripped out of seq.
#     """
#     return [x for x in seq if x]
# 
# def extract(predicate, iterable):
#     """Separates the wheat from the shaft (`predicate` defines what's the wheat), and returns both.
#     """
#     wheat = []
#     shaft = []
#     for item in iterable:
#         if predicate(item):
#             wheat.append(item)
#         else:
#             shaft.append(item)
#     return wheat, shaft
# 
# def allsame(iterable):
#     """Returns whether all elements of 'iterable' are the same.
#     """
#     it = iter(iterable)
#     try:
#         first_item = next(it)
#     except StopIteration:
#         raise ValueError("iterable cannot be empty")
#     return all(element == first_item for element in it)
# 
# def escape(s, to_escape, escape_with='\\'):
#     """Returns ``s`` with characters in ``to_escape`` all prepended with ``escape_with``.
#     """
#     return ''.join((escape_with + c if c in to_escape else c) for c in s)


from .testutil import eq_
from ..util import (
    nonone, tryint, minmax, first, flatten, dedupe, stripfalse, extract,
    allsame, escape
)
    eq_(first([3, 2, 1]), 3)
    eq_(first(i for i in [3, 2, 1] if i < 3), 2)

def test_flatten():
    eq_([1,2,3,4],flatten([[1,2],[3,4]]))
    eq_([],flatten([]))

def test_dedupe():
    reflist = [0,7,1,2,3,4,4,5,6,7,1,2,3]
    eq_(dedupe(reflist),[0,7,1,2,3,4,5,6])

def test_stripfalse():
    eq_([1, 2, 3], stripfalse([None, 0, 1, 2, 3, None]))

def test_extract():
    wheat, shaft = extract(lambda n: n % 2 == 0, list(range(10)))
    eq_(wheat, [0, 2, 4, 6, 8])
    eq_(shaft, [1, 3, 5, 7, 9])

def test_allsame():
    assert allsame([42, 42, 42])
    assert not allsame([42, 43, 42])
    assert not allsame([43, 42, 42])
    # Works on non-sequence as well
    assert allsame(iter([42, 42, 42]))

#--- String

def test_escape():
    eq_('f\\o\\ob\\ar', escape('foobar', 'oa'))

# Path: core/util.py
# def nonone(value, replace_value):
#     """Returns ``value`` if ``value`` is not ``None``. Returns ``replace_value`` otherwise.
#     """
#     if value is None:
#         return replace_value
#     else:
#         return value
# 
# def tryint(value, default=0):
#     """Tries to convert ``value`` to in ``int`` and returns ``default`` if it fails.
#     """
#     try:
#         return int(value)
#     except (TypeError, ValueError):
#         return default
# 
# def minmax(value, min_value, max_value):
#     """Returns `value` or one of the min/max bounds if `value` is not between them.
#     """
#     return min(max(value, min_value), max_value)
# 
# def first(iterable):
#     """Returns the first item of ``iterable``.
#     """
#     try:
#         return next(iter(iterable))
#     except StopIteration:
#         return None
# 
# def flatten(iterables, start_with=None):
#     """Takes a list of lists ``iterables`` and returns a list containing elements of every list.
# 
#     If ``start_with`` is not ``None``, the result will start with ``start_with`` items, exactly as
#     if ``start_with`` would be the first item of lists.
#     """
#     result = []
#     if start_with:
#         result.extend(start_with)
#     for iterable in iterables:
#         result.extend(iterable)
#     return result
# 
# def dedupe(iterable):
#     """Returns a list of elements in ``iterable`` with all dupes removed.
# 
#     The order of the elements is preserved.
#     """
#     result = []
#     seen = {}
#     for item in iterable:
#         if item in seen:
#             continue
#         seen[item] = 1
#         result.append(item)
#     return result
# 
# def stripfalse(seq):
#     """Returns a sequence with all false elements stripped out of seq.
#     """
#     return [x for x in seq if x]
# 
# def extract(predicate, iterable):
#     """Separates the wheat from the shaft (`predicate` defines what's the wheat), and returns both.
#     """
#     wheat = []
#     shaft = []
#     for item in iterable:
#         if predicate(item):
#             wheat.append(item)
#         else:
#             shaft.append(item)
#     return wheat, shaft
# 
# def allsame(iterable):
#     """Returns whether all elements of 'iterable' are the same.
#     """
#     it = iter(iterable)
#     try:
#         first_item = next(it)
#     except StopIteration:
#         raise ValueError("iterable cannot be empty")
#     return all(element == first_item for element in it)
# 
# def escape(s, to_escape, escape_with='\\'):
#     """Returns ``s`` with characters in ``to_escape`` all prepended with ``escape_with``.
#     """
#     return ''.join((escape_with + c if c in to_escape else c) for c in s)


# Path: core/tests/util_test.py
from .testutil import eq_
from ..util import (
    nonone, tryint, minmax, first, flatten, dedupe, stripfalse, extract,
    allsame, escape
)

    eq_(first([3, 2, 1]), 3)
    eq_(first(i for i in [3, 2, 1] if i < 3), 2)

def test_flatten():
    eq_([1,2,3,4],flatten([[1,2],[3,4]]))
    eq_([],flatten([]))

def test_dedupe():
    reflist = [0,7,1,2,3,4,4,5,6,7,1,2,3]
    eq_(dedupe(reflist),[0,7,1,2,3,4,5,6])

def test_stripfalse():
    eq_([1, 2, 3], stripfalse([None, 0, 1, 2, 3, None]))

def test_extract():
    wheat, shaft = extract(lambda n: n % 2 == 0, list(range(10)))
    eq_(wheat, [0, 2, 4, 6, 8])
    eq_(shaft, [1, 3, 5, 7, 9])

def test_allsame():
    assert allsame([42, 42, 42])
    assert not allsame([42, 43, 42])
    assert not allsame([43, 42, 42])
    # Works on non-sequence as well
    assert allsame(iter([42, 42, 42]))

#--- String

def test_escape():
    eq_('f\\o\\ob\\ar', escape('foobar', 'oa'))

    eq_('f*o*ob*ar', escape('foobar', 'oa', '*'))