from bluesky.plans import scan
from bluesky.simulators import (print_summary, print_summary_wrapper,
                                summarize_plan,
                                check_limits,
                                plot_raster_path)
import pytest
from bluesky.plans import grid_scan


def test_print_summary(hw):
    det = hw.det
    motor = hw.motor
    print_summary(scan([det], motor, -1, 1, 10))  # old name
    summarize_plan(scan([det], motor, -1, 1, 10))  # new name
    list(print_summary_wrapper(scan([det], motor, -1, 1, 10)))


def test_old_module_name(hw):
    det = hw.det
    motor = hw.motor
    motor1 = hw.motor1
    motor2 = hw.motor2
    from bluesky.plan_tools import (print_summary, print_summary_wrapper,
                                    plot_raster_path)
    with pytest.warns(UserWarning):
        print_summary(scan([det], motor, -1, 1, 10))
    with pytest.warns(UserWarning):
        list(print_summary_wrapper(scan([det], motor, -1, 1, 10)))
    with pytest.warns(UserWarning):
        plan = grid_scan([det], motor1, -5, 5, 10, motor2, -7, 7, 15, True)
        plot_raster_path(plan, 'motor1', 'motor2', probe_size=.3)


def test_check_limits(RE, hw):
    det = hw.det
    motor = hw.motor
    # The motor object does not currently implement limits.
    # Use an assert to help us out if this changes in the future.
    assert not hasattr(motor, 'limits')

    # # check_limits should warn if it can't find check_value
    # TODO: Is there _any_ object to test?
    # with pytest.warns(UserWarning):
    #     check_limits(scan([det], motor, -1, 1, 3))

    # monkey-patch some limits
    motor.limits = (-2, 2)
    # check_limits should do nothing here
    check_limits(scan([det], motor, -1, 1, 3))

    # check_limits should error if limits are exceeded only if object raises
    # this object does not raise
    check_limits(scan([det], motor, -3, 3, 3))

    # check_limits should raise if limits are equal only if object raises
    # this object does not raise
    motor.limits = (2, 2)
    check_limits(scan([det], motor, -1, 1, 3))


def test_check_limits_needs_RE():
    with pytest.raises(RuntimeError) as ctx:
        check_limits([])
    assert str(ctx.value) == "Bluesky event loop not running"


def test_plot_raster_path(hw):
    det = hw.det
    motor1 = hw.motor1
    motor2 = hw.motor2
    plan = grid_scan([det], motor1, -5, 5, 10, motor2, -7, 7, 15, True)
    plot_raster_path(plan, 'motor1', 'motor2', probe_size=.3)

[((2359, 2419), 'bluesky.plans.grid_scan', 'grid_scan', (['[det]', 'motor1', '(-5)', '(5)', '(10)', 'motor2', '(-7)', '(7)', '(15)', '(True)'], {}), '([det], motor1, -5, 5, 10, motor2, -7, 7, 15, True)\n', (2368, 2419), False, 'from bluesky.plans import grid_scan\n'), ((2424, 2482), 'bluesky.plan_tools.plot_raster_path', 'plot_raster_path', (['plan', '"""motor1"""', '"""motor2"""'], {'probe_size': '(0.3)'}), "(plan, 'motor1', 'motor2', probe_size=0.3)\n", (2440, 2482), False, 'from bluesky.plan_tools import print_summary, print_summary_wrapper, plot_raster_path\n'), ((381, 410), 'bluesky.plans.scan', 'scan', (['[det]', 'motor', '(-1)', '(1)', '(10)'], {}), '([det], motor, -1, 1, 10)\n', (385, 410), False, 'from bluesky.plans import scan\n'), ((443, 472), 'bluesky.plans.scan', 'scan', (['[det]', 'motor', '(-1)', '(1)', '(10)'], {}), '([det], motor, -1, 1, 10)\n', (447, 472), False, 'from bluesky.plans import scan\n'), ((802, 827), 'pytest.warns', 'pytest.warns', (['UserWarning'], {}), '(UserWarning)\n', (814, 827), False, 'import pytest\n'), ((891, 916), 'pytest.warns', 'pytest.warns', (['UserWarning'], {}), '(UserWarning)\n', (903, 916), False, 'import pytest\n'), ((994, 1019), 'pytest.warns', 'pytest.warns', (['UserWarning'], {}), '(UserWarning)\n', (1006, 1019), False, 'import pytest\n'), ((1036, 1096), 'bluesky.plans.grid_scan', 'grid_scan', (['[det]', 'motor1', '(-5)', '(5)', '(10)', 'motor2', '(-7)', '(7)', '(15)', '(True)'], {}), '([det], motor1, -5, 5, 10, motor2, -7, 7, 15, True)\n', (1045, 1096), False, 'from bluesky.plans import grid_scan\n'), ((1105, 1163), 'bluesky.plan_tools.plot_raster_path', 'plot_raster_path', (['plan', '"""motor1"""', '"""motor2"""'], {'probe_size': '(0.3)'}), "(plan, 'motor1', 'motor2', probe_size=0.3)\n", (1121, 1163), False, 'from bluesky.plan_tools import print_summary, print_summary_wrapper, plot_raster_path\n'), ((1715, 1743), 'bluesky.plans.scan', 'scan', (['[det]', 'motor', '(-1)', '(1)', '(3)'], {}), '([det], motor, -1, 1, 3)\n', (1719, 1743), False, 'from bluesky.plans import scan\n'), ((1873, 1901), 'bluesky.plans.scan', 'scan', (['[det]', 'motor', '(-3)', '(3)', '(3)'], {}), '([det], motor, -3, 3, 3)\n', (1877, 1901), False, 'from bluesky.plans import scan\n'), ((2054, 2082), 'bluesky.plans.scan', 'scan', (['[det]', 'motor', '(-1)', '(1)', '(3)'], {}), '([det], motor, -1, 1, 3)\n', (2058, 2082), False, 'from bluesky.plans import scan\n'), ((2129, 2156), 'pytest.raises', 'pytest.raises', (['RuntimeError'], {}), '(RuntimeError)\n', (2142, 2156), False, 'import pytest\n'), ((2173, 2189), 'bluesky.simulators.check_limits', 'check_limits', (['[]'], {}), '([])\n', (2185, 2189), False, 'from bluesky.simulators import print_summary, print_summary_wrapper, summarize_plan, check_limits, plot_raster_path\n'), ((517, 546), 'bluesky.plans.scan', 'scan', (['[det]', 'motor', '(-1)', '(1)', '(10)'], {}), '([det], motor, -1, 1, 10)\n', (521, 546), False, 'from bluesky.plans import scan\n'), ((851, 880), 'bluesky.plans.scan', 'scan', (['[det]', 'motor', '(-1)', '(1)', '(10)'], {}), '([det], motor, -1, 1, 10)\n', (855, 880), False, 'from bluesky.plans import scan\n'), ((953, 982), 'bluesky.plans.scan', 'scan', (['[det]', 'motor', '(-1)', '(1)', '(10)'], {}), '([det], motor, -1, 1, 10)\n', (957, 982), False, 'from bluesky.plans import scan\n')]