test_utils.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. # -*- coding: utf-8 -*-
  2. '''stuf utility tests'''
  3. from stuf.six import unittest
  4. class TestUtils(unittest.TestCase):
  5. def test_attr_or_item(self):
  6. from collections import namedtuple
  7. from stuf.deep import attr_or_item
  8. Test = namedtuple('Test', 'a')
  9. foo = Test(a=2)
  10. foo2 = dict(a=2)
  11. self.assertEqual(attr_or_item(foo, 'a'), attr_or_item(foo2, 'a'))
  12. def test_setdefault(self):
  13. from stuf import stuf
  14. from stuf.deep import setdefault
  15. foo = stuf(a=1)
  16. self.assertEqual(setdefault(foo, 'a', 1), 1)
  17. self.assertEqual(setdefault(foo, 'b', 2), 2)
  18. def test_clsname(self):
  19. from stuf import stuf
  20. from stuf.deep import clsname
  21. foo = stuf(a=2)
  22. self.assertEqual(clsname(foo), 'stuf')
  23. def test_deepget(self):
  24. from stuf import stuf
  25. from stuf.deep import deepget
  26. foo = stuf(a=stuf(b=(stuf(f=stuf(g=1)))))
  27. self.assertEqual(deepget(foo, 'a.b.f.g'), 1)
  28. self.assertEqual(deepget(foo, 'a.b.f.e'), None)
  29. def test_deleter(self):
  30. from stuf import stuf
  31. from stuf.deep import deleter
  32. foo = stuf(a=1)
  33. self.assertEqual(deleter(foo, 'a'), None)
  34. def test_getcls(self):
  35. from stuf import stuf
  36. from stuf.deep import getcls
  37. foo = stuf(a=1)
  38. self.assertEqual(getcls(foo), stuf)
  39. def test_getter(self):
  40. from stuf import stuf
  41. from stuf.deep import getter
  42. foo = stuf(a=1)
  43. self.assertEqual(getter(foo, 'a'), 1)
  44. self.assertEqual(getter(stuf, 'items'), stuf.items)
  45. def test_selfname(self):
  46. from stuf.deep import selfname
  47. class Foo(object): #@IgnorePep8
  48. pass
  49. class Foo2: #@IgnorePep8
  50. pass
  51. self.assertEqual(selfname(Foo), 'Foo')
  52. self.assertEqual(selfname(Foo2), 'Foo2')
  53. def test_setter(self):
  54. from stuf.deep import setter
  55. class Foo(object): #@IgnorePep8
  56. pass
  57. foo = Foo()
  58. self.assertEqual(setter(Foo, 'a', 1), 1)
  59. self.assertEqual(setter(foo, 'b', 1), 1)
  60. def test_deferfunc(self):
  61. from stuf.six import next
  62. from stuf.iterable import deferfunc
  63. deferred = deferfunc(lambda: 1)
  64. self.assertEqual(next(deferred), 1)
  65. def test_deferiter(self):
  66. from stuf.six import next
  67. from stuf.iterable import deferiter
  68. deferred = deferiter(iter([1, 2, 3]))
  69. self.assertEqual(next(deferred), 1)
  70. def test_count(self):
  71. from stuf.iterable import count
  72. self.assertEqual(count([1, 2, 3]), 3)
  73. def test_breakcount(self):
  74. from stuf.six import next
  75. from functools import partial
  76. from stuf.iterable import breakcount
  77. deferred = breakcount(partial(next, iter([1, 2, 3])), 2)
  78. self.assertEqual(list(deferred), [1, 2])
  79. def test_iterexcept(self):
  80. from stuf.six import next
  81. from functools import partial
  82. from stuf.iterable import iterexcept
  83. deferred = iterexcept(
  84. partial(next, iter([1, 2, 3])), StopIteration, lambda: 1,
  85. )
  86. self.assertEqual(list(deferred), [1, 1, 2, 3])
  87. def test_exhaustmap(self):
  88. from stuf import exhaustmap
  89. deferred = exhaustmap(lambda x: x + x, iter([1, 2, 3]), StopIteration)
  90. self.assertIsNone(deferred)
  91. def test_exhauststar(self):
  92. from stuf.iterable import exhauststar
  93. deferred = exhauststar(lambda x, y: x + y, iter([(1, 2), (3, 4)]))
  94. self.assertIsNone(deferred)
  95. def test_exhaustitems(self):
  96. from stuf import exhaustitems
  97. deferred = exhaustitems(lambda x, y: x + y, {1: 2})
  98. self.assertIsNone(deferred)
  99. def test_lazy_class(self):
  100. from stuf.desc import lazy_class
  101. class Foo(object): #@IgnorePep8
  102. @lazy_class
  103. def this(self):
  104. return self
  105. self.assertEqual(Foo, Foo.this)
  106. def test_lazy_set(self):
  107. from stuf.desc import lazyset
  108. class Foo(object): #@IgnorePep8
  109. @lazyset
  110. def this(self):
  111. return self._foo + 1
  112. @this.setter #@IgnorePep8
  113. def this(self, this):
  114. self._foo = this
  115. foo = Foo()
  116. foo.this = 1
  117. self.assertEqual(foo.this, 2)
  118. del foo.this
  119. def test_twoway(self):
  120. from stuf.desc import twoway
  121. class Foo(object): #@IgnorePep8
  122. @twoway
  123. def this(self):
  124. return 1
  125. @this.expression #@IgnorePep8
  126. def this(self):
  127. return 2
  128. self.assertEqual(Foo.this, 2)
  129. foo = Foo()
  130. self.assertEqual(foo.this, 1)
  131. def test_either(self):
  132. from stuf.desc import either
  133. class Foo(object): #@IgnorePep8
  134. @either
  135. def this(self):
  136. return 1
  137. @this.expression #@IgnorePep8
  138. def this(self):
  139. return 2
  140. self.assertEqual(Foo.this, 2)
  141. foo = Foo()
  142. self.assertEqual(foo.this, 2)
  143. def test_both(self):
  144. from stuf.desc import both
  145. class Foo(object): #@IgnorePep8
  146. @both
  147. def this(self):
  148. return 1
  149. @this.expression #@IgnorePep8
  150. def this(self):
  151. return 2
  152. self.assertEqual(Foo.this, 2)
  153. foo = Foo()
  154. self.assertEqual(foo.this, 1)
  155. def test_lazyimport(self):
  156. from stuf.six import callable
  157. from stuf.utils import lazyimport
  158. fsum = lazyimport('math.fsum')
  159. self.assertTrue(callable(fsum))
  160. fsum = lazyimport('math', 'fsum')
  161. self.assertTrue(callable(fsum))
  162. def test_checkname(self):
  163. from stuf.base import checkname
  164. self.assertEqual(checkname('from'), 'from_')
  165. def test_sluggify(self):
  166. from stuf.utils import sluggify
  167. self.assertEqual(sluggify('This is a slug'), 'this-is-a-slug')
  168. def test_lru(self):
  169. from stuf.utils import lru, sluggify
  170. slug = lru(2)(sluggify)
  171. self.assertEqual(slug('This is a slug'), 'this-is-a-slug')
  172. self.assertEqual(slug('This is a plug'), 'this-is-a-plug')
  173. self.assertEqual(slug('This is a flug'), 'this-is-a-flug')
  174. self.assertEqual(slug('This is a dug'), 'this-is-a-dug')
  175. self.assertEqual(slug('This is a slug'), 'this-is-a-slug')
  176. self.assertEqual(slug('This is a plug'), 'this-is-a-plug')
  177. self.assertEqual(slug('This is a flug'), 'this-is-a-flug')
  178. self.assertEqual(slug('This is a dug'), 'this-is-a-dug')
  179. slug = lru(None)(sluggify)
  180. self.assertEqual(slug('This is a slug'), 'this-is-a-slug')
  181. self.assertEqual(slug('This is a plug'), 'this-is-a-plug')
  182. self.assertEqual(slug('This is a flug'), 'this-is-a-flug')
  183. self.assertEqual(slug('This is a slug'), 'this-is-a-slug')
  184. self.assertEqual(slug('This is a plug'), 'this-is-a-plug')
  185. self.assertEqual(slug('This is a dug'), 'this-is-a-dug')
  186. def test_ascii(self):
  187. from stuf.six import u, b, tobytes
  188. self.assertEqual(
  189. [tobytes(i, 'ascii') for i in [[1], True, r't', b('i'), u('g'), None, (1,)]],
  190. [b('[1]'), b('True'), b('t'), b('i'), b('g'), b('None'), b('(1,)')]
  191. )
  192. def test_bytes(self):
  193. from stuf.six import u, b, tobytes
  194. self.assertEqual(
  195. [tobytes(i) for i in [[1], True, r't', b('i'), u('g'), None, (1,)]],
  196. [b('[1]'), b('True'), b('t'), b('i'), b('g'), b('None'), b('(1,)')]
  197. )
  198. def test_unicode(self):
  199. from stuf.six import u, b, tounicode
  200. self.assertEqual(
  201. [tounicode(i) for i in [[1], True, r't', b('i'), u('g'), None, (1,)]],
  202. [u('[1]'), u('True'), u('t'), u('i'), u('g'), u('None'), u('(1,)')]
  203. )
  204. if __name__ == '__main__':
  205. unittest.main()