Files
Gymnasium/tests/wrappers/test_order_enforcing.py

46 lines
1.7 KiB
Python
Raw Normal View History

2022-05-13 13:58:19 +01:00
import pytest
import gym
2022-05-13 13:58:19 +01:00
from gym.envs.classic_control import CartPoleEnv
2022-05-18 15:07:54 +01:00
from gym.error import ResetNeeded
from gym.wrappers import OrderEnforcing
2022-06-16 14:29:13 +01:00
from tests.envs.utils import all_testing_env_specs
2022-05-13 13:58:19 +01:00
from tests.wrappers.utils import has_wrapper
2022-06-16 14:29:13 +01:00
@pytest.mark.parametrize(
"spec", all_testing_env_specs, ids=[spec.id for spec in all_testing_env_specs]
)
2022-05-13 13:58:19 +01:00
def test_gym_make_order_enforcing(spec):
2022-05-18 15:07:54 +01:00
"""Checks that gym.make wrappers the environment with the OrderEnforcing wrapper."""
2022-06-16 14:29:13 +01:00
env = gym.make(spec.id, disable_env_checker=True)
2022-05-13 13:58:19 +01:00
assert has_wrapper(env, OrderEnforcing)
def test_order_enforcing():
2022-05-18 15:07:54 +01:00
"""Checks that the order enforcing works as expected, raising an error before reset is called and not after."""
2022-05-13 13:58:19 +01:00
# The reason for not using gym.make is that all environments are by default wrapped in the order enforcing wrapper
env = CartPoleEnv(render_mode="rgb_array")
2022-05-13 13:58:19 +01:00
assert not has_wrapper(env, OrderEnforcing)
# Assert that the order enforcing works for step and render before reset
order_enforced_env = OrderEnforcing(env)
assert order_enforced_env.has_reset is False
2022-05-18 15:07:54 +01:00
with pytest.raises(ResetNeeded):
2022-05-13 13:58:19 +01:00
order_enforced_env.step(0)
2022-05-18 15:07:54 +01:00
with pytest.raises(ResetNeeded):
order_enforced_env.render()
assert order_enforced_env.has_reset is False
2022-05-13 13:58:19 +01:00
# Assert that the Assertion errors are not raised after reset
order_enforced_env.reset()
assert order_enforced_env.has_reset is True
2022-05-13 13:58:19 +01:00
order_enforced_env.step(0)
order_enforced_env.render()
2022-05-18 15:07:54 +01:00
# Assert that with disable_render_order_enforcing works, the environment has already been reset
env = CartPoleEnv(render_mode="rgb_array")
2022-05-18 15:07:54 +01:00
env = OrderEnforcing(env, disable_render_order_enforcing=True)
env.render() # no assertion error