nutools/lib/pyulib/test/test_base/test_base.py

331 lines
9.1 KiB
Python
Raw Normal View History

2021-02-24 10:55:56 +04:00
#!/usr/bin/env python2
2013-08-27 15:14:44 +04:00
# -*- 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]))
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
def test_updated(self):
eq, ist, isf, _ = self.tf()
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
eq({"n": "v"}, updated(None, n="v"))
eq({"n": "v"}, updated({}, n="v"))
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
d = {"k": "v"}
eq({"n": "v", "k": "v"}, updated(d, n="v"))
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
d = {"n": "v"}
eq({"n": "v"}, updated(d, n="v"))
ist(d == updated(d, n="v"))
isf(d is updated(d, n="v"))
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
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([]))
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
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([]))
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
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([]))
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
def test_seqof(self):
eq, ist, _, _ = self.tf()
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
et = ()
t = (1,2,3)
el = []
l = [1,2,3]
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
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))
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
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))
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
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([]))
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
def test_all_matches(self):
_, ist, isf, _ = self.tf()
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
l1 = [1,2,3]
l2 = [-3,-2,-1,1,2,3]
l3 = [-3,-2,-1]
positive = lambda n: n>0
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
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))
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
def test_one_match(self):
_, ist, isf, _ = self.tf()
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
l1 = [1,2,3]
l2 = [-3,-2,-1,1,2,3]
l3 = [-3,-2,-1]
positive = lambda n: n>0
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
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))
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
def test_strip_nl(self):
eq, ist, _, _ = self.tf()
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
eq(None, strip_nl(None))
eq("", strip_nl(""))
ist(isbytes(strip_nl("")))
eq(u"", strip_nl(u""))
ist(isunicode(strip_nl(u"")))
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
eq("str", strip_nl("str"))
ist(isbytes(strip_nl("str")))
eq(u"unicode", strip_nl(u"unicode"))
ist(isunicode(strip_nl(u"unicode")))
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
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")))
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
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")))
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
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")))
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
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")))
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
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()
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
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"")))
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
eq("str", norm_nl("str"))
ist(isbytes(norm_nl("str")))
eq(u"unicode", norm_nl(u"unicode"))
ist(isunicode(norm_nl(u"unicode")))
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
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")))
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
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")))
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
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")))
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
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")))
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
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")))
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
def test_make_getter(self): pass
def test_make_setter(self): pass
def test_make_deleter(self): pass
def test_make_prop(self): pass
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
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"))
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
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)
2015-08-20 07:58:17 +04:00
2013-08-27 15:14:44 +04:00
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()