Example #1
0
def test_bundle():

    from flexx import ui

    store = {}
    m1 = app.JSModule('flexx.ui.widgets._button', store)
    m1.add_variable('Button')
    m2 = app.JSModule('flexx.ui.widgets._tree', store)
    m2.add_variable('TreeWidget')
    m3 = store['flexx.ui._widget']  # because its a dep of the above

    # JS bundle
    bundle = app.Bundle('flexx.ui.js')
    assert 'flexx.ui' in repr(bundle)

    bundle.add_module(m1)
    bundle.add_module(m2)
    bundle.add_module(m3)

    # Modules are sorted
    assert bundle.modules == (m3, m1, m2)

    # Deps are agregated
    assert 'flexx.app._clientcore.js' in bundle.deps
    assert 'flexx.app.js' in bundle.deps
    assert 'flexx.app._model.js' in bundle.deps
    assert not any('flexx.ui' in dep for dep in bundle.deps)

    # Strings are combined
    code = bundle.to_string()
    assert '.Widget =' in code
    assert '.Button =' in code
    assert '.TreeWidget =' in code

    # CSS bundle
    bundle = app.Bundle('flexx.ui.css')
    bundle.add_module(m1)
    bundle.add_module(m2)
    bundle.add_module(m3)
    #
    code = bundle.to_string()
    assert '.Widget =' not in code
    assert '.flx-Widget {' in code
    assert '.flx-TreeWidget {' in code

    # This works too
    bundle = app.Bundle('-foo.js')
    bundle.add_module(m1)

    # But this does not
    bundle = app.Bundle('foo.js')
    with raises(ValueError):
        bundle.add_module(m1)
Example #2
0
def test_bundle():

    try:
        from flexx import ui
    except ImportError:
        skip('no flexx.ui')

    store = {}
    m1 = app.JSModule('flexx.ui.widgets._button', store)
    m1.add_variable('Button')
    m2 = app.JSModule('flexx.ui.widgets._tree', store)
    m2.add_variable('TreeWidget')
    m3 = store['flexx.ui._widget']  # because its a dep of the above

    # JS bundle
    bundle = app.Bundle('flexx.ui.js')
    assert 'flexx.ui' in repr(bundle)

    bundle.add_module(m1)
    bundle.add_module(m2)
    bundle.add_module(m3)

    # Modules are sorted
    assert bundle.modules == (m3, m1, m2)

    # Deps are agregated
    assert 'flexx.app.js' in bundle.deps
    assert 'flexx.app._component2.js' in bundle.deps
    assert not any('flexx.ui' in dep for dep in bundle.deps)

    # Strings are combined
    code = bundle.to_string()
    assert '$Widget =' in code
    assert '$Button =' in code
    assert '$TreeWidget =' in code

    # CSS bundle
    bundle = app.Bundle('flexx.ui.css')
    bundle.add_module(m1)
    bundle.add_module(m2)
    bundle.add_module(m3)
    #
    code = bundle.to_string()
    assert '.Widget =' not in code
    assert '.flx-Widget {' in code
    assert '.flx-TreeWidget {' in code

    # This works too
    bundle = app.Bundle('-foo.js')
    bundle.add_module(m1)

    # But this does not
    bundle = app.Bundle('foo.js')
    with raises(ValueError):
        bundle.add_module(m1)

    # Assets can be bundled too

    bundle = app.Bundle('flexx.ui.css')
    bundle.add_module(m1)
    bundle.add_module(m2)

    a1 = app.Asset('foo.css', 'foo-xxx')
    a2 = app.Asset('bar.css', 'bar-yyy')
    bundle.add_asset(a1)
    bundle.add_asset(a2)

    assert a1 in bundle.assets
    assert a2 in bundle.assets

    code = bundle.to_string()
    assert 'foo-xxx' in code
    assert 'bar-yyy' in code

    with raises(TypeError):
        bundle.add_asset()
    with raises(TypeError):
        bundle.add_asset(3)
    with raises(TypeError):
        bundle.add_asset(bundle)  # no bundles