Files
Gymnasium/tests/envs/test_spec.py

94 lines
2.7 KiB
Python
Raw Normal View History

"""Tests that gym.spec works as expected."""
2022-06-23 13:54:50 +01:00
import re
import pytest
import gymnasium as gym
2022-06-23 13:54:50 +01:00
def test_spec():
spec = gym.spec("CartPole-v1")
2022-06-23 13:54:50 +01:00
assert spec.id == "CartPole-v1"
assert spec is gym.envs.registry["CartPole-v1"]
2022-06-23 13:54:50 +01:00
def test_spec_kwargs():
map_name_value = "8x8"
env = gym.make("FrozenLake-v1", map_name=map_name_value)
assert env.spec is not None
2022-06-23 13:54:50 +01:00
assert env.spec.kwargs["map_name"] == map_name_value
def test_spec_missing_lookup():
gym.register(id="Test1-v0", entry_point="no-entry-point")
gym.register(id="Test1-v15", entry_point="no-entry-point")
gym.register(id="Test1-v9", entry_point="no-entry-point")
gym.register(id="Other1-v100", entry_point="no-entry-point")
2022-06-23 13:54:50 +01:00
with pytest.raises(
gym.error.DeprecatedEnv,
2022-06-23 13:54:50 +01:00
match=re.escape(
"Environment version v1 for `Test1` is deprecated. Please use `Test1-v15` instead."
),
):
gym.spec("Test1-v1")
2022-06-23 13:54:50 +01:00
with pytest.raises(
gym.error.UnregisteredEnv,
2022-06-23 13:54:50 +01:00
match=re.escape(
"Environment version `v1000` for environment `Test1` doesn't exist. It provides versioned environments: [ `v0`, `v9`, `v15` ]."
),
):
gym.spec("Test1-v1000")
2022-06-23 13:54:50 +01:00
with pytest.raises(
gym.error.UnregisteredEnv,
2022-06-23 13:54:50 +01:00
match=re.escape("Environment Unknown1 doesn't exist. "),
):
gym.spec("Unknown1-v1")
2022-06-23 13:54:50 +01:00
def test_spec_malformed_lookup():
with pytest.raises(
gym.error.Error,
2022-06-23 13:54:50 +01:00
match=f'^{re.escape("Malformed environment ID: “Breakout-v0”.(Currently all IDs must be of the form [namespace/](env-name)-v(version). (namespace is optional))")}$',
):
gym.spec("“Breakout-v0”")
2022-06-23 13:54:50 +01:00
def test_spec_versioned_lookups():
gym.register("test/Test2-v5", "no-entry-point")
2022-06-23 13:54:50 +01:00
with pytest.raises(
gym.error.VersionNotFound,
2022-06-23 13:54:50 +01:00
match=re.escape(
"Environment version `v9` for environment `test/Test2` doesn't exist. It provides versioned environments: [ `v5` ]."
),
):
gym.spec("test/Test2-v9")
2022-06-23 13:54:50 +01:00
with pytest.raises(
gym.error.DeprecatedEnv,
2022-06-23 13:54:50 +01:00
match=re.escape(
"Environment version v4 for `test/Test2` is deprecated. Please use `test/Test2-v5` instead."
),
):
gym.spec("test/Test2-v4")
2022-06-23 13:54:50 +01:00
assert gym.spec("test/Test2-v5") is not None
2022-06-23 13:54:50 +01:00
def test_spec_default_lookups():
gym.register("test/Test3", "no-entry-point")
2022-06-23 13:54:50 +01:00
with pytest.raises(
gym.error.DeprecatedEnv,
2022-06-23 13:54:50 +01:00
match=re.escape(
"Environment version `v0` for environment `test/Test3` doesn't exist. It provides the default version test/Test3`."
),
):
gym.spec("test/Test3-v0")
2022-06-23 13:54:50 +01:00
assert gym.spec("test/Test3") is not None