#!/usr/bin/env python
# -*- coding: utf-8 mode: python -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8

__all__ = ('suite',
           )

from base import TC
from utools.base.version import check_version
from utools.base.base import sorted, updated, isnum, isbool, isseq
from utools.base.base import seqof, firstof, isbytes, isunicode, isstr
from utools.base.base import all_matches, one_match, strip_nl, norm_nl
from utools.base.base import getattrs

class ClassTest(TC):
    def test_sorted(self):
        eq, _, _, _ = self.tf()
        eq([1,2,3], sorted([3,2,1]))

    def test_updated(self):
        eq, ist, isf, _ = self.tf()

        eq({"n": "v"}, updated(None, n="v"))
        eq({"n": "v"}, updated({}, n="v"))

        d = {"k": "v"}
        eq({"n": "v", "k": "v"}, updated(d, n="v"))

        d = {"n": "v"}
        eq({"n": "v"}, updated(d, n="v"))
        ist(d == updated(d, n="v"))
        isf(d is updated(d, n="v"))

    def test_isnum(self):
        _, ist, isf, _ = self.tf()

        ist(isnum(0))
        ist(isnum(0L))
        isf(isnum(None))
        isf(isnum(True))
        isf(isnum(False))
        isf(isnum("str"))
        isf(isnum(u"unicode"))
        isf(isnum(()))
        isf(isnum([]))

    def test_isbool(self):
        _, ist, isf, _ = self.tf()

        isf(isbool(0))
        isf(isbool(0L))
        isf(isbool(None))
        ist(isbool(True))
        ist(isbool(False))
        isf(isbool("str"))
        isf(isbool(u"unicode"))
        isf(isbool(()))
        isf(isbool([]))

    def test_isseq(self):
        _, ist, isf, _ = self.tf()

        isf(isseq(0))
        isf(isseq(0L))
        isf(isseq(None))
        isf(isseq(True))
        isf(isseq(False))
        isf(isseq("str"))
        isf(isseq(u"unicode"))
        ist(isseq(()))
        ist(isseq([]))

    def test_seqof(self):
        eq, ist, _, _ = self.tf()

        et = ()
        t = (1,2,3)
        el = []
        l = [1,2,3]

        eq([0], seqof(0))
        eq([0L], seqof(0L))
        eq([None], seqof(None))
        eq([True], seqof(True))
        eq([False], seqof(False))
        eq(["str"], seqof("str"))
        ist(isbytes(seqof("str")[0]))
        eq([u"unicode"], seqof(u"unicode"))
        ist(isunicode(seqof(u"unicode")[0]))
        eq(t, seqof(t))
        eq(l, seqof(l))
        ist(t is seqof(t))
        ist(l is seqof(l))
        eq(et, seqof(et))
        eq(el, seqof(el))
        ist(et is seqof(et))
        ist(el is seqof(el))

        eq(t, seqof(None, t))
        eq(l, seqof(None, l))
        ist(t is seqof(None, t))
        ist(l is seqof(None, l))
        eq(et, seqof(None, et))
        eq(el, seqof(None, el))
        ist(et is seqof(None, et))
        ist(el is seqof(None, el))

    def test_firstof(self):
        eq, _, _, _ = self.tf()

        et = ()
        t = (1,2,3)
        el = []
        l = [1,2,3]

        eq(None, firstof(et))
        eq(None, firstof(el))
        eq(1, firstof(t))
        eq(1, firstof(l))
        eq(0, firstof(0))
        eq(0L, firstof(0L))
        eq(None, firstof(None))
        eq(True, firstof(True))
        eq(False, firstof(False))
        eq("str", firstof("str"))
        eq(u"unicode", firstof(u"unicode"))

    def test_isbytes(self):
        _, ist, isf, _ = self.tf()

        isf(isbytes(0))
        isf(isbytes(0L))
        isf(isbytes(None))
        isf(isbytes(True))
        isf(isbytes(False))
        ist(isbytes("str"))
        isf(isbytes(u"unicode"))
        isf(isbytes(()))
        isf(isbytes([]))

    def test_isunicode(self):
        _, ist, isf, _ = self.tf()

        isf(isunicode(0))
        isf(isunicode(0L))
        isf(isunicode(None))
        isf(isunicode(True))
        isf(isunicode(False))
        isf(isunicode("str"))
        ist(isunicode(u"unicode"))
        isf(isunicode(()))
        isf(isunicode([]))

    def test_isstr(self):
        _, ist, isf, _ = self.tf()

        isf(isstr(0))
        isf(isstr(0L))
        isf(isstr(None))
        isf(isstr(True))
        isf(isstr(False))
        ist(isstr("str"))
        ist(isstr(u"unicode"))
        isf(isstr(()))
        isf(isstr([]))

    def test_all_matches(self):
        _, ist, isf, _ = self.tf()

        l1 = [1,2,3]
        l2 = [-3,-2,-1,1,2,3]
        l3 = [-3,-2,-1]
        positive = lambda n: n>0

        ist(all_matches(positive, l1))
        isf(all_matches(positive, l2))
        isf(all_matches(positive, l3))
        ist(all_matches(positive, 1))
        isf(all_matches(positive, -1))

    def test_one_match(self):
        _, ist, isf, _ = self.tf()

        l1 = [1,2,3]
        l2 = [-3,-2,-1,1,2,3]
        l3 = [-3,-2,-1]
        positive = lambda n: n>0

        ist(one_match(positive, l1))
        ist(one_match(positive, l2))
        isf(one_match(positive, l3))
        ist(one_match(positive, 1))
        isf(one_match(positive, -1))

    def test_strip_nl(self):
        eq, ist, _, _ = self.tf()

        eq(None, strip_nl(None))
        eq("", strip_nl(""))
        ist(isbytes(strip_nl("")))
        eq(u"", strip_nl(u""))
        ist(isunicode(strip_nl(u"")))

        eq("str", strip_nl("str"))
        ist(isbytes(strip_nl("str")))
        eq(u"unicode", strip_nl(u"unicode"))
        ist(isunicode(strip_nl(u"unicode")))

        eq("str", strip_nl("str\r"))
        ist(isbytes(strip_nl("str\r")))
        eq(u"unicode", strip_nl(u"unicode\r"))
        ist(isunicode(strip_nl(u"unicode\r")))

        eq("str", strip_nl("str\r\n"))
        ist(isbytes(strip_nl("str\r\n")))
        eq(u"unicode", strip_nl(u"unicode\r\n"))
        ist(isunicode(strip_nl(u"unicode\r\n")))

        eq("str", strip_nl("str\n"))
        ist(isbytes(strip_nl("str\n")))
        eq(u"unicode", strip_nl(u"unicode\n"))
        ist(isunicode(strip_nl(u"unicode\n")))

        eq("str\n", strip_nl("str\n\n"))
        ist(isbytes(strip_nl("str\n\n")))
        eq(u"unicode\n", strip_nl(u"unicode\n\n"))
        ist(isunicode(strip_nl(u"unicode\n\n")))

        eq("str\r\n", strip_nl("str\r\n\n"))
        ist(isbytes(strip_nl("str\r\n\n")))
        eq(u"unicode\r\n", strip_nl(u"unicode\r\n\n"))
        ist(isunicode(strip_nl(u"unicode\r\n\n")))

    def test_norm_nl(self):
        eq, ist, _, _ = self.tf()

        eq(None, norm_nl(None))
        eq("", norm_nl(""))
        ist(isbytes(norm_nl("")))
        eq(u"", norm_nl(u""))
        if check_version("2.5"): ist(isunicode(norm_nl(u"")))
        elif check_version("2.4"): ist(isbytes(norm_nl(u"")))
        elif check_version("2.3"): ist(isbytes(norm_nl(u"")))

        eq("str", norm_nl("str"))
        ist(isbytes(norm_nl("str")))
        eq(u"unicode", norm_nl(u"unicode"))
        ist(isunicode(norm_nl(u"unicode")))

        eq("str\n", norm_nl("str\r"))
        ist(isbytes(norm_nl("str\r")))
        eq(u"unicode\n", norm_nl(u"unicode\r"))
        ist(isunicode(norm_nl(u"unicode\r")))

        eq("str\n", norm_nl("str\r\n"))
        ist(isbytes(norm_nl("str\r\n")))
        eq(u"unicode\n", norm_nl(u"unicode\r\n"))
        ist(isunicode(norm_nl(u"unicode\r\n")))

        eq("str\n", norm_nl("str\n"))
        ist(isbytes(norm_nl("str\n")))
        eq(u"unicode\n", norm_nl(u"unicode\n"))
        ist(isunicode(norm_nl(u"unicode\n")))

        eq("str\n\n", norm_nl("str\n\n"))
        ist(isbytes(norm_nl("str\n\n")))
        eq(u"unicode\n\n", norm_nl(u"unicode\n\n"))
        ist(isunicode(norm_nl(u"unicode\n\n")))

        eq("str\n\n", norm_nl("str\r\n\n"))
        ist(isbytes(norm_nl("str\r\n\n")))
        eq(u"unicode\n\n", norm_nl(u"unicode\r\n\n"))
        ist(isunicode(norm_nl(u"unicode\r\n\n")))

    def test_make_getter(self): pass
    def test_make_setter(self): pass
    def test_make_deleter(self): pass
    def test_make_prop(self): pass

    def test_getattrs(self):
        eq, _, _, ex = self.tf()

        class O(object): pass
        a=O()
        a.b=O()
        a.b.c=O()
        a.x=1
        a.n=None
        a.b.y=2
        a.b.n=None
        a.b.c.z=3
        a.b.c.n=None

        eq(1, getattrs(a, "x"))
        ex(AttributeError, getattrs, a, "x.m")
        eq(None, getattrs(a, "n"))
        eq(None, getattrs(a, "n.m"))
        eq(2, getattrs(a, "b.y"))
        ex(AttributeError, getattrs, a, "b.y.m")
        eq(None, getattrs(a, "b.n"))
        eq(None, getattrs(a, "b.n.m"))
        eq(3, getattrs(a, "b.c.z"))
        ex(AttributeError, getattrs, a, "b.c.z.m")
        eq(None, getattrs(a, "b.c.n"))
        eq(None, getattrs(a, "b.c.n.m"))

        eq(1, getattrs(a, "x", True))
        ex(AttributeError, getattrs, a, "x.m", True)
        eq(None, getattrs(a, "n", True))
        ex(AttributeError, getattrs, a, "n.m", True)
        eq(2, getattrs(a, "b.y", True))
        ex(AttributeError, getattrs, a, "b.y.m", True)
        eq(None, getattrs(a, "b.n", True))
        ex(AttributeError, getattrs, a, "b.n.m", True)
        eq(3, getattrs(a, "b.c.z", True))
        ex(AttributeError, getattrs, a, "b.c.z.m", True)
        eq(None, getattrs(a, "b.c.n", True))
        ex(AttributeError, getattrs, a, "b.c.n.m", True)

    def test_setattrs(self): pass
    def test_delattrs(self): pass
    def test_make_delegate(self): pass
    def test_get__all__(self): pass

def suite():
    from base import TestLoader
    return TestLoader().loadTestsFromTestCase(ClassTest)

if __name__ == '__main__':
    from base import main
    main()
