def test_relevant_filter(self): ''' Tests for Filter.relevant ''' from mobilize.filters import filterapi, Filter from mobilize.components import CssPath # test filters # The three test filters t1, t2, and t3 mark a div with an attribute. # Only t1 and t3 are meant to be relevant; t2 is not. @filterapi def tf1(elem): elem.attrib['tf1'] = '1' class TF2(Filter): def __call__(self, elem): elem.attrib['tf2'] = '2' def relevant(self, reqinfo): return False tf2 = TF2() class TF3(Filter): def __call__(self, elem): elem.attrib['tf3'] = '3' def relevant(self, reqinfo): return True tf3 = TF3() root = html.fromstring('<div id="foo">Hello.</div>') component = CssPath('div#foo') component.extract(root) actual = component.process('idname', extra_filters=[tf1, tf2, tf3]) actual_str = html.tostring(actual).decode('utf-8') self.assertSequenceEqual('<div class="mwu-elem" id="idname"><div id="foo" tf1="1" tf3="3">Hello.</div></div>', actual_str)
def test_process_idname(self): from mobilize.components import CssPath, XPath src_html = '''<div> <nav> <a href="/A">A</a> <a href="/B">B</a> </nav> ''' def prep_component(**kw): return c # check that default_idname is required if self.idname not defined c1 = CssPath('nav') c1.extract(html.fromstring(src_html)) with self.assertRaises(AssertionError): c1.process() # check that idname argument c2 = CssPath('nav', idname='foo') c2.extract(html.fromstring(src_html)) c2.process() # no AssertionError on this line meanst the test passes # check that default_idname supresses the error c3 = CssPath('nav') c3.extract(html.fromstring(src_html)) c3.process('foo') # no AssertionError on this line meanst the test passes
def test_select_multiple(self): ''' Test that extracted components can accept multiple selectors ''' from mobilize.components import CssPath, XPath selectors = [ 'nav', 'section', ] src_html = '''<div> <nav> <a href="/A">A</a> <a href="/B">B</a> </nav> <table><tr><td> </td><td>I'm using tables for layout!!! DUR</td></tr></table> <section> <p>Hello.</p> </section> </div> ''' expected_html = '''<div class="mwu-elem" id="foo"> <nav> <a href="/A">A</a> <a href="/B">B</a> </nav> <section> <p>Hello.</p> </section> </div>''' # test for CssPath css_component = CssPath(selectors, idname='foo') css_component.extract(html.fromstring(src_html)) extracted = css_component.process() extracted_str = html.tostring(extracted) self.assertSequenceEqual(normxml(expected_html), normxml(extracted_str)) # test for XPath x_component = XPath(selectors, idname='foo') x_component.extract(html.fromstring(src_html)) extracted = x_component.process() extracted_str = html.tostring(extracted) self.assertSequenceEqual(normxml(expected_html), normxml(extracted_str))