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, 11, 71, 71), 'bluesky.plans.grid_scan', 'grid_scan', ({(71, 21, 71, 26): '[det]', (71, 28, 71, 34): 'motor1', (71, 36, 71, 38): '-5', (71, 40, 71, 41): '5', (71, 43, 71, 45): '10', (71, 47, 71, 53): 'motor2', (71, 55, 71, 57): '-7', (71, 59, 71, 60): '7', (71, 62, 71, 64): '15', (71, 66, 71, 70): 'True'}, {}), '([det], motor1, -5, 5, 10, motor2, -7, 7, 15, True)', False, 'from bluesky.plans import grid_scan\n'), ((72, 4, 72, 61), 'bluesky.plan_tools.plot_raster_path', 'plot_raster_path', (), '', False, 'from bluesky.plan_tools import print_summary, print_summary_wrapper, plot_raster_path\n'), ((13, 18, 13, 47), 'bluesky.plans.scan', 'scan', ({(13, 23, 13, 28): '[det]', (13, 30, 13, 35): 'motor', (13, 37, 13, 39): '(-1)', (13, 41, 13, 42): '(1)', (13, 44, 13, 46): '(10)'}, {}), '([det], motor, -1, 1, 10)', False, 'from bluesky.plans import scan\n'), ((14, 19, 14, 48), 'bluesky.plans.scan', 'scan', ({(14, 24, 14, 29): '[det]', (14, 31, 14, 36): 'motor', (14, 38, 14, 40): '(-1)', (14, 42, 14, 43): '(1)', (14, 45, 14, 47): '(10)'}, {}), '([det], motor, -1, 1, 10)', False, 'from bluesky.plans import scan\n'), ((25, 9, 25, 34), 'pytest.warns', 'pytest.warns', ({(25, 22, 25, 33): 'UserWarning'}, {}), '(UserWarning)', False, 'import pytest\n'), ((27, 9, 27, 34), 'pytest.warns', 'pytest.warns', ({(27, 22, 27, 33): 'UserWarning'}, {}), '(UserWarning)', False, 'import pytest\n'), ((29, 9, 29, 34), 'pytest.warns', 'pytest.warns', ({(29, 22, 29, 33): 'UserWarning'}, {}), '(UserWarning)', False, 'import pytest\n'), ((30, 15, 30, 75), 'bluesky.plans.grid_scan', 'grid_scan', ({(30, 25, 30, 30): '[det]', (30, 32, 30, 38): 'motor1', (30, 40, 30, 42): '-5', (30, 44, 30, 45): '5', (30, 47, 30, 49): '10', (30, 51, 30, 57): 'motor2', (30, 59, 30, 61): '-7', (30, 63, 30, 64): '7', (30, 66, 30, 68): '15', (30, 70, 30, 74): 'True'}, {}), '([det], motor1, -5, 5, 10, motor2, -7, 7, 15, True)', False, 'from bluesky.plans import grid_scan\n'), ((31, 8, 31, 65), 'bluesky.plan_tools.plot_raster_path', 'plot_raster_path', (), '', False, 'from bluesky.plan_tools import print_summary, print_summary_wrapper, plot_raster_path\n'), ((49, 17, 49, 45), 'bluesky.plans.scan', 'scan', ({(49, 22, 49, 27): '[det]', (49, 29, 49, 34): 'motor', (49, 36, 49, 38): '(-1)', (49, 40, 49, 41): '(1)', (49, 43, 49, 44): '(3)'}, {}), '([det], motor, -1, 1, 3)', False, 'from bluesky.plans import scan\n'), ((53, 17, 53, 45), 'bluesky.plans.scan', 'scan', ({(53, 22, 53, 27): '[det]', (53, 29, 53, 34): 'motor', (53, 36, 53, 38): '(-3)', (53, 40, 53, 41): '(3)', (53, 43, 53, 44): '(3)'}, {}), '([det], motor, -3, 3, 3)', False, 'from bluesky.plans import scan\n'), ((58, 17, 58, 45), 'bluesky.plans.scan', 'scan', ({(58, 22, 58, 27): '[det]', (58, 29, 58, 34): 'motor', (58, 36, 58, 38): '(-1)', (58, 40, 58, 41): '(1)', (58, 43, 58, 44): '(3)'}, {}), '([det], motor, -1, 1, 3)', False, 'from bluesky.plans import scan\n'), ((62, 9, 62, 36), 'pytest.raises', 'pytest.raises', ({(62, 23, 62, 35): 'RuntimeError'}, {}), '(RuntimeError)', False, 'import pytest\n'), ((63, 8, 63, 24), 'bluesky.simulators.check_limits', 'check_limits', ({(63, 21, 63, 23): '[]'}, {}), '([])', False, 'from bluesky.simulators import print_summary, print_summary_wrapper, summarize_plan, check_limits, plot_raster_path\n'), ((15, 31, 15, 60), 'bluesky.plans.scan', 'scan', ({(15, 36, 15, 41): '[det]', (15, 43, 15, 48): 'motor', (15, 50, 15, 52): '(-1)', (15, 54, 15, 55): '(1)', (15, 57, 15, 59): '(10)'}, {}), '([det], motor, -1, 1, 10)', False, 'from bluesky.plans import scan\n'), ((26, 22, 26, 51), 'bluesky.plans.scan', 'scan', ({(26, 27, 26, 32): '[det]', (26, 34, 26, 39): 'motor', (26, 41, 26, 43): '(-1)', (26, 45, 26, 46): '(1)', (26, 48, 26, 50): '(10)'}, {}), '([det], motor, -1, 1, 10)', False, 'from bluesky.plans import scan\n'), ((28, 35, 28, 64), 'bluesky.plans.scan', 'scan', ({(28, 40, 28, 45): '[det]', (28, 47, 28, 52): 'motor', (28, 54, 28, 56): '(-1)', (28, 58, 28, 59): '(1)', (28, 61, 28, 63): '(10)'}, {}), '([det], motor, -1, 1, 10)', False, 'from bluesky.plans import scan\n')]