Exemple #1
0
def test_rendering_base_url():
    foo = Library('foo', '')
    x1 = Resource(foo, 'a.js')
    x2 = Resource(foo, 'b.css')
    y1 = Resource(foo, 'c.js', depends=[x1, x2])

    needed = NeededResources()
    needed.need(y1)
    assert needed.render() == '''\
<link rel="stylesheet" type="text/css" href="/fanstatic/foo/b.css" />
<script type="text/javascript" src="/fanstatic/foo/a.js"></script>
<script type="text/javascript" src="/fanstatic/foo/c.js"></script>'''

    needed = NeededResources(base_url='http://localhost/static')
    needed.need(y1)
    assert needed.render() == '''\
<link rel="stylesheet" type="text/css" href="http://localhost/static/fanstatic/foo/b.css" />
<script type="text/javascript" src="http://localhost/static/fanstatic/foo/a.js"></script>
<script type="text/javascript" src="http://localhost/static/fanstatic/foo/c.js"></script>'''
    # The base_url has been set.
    assert needed.has_base_url()

    needed.set_base_url('foo')
    # The base_url can only be set once.
    assert needed._base_url == 'http://localhost/static'
Exemple #2
0
def test_rendering_base_url():
    foo = Library('foo', '')
    x1 = Resource(foo, 'a.js')
    x2 = Resource(foo, 'b.css')
    y1 = Resource(foo, 'c.js', depends=[x1, x2])

    needed = NeededResources()
    needed.need(y1)
    assert needed.render() == '''\
<link rel="stylesheet" type="text/css" href="/fanstatic/foo/b.css" />
<script type="text/javascript" src="/fanstatic/foo/a.js"></script>
<script type="text/javascript" src="/fanstatic/foo/c.js"></script>'''

    needed = NeededResources(base_url='http://localhost/static')
    needed.need(y1)
    assert needed.render() == '''\
<link rel="stylesheet" type="text/css" href="http://localhost/static/fanstatic/foo/b.css" />
<script type="text/javascript" src="http://localhost/static/fanstatic/foo/a.js"></script>
<script type="text/javascript" src="http://localhost/static/fanstatic/foo/c.js"></script>'''
    # The base_url has been set.
    assert needed.has_base_url()

    needed.set_base_url('foo')
    # The base_url can only be set once.
    assert needed._base_url == 'http://localhost/static'
Exemple #3
0
def test_slot_depends_incorrect():
    needed = NeededResources()

    lib = Library('lib', '')

    c = Resource(lib, 'c.js')
    slot = Slot(lib, '.js', depends=[c])
    a = Resource(lib, 'a.js', depends=[slot])
    d = Resource(lib, 'd.js')
    b = Resource(lib, 'b.js', depends=[d])

    needed.need(a, {slot: b})

    with pytest.raises(SlotError):
        needed.render()
Exemple #4
0
def test_slot_depends_incorrect():
    needed = NeededResources()

    lib = Library('lib', '')

    c = Resource(lib, 'c.js')
    slot = Slot(lib, '.js', depends=[c])
    a = Resource(lib, 'a.js', depends=[slot])
    d = Resource(lib, 'd.js')
    b = Resource(lib, 'b.js', depends=[d])
    
    needed.need(a, {slot: b})

    with pytest.raises(SlotError):
        needed.render()
Exemple #5
0
def test_custom_renderer_for_resource():
    foo = Library('foo', '')
    from fanstatic.core import render_print_css

    a = Resource(foo, 'printstylesheet.css', renderer=render_print_css)
    needed = NeededResources()
    needed.need(a)
    assert needed.render() == """\
<link rel="stylesheet" type="text/css" href="/fanstatic/foo/printstylesheet.css" media="print" />"""

    def render_unknown(url):
        return '<unknown href="%s"/>' % url

    b = Resource(foo, 'nothing.unknown', renderer=render_unknown)
    needed.need(b)
    assert needed.render() == """\
Exemple #6
0
def test_custom_renderer_for_resource():
    foo = Library('foo', '')
    from fanstatic.core import render_print_css

    a = Resource(foo, 'printstylesheet.css', renderer=render_print_css)
    needed = NeededResources()
    needed.need(a)
    assert needed.render() == """\
<link rel="stylesheet" type="text/css" href="/fanstatic/foo/printstylesheet.css" media="print" />"""

    def render_unknown(url):
        return '<unknown href="%s"/>' % url

    b = Resource(foo, 'nothing.unknown', renderer=render_unknown)
    needed.need(b)
    assert needed.render() == """\
def test_render_bundle():
    foo = Library('foo', '')
    x1 = Resource(foo, 'a.css')
    x2 = Resource(foo, 'b.css')
    x3 = Resource(foo, 'c.css', dont_bundle=True)
    needed = NeededResources(resources=[x1, x2, x3])
    assert needed.render() == '''<link rel="stylesheet" type="text/css" href="/fanstatic/foo/a.css" />
<link rel="stylesheet" type="text/css" href="/fanstatic/foo/b.css" />
<link rel="stylesheet" type="text/css" href="/fanstatic/foo/c.css" />'''

    needed = NeededResources(resources=[x1, x2, x3], bundle=True)
    assert needed.render() == '''<link rel="stylesheet" type="text/css" href="/fanstatic/foo/:bundle:a.css;b.css" />
<link rel="stylesheet" type="text/css" href="/fanstatic/foo/c.css" />'''

    x4 = Resource(foo, 'subdir/subdir/x4.css')
    x5 = Resource(foo, 'subdir/subdir/x5.css')
    needed = NeededResources(resources=[x1, x2, x4, x5], bundle=True)
    assert needed.render() == '''<link rel="stylesheet" type="text/css" href="/fanstatic/foo/:bundle:a.css;b.css" />
Exemple #8
0
def test_empty_base_url_and_publisher_signature():
    ''' When the base_url is not set and the publisher_signature is an empty string,
    render a URL without them. '''
    foo = Library('foo', '')
    x1 = Resource(foo, 'a.js')
    needed = NeededResources(publisher_signature='')
    needed.need(x1)

    assert needed.render() == '''\
Exemple #9
0
def test_empty_base_url_and_publisher_signature():
    ''' When the base_url is not set and the publisher_signature is an empty string,
    render a URL without them. '''
    foo = Library('foo', '')
    x1 = Resource(foo, 'a.js')
    needed = NeededResources(publisher_signature='')
    needed.need(x1)

    assert needed.render() == '''\
Exemple #10
0
def test_rendering():
    foo = Library('foo', '')
    x1 = Resource(foo, 'a.js')
    x2 = Resource(foo, 'b.css')
    y1 = Resource(foo, 'c.js', depends=[x1, x2])

    needed = NeededResources()
    needed.need(y1)

    assert needed.render() == '''\
Exemple #11
0
def test_rendering_base_url_assign():
    foo = Library('foo', '')
    x1 = Resource(foo, 'a.js')
    x2 = Resource(foo, 'b.css')
    y1 = Resource(foo, 'c.js', depends=[x1, x2])

    needed = NeededResources()
    needed.need(y1)
    needed.set_base_url('http://localhost/static')
    assert needed.render() == '''\
Exemple #12
0
def test_rendering_base_url_assign():
    foo = Library('foo', '')
    x1 = Resource(foo, 'a.js')
    x2 = Resource(foo, 'b.css')
    y1 = Resource(foo, 'c.js', depends=[x1, x2])

    needed = NeededResources()
    needed.need(y1)
    needed.set_base_url('http://localhost/static')
    assert needed.render() == '''\
Exemple #13
0
def test_rendering():
    foo = Library('foo', '')
    x1 = Resource(foo, 'a.js')
    x2 = Resource(foo, 'b.css')
    y1 = Resource(foo, 'c.js', depends=[x1, x2])

    needed = NeededResources()
    needed.need(y1)

    assert needed.render() == '''\
Exemple #14
0
def test_resource_subclass_render():
    foo = Library('foo', '')

    class MyResource(Resource):
        def render(self, library_url):
            return '<myresource reference="%s/%s"/>' % (library_url, self.relpath)

    a = MyResource(foo, 'printstylesheet.css')
    needed = NeededResources()
    needed.need(a)
    assert needed.render() == """\
Exemple #15
0
def test_slot_minified():
    needed = NeededResources(minified=True)

    lib = Library('lib', '')

    slot = Slot(lib, '.js')
    a = Resource(lib, 'a.js', depends=[slot])

    b = Resource(lib, 'b.js', minified='b-min.js')

    needed.need(a, {slot: b})
    assert needed.render() == '''\
Exemple #16
0
def test_slot_minified():
    needed = NeededResources(minified=True)

    lib = Library('lib', '')

    slot = Slot(lib, '.js')
    a = Resource(lib, 'a.js', depends=[slot])

    b = Resource(lib, 'b.js', minified='b-min.js')

    needed.need(a, {slot: b})
    assert needed.render() == '''\
Exemple #17
0
def test_resource_subclass_render():
    foo = Library('foo', '')

    class MyResource(Resource):
        def render(self, library_url):
            return '<myresource reference="%s/%s"/>' % (library_url,
                                                        self.relpath)

    a = MyResource(foo, 'printstylesheet.css')
    needed = NeededResources()
    needed.need(a)
    assert needed.render() == """\
Exemple #18
0
def test_render_bundle():
    foo = Library('foo', '')
    x1 = Resource(foo, 'a.css')
    x2 = Resource(foo, 'b.css')
    x3 = Resource(foo, 'c.css', dont_bundle=True)
    needed = NeededResources(resources=[x1, x2, x3])
    assert needed.render(
    ) == '''<link rel="stylesheet" type="text/css" href="/fanstatic/foo/a.css" />
<link rel="stylesheet" type="text/css" href="/fanstatic/foo/b.css" />
<link rel="stylesheet" type="text/css" href="/fanstatic/foo/c.css" />'''

    needed = NeededResources(resources=[x1, x2, x3], bundle=True)
    assert needed.render(
    ) == '''<link rel="stylesheet" type="text/css" href="/fanstatic/foo/:bundle:a.css;b.css" />
<link rel="stylesheet" type="text/css" href="/fanstatic/foo/c.css" />'''

    x4 = Resource(foo, 'subdir/subdir/x4.css')
    x5 = Resource(foo, 'subdir/subdir/x5.css')
    needed = NeededResources(resources=[x1, x2, x4, x5], bundle=True)
    assert needed.render(
    ) == '''<link rel="stylesheet" type="text/css" href="/fanstatic/foo/:bundle:a.css;b.css" />
Exemple #19
0
def test_slot_depends_subset():
    needed = NeededResources()

    lib = Library('lib', '')

    c = Resource(lib, 'c.js')
    slot = Slot(lib, '.js', depends=[c])
    a = Resource(lib, 'a.js', depends=[slot])
    b = Resource(lib, 'b.js', depends=[])

    needed.need(a, {slot: b})

    assert needed.render() == '''\
Exemple #20
0
def test_render_filled_slots():
    needed = NeededResources()

    lib = Library('lib', '')

    slot = Slot(lib, '.js')
    a = Resource(lib, 'a.js', depends=[slot])

    b = Resource(lib, 'b.js')

    needed.need(a, {slot: b})

    assert needed.render() == '''\
Exemple #21
0
def test_render_filled_slots():
    needed = NeededResources()

    lib = Library('lib', '')

    slot = Slot(lib, '.js')
    a = Resource(lib, 'a.js', depends=[slot])

    b = Resource(lib, 'b.js')

    needed.need(a, {slot: b})

    assert needed.render() == '''\
Exemple #22
0
def test_slot_depends_subset():
    needed = NeededResources()

    lib = Library('lib', '')

    c = Resource(lib, 'c.js')
    slot = Slot(lib, '.js', depends=[c])
    a = Resource(lib, 'a.js', depends=[slot])
    b = Resource(lib, 'b.js', depends=[])
    
    needed.need(a, {slot: b})

    assert needed.render() == '''\
Exemple #23
0
def test_registered_inclusion_renderers_in_order():
    foo = Library('foo', '')

    def render_unknown(url):
        return '<unknown href="%s"/>' % url

    register_inclusion_renderer('.later', render_unknown, 50)
    a = Resource(foo, 'nothing.later')
    b = Resource(foo, 'something.js')
    c = Resource(foo, 'something.css')
    d = Resource(foo, 'something.ico')

    needed = NeededResources()
    needed.need(a)
    needed.need(b)
    needed.need(c)
    needed.need(d)

    assert needed.render() == """\
<link rel="stylesheet" type="text/css" href="/fanstatic/foo/something.css" />
<script type="text/javascript" src="/fanstatic/foo/something.js"></script>
<link rel="shortcut icon" type="image/x-icon" href="/fanstatic/foo/something.ico"/>
<unknown href="/fanstatic/foo/nothing.later"/>"""

    register_inclusion_renderer('.sooner', render_unknown, 5)
    e = Resource(foo, 'nothing.sooner')
    needed.need(e)
    assert needed.render() == """\
<unknown href="/fanstatic/foo/nothing.sooner"/>
<link rel="stylesheet" type="text/css" href="/fanstatic/foo/something.css" />
<script type="text/javascript" src="/fanstatic/foo/something.js"></script>
<link rel="shortcut icon" type="image/x-icon" href="/fanstatic/foo/something.ico"/>
<unknown href="/fanstatic/foo/nothing.later"/>"""

    register_inclusion_renderer('.between', render_unknown, 25)
    f = Resource(foo, 'nothing.between')
    needed.need(f)
    assert needed.render() == """\
Exemple #24
0
def test_registered_inclusion_renderers_in_order():
    foo = Library('foo', '')

    def render_unknown(url):
        return '<unknown href="%s"/>' % url

    register_inclusion_renderer('.later', render_unknown, 50)
    a = Resource(foo, 'nothing.later')
    b = Resource(foo, 'something.js')
    c = Resource(foo, 'something.css')
    d = Resource(foo, 'something.ico')

    needed = NeededResources()
    needed.need(a)
    needed.need(b)
    needed.need(c)
    needed.need(d)

    assert needed.render() == """\
<link rel="stylesheet" type="text/css" href="/fanstatic/foo/something.css" />
<script type="text/javascript" src="/fanstatic/foo/something.js"></script>
<link rel="shortcut icon" type="image/x-icon" href="/fanstatic/foo/something.ico"/>
<unknown href="/fanstatic/foo/nothing.later"/>"""

    register_inclusion_renderer('.sooner', render_unknown, 5)
    e = Resource(foo, 'nothing.sooner')
    needed.need(e)
    assert needed.render() == """\
<unknown href="/fanstatic/foo/nothing.sooner"/>
<link rel="stylesheet" type="text/css" href="/fanstatic/foo/something.css" />
<script type="text/javascript" src="/fanstatic/foo/something.js"></script>
<link rel="shortcut icon" type="image/x-icon" href="/fanstatic/foo/something.ico"/>
<unknown href="/fanstatic/foo/nothing.later"/>"""

    register_inclusion_renderer('.between', render_unknown, 25)
    f = Resource(foo, 'nothing.between')
    needed.need(f)
    assert needed.render() == """\
Exemple #25
0
def test_register_inclusion_renderer():
    foo = Library('foo', '')

    with pytest.raises(UnknownResourceExtensionError):
        # The renderer for '.unknown' is not yet defined.
        Resource(foo, 'nothing.unknown')

    def render_unknown(url):
        return '<link rel="unknown" href="%s" />' % url

    register_inclusion_renderer('.unknown', render_unknown)
    a = Resource(foo, 'nothing.unknown')

    needed = NeededResources()
    needed.need(a)
    assert needed.render() == ('<link rel="unknown" href="/fanstatic/foo/nothing.unknown" />')
Exemple #26
0
def test_register_inclusion_renderer():
    foo = Library('foo', '')

    with pytest.raises(UnknownResourceExtensionError):
        # The renderer for '.unknown' is not yet defined.
        Resource(foo, 'nothing.unknown')

    def render_unknown(url):
        return '<link rel="unknown" href="%s" />' % url

    register_inclusion_renderer('.unknown', render_unknown)
    a = Resource(foo, 'nothing.unknown')

    needed = NeededResources()
    needed.need(a)
    assert needed.render() == (
        '<link rel="unknown" href="/fanstatic/foo/nothing.unknown" />')
Exemple #27
0
def test_custom_renderer_keep_together():
    foo = Library('foo', '')

    def render_print_css(url):
        return ('<link rel="stylesheet" type="text/css" href="%s" media="print"/>' %
                url)

    a = Resource(foo, 'printstylesheet.css', renderer=render_print_css)
    b = Resource(foo, 'regular.css')
    c = Resource(foo, 'something.js')

    needed = NeededResources()
    needed.need(a)
    needed.need(b)
    needed.need(c)

    assert needed.render() == """\
Exemple #28
0
def test_custom_renderer_keep_together():
    foo = Library('foo', '')

    def render_print_css(url):
        return (
            '<link rel="stylesheet" type="text/css" href="%s" media="print"/>'
            % url)

    a = Resource(foo, 'printstylesheet.css', renderer=render_print_css)
    b = Resource(foo, 'regular.css')
    c = Resource(foo, 'something.js')

    needed = NeededResources()
    needed.need(a)
    needed.need(b)
    needed.need(c)

    assert needed.render() == """\