# -*- coding: utf-8 -*-

"""Tests of misc type utility functions."""

#------------------------------------------------------------------------------
# Imports
#------------------------------------------------------------------------------

import numpy as np

from .._types import (Bunch, _is_integer, _is_list, _is_float,
                      _as_list, _is_array_like, _as_array, _as_tuple,
                      )


#------------------------------------------------------------------------------
# Tests
#------------------------------------------------------------------------------

def test_bunch():
    obj = Bunch()
    obj['a'] = 1
    assert obj.a == 1
    obj.b = 2
    assert obj['b'] == 2
    assert obj.copy() == obj


def test_number():
    assert not _is_integer(None)
    assert not _is_integer(3.)
    assert _is_integer(3)
    assert _is_integer(np.arange(1)[0])

    assert not _is_float(None)
    assert not _is_float(3)
    assert not _is_float(np.array([3])[0])
    assert _is_float(3.)
    assert _is_float(np.array([3.])[0])


def test_list():
    assert not _is_list(None)
    assert not _is_list(())
    assert _is_list([])

    assert _as_list(None) is None
    assert _as_list(3) == [3]
    assert _as_list([3]) == [3]
    assert _as_list((3,)) == [3]
    assert _as_list('3') == ['3']
    assert np.all(_as_list(np.array([3])) == np.array([3]))


def test_as_tuple():
    assert _as_tuple(3) == (3,)
    assert _as_tuple((3,)) == (3,)
    assert _as_tuple(None) is None
    assert _as_tuple((None,)) == (None,)
    assert _as_tuple((3, 4)) == (3, 4)
    assert _as_tuple([3]) == ([3], )
    assert _as_tuple([3, 4]) == ([3, 4], )


def test_array():
    def _check(arr):
        assert isinstance(arr, np.ndarray)
        assert np.all(arr == [3])

    _check(_as_array(3))
    _check(_as_array(3.))
    _check(_as_array([3]))

    _check(_as_array(3, np.float))
    _check(_as_array(3., np.float))
    _check(_as_array([3], np.float))

    assert _as_array(None) is None
    assert not _is_array_like(None)
    assert not _is_array_like(3)
    assert _is_array_like([3])
    assert _is_array_like(np.array([3]))

@@ -252,16 +252,42 @@
 py as np
+%0Afrom pytest import raises
 %0A%0Afrom .
@@ -1985,16 +1985,187 @@
 .float))
+%0A    _check(_as_array(np.array(%5B3%5D)))%0A    with raises(ValueError):%0A        _check(_as_array(np.array(%5B3%5D), dtype=np.object))%0A    _check(_as_array(np.array(%5B3%5D), np.float))
 %0A%0A    as

Increase coverage in utils