1. Olivier Lauzanne
  2. pyquery

Commits

Gael Pasgrimaud  committed 63b9883

add tests

  • Participants
  • Parent commits 0de160a
  • Branches default

Comments (0)

Files changed (3)

File pyquery/README.txt

View file
 It can be used for many purposes, one idea that I might try in the future is to
 use it for templating with pure http templates that you modify using pyquery.
 
-You can use the PyQuery class to load an xml document from a string, from a
-file or from an url::
+You can use the PyQuery class to load an xml document from a string, a lxml
+document, from a file or from an url::
 
     >>> from pyquery import PyQuery
+    >>> from lxml import etree
     >>> d = PyQuery("<html></html>")
+    >>> d = PyQuery(etree.fromstring("<html></html>"))
     >>> d = PyQuery(url='http://w3c.org/')
     >>> d = PyQuery(filename=path_to_html_file)
 

File pyquery/pyquery.py

View file
 
     def __call__(self, *args):
         # just return a new instance
+        length = len(args)
+        if length == 0:
+            raise ValueError('You must provide at least a selector')
         if len(args) == 1:
             args += (self,)
         return self.__class__(*args)

File pyquery/test.py

View file
 # Copyright (C) 2008 - Olivier Lauzanne <olauzanne@gmail.com>
 #
 # Distributed under the BSD license, see LICENSE.txt
+import unittest
 import doctest
+from lxml import etree
 import os
 
+from pyquery import PyQuery as pq
+
 dirname = os.path.dirname(__file__)
 path_to_html_file = os.path.join(dirname, 'test.html')
 
         test = self._dt_test
         test.globs.update(globals())
 
+class TestSelector(unittest.TestCase):
+
+    html = """
+           <html>
+            <body>
+              <div>node1</div>
+              <div id="node2">node2</div>
+              <div class="node3">node3</div>
+            </body>
+           </html>
+           """
+
+    html2 = """
+           <html>
+            <body>
+              <div>node1</div>
+            </body>
+           </html>
+           """
+    def test_selector_from_doc(self):
+        doc = etree.fromstring(self.html)
+        assert len(pq(doc)) == 1
+        assert len(pq('div', doc)) == 3
+        assert len(pq('div#node2', doc)) == 1
+
+    def test_selector_from_html(self):
+        assert len(pq(self.html)) == 1
+        assert len(pq('div', self.html)) == 3
+        assert len(pq('div#node2', self.html)) == 1
+
+    def test_selector_from_obj(self):
+        e = pq(self.html)
+        assert len(e('div')) == 3
+        assert len(e('div#node2')) == 1
+
+    def test_selector_from_html_from_obj(self):
+        e = pq(self.html)
+        assert len(e('div', self.html2)) == 1
+        assert len(e('div#node2', self.html2)) == 0
+
+
 def main():
     doctest.testfile('README.txt', globs=globals(),
                      optionflags=doctest.ELLIPSIS)