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)

[(71, 'bluesky.plans.grid_scan', 'grid_scan', (['[det]', 'motor1', '(-5)', '(5)', '(10)', 'motor2', '(-7)', '(7)', '(15)', '(True)'], {}), False, 'from bluesky.plans import grid_scan\n'), (72, 'bluesky.plan_tools.plot_raster_path', 'plot_raster_path', (['plan', '"""motor1"""', '"""motor2"""'], {'probe_size': '(0.3)'}), False, 'from bluesky.plan_tools import print_summary, print_summary_wrapper, plot_raster_path\n'), (13, 'bluesky.plans.scan', 'scan', (['[det]', 'motor', '(-1)', '(1)', '(10)'], {}), False, 'from bluesky.plans import scan\n'), (14, 'bluesky.plans.scan', 'scan', (['[det]', 'motor', '(-1)', '(1)', '(10)'], {}), False, 'from bluesky.plans import scan\n'), (25, 'pytest.warns', 'pytest.warns', (['UserWarning'], {}), False, 'import pytest\n'), (27, 'pytest.warns', 'pytest.warns', (['UserWarning'], {}), False, 'import pytest\n'), (29, 'pytest.warns', 'pytest.warns', (['UserWarning'], {}), False, 'import pytest\n'), (30, 'bluesky.plans.grid_scan', 'grid_scan', (['[det]', 'motor1', '(-5)', '(5)', '(10)', 'motor2', '(-7)', '(7)', '(15)', '(True)'], {}), False, 'from bluesky.plans import grid_scan\n'), (31, 'bluesky.plan_tools.plot_raster_path', 'plot_raster_path', (['plan', '"""motor1"""', '"""motor2"""'], {'probe_size': '(0.3)'}), False, 'from bluesky.plan_tools import print_summary, print_summary_wrapper, plot_raster_path\n'), (49, 'bluesky.plans.scan', 'scan', (['[det]', 'motor', '(-1)', '(1)', '(3)'], {}), False, 'from bluesky.plans import scan\n'), (53, 'bluesky.plans.scan', 'scan', (['[det]', 'motor', '(-3)', '(3)', '(3)'], {}), False, 'from bluesky.plans import scan\n'), (58, 'bluesky.plans.scan', 'scan', (['[det]', 'motor', '(-1)', '(1)', '(3)'], {}), False, 'from bluesky.plans import scan\n'), (62, 'pytest.raises', 'pytest.raises', (['RuntimeError'], {}), False, 'import pytest\n'), (63, 'bluesky.simulators.check_limits', 'check_limits', (['[]'], {}), False, 'from bluesky.simulators import print_summary, print_summary_wrapper, summarize_plan, check_limits, plot_raster_path\n'), (15, 'bluesky.plans.scan', 'scan', (['[det]', 'motor', '(-1)', '(1)', '(10)'], {}), False, 'from bluesky.plans import scan\n'), (26, 'bluesky.plans.scan', 'scan', (['[det]', 'motor', '(-1)', '(1)', '(10)'], {}), False, 'from bluesky.plans import scan\n'), (28, 'bluesky.plans.scan', 'scan', (['[det]', 'motor', '(-1)', '(1)', '(10)'], {}), False, 'from bluesky.plans import scan\n')]