Skip to content

Commit

Permalink
support for keyword arguments in PyQuery custom functions (PyQuery.fn…
Browse files Browse the repository at this point in the history
…); hook tests
  • Loading branch information
randomir committed Jul 4, 2014
1 parent 9614f4f commit 1846c06
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pyquery/pyquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -1393,9 +1393,9 @@ class Fn(object):
"""
def __setattr__(self, name, func):
def fn(self, *args):
def fn(self, *args, **kwargs):
func_globals(func)['this'] = self
return func(*args)
return func(*args, **kwargs)
fn.__name__ = name
setattr(PyQuery, name, fn)
fn = Fn()
Expand Down
26 changes: 26 additions & 0 deletions tests/test_pyquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,32 @@ def test_parameterless_callback(self):
['Coffee', 'Tea', 'Milk'])


class TestHook(TestCase):
html = """
<ol>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
"""

def test_fn(self):
"Example from `PyQuery.Fn` docs."
fn = lambda: this.map(lambda i, el: pq(this).outerHtml())
pq.fn.listOuterHtml = fn
S = pq(self.html)
self.assertEqual(S('li').listOuterHtml(),
['<li>Coffee</li>', '<li>Tea</li>', '<li>Milk</li>'])

def test_fn_with_kwargs(self):
"fn() with keyword arguments."
pq.fn.test = lambda p=1: pq(this).eq(p)
S = pq(self.html)
self.assertEqual(S('li').test(0).text(), 'Coffee')
self.assertEqual(S('li').test().text(), 'Tea')
self.assertEqual(S('li').test(p=2).text(), 'Milk')


class TestAjaxSelector(TestSelector):
klass = pqa

Expand Down

0 comments on commit 1846c06

Please sign in to comment.