Gael Pasgrimaud avatar Gael Pasgrimaud committed 63b9883

add tests

Comments (0)

Files changed (3)

pyquery/README.txt

 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)
 

pyquery/pyquery.py

 
     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)
 # 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)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.