o
    
i\8                     @   s   d Z ddlZddlmZ ddlZddlZddlmZmZmZm	Z	m
Z
 dd Zdd	 ZG d
d dZG dd dZG dd dZG dd dZdS )z6
testing of anchors and the aliases referring to them
    N)dedent   )
round_tripr   round_trip_loadround_trip_dumpYAMLc                 C   s   t t| S N)r   r   )s r
   W/home/ubuntu/.local/lib/python3.10/site-packages/srsly/tests/ruamel_yaml/test_anchor.pyload   s   r   c                 C   s   t | t|ks
J d S r   )r   r   )dr	   r
   r
   r   compare   s   r   c                   @   s   e Zd Zdd Zdd Zdd Zdd Zejj	e
 d	kd
ddd Zdd ZedZdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#S )$TestAnchorsAliasesc                 C   s0   ddl m} |jdksJ td}t|d d S )Nr   )
Serializerzid%03dzK
        a: &id002
          b: 1
          c: 2
        d: *id002
        zK
        a: &id001
          b: 1
          c: 2
        d: *id001
        )srsly.ruamel_yaml.serializerr   ANCHOR_TEMPLATEr   r   )selfr   datar
   r
   r   test_anchor_id_renumber   s   z*TestAnchorsAliases.test_anchor_id_renumberc                 C   sp   ddl m} |dsJ |dsJ |dsJ |dsJ |ds$J |dr*J |d	r0J |d
r6J dS )z&test if id matches the anchor templater   )templated_idid001id999id1000id0001id0000id02id000x000N)r   r   )r   r   r
   r
   r   test_template_matcher2   s   z(TestAnchorsAliases.test_template_matcherc                 C   sx   ddl m} td}|d }t||sJ | d u sJ |d }t||s(J | jdks1J | jdu s:J d S )Nr   CommentedMap
        a: &id002
          b: 1
          c: 2
        d: *id002
        e: &etemplate
          b: 1
          c: 2
        f: *etemplate
        r   e	etemplateF)srsly.ruamel_yaml.commentsr!   r   
isinstanceyaml_anchorvaluealways_dump)r   r!   r   r   r#   r
   r
   r   test_anchor_assignedE   s   z'TestAnchorsAliases.test_anchor_assignedc                 C   s   t d}t|d d S )Nr"   z
        a: &id001
          b: 1
          c: 2
        d: *id001
        e: &etemplate
          b: 1
          c: 2
        f: *etemplate
        )r   r   r   r   r
   r
   r   test_anchor_id_retained\   s   z*TestAnchorsAliases.test_anchor_id_retainedJythonzJython throws RepresenterError)reasonc                 C   sH   ddl m} t| td}|}W d    d S 1 sw   Y  d S )Nr   )ComposerErrorz_
            d: *id002
            a: &id002
              b: 1
              c: 2
            )srsly.ruamel_yaml.composerr/   pytestraisesr   )r   r/   r   r
   r
   r   test_alias_before_anchorw   s   "z+TestAnchorsAliases.test_alias_before_anchorc                 C   sP   ddl m} td}|d }t||sJ | d usJ | jdks&J d S )Nr   )CommentedSeqz
        nut1: &alice
         - 1
         - 2
        nut2: &blake
         - some data
         - *alice
        nut3:
         - *blake
         - *alice
        nut1alice)r%   r4   r   r&   r'   r(   )r   r4   r   rr
   r
   r   test_anchor_on_sequence   s   z*TestAnchorsAliases.test_anchor_on_sequencea  
        - &CENTER {x: 1, y: 2}
        - &LEFT {x: 0, y: 2}
        - &BIG {r: 10}
        - &SMALL {r: 1}
        # All the following maps are equal:
        # Explicit keys
        - x: 1
          y: 2
          r: 10
          label: center/small
        # Merge one map
        - <<: *CENTER
          r: 10
          label: center/medium
        # Merge multiple maps
        - <<: [*CENTER, *BIG]
          label: center/big
        # Override
        - <<: [*BIG, *LEFT, *SMALL]
          x: 1
          label: center/huge
        c              	   C   s   t | j}|d }d}|D ];}dD ]6}||}|| |}t|ts2|dd }|dd }||krGd}td||||| | qq|sMJ d S )N   T)         /r   Fkey)r   
merge_yamlgetr&   intsplitprint)r   r   r   okkoxyr
   r
   r   test_merge_00   s    


	z TestAnchorsAliases.test_merge_00c                 C   s@   ddl m}m} td}|d }t||sJ t||sJ d S )Nr   )r!   merge_attribz
        k: &level_2 { a: 1, b2 }
        l: &level_1 { a: 10, c: 3 }
        m:
          <<: *level_1
          c: 30
          d: 40
        m)r%   r!   rJ   r   r&   hasattr)r   r!   rJ   r   r   r
   r
   r   test_merge_accessible   s   
z(TestAnchorsAliases.test_merge_accessiblec                 C   s   t | j}t|| j d S r   )r   r?   r   r+   r
   r
   r   test_merge_01   s   
z TestAnchorsAliases.test_merge_01c                 C      d}t |}d S )Nz
        a:
          <<: &content
            1: plugh
            2: plover
          0: xyzzy
        b:
          <<: *content
        r   r   yamlr   r
   r
   r   test_merge_nested   s   	z$TestAnchorsAliases.test_merge_nestedc                 C   rO   )Nz
        a:
          <<: &content
            <<: &y2
              1: plugh
            2: plover
          0: xyzzy
        b:
          <<: [*content, *y2]
        rP   rQ   r
   r
   r   test_merge_nested_with_sequence   s   
z2TestAnchorsAliases.test_merge_nested_with_sequencec                 C   s^   ddl m} | }| }||d< d|d< d|d< |jdd	d
 |d jdd	d
 t|d d S )Nr   r    a   c   bklmT)r)   xyzzB
        &klm
        a: &xyz
          c: 3
        b: 2
        )r%   r!   yaml_set_anchorr   )r   r!   r   data_ar
   r
   r   test_add_anchor   s   z"TestAnchorsAliases.test_add_anchorc                 C   sH   ddl m} d}t| t|}W d    d S 1 sw   Y  d S )Nr   )ReusedAnchorWarningzc
        - &a
          x: 1
        - <<: *a
        - &a
          x: 2
        - <<: *a
        )srsly.ruamel_yaml.errorr_   r1   warnsr   )r   r_   rR   r   r
   r
   r   test_reused_anchor  s
   
"z%TestAnchorsAliases.test_reused_anchorc                 C   8   t d}tj|}|d d d d d dksJ d S )Na          components:
          server: &server_component
            type: spark.server:ServerComponent
            host: 0.0.0.0
            port: 8000
          shell: &shell_component
            type: spark.shell:ShellComponent

        services:
          server: &server_service
            <<: *server_component
          shell: &shell_service
            <<: *shell_component
            components:
              server: {<<: *server_service}
        servicesshell
componentsserverporti@  r   srslyruamel_yaml	safe_loadr   ysr   r
   r
   r   test_issue_130  s
   $z!TestAnchorsAliases.test_issue_130c                 C   rc   )Na          components:
          server: &server_component
            type: spark.server:ServerComponent
            host: 0.0.0.0
            port: 8000
          shell: &shell_component
            type: spark.shell:ShellComponent

        services:
          server: &server_service
            <<: *server_component
            port: 4000
          shell: &shell_service
            <<: *shell_component
            components:
              server: {<<: *server_service}
        rd   re   rf   rg   rh   i  ri   rm   r
   r
   r   test_issue_130a3  s
   $z"TestAnchorsAliases.test_issue_130aN)__name__
__module____qualname__r   r   r*   r,   r1   markskipifplatformpython_implementationr3   r8   r   r?   rI   rM   rN   rS   rT   r^   rb   ro   rp   r
   r
   r
   r   r      s.    

r   c                   @   sT   e Zd ZedZdd Zdd Zdd Zdd	 Zd
d Z	dd Z
dd Zdd ZdS )TestMergeKeysValuesz    - &mx
      a: x1
      b: x2
      c: x3
    - &my
      a: y1
      b: y2  # masked by the one in &mx
      d: y4
    -
      a: 1
      <<: [*mx, *my]
      m: 6
    c                 C   s\   ddl m} || j}t| j}d}|d D ]}|d7 }t|| q|t|d ks,J d S Nr   rl   rX   r   )srsly.ruamel_yamlrl   yaml_strr   rC   lenr   rl   r   r   countrG   r
   r
   r   test_merge_forb  s   

z"TestMergeKeysValues.test_merge_forc                 C   `   ddl m} || j}t| j}d}|d  D ]}|d7 }t|| q|t|d ks.J d S ry   )r{   rl   r|   r   keysrC   r}   r~   r
   r
   r   test_merge_keysm     

z#TestMergeKeysValues.test_merge_keysc                 C   r   ry   )r{   rl   r|   r   valuesrC   r}   r~   r
   r
   r   test_merge_valuesx  r   z%TestMergeKeysValues.test_merge_valuesc                 C   r   ry   )r{   rl   r|   r   itemsrC   r}   r~   r
   r
   r   test_merge_items  r   z$TestMergeKeysValues.test_merge_itemsc                 C   s   ddl m} ddlm} || j}t| j}|d  }td|d  t|d  |t| t|d  }td| t||ksEJ |d d= |rP|d8 }t||ksXJ |d d	= |rc|d8 }t||kskJ |d d
= |rv|d8 }t||ks~J d S )Nr   rz   )PY3rX   zd2 itemsrefrK   r   r   rU   )	r{   rl   srsly.ruamel_yaml.compatr   r|   r   r   rC   r}   )r   rl   r   r   r   rG   r   r
   r
   r   test_len_items_delete  s*   

(



z)TestMergeKeysValues.test_len_items_deletec                 C   s"  ddl m} | }|dd }|D ]}td| q| D ]}td| qtdt|  tdt|  tdt|  tt	| td d	|v sQJ i }|D ]}|| ||< qUd	|v sdJ d	| v slJ |d	 d
kstJ |
d	d
ks}J d	t|v sJ d	t| v sJ d S )Nr   r   zi        anchored: &anchor
          a : 1

        mapping:
          <<: *anchor
          b: 2
        mappingrE   kcvz-----rU   r   )r{   r   r   rC   copylistr   r   r   r}   __getitem__dict)r   capsysr   rR   r   rE   rG   r
   r
   r   test_issue_196_cast_of_dict  s4   	z/TestMergeKeysValues.test_issue_196_cast_of_dictc                 C   sB   ddl m} | }|t| j}t|d  g dksJ d S )Nr   r   rX   )r   r;   x2x3y4)r{   r   r   r   r|   r   r   )r   r   rR   r   r
   r
   r   test_values_of_merged  s    z)TestMergeKeysValues.test_values_of_mergedc                 C   st   ddl m} | }|dd }|d dksJ | }|d dks$J td| |d= d|vs2J d|v s8J d S )Nr   r   zY        foo: &foo
          a: a
        foo2:
          <<: *foo
          b: b
        foo2rU   r   )r{   r   r   r   rC   )r   r   rR   r   d2r
   r
   r   test_issue_213_copy_of_merge  s   	
z0TestMergeKeysValues.test_issue_213_copy_of_mergeN)rq   rr   rs   r   r|   r   r   r   r   r   r   r   r   r
   r
   r
   r   rx   M  s    (rx   c                   @   s   e Zd Zdd Zdd ZdS )TestDuplicateKeyThroughAnchorc                 C   s  ddl m} ddl m}m} ddlm}m} td}|dk r d S |dk rYt	| || W d    n1 s8w   Y  t	| || W d    d S 1 sRw   Y  d S t
| || W d    n1 smw   Y  t
| || W d    d S 1 sw   Y  d S )Nr   version_info)rl   r   )DuplicateKeyFutureWarningDuplicateKeyErrorz        &anchor foo:
            foo: bar
            *anchor : duplicate key
            baz: bat
            *anchor : duplicate key
        r      r   )r      r   )r{   r   rl   r   srsly.ruamel_yaml.constructorr   r   r   r1   ra   r2   )r   r   rl   r   r   r   r	   r
   r
   r   test_duplicate_key_00  s*   	

"

"z3TestDuplicateKeyThroughAnchor.test_duplicate_key_00c                 C   s   ddl m} ddlm} td}|dk rd S t| tdd}|| W d    n1 s0w   Y  t| t }|| W d    d S 1 sNw   Y  d S )Nr   r   )r   z        - &name-name
          a: 1
        - &help-name
          b: 2
        - <<: *name-name
          <<: *help-name
        r   safe)typ)	r{   r   r   r   r   r1   r2   r   r   )r   r   r   r	   rR   r
   r
   r   test_duplicate_key_01
  s   

"z3TestDuplicateKeyThroughAnchor.test_duplicate_key_01N)rq   rr   rs   r   r   r
   r
   r
   r   r     s    r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestFullCharSetAnchorsc                 C   rO   )Nz
        - collection: &Backend.Civilizations.RacialPerk
            items:
                  - key: perk_population_growth_modifier
        - *Backend.Civilizations.RacialPerk
        )r   r   r|   r   r
   r
   r   test_master_of_orion%  s   z+TestFullCharSetAnchors.test_master_of_orionc                 C   rO   )Nza
        - &dotted.words.here
          a: 1
          b: 2
        - *dotted.words.here
        rP   r   r
   r
   r   test_roundtrip_000  s   z(TestFullCharSetAnchors.test_roundtrip_00c                 C   s"   d}t |}t||dd d S )NzI
        - &dotted.words.here[a, b]
        - *dotted.words.here
        [z [)r   r   replacer   r
   r
   r   test_roundtrip_019  s   z(TestFullCharSetAnchors.test_roundtrip_01N)rq   rr   rs   r   r   r   r
   r
   r
   r   r   $  s    	r   )__doc__r1   textwrapr   rv   rj   	roundtripr   r   r   r   r   r   r   rx   r   r   r
   r
   r
   r   <module>   s    	  4 9