3
d[                 @   sR   d dl mZmZmZ d dlZddlmZmZmZm	Z	m
Z
mZmZ G dd dZdS )    )absolute_importprint_functionunicode_literalsN   )
round_tripna_round_tripround_trip_loadround_trip_dumpdedentsave_and_runYAMLc               @   s  e Z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Z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'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Z d;d< Z!d=d> Z"d?d@ Z#dAdB Z$dCdD Z%dEdF Z&dGdH Z'dIdJ Z(dKdL Z)dMdN Z*dOdP Z+dQdR Z,dSdT Z-dUdV Z.dWdX Z/dYdZ Z0d[d\ Z1d]d^ Z2d_d` Z3dadb Z4dcdd Z5dedf Z6dgdh Z7diS )j
TestIssuesc             C   sP   dd l }td}|jj|}t|d t|d ks8tt|d dksLtd S )Nr   z
        def1: &ANCHOR1
            key1: value1
        def: &ANCHOR
            <<: *ANCHOR1
            key: value
        comb:
            <<: *ANCHOR
        Zcombdefz3ordereddict([('key', 'value'), ('key1', 'value1')]))srsly.ruamel_yamlr
   ruamel_yamlr   strAssertionError)selfsrslysdata r   W/var/www/html/virt/lib64/python3.6/site-packages/srsly/tests/ruamel_yaml/test_issues.pytest_issue_61   s    	zTestIssues.test_issue_61c             C   s   d}t t||dkstd S )Nao  
        from __future__ import print_function

        import srsly.ruamel_yaml as yaml

        import re


        class SINumber(yaml.YAMLObject):
            PREFIXES = {'k': 1e3, 'M': 1e6, 'G': 1e9}
            yaml_loader = yaml.Loader
            yaml_dumper = yaml.Dumper
            yaml_tag = u'!si'
            yaml_implicit_pattern = re.compile(
                r'^(?P<value>[0-9]+(?:\.[0-9]+)?)(?P<prefix>[kMG])$')

            @classmethod
            def from_yaml(cls, loader, node):
                return cls(node.value)

            @classmethod
            def to_yaml(cls, dumper, data):
                return dumper.represent_scalar(cls.yaml_tag, str(data))

            def __init__(self, *args):
                m = self.yaml_implicit_pattern.match(args[0])
                self.value = float(m.groupdict()['value'])
                self.prefix = m.groupdict()['prefix']

            def __str__(self):
                return str(self.value)+self.prefix

            def __int__(self):
                return int(self.value*self.PREFIXES[self.prefix])

        # This fails:
        yaml.add_implicit_resolver(SINumber.yaml_tag, SINumber.yaml_implicit_pattern)

        ret = yaml.load("""
        [1,2,3, !si 10k, 100G]
        """, Loader=yaml.Loader)
        for idx, l in enumerate([1, 2, 3, 10000, 100000000000]):
            assert int(ret[idx]) == l
        r   )r   r
   r   )r   tmpdirprogram_srcr   r   r   test_issue_82)   s    ,zTestIssues.test_issue_82c             C   s   d}t |dd}d S )Nz[1, 2, 3, !si 10k, 100G]
T)preserve_quotes)r   )r   r   yaml_strxr   r   r   test_issue_82rtX   s    zTestIssues.test_issue_82rtc             C   s   t d}t|dd}d S )Nz
        var1: #empty
        var2: something #notempty
        var3: {} #empty object
        var4: {a: 1} #filled object
        var5: [] #empty array
        T)r   )r
   r   )r   r   r   r   r   r   test_issue_102\   s    zTestIssues.test_issue_102c             C   s>   ddl m} d}| }|j|}|d }dtf |ks:td S )Nr   )r   z        base: &base_key
          first: 123
          second: 234

        child:
          <<: *base_key
          third: 345
        childsecond)r   r   loaddictr   )r   r   inpyamlr   r"   r   r   r   test_issue_150h   s    

zTestIssues.test_issue_150c             C   sZ   ddl m} td}t|}|d d d= | }t||dd td}|j |ksVtd S )	Nr   )StringIOzq        root:
            # a comment
            - {some_key: "value"}

        foo: 32
        bar: 32
        rootZsome_key   )Zblock_seq_indentz`        root:
            # a comment
            - {}

        foo: 32
        bar: 32
        )srsly.ruamel_yaml.compatr)   r
   r   r	   getvaluer   )r   r)   r   abufexpr   r   r   test_issue_160y   s    zTestIssues.test_issue_160c             C   s,   t d}xdD ]}|j|}t|}qW d S )NzA        mapping-A:
          key-A:{}
        mapping-B:
          # no-newline  # some comment

)r2   r3   r4   r5   )r
   formatr   )r   r   commentr   resr   r   r   test_issue_161   s
    

zTestIssues.test_issue_161c             C   s,   t d}xdD ]}|j|}t|}qW d S )NzA        mapping-A:
          key-A:{}
        mapping-B:
        

# between)r:   )r
   r6   r   )r   r   r7   r   r8   r   r   r   test_issue_161a   s
    

zTestIssues.test_issue_161ac             C   s   t d}t|dd}d S )Nz?        some-list:
        # List comment
        - {}
        T)r   )r
   r   )r   r   r   r   r   r   test_issue_163   s    zTestIssues.test_issue_163z{"sshKeys":[{"name":"AETROS\/google-k80-1","uses":0,"getLastUse":0,"fingerprint":"MD5:19:dd:41:93:a1:a3:f5:91:4a:8e:9b:d0:ae:ce:66:4c","created":1509497961}]}z{"abc":[{"a":"1", "uses":0}]}c             C   s   t tj}t tj}d S )N)r   r   	json_str2json_str)r   r   r   r   r   test_issue_172   s    
zTestIssues.test_issue_172c             C   s4   ddl m} | }|jd}ddddg|d d < d S )Nr   )r   z[1,2,3]r         r+   )r   r   r$   )r   r   r'   seqr   r   r   test_issue_176   s    
zTestIssues.test_issue_176c             C   sJ   t d}t|}ddg|dd d< t|}||jddjdd	ksFtd S )
Nz}        - a
        - b  # comment
        - c  # commment c
        # comment c+
        - d

        - e # comment
        BDr   r@   z b z B z d
z D
)r
   r   r	   replacer   )r   r   rB   r8   r   r   r   =test_issue_176_preserve_comments_on_extended_slice_assignment   s    	zHTestIssues.test_issue_176_preserve_comments_on_extended_slice_assignmentc             C   s2  ddl m} td}t|dks$t|dd g ks8t|dd ddgksPt|dd d ddgksjt|d d  }d	g|dd< |ddd	dddgkst|d d  }d	d
dg|d d< |d	d
dddgkst|d d  }d	d
dg|dd < |ddd	d
dgkst|d d  }d	d
dg|d d < |d	d
dgks8t|d d  }d	d
dg|dd< |ddd	d
ddgkspt|d d  }d	d
g|dd d< |dd	dd
dgkst|d d  }|rtjtdd d	d
dg|dd d< W d Q R X n.tjtdd d	d
dg|dd d< W d Q R X |rHtjtdd d	g|dd d< W d Q R X n*tjtdd d	g|dd d< W d Q R X |d d  }|dg7 }d	d
dg|dd d< |dd	dd
ddgkst|d d  }|dd= |dddgkst|d d  }|d d d= |ddgks
t|d d  }|d d = |g ks.td S )Nr   )PY2z[0, 1, 2, 3, 4]   r@   r+   rA   r   *   +   ,   zattempt to assign)matchztoo manyz
not enough)	r,   rH   r   lenr   pytestraises
ValueError	TypeError)r   rH   Zmssmr   r   r   test_issue_176_test_slicing   s`     


z&TestIssues.test_issue_176_test_slicingc             C   s<   t d}t|}d|d< |jdd t||d ks8td S )NzQ        test::test:
          # test
          foo:
            bar: baz
        ZfoobarZtest1zbar: foo # test1
)r
   r   Zyaml_add_eol_commentr	   r   )r   r   dr   r   r   test_issue_184  s    zTestIssues.test_issue_184c             C   s   t d}t|}d S )Nz,        [StackName: AWS::StackName]
        )r
   r   )r   r   rV   r   r   r   test_issue_219'  s    zTestIssues.test_issue_219c             C   s   t d}t|}d S )Nz4        [StackName:
        AWS::StackName]
        )r
   r   )r   r   rV   r   r   r   test_issue_219a/  s    zTestIssues.test_issue_219ac             C   s"   d}t t||dddkstd S )Nz
        from srsly.ruamel_yaml import YAML

        yaml_str = u"""\
        ---
        foo: ["bar"]
        """

        yaml = YAML(typ='safe', pure=True)
        d = yaml.load(yaml_str)
        print(d)
        T)Z	optimizedr   )r   r
   r   )r   r   r   r   r   r   test_issue_2208  s    zTestIssues.test_issue_220c             C   s*   ddl m} |dddg}|ddg  d S )Nr   )CommentedSeqr   r@   rA   r+   rI   )srsly.ruamel_yaml.commentsr[   )r   r[   r.   r   r   r   test_issue_221_addG  s    zTestIssues.test_issue_221_addc             C   sj   ddl m} ddlm} | }td}|jt|}|j  | }|j|| td}|j |ksft	d S )Nr   )r   )r)   zX        - d
        - a  # 1
        - c  # 3
        - e  # 5
        - b  # 2
        zX        - a  # 1
        - b  # 2
        - c  # 3
        - d
        - e  # 5
        )
r   r   r,   r)   r
   r$   sortdumpr-   r   )r   r   r)   r'   r&   r.   r/   r0   r   r   r   test_issue_221_sortM  s    zTestIssues.test_issue_221_sortc             C   sn   ddl m} ddlm} | }td}|jt|}|jdd | }|j|| td}|j |ksjt	d S )Nr   )r   )r)   zX        - d
        - a  # 1
        - c  # 3
        - e  # 5
        - b  # 2
        T)reversezX        - e  # 5
        - d
        - c  # 3
        - b  # 2
        - a  # 1
        )
r   r   r,   r)   r
   r$   r^   r_   r-   r   )r   r   r)   r'   r&   r.   r/   r0   r   r   r   test_issue_221_sort_reversej  s    z&TestIssues.test_issue_221_sort_reversec             C   sp   ddl m} ddlm} | }td}|jt|}|jtjd | }|j	|| td}|j
 |ksltd S )Nr   )r   )r)   zk        - four
        - One    # 1
        - Three  # 3
        - five   # 5
        - two    # 2
        )keyzk        - five   # 5
        - four
        - One    # 1
        - Three  # 3
        - two    # 2
        )r   r   r,   r)   r
   r$   r^   r   lowerr_   r-   r   )r   r   r)   r'   r&   r.   r/   r0   r   r   r   test_issue_221_sort_key  s    z"TestIssues.test_issue_221_sort_keyc             C   sr   ddl m} ddlm} | }td}|jt|}|jtjdd | }|j	|| td}|j
 |ksntd S )Nr   )r   )r)   zk        - four
        - One    # 1
        - Three  # 3
        - five   # 5
        - two    # 2
        T)rc   ra   zk        - two    # 2
        - Three  # 3
        - One    # 1
        - four
        - five   # 5
        )r   r   r,   r)   r
   r$   r^   r   rd   r_   r-   r   )r   r   r)   r'   r&   r.   r/   r0   r   r   r   test_issue_221_sort_key_reverse  s    z*TestIssues.test_issue_221_sort_key_reversec             C   s>   dd l }ddlm} | }|jjdg| |j dks:td S )Nr   )r)   Z012923z['012923']
)r   r,   r)   r   Z	safe_dumpr-   r   )r   r   r)   r/   r   r   r   test_issue_222  s
    zTestIssues.test_issue_222c             C   s$   dd l }|jjdd}|jd d S )Nr   safe)typzphone: 0123456789)r   r   r   r$   )r   r   r'   r   r   r   test_issue_223  s    zTestIssues.test_issue_223c             C   sb   dd l }dd l j}tj|jjj |jd W d Q R X tj|jjj |jd W d Q R X d S )Nr   ]z{])r   r   rO   rP   parserParserErrorZ	safe_load)r   r   r'   r   r   r   test_issue_232  s    
zTestIssues.test_issue_232c             C   s2   ddl m} dd l}| }|jd}|j|}d S )Nr   )r   z{})r   r   jsonr$   dumps)r   r   ro   r'   r   r>   r   r   r   test_issue_233  s
    
zTestIssues.test_issue_233c             C   s2   ddl m} dd l}| }|jd}|j|}d S )Nr   )r   z[])r   r   ro   r$   rp   )r   r   ro   r'   r   r>   r   r   r   test_issue_233a  s
    
zTestIssues.test_issue_233ac             C   sR   ddl m} td}|ddd}|j|}|d d }tt| d|ksNtd S )	Nr   )r   z        - key: key1
          ctx: [one, two]
          help: one
          cmd: >
            foo bar
            foo bar
        rh   T)ri   purecmd)r   r   r
   r$   printreprr   )r   r   r&   r'   r   foldr   r   r   test_issue_234  s    
zTestIssues.test_issue_234c             C   s   d}t |dd}d S )NzI
        conf:
          xx: {a: "b", c: []}
          asd: "nn"
        T)r   )r   )r   r&   rV   r   r   r   test_issue_236  s    zTestIssues.test_issue_236c             C   s   d}t t||dkstd S )Na  
        import srsly.ruamel_yaml
        from srsly.ruamel_yaml.compat import StringIO

        yaml = srsly.ruamel_yaml.YAML(typ='unsafe')


        class A:
            def __setstate__(self, d):
                self.__dict__ = d


        class B:
            pass


        a = A()
        b = B()

        a.x = b
        b.y = [b]
        assert a.x.y[0] == a.x

        buf = StringIO()
        yaml.dump(a, buf)

        data = yaml.load(buf.getvalue())
        assert data.x.y[0] == data.x
        r   )r   r
   r   )r   r   r   r   r   r   test_issue_238  s    zTestIssues.test_issue_238c             C   s   d}t  j|}d S )Na~  
        first_name: Art
        occupation: Architect
        # I'm safe
        about: Art Vandelay is a fictional character that George invents...
        # we are not :(
        # help me!
        ---
        # what?!
        hello: world
        # someone call the Batman
        foo: bar # or quz
        # Lost again
        ---
        I: knew
        # final words
        )r   Zround_trip_all)r   r&   rV   r   r   r   test_issue_239#  s    zTestIssues.test_issue_239c             C   s*   ddl m} |dg}|d dks&td S )Nr   )CommentedMapr.   b)r.   r~   )r\   r}   r   )r   r}   Zd0r   r   r   test_issue_2427  s    
zTestIssues.test_issue_242c             C   s~   ddl m} d}xhdD ]`}|jdr6d}|d d }nd }|||d
}d|_|j|}t||j|j |d dkstqW d S )Nr   )r   z
        d: yes
        safepurertrh   rs   Tr+   )ri   rs   r   rV   )r   r   rh   )r   r   )	r   r   endswithversionr$   rv   rl   resolverr   )r   r   r&   ri   rs   r'   rV   r   r   r   test_issue_245=  s    


zTestIssues.test_issue_245c             C   s(   t  }td}td}|j||d d S )NzN        # comment
        -
          - 1
          - 2
          - 3
        zD        # comment
        - - 1
          - 2
          - 3
        )Zoutp)r   r
   r   )r   r'   r&   r0   r   r   r   test_issue_249P  s    zTestIssues.test_issue_249c             C   s   d}t |}d S )Nz[
        # 1.
        - - 1
        # 2.
        - map: 2
        # 3.
        - 4
        )r   )r   r&   rV   r   r   r   test_issue_250e  s    zTestIssues.test_issue_250c             C   sp   ddl m} ddlm} | }|jddd td}|j|}| }|j|| t|j	  |j	 |kslt
d S )Nr   )r   )r)   r+   r@   )sequenceoffsetz        experiments:
          - datasets:
        # ATLAS EWK
              - {dataset: ATLASWZRAP36PB, frac: 1.0}
              - {dataset: ATLASZHIGHMASS49FB, frac: 1.0}
        )r   r   r,   r)   indentr
   r$   r_   rv   r-   r   )r   r   r)   r'   r&   r.   r/   r   r   r   test_issue_279q  s    
zTestIssues.test_issue_279c          
   C   sx   ddl m} ddlm} ddlm} ddlm} |dd}|d	d
}| }tj	|dd |j
d|i| W d Q R X d S )Nr   )r   )RepresenterError)
namedtuple)stdoutTr.   r~   r   r@   zcannot represent)rM   t)r.   r~   )r   r   Zsrsly.ruamel_yaml.representerr   collectionsr   sysr   rO   rP   r_   )r   r   r   r   r   r   r   r'   r   r   r   test_issue_280  s    

zTestIssues.test_issue_280c             C   sX   dd l }|jjjddg}|jdg |jdd	d
 d|j ksFtd|jksTtd S )Nr   r.   appler~   bananac
cantaloupedateZkiwi)rV   k)r.   r   )r~   r   )r   r   )r   r   commentsr}   updatekeysr   Z_ok)r   r   Z	yaml_datar   r   r   test_issue_282  s    
zTestIssues.test_issue_282c          	   C   s   dd l }td}|jjdd}d	|_|j|}|d  d ks>t|jjdd}d
|_tj|jj	j
dd |j|}W d Q R X d S )Nr   zf        plain key: in-line value
        : # Both empty
        "quoted key":
        - entry
        r   )ri   r   r@   zexpected <block end>)rM   )r   r@   )r   r   )r   r
   r   r   r   r$   r   rO   rP   rl   rm   )r   r   r&   r'   rV   r   r   r   test_issue_284  s    
zTestIssues.test_issue_284c             C   s\   ddl m} | }td}|j|}|d s0t|d s<t|d  sJt|d  sXtd S )Nr   )r   zV        %YAML 1.1
        ---
        - y
        - n
        - Y
        - N
        r@   r   rA   )r   r   r
   r$   r   )r   r   r'   r&   r.   r   r   r   test_issue_285  s    
zTestIssues.test_issue_285c             C   s`   ddl m} ddlm} | }td}|j|}d|d< | }|j|| |j jds\t	d S )Nr   )r   )r)   z?        parent_key:
        - sub_key: sub_value

        # xxx	new_valueZnew_keyzxxx
new_key: new_value
)
r   r   r,   r)   r
   r$   r_   r-   r   r   )r   r   r)   r'   r&   r.   r/   r   r   r   test_issue_286  s    
zTestIssues.test_issue_286c             C   s   dd l }ddlm} ddlm} td}| }|jdddd d|_d|_|j	|_
|j|}| }|j|| |j |ks~td S )	Nr   )r)   )r   aN          ---
        # Reusable values
        aliases:
          # First-element comment
          - &firstEntry First entry
          # Second-element comment
          - &secondEntry Second entry

          # Third-element comment is
          # a multi-line value
          - &thirdEntry Third entry

        # EOF Comment
        r@   r+   )mappingr   r   T)r   r,   r)   r   r   r
   r   explicit_startr   maxsizewidthr$   r_   r-   r   )r   r   r)   r   yamldocr'   r   r/   r   r   r   test_issue_288  s    
zTestIssues.test_issue_288c             C   s   dd l }ddlm} ddlm} td}| }|jdddd d|_d|_|j	|_
|j|}| }|j|| |j |ks~td S )	Nr   )r)   )r   aO          ---
        # Reusable values
        aliases:
          # First-element comment
          - &firstEntry First entry
          # Second-element comment
          - &secondEntry Second entry

          # Third-element comment is
           # a multi-line value
          - &thirdEntry Third entry

        # EOF Comment
        r@   r+   )r   r   r   T)r   r,   r)   r   r   r
   r   r   r   r   r   r$   r_   r-   r   )r   r   r)   r   r   r'   r   r/   r   r   r   test_issue_288a  s    
zTestIssues.test_issue_288ac             C   s   dd l }ddlm} ddlm} td}| }|jdddd d|_d|_|j	|_
|j|}| }|j|| |j |ks~td S )	Nr   )r)   )r   a          ---
        aliases:
          # Folded-element comment
          # for a multi-line value
          - &FoldedEntry >
            THIS IS A
            FOLDED, MULTI-LINE
            VALUE

          # Literal-element comment
          # for a multi-line value
          - &literalEntry |
            THIS IS A
            LITERAL, MULTI-LINE
            VALUE

          # Plain-element comment
          - &plainEntry Plain entry
        r@   r+   )r   r   r   T)r   r,   r)   r   r   r
   r   r   r   r   r   r$   r_   r-   r   )r   r   r)   r   r   r'   r   r/   r   r   r   test_issue_290  s    
zTestIssues.test_issue_290c             C   s   dd l }ddlm} ddlm} td}| }|jdddd d|_d|_|j	|_
|j|}| }|j|| |j |ks~td S )	Nr   )r)   )r   a          ---
        aliases:
          # Folded-element comment
          # for a multi-line value
          - &FoldedEntry >
            THIS IS A
            FOLDED, MULTI-LINE
            VALUE

          # Literal-element comment
          # for a multi-line value
          - &literalEntry |
            THIS IS A
            LITERAL, MULTI-LINE
            VALUE

          # Plain-element comment
          - &plainEntry Plain entry
        r@   r+   )r   r   r   T)r   r,   r)   r   r   r
   r   r   r   r   r   r$   r_   r-   r   )r   r   r)   r   r   r'   r   r/   r   r   r   test_issue_290aC  s    
zTestIssues.test_issue_290ac             C   s6   dd l }td}t|}|j|}t||ks2td S )Nr   z
        A:
          b:
          # comment
          - l1
          - l2

        C:
          d: e
          f:
          # comment2
          - - l31
            - l32
            - l33: '5'
        )copyr
   r   deepcopyr	   r   )r   r   r&   r   dcr   r   r   test_issue_295j  s    
zTestIssues.test_issue_295c             C   s$   ddl m} td}| j| d S )Nr   )r   zf
        %YAML 1.2
        %TAG ! tag:example.com,2019/path#fragment
        ---
        null
        )r   r   r
   r$   )r   r   r&   r   r   r   test_issue_300  s    zTestIssues.test_issue_300c          	   C   sD   dd l }td}t }tj|jjjdd |j| W d Q R X d S )Nr   zf
        %YAML 1.1
        %TAG ! tag:example.com,2019/path#fragment
        ---
        null
        zwhile scanning a directive)rM   )	r   r
   r   rO   rP   r   scannerZScannerErrorr$   )r   r   r&   r'   r   r   r   test_issue_300a  s    zTestIssues.test_issue_300ac             C   s   d}t |}d S )Nzj
        %YAML 1.2
        %TAG ! tag:example.com,2019:
        ---
        !foo null
        ...
        )r   )r   r&   rV   r   r   r   test_issue_304  s    zTestIssues.test_issue_304c             C   s   d}t |}d S )Nza
        %YAML 1.2
        ---
        !<tag:example.com,2019/path#foo> null
        ...
        )r   )r   r&   rV   r   r   r   test_issue_305  s    zTestIssues.test_issue_305c             C   s   d}t |}d S )Nzj
        %YAML 1.2
        %TAG ! tag:example.com,2019/path#
        ---
        null
        ...
        )r   )r   r&   rV   r   r   r   test_issue_307  s    zTestIssues.test_issue_307N)8__name__
__module____qualname__r   r   r    r!   r(   r1   r9   r;   r<   r>   r=   r?   rC   rG   rT   rW   rX   rY   rZ   r]   r`   rb   re   rf   rg   rj   rn   rq   rr   ry   rz   r{   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      sj   /<		 !!&'
	r   )
__future__r   r   r   rO   Z	roundtripr   r   r   r	   r
   r   r   r   r   r   r   r   <module>   s   $