2022-09-16 23:41:27 +01:00
""" Tests that gym.spec works as expected. """
2022-06-23 13:54:50 +01:00
import re
import pytest
2022-09-16 23:41:27 +01:00
import gymnasium as gym
2022-06-23 13:54:50 +01:00
def test_spec ( ) :
2022-09-16 23:41:27 +01:00
spec = gym . spec ( " CartPole-v1 " )
2022-06-23 13:54:50 +01:00
assert spec . id == " CartPole-v1 "
2022-09-16 23:41:27 +01:00
assert spec is gym . envs . registry [ " CartPole-v1 " ]
2022-06-23 13:54:50 +01:00
def test_spec_kwargs ( ) :
map_name_value = " 8x8 "
2022-09-16 23:41:27 +01:00
env = gym . make ( " FrozenLake-v1 " , map_name = map_name_value )
2022-11-12 10:21:24 +00:00
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 ( ) :
2022-09-16 23:41:27 +01:00
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 (
2022-09-16 23:41:27 +01:00
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. "
) ,
) :
2022-09-16 23:41:27 +01:00
gym . spec ( " Test1-v1 " )
2022-06-23 13:54:50 +01:00
with pytest . raises (
2022-09-16 23:41:27 +01:00
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` ]. "
) ,
) :
2022-09-16 23:41:27 +01:00
gym . spec ( " Test1-v1000 " )
2022-06-23 13:54:50 +01:00
with pytest . raises (
2022-09-16 23:41:27 +01:00
gym . error . UnregisteredEnv ,
2022-06-23 13:54:50 +01:00
match = re . escape ( " Environment Unknown1 doesn ' t exist. " ) ,
) :
2022-09-16 23:41:27 +01:00
gym . spec ( " Unknown1-v1 " )
2022-06-23 13:54:50 +01:00
def test_spec_malformed_lookup ( ) :
with pytest . raises (
2022-09-16 23:41:27 +01:00
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)) " ) } $ ' ,
) :
2022-09-16 23:41:27 +01:00
gym . spec ( " “Breakout-v0” " )
2022-06-23 13:54:50 +01:00
def test_spec_versioned_lookups ( ) :
2022-09-16 23:41:27 +01:00
gym . register ( " test/Test2-v5 " , " no-entry-point " )
2022-06-23 13:54:50 +01:00
with pytest . raises (
2022-09-16 23:41:27 +01:00
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` ]. "
) ,
) :
2022-09-16 23:41:27 +01:00
gym . spec ( " test/Test2-v9 " )
2022-06-23 13:54:50 +01:00
with pytest . raises (
2022-09-16 23:41:27 +01:00
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. "
) ,
) :
2022-09-16 23:41:27 +01:00
gym . spec ( " test/Test2-v4 " )
2022-06-23 13:54:50 +01:00
2022-09-16 23:41:27 +01:00
assert gym . spec ( " test/Test2-v5 " ) is not None
2022-06-23 13:54:50 +01:00
def test_spec_default_lookups ( ) :
2022-09-16 23:41:27 +01:00
gym . register ( " test/Test3 " , " no-entry-point " )
2022-06-23 13:54:50 +01:00
with pytest . raises (
2022-09-16 23:41:27 +01:00
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`. "
) ,
) :
2022-09-16 23:41:27 +01:00
gym . spec ( " test/Test3-v0 " )
2022-06-23 13:54:50 +01:00
2022-09-16 23:41:27 +01:00
assert gym . spec ( " test/Test3 " ) is not None