U
    d»{jR  ã                   @   s¦   d dl Z d dlmZ d dlmZmZmZ d dlmZ d dl	m
Z
mZmZmZ d dlmZmZmZ ddlmZmZ dd	„ Zd
d„ ZG dd„ deƒZG dd„ deeƒZdS )é    N)Ú	CosmologyÚFlatLambdaCDMÚPlanck18)Úunits)Ú	from_yamlÚto_yamlÚyaml_constructorÚyaml_representer)ÚAstropyDumperÚdumpÚloadé   )ÚToFromDirectTestBaseÚToFromTestMixinBasec                  C   s<   t dƒ} t| ƒst‚ttƒ}t|tƒs*t‚| d¡s8t‚dS )z9Test :func:`~astropy.cosmology.io.yaml.yaml_representer`.z!!astropy.cosmology.flrw.LambdaCDMz%!astropy.cosmology.flrw.FlatLambdaCDMN)r	   ÚcallableÚAssertionErrorr   r   Ú
isinstanceÚstrÚ
startswith)ZrepresenterÚyml© r   úc/var/www/html/bwave/python/venv/lib/python3.8/site-packages/astropy/cosmology/io/tests/test_yaml.pyÚtest_yaml_representer   s
    r   c               	   C   sh   t tƒ} t| ƒst‚ttƒ}t t¡� t	|ƒ}W 5 Q R X t
|tƒsHt‚|tksTt‚|jtjksdt‚dS )z9Test :func:`~astropy.cosmology.io.yaml.yaml_constructor`.N)r   r   r   r   r   r   ÚuZadd_enabled_unitsÚcur   r   Úmeta)Úconstructorr   Úcosmor   r   r   Útest_yaml_constructor&   s    r   c                   @   sZ   e Zd ZdZejdd„ ƒZdd„ Zdd„ Zdd	„ Z	ej
 d
dddg¡dd„ ƒZdd„ ZdS )ÚToFromYAMLTestMixina¼  
    Tests for a Cosmology[To/From]Format with ``format="yaml"``.
    This class will not be directly called by :mod:`pytest` since its name does
    not begin with ``Test``. To activate the contained tests this class must
    be inherited in a subclass. Subclasses must define a :func:`pytest.fixture`
    ``cosmo`` that returns/yields an instance of a |Cosmology|.
    See ``TestCosmologyToFromFormat`` or ``TestCosmology`` for examples.
    c                 C   s    |t jkrt d|› d�¡ dS )zð
        YAML I/O only works on registered classes. So the thing to check is
        if this class is registered. If not, :func:`pytest.xfail` this test.
        Some of the tests define custom cosmologies. They are not registered.
        zCosmologies of type z are not registered with YAML.N)r
   Zyaml_representersÚpytestZxfail)ÚselfZ	cosmo_clsr   r   r   Ú!xfail_if_not_registered_with_yamlD   s    

ÿz5ToFromYAMLTestMixin.xfail_if_not_registered_with_yamlc                 C   s(   |dƒ}t |tƒst‚| d¡s$t‚dS )zTest cosmology -> YAML.Úyamlz!astropy.cosmology.N)r   r   r   r   )r!   r   Ú	to_formatr"   r   r   r   r   Útest_to_yamlR   s    z ToFromYAMLTestMixin.test_to_yamlc                 C   s`   |dƒ}||dd�}|j |j ks$t‚|j|jks4t‚||dd�}||ksLt‚|j|jks\t‚dS )z$Test cosmology -> YAML -> cosmology.r#   ©ÚformatN)Únamer   r   )r!   r   r$   Úfrom_formatr"   r   Úgotr   r   r   Útest_from_yaml_defaultY   s    z*ToFromYAMLTestMixin.test_from_yaml_defaultc              	   C   s@   |   d¡dkst‚|dƒ}t ttf¡� ||ƒ W 5 Q R X dS )zÕAs a non-path string, it does NOT auto-identifies 'format'.

        TODO! this says there should be different types of I/O registries.
              not just hacking object conversion on top of file I/O.
        r#   FN)Zcan_autodentifyr   r    ZraisesÚFileNotFoundErrorÚOSError)r!   r   r$   r)   r"   r   r   r   r   Útest_from_yaml_autoidentifyl   s    z/ToFromYAMLTestMixin.test_from_yaml_autoidentifyr'   TFNc                 C   s4   |dƒ}t |tƒrt‚|j||d�}|dks0t‚dS )a#  Test :meth:`astropy.cosmology.Cosmology.is_equivalent`.

        This test checks that Cosmology equivalency can be extended to any
        Python object that can be converted to a Cosmology -- in this case
        a YAML string. YAML can't be identified without "format" specified.
        r#   r&   FN)r   r   r   Úis_equivalent)r!   r   r$   r'   r"   ÚobjZis_equivr   r   r   Útest_is_equivalent_to_yaml…   s    
z.ToFromYAMLTestMixin.test_is_equivalent_to_yamlc                 C   s   |j |dƒdd�dkst‚dS )zŒTest :meth:`astropy.cosmology.Cosmology.is_equivalent`.

        Same as ``test_is_equivalent_to_yaml`` but with ``format="yaml"``.
        r#   r&   TN)r/   r   )r!   r   r$   r"   r   r   r   Ú)test_is_equivalent_to_yaml_specify_format•   s    z=ToFromYAMLTestMixin.test_is_equivalent_to_yaml_specify_format)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r    Úfixturer"   r%   r+   r.   ÚmarkZparametrizer1   r2   r   r   r   r   r   :   s   	

r   c                   @   s6   e Zd ZdZdd„ Zejddd�dd„ ƒZd	d
„ ZdS )ÚTestToFromYAMLa'  
    Directly test ``to/from_yaml``.
    These are not public API and are discouraged from use, in favor of
    ``Cosmology.to/from_format(..., format="yaml")``, but should be tested
    regardless b/c 3rd party packages might use these in their Cosmology I/O.
    Also, it's cheap to test.
    c                 C   s   t tdœ| _dS )zBSet up fixtures to use ``to/from_yaml``, not the I/O abstractions.)ÚtoÚfromN)r   r   Z	functions©r!   r   r   r   Úsetup_class¨   s    zTestToFromYAML.setup_classÚclassT)ÚscopeZautousec                 c   s
   dV  dS )zÊ
        Setup and teardown for tests.
        This overrides from super because `ToFromDirectTestBase` adds a custom
        Cosmology ``CosmologyWithKwargs`` that is not registered with YAML.
        Nr   r<   r   r   r   Úsetup¬   s    zTestToFromYAML.setupc                 C   s   dS )z�
        If directly calling the function there's no auto-identification.
        So this overrides the test from `ToFromYAMLTestMixin`
        Nr   )r!   r   r$   r)   r   r   r   r.   µ   s    z*TestToFromYAML.test_from_yaml_autoidentifyN)	r3   r4   r5   r6   r=   r    r7   r@   r.   r   r   r   r   r9   Ÿ   s
   
r9   )r    Zastropy.unitsr   r   Zastropy.cosmologyr   r   r   r   Zastropy.cosmology.io.yamlr   r   r   r	   Zastropy.io.misc.yamlr
   r   r   Úbaser   r   r   r   r   r9   r   r   r   r   Ú<module>   s   e