예제 #1
0
def test_lazy_asset():
    side_effect = []

    def lazy():
        side_effect.append(True)
        return 'spaaam'

    asset = app.Asset('foo.js', lazy)
    assert asset.source is lazy
    assert not side_effect

    assert asset.to_string() == 'spaaam'
    assert side_effect

    while side_effect:
        side_effect.pop(0)

    assert asset.to_string() == 'spaaam'
    assert not side_effect

    # Fail

    def lazy_wrong():
        return None

    asset = app.Asset('foo.js', lazy_wrong)
    assert asset.source is lazy_wrong

    with raises(ValueError):
        asset.to_string()
예제 #2
0
def test_remote_asset():

    # Prepare example asset info
    # Note: use http instead of https to avoid spurious certificate errors
    bootstrap_url = 'http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css'
    jquery_url = 'http://code.jquery.com/jquery-3.1.1.slim.min.js'
    with open(test_filename + '.js', 'wb') as f:
        f.write('var blablabla=7;'.encode())

    # JS from url
    asset = app.Asset(jquery_url)
    assert asset.remote
    assert asset.source == jquery_url
    assert 'jQuery v3.1.1' in asset.to_string()
    assert 'jQuery v3.1.1' in asset.to_html('{}', 0)
    assert 'jQuery v3.1.1' not in asset.to_html('{}', 1)
    assert 'jQuery v3.1.1' not in asset.to_html('{}', 2)
    assert 'jQuery v3.1.1' not in asset.to_html('{}', 3)
    assert 'src=' not in asset.to_html('{}', 0)
    assert 'src=' in asset.to_html('{}', 1)
    assert 'src=' in asset.to_html('{}', 2)
    assert 'src=' in asset.to_html('{}', 3)
    assert 'http://' in asset.to_html('{}', 1)
    assert 'http://' not in asset.to_html('{}', 2)
    assert 'http://' in asset.to_html('{}', 3)

    # CSS from url
    asset = app.Asset(bootstrap_url)
    assert asset.remote
    assert asset.source == bootstrap_url
    assert 'Bootstrap v3.3.7' in asset.to_string()
    assert 'Bootstrap v3.3.7' in asset.to_html('{}', 0)
    assert 'Bootstrap v3.3.7' not in asset.to_html('{}', 1)
    assert 'Bootstrap v3.3.7' not in asset.to_html('{}', 2)
    assert 'Bootstrap v3.3.7' not in asset.to_html('{}', 3)
    assert 'href=' not in asset.to_html('{}', 0)
    assert 'href=' in asset.to_html('{}', 1)
    assert 'href=' in asset.to_html('{}', 2)
    assert 'href=' in asset.to_html('{}', 3)
    assert 'http://' in asset.to_html('{}', 1)
    assert 'http://' not in asset.to_html('{}', 2)
    assert 'http://' in asset.to_html('{}', 3)

    # JS from file
    asset = app.Asset('file://' + test_filename + '.js')
    assert asset.remote
    assert test_filename in asset.source
    assert 'blablabla=7' in asset.to_string()
    assert 'blablabla=7' in asset.to_html('{}', 0)
    assert 'blablabla=7' not in asset.to_html('{}', 1)
    assert 'blablabla=7' not in asset.to_html('{}', 2)
    assert 'blablabla=7' not in asset.to_html('{}', 3)

    with raises(TypeError):
        app.Asset(jquery_url, 'foo=3')  # no sources for remote asset
    with raises(TypeError):
        app.Asset(jquery_url, ['foo=3'])  # no sources for remote asset
예제 #3
0
파일: test_asset.py 프로젝트: zxjlm/flexx
def test_asset():

    # Initialization

    asset1 = app.Asset('foo.js', 'foo=3')
    assert 'foo.js' in repr(asset1)
    assert 'foo.js' == asset1.name
    assert asset1.source == 'foo=3'

    asset2 = app.Asset('bar.css', 'bar=2')
    assert 'bar.css' in repr(asset2)
    assert 'bar.css' == asset2.name
    assert asset2.source == 'bar=2'

    with raises(TypeError):
        app.Asset()  # :/
    with raises(TypeError):
        app.Asset('foo.js')  # need source

    with raises(TypeError):
        app.Asset(3, 'bar=2')  # name not a str
    with raises(ValueError):
        app.Asset('foo.png', '')  # js and css only
    with raises(TypeError):
        app.Asset('bar.css', 3)  # source not str
    with raises(TypeError):
        app.Asset('bar.css', ['a'])  # source not str

    # To html JS
    asset = app.Asset('foo.js', 'foo=3;bar=3')
    code = asset.to_html('', 0)
    assert code.startswith('<script') and code.strip().endswith('</script>')
    assert 'foo=3' in code
    assert '\n' not in code  # because source had no newlines

    asset = app.Asset('foo.js', 'foo=3\nbar=3')
    code = asset.to_html('', 0)
    assert code.startswith('<script') and code.strip().endswith('</script>')
    assert '\nfoo=3\nbar=3\n' in code  # note the newlines

    asset = app.Asset('foo.js', 'foo=3\nbar=3')
    code = asset.to_html()
    assert code.startswith('<script ') and code.strip().endswith('</script>')
    assert 'foo=' not in code
    assert '\n' not in code  # because its a link

    # To html CSS
    asset = app.Asset('bar.css', 'foo=3;bar=3')
    code = asset.to_html('', 0)
    assert code.startswith('<style') and code.strip().endswith('</style>')
    assert 'foo=' in code
    assert '\n' not in code  # because source had no newlines

    asset = app.Asset('bar.css', 'foo=3\nbar=3')
    code = asset.to_html('', 0)
    assert code.startswith('<style') and code.strip().endswith('</style>')
    assert '\nfoo=3\nbar=3\n' in code  # note the newlines

    asset = app.Asset('bar.css', 'foo=3\nbar=3')
    code = asset.to_html()
    assert code.startswith('<link') and code.strip().endswith('/>')
    assert 'foo-' not in code
    assert '\n' not in code  # becasue its a link

    # Test asset via uri
    fname = 'file:///home/xx/foobar.css'
    with raises(TypeError):
        app.Asset('bar.css', fname)
    with raises(TypeError):
        app.Asset(fname)
예제 #4
0
파일: test_asset.py 프로젝트: zxjlm/flexx
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
예제 #5
0
def test_asset():

    # Initialization

    asset1 = app.Asset('foo.js', 'foo=3')
    assert 'foo.js' in repr(asset1)
    assert 'foo.js' == asset1.name
    assert asset1.source == 'foo=3'

    asset2 = app.Asset('bar.css', 'bar=2')
    assert 'bar.css' in repr(asset2)
    assert 'bar.css' == asset2.name
    assert asset2.source == 'bar=2'

    with raises(TypeError):
        app.Asset()  # :/
    with raises(TypeError):
        app.Asset('foo.js')  # need source

    with raises(TypeError):
        app.Asset(3, 'bar=2')  # name not a str
    with raises(ValueError):
        app.Asset('foo.png', '')  # js and css only
    with raises(TypeError):
        app.Asset('bar.css', 3)  # source not str
    with raises(TypeError):
        app.Asset('bar.css', ['a'])  # source not str

    # To html JS
    asset = app.Asset('foo.js', 'foo=3;bar=3')
    code = asset.to_html('', 0)
    assert code.startswith('<script') and code.strip().endswith('</script>')
    assert 'foo=3' in code
    assert '\n' not in code  # because source had no newlines

    asset = app.Asset('foo.js', 'foo=3\nbar=3')
    code = asset.to_html('', 0)
    assert code.startswith('<script') and code.strip().endswith('</script>')
    assert '\nfoo=3\nbar=3\n' in code  # note the newlines

    asset = app.Asset('foo.js', 'foo=3\nbar=3')
    code = asset.to_html()
    assert code.startswith('<script ') and code.strip().endswith('</script>')
    assert 'foo=' not in code
    assert '\n' not in code  # because its a link

    # To html CSS
    asset = app.Asset('bar.css', 'foo=3;bar=3')
    code = asset.to_html('', 0)
    assert code.startswith('<style') and code.strip().endswith('</style>')
    assert 'foo=' in code
    assert '\n' not in code  # because source had no newlines

    asset = app.Asset('bar.css', 'foo=3\nbar=3')
    code = asset.to_html('', 0)
    assert code.startswith('<style') and code.strip().endswith('</style>')
    assert '\nfoo=3\nbar=3\n' in code  # note the newlines

    asset = app.Asset('bar.css', 'foo=3\nbar=3')
    code = asset.to_html()
    assert code.startswith('<link') and code.strip().endswith('/>')
    assert 'foo-' not in code
    assert '\n' not in code  # becasue its a link

    # Test asset via uri
    with open(test_filename, 'wb') as f:
        f.write('var blablabla=7;'.encode())
    asset = app.Asset('bar.css', 'file://' + test_filename)
    assert 'blablabla=7' in asset.to_string()
    asset = app.Asset('bar.css',
                      'http://code.jquery.com/jquery-3.1.1.slim.min.js')
    assert 'jQuery v3.1.1' in asset.to_string()