from pytest import raises
from bokeh.models import CustomJS, Slider


def test_js_callback():
    slider = Slider()

    cb = CustomJS(code="foo();", args=dict(x=slider))
    assert 'foo()' in cb.code
    assert cb.args['x'] is slider

    cb = CustomJS(code="foo();", args=dict(x=3))
    assert 'foo()' in cb.code
    assert cb.args['x'] is 3

    with raises(AttributeError):  # kwargs not supported
        CustomJS(code="foo();", x=slider)


def test_py_callback():
    slider = Slider()
    foo = None  # fool pyflakes

    def cb(x=slider):
        foo()
    cb = CustomJS.from_py_func(cb)
    assert 'foo()' in cb.code
    assert cb.args['x'] is slider

    def cb(x=4):
        foo()
    cb = CustomJS.from_py_func(cb)
    assert 'foo()' in cb.code
    assert cb.args['x'] is 4

[((6, 13, 6, 21), 'bokeh.models.Slider', 'Slider', ({}, {}), '()', False, 'from bokeh.models import CustomJS, Slider\n'), ((21, 13, 21, 21), 'bokeh.models.Slider', 'Slider', ({}, {}), '()', False, 'from bokeh.models import CustomJS, Slider\n'), ((26, 9, 26, 34), 'bokeh.models.CustomJS.from_py_func', 'CustomJS.from_py_func', ({(26, 31, 26, 33): 'cb'}, {}), '(cb)', False, 'from bokeh.models import CustomJS, Slider\n'), ((32, 9, 32, 34), 'bokeh.models.CustomJS.from_py_func', 'CustomJS.from_py_func', ({(32, 31, 32, 33): 'cb'}, {}), '(cb)', False, 'from bokeh.models import CustomJS, Slider\n'), ((16, 9, 16, 31), 'pytest.raises', 'raises', ({(16, 16, 16, 30): 'AttributeError'}, {}), '(AttributeError)', False, 'from pytest import raises\n'), ((17, 8, 17, 41), 'bokeh.models.CustomJS', 'CustomJS', (), '', False, 'from bokeh.models import CustomJS, Slider\n')]