2015-05-18 03:48:56 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from boltons.funcutils import (copy_function,
|
2016-07-19 05:11:47 +00:00
|
|
|
total_ordering,
|
2019-11-06 02:27:02 +00:00
|
|
|
format_invocation,
|
2015-05-18 03:48:56 +00:00
|
|
|
InstancePartial,
|
2021-02-22 06:35:25 +00:00
|
|
|
CachedInstancePartial,
|
|
|
|
noop)
|
2015-05-18 03:48:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Greeter(object):
|
|
|
|
def __init__(self, greeting):
|
|
|
|
self.greeting = greeting
|
|
|
|
|
|
|
|
def greet(self, excitement='.'):
|
|
|
|
return self.greeting.capitalize() + excitement
|
|
|
|
|
|
|
|
partial_greet = InstancePartial(greet, excitement='!')
|
|
|
|
cached_partial_greet = CachedInstancePartial(greet, excitement='...')
|
|
|
|
|
|
|
|
def native_greet(self):
|
|
|
|
return self.greet(';')
|
|
|
|
|
|
|
|
|
|
|
|
class SubGreeter(Greeter):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def test_partials():
|
|
|
|
g = SubGreeter('hello')
|
|
|
|
|
|
|
|
assert g.greet() == 'Hello.'
|
|
|
|
assert g.native_greet() == 'Hello;'
|
|
|
|
assert g.partial_greet() == 'Hello!'
|
|
|
|
assert g.cached_partial_greet() == 'Hello...'
|
|
|
|
assert CachedInstancePartial(g.greet, excitement='s')() == 'Hellos'
|
|
|
|
|
2020-03-23 18:54:15 +00:00
|
|
|
g.native_greet = 'native reassigned'
|
|
|
|
assert g.native_greet == 'native reassigned'
|
|
|
|
|
|
|
|
g.partial_greet = 'partial reassigned'
|
|
|
|
assert g.partial_greet == 'partial reassigned'
|
|
|
|
|
|
|
|
g.cached_partial_greet = 'cached_partial reassigned'
|
|
|
|
assert g.cached_partial_greet == 'cached_partial reassigned'
|
|
|
|
|
2015-05-18 03:48:56 +00:00
|
|
|
|
|
|
|
def test_copy_function():
|
|
|
|
def callee():
|
|
|
|
return 1
|
|
|
|
callee_copy = copy_function(callee)
|
|
|
|
assert callee is not callee_copy
|
|
|
|
assert callee() == callee_copy()
|
2016-07-19 05:11:47 +00:00
|
|
|
|
|
|
|
|
2018-12-10 19:52:33 +00:00
|
|
|
def test_total_ordering():
|
2016-07-19 05:11:47 +00:00
|
|
|
@total_ordering
|
|
|
|
class Number(object):
|
|
|
|
def __init__(self, val):
|
|
|
|
self.val = int(val)
|
|
|
|
|
|
|
|
def __gt__(self, other):
|
|
|
|
return self.val > other
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return self.val == other
|
|
|
|
|
|
|
|
num = Number(3)
|
|
|
|
assert num > 0
|
|
|
|
assert num == 3
|
|
|
|
|
|
|
|
assert num < 5
|
|
|
|
assert num >= 2
|
|
|
|
assert num != 1
|
2019-11-06 02:27:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_format_invocation():
|
|
|
|
assert format_invocation('d') == "d()"
|
|
|
|
assert format_invocation('f', ('a', 'b')) == "f('a', 'b')"
|
|
|
|
assert format_invocation('g', (), {'x': 'y'}) == "g(x='y')"
|
|
|
|
assert format_invocation('h', ('a', 'b'), {'x': 'y', 'z': 'zz'}) == "h('a', 'b', x='y', z='zz')"
|
2021-02-22 06:35:25 +00:00
|
|
|
|
|
|
|
def test_noop():
|
|
|
|
assert noop() is None
|
|
|
|
assert noop(1, 2) is None
|
|
|
|
assert noop(a=1, b=2) is None
|