示例#1
0
def test_no_need_to_fill_in_not_required():
    lib = Library('lib', '')
    slot = Slot(lib, '.js', required=False)
    a = Resource(lib, 'a.js', depends=[slot])

    needed = init_needed(resources=[a])
    # slot wasn't required and not filled in, so filled slot doesn't show up
    assert needed.resources() == set([a])
示例#2
0
def test_slot_with_default_uses_default_if_nothing_given_in_need():
    lib = Library('lib', '')
    default_resource_for_slot = Resource(lib, 'b.js')
    slot = Slot(lib, '.js', default=default_resource_for_slot)
    a = Resource(lib, 'a.js', depends=[slot])
    needed = init_needed(resources=[a])
    relpaths = [r.relpath for r in sort_resources(needed.resources())]
    assert relpaths == ['b.js', 'a.js']
示例#3
0
def test_slot_minified():
    lib = Library('lib', '')

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

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

    incl = Inclusion(needed, mode=MINIFIED)
    assert [r.relpath for r in incl.resources] == ['b-min.js', 'a.js']
示例#4
0
def test_slot_depends_subset():
    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 = init_needed()
    needed.need(a, {slot: b})
    assert [r.relpath for r in sort_resources(needed.resources())] == \
        ['c.js', 'b.js', 'a.js']
示例#5
0
def test_dont_fill_required_slot():
    lib = Library('lib', '')

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

    needed = init_needed()
    needed.need(a)

    with pytest.raises(SlotError):
        needed.resources()
示例#6
0
def test_default_can_be_overridden():
    lib = Library('lib', '')
    default_resource_for_slot = Resource(lib, 'b.js')
    slot = Slot(lib, '.js', default=default_resource_for_slot)
    a = Resource(lib, 'a.js', depends=[slot])
    custom_resource_for_slot = Resource(lib, 'c.js')

    needed = init_needed()
    needed.need(a, {slot: custom_resource_for_slot})

    relpaths = [r.relpath for r in sort_resources(needed.resources())]
    assert relpaths == ['c.js', 'a.js']
def test_compile_with_slots(compilers):
    compilers.add_compiler(MockCompiler())

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

    needed = init_needed()
    needed.need(b, {slot: a})
    incl = Inclusion(needed, compile=True)
    assert len(compilers.compiler('mock').calls) == 1
示例#8
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() == '''\
示例#9
0
def test_fill_slot_wrong_extension():
    lib = Library('lib', '')

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

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

    with pytest.raises(SlotError):
        needed.resources()
示例#10
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() == '''\
示例#11
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() == '''\
示例#12
0
def test_render_filled_slots():
    lib = Library('lib', '')

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

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

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

    assert [r.relpath for r in sort_resources(needed.resources())] == \
        ['b.js', 'a.js']
示例#13
0
def test_resource_need_should_pass_slots_to_needed():
    import fanstatic
    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=[c])
    needed = fanstatic.init_needed()
    try:
        a.need({slot: c})
    finally:
        fanstatic.del_needed()
    assert slot in needed._slots
示例#14
0
def test_slot_depends_incorrect():
    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 = init_needed()
    needed.need(a, {slot: b})

    with pytest.raises(SlotError):
        needed.resources()
示例#15
0
def test_fill_slot_wrong_dependencies():
    needed = NeededResources()

    lib = Library('lib', '')

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

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

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

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

    with pytest.raises(SlotError):
        resources = needed.resources()
示例#16
0
def test_rollup_with_slot():
    from fanstatic import Slot

    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=[c])

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

    incl = Inclusion(needed, rollup=True)
    assert len(incl.resources) == 3
    assert a in incl.resources
    assert c in incl.resources
示例#17
0
def test_fill_slot():
    lib = Library('lib', '')

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

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

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

    resources = sort_resources(needed.resources())
    assert len(resources) == 2
    # verify filled slot is correctly
    assert resources[0].library is b.library
    assert resources[0].relpath is b.relpath
def test_inject_filled_slot():
    lib = Library('foo', '')
    c = Resource(lib, 'c.js')
    slot = Slot(lib, '.js', depends=[c])
    a = Resource(lib, 'a.js', depends=[slot])
    b = Resource(lib, 'b.js', depends=[c])

    def app(environ, start_response):
        start_response('200 OK', [('Content-Type', 'text/html')])
        needed = get_needed()
        needed.need(a, {slot: b})
        needed.set_base_url('http://testapp')
        return [b'<html><head></head><body></body></html>']

    wrapped_app = Injector(app)

    request = webob.Request.blank('/')
    response = request.get_response(wrapped_app)
    assert response.body == b'''\
示例#19
0
def test_slot_with_default_can_not_set_required_explicitly():
    lib = Library('lib', '')

    a = Resource(lib, 'a.js')
    with pytest.raises(ValueError):
        slot = Slot(lib, '.js', default=a, required=True)