예제 #1
0
    def test_get_handler_for(self):
        from mobilize.exceptions import NoMatchingHandlerException
        # test templates
        t_a = mobilize.Moplate([], template=gtt('a.html'))
        t_b = mobilize.Moplate([], template=gtt('b.html'))
        t_c = mobilize.Moplate([], template=gtt('c.html'))
        t_d = mobilize.Moplate([], template=gtt('d.html'))

        mapping = [
            (r'/alpha/',    t_a),
            (r'/beta/$',    t_b),
            (r'/beta/',     t_c),
            (r'/\w+$',      t_d),
            ]
        hmap = mobilize.HandlerMap(mapping)
        def matching(url):
            # the name of the moplate actually matched
            h = hmap.get_handler_for(url)
            return h.template.name

        self.assertEqual('a.html', matching('/alpha/'))
        self.assertEqual('b.html', matching('/beta/'))
        self.assertEqual('c.html', matching('/beta/flava/'))
        self.assertEqual('d.html', matching('/foobar'))
        self.assertRaises(NoMatchingHandlerException, matching, '/no/such/url/')
예제 #2
0
 def test_params(self):
     # Expect the mobilize.Moplate ctor to loudly fail if we try to pass in controlled parameters
     ok = False
     try:
         moplate = mobilize.Moplate([], {'elements' : ['a', 'b']} , template=gtt('a.html'))
     except AssertionError:
         ok = True
     self.assertTrue(ok)
예제 #3
0
 def test_ctor_template(self):
     '''
     test that Moplate ctor can automatically create a Template from a name
     '''
     m_name = TestMoplate([], template='a.html')
     m_template = TestMoplate([], template=gtt('a.html'))
     # select the first matching template by name
     m_multi_a = TestMoplate([], template=['a.html', 'b.html'])
     self.assertEqual('a.html', m_multi_a.template.name)
     m_multi_b = TestMoplate([], template=['b.html', 'a.html'])
     self.assertEqual('b.html', m_multi_b.template.name)
     # if no match for first named template, fall back to second
     m_multi_a2 = TestMoplate([], template=['doesnotexist.html', 'a.html'])
     self.assertEqual('a.html', m_multi_a2.template.name)
예제 #4
0
    def test_render(self):
        moplate = mobilize.Moplate([], template=gtt('a.html'))
        expected = 'abc xyz'
        actual = moplate.render(MINIMAL_HTML_DOCUMENT)
        self.assertEqual(expected, actual)

        moplate = mobilize.Moplate([], {'a' : 42}, template=gtt('b.html'))
        expected = 'a: 42'
        actual = moplate.render(MINIMAL_HTML_DOCUMENT)
        self.assertEqual(expected, actual)

        params = {
            'a' : 42,
            'elems' : ['X', 'Y', 'Z'],
            }
        moplate = mobilize.Moplate([], params, template=gtt('c.html'))
        expected = 'a: 42\nX\nY\nZ\n'
        actual = moplate.render(MINIMAL_HTML_DOCUMENT)
        self.assertEqual(expected, actual)

        params = {
            'a' : 42,
            'elems' : ['X', 'Y', 'Z'],
            }
        moplate = mobilize.Moplate([], params, template=gtt('c.html'))
        expected = 'a: 84\nX\nY\nZ\n'
        actual = moplate.render(MINIMAL_HTML_DOCUMENT, {'a' : 84})
        self.assertEqual(expected, actual)

        # override params with extra_params
        params = {
            'a' : 42,
            'elems' : ['X', 'Y', 'Z'],
            }
        moplate = mobilize.Moplate([], params, template=gtt('c.html'))
        expected = 'a: 84\nX\nY\nZ\n'
        actual = moplate.render(MINIMAL_HTML_DOCUMENT, {'a' : 84})
        self.assertEqual(expected, actual)

        # add in extra_params (no clobbering)
        params = {
            'a' : 42,
            'b' : 21,
            'elems' : ['X', 'Y', 'Z'],
            }
        moplate = mobilize.Moplate([], params, template=gtt('c.html'))
        expected = 'a: 84\nX\nY\nZ\nb: 21'
        actual = moplate.render(MINIMAL_HTML_DOCUMENT, {'a' : 84})
        self.assertEqual(expected, actual)
예제 #5
0
    def test_render(self):
        full_body = '''<!doctype html>
<html>
  <title>Test Page Content</title>
  <body>
    <div id="header">
      <img src="/logo.png" alt="Acme"/>
      <h1>Acme Services</h1>
    </div>
    <ul class="navigation">
      <li><a href="/">Home</a></li>
      <li><a href="/services/">Services</a></li>
      <li><a href="/about/">About</a></li>
      <li><a href="/contact/">Contact Us</a></li>
    </ul>
    <!-- main page content -->
    <div id="main-content">
      <h2>Services by Acme</h2>
      <p>Lorem ipsum</p>
      <p>forum gypsum</p>
    </div>
    <!-- sidebar -->
    <div id="sidebar">
      <h2>Acme News</h2>
      <p>Our new product has launched! <a href="/press-releases/2020-04-01/acme-new-product/">Learn More</a></p>
    </div>
  </body>
</html>
'''
        mobile_body = '''<!doctype html>
<html>
  <head>
    <title>&lt;Mobile Test One&gt;</title>
    <meta http-equiv="HandheldFriendly" content="True" />
  </head>
  <body>
    <div class="mwu-elem" id="mwu-elem-0">
      <div id="header">
        <img src="/logo.png" alt="Acme">
        <h1>Acme Services</h1>
      </div>
    </div>
    <div class="mwu-elem" id="mwu-elem-1">
      <ul class="navigation">
        <li><a href="/">Home</a></li>
        <li><a href="/services/">Services</a></li>
        <li><a href="/about/">About</a></li>
        <li><a href="/contact/">Contact Us</a></li>
      </ul>
    </div>
    <div class="mwu-elem" id="mwu-elem-2">
      <div id="main-content">
        <h2>Services by Acme</h2>
        <p>Lorem ipsum</p>
        <p>forum gypsum</p>
      </div>
    </div>
      <div class="custom-elem"><a href="http://mobilewebup.com">Mobile Websites</a> by Mobile Web Up</div>
<div class="mwu-mfoot">
  <a href="http://www.example.com">Full Site</a>
</div>
  </body>
</html>
'''
        from utils4test import TEST_TEMPLATE_DIR
        from mobilize.components import (
            FromTemplate,
            XPath,
            CssPath,
            RawString,
            )
        components = [
            XPath(r'//*[@id="header"]'),
            CssPath('ul.navigation'),
            CssPath('div#main-content'),
            RawString('<div class="custom-elem"><a href="http://mobilewebup.com">Mobile Websites</a> by Mobile Web Up</div>'),
            FromTemplate('footer1.html', {'full_site_url' : 'http://www.example.com'}, template_dirs=[TEST_TEMPLATE_DIR]),
            ]
        params = {
            'title'        : '&lt;Mobile Test One&gt;',
            'fullsite'     : 'example.com',
            'request_path' : '/foo',
            }
        moplate = mobilize.Moplate(components, params, template=gtt('one.html'))
        hmap = mobilize.HandlerMap([('/foo$', moplate)])
        domains = mobilize.Domains(mobile='m.example.com', desktop='example.com')
        msite = mobilize.MobileSite(domains, hmap)

        expected = mobile_body
        actual = moplate.render(full_body, params)
        self.assertSequenceEqual(norm_html(expected), norm_html(actual))