Example #1
0
def test_attack_melee_range_uses_full_power(capsys):
    power = 2
    expected_power = power
    attack_range = 3
    attacker = MagicAttack(power=power, attack_range=attack_range)
    from_pos = mock.MagicMock()
    target_pos = mock.MagicMock()
    distance = 1
    from_pos.distance.return_value = distance

    attacker.position = from_pos
    target = mock.MagicMock()
    target.position = target_pos
    target.name = "Some Enemy"
    target.take_damage.return_value = True

    msgs = [
        f"Attacking {target.name} with power {expected_power}",
    ]

    result = attacker.attack(target=target)
    out, _ = capsys.readouterr()

    assert result == True, f"MagicAttack should return the result of take_damage"
    for msg in msgs:
        assert msg.lower() in out.lower()

    from_pos.distance.assert_called_once_with(target_pos)
    target.take_damage.assert_called_once_with(expected_power)
Example #2
0
def test_attack_out_of_reach(capsys, attack_range):
    attacker = MagicAttack(power=1, attack_range=attack_range)
    from_pos = mock.MagicMock()
    target_pos = mock.MagicMock()
    distance = attack_range + 1
    from_pos.distance.return_value = distance
    msg = "Target is out of reach, can't attack!"

    attacker.position = from_pos
    target = mock.MagicMock()
    target.position = target_pos

    result = attacker.attack(target=target)
    out, _ = capsys.readouterr()

    assert result == False, f"MagicAttack with range of {attack_range} to a distance of {distance} should return False"
    assert msg.lower() in out.lower()
    from_pos.distance.assert_called_once_with(target_pos)