|
| 1 | + |
| 2 | +import pytest |
| 3 | +import io |
| 4 | +import ruyaml # NOQA |
| 5 | +from .roundtrip import round_trip, dedent |
| 6 | + |
| 7 | +from ruyaml import YAML |
| 8 | +from ruyaml.util import load_yaml_guess_indent |
| 9 | +from ruyaml.scalarbool import ScalarBoolean |
| 10 | +from ruyaml.comments import CommentedSeq |
| 11 | +from ruyaml.representer import RoundTripRepresenter, ScalarNode |
| 12 | +from ruyaml.constructor import RoundTripConstructor |
| 13 | +from typing import Text, Any, Dict, List # NOQA |
| 14 | + |
| 15 | + |
| 16 | +class ScalarNodePositioned(ScalarNode): |
| 17 | + lc_position = None |
| 18 | + |
| 19 | + |
| 20 | +class ScalarBooleanStable(ScalarBoolean): |
| 21 | + |
| 22 | + def __new__(cls: Any, boolval, origrepr, anchor=None) -> Any: |
| 23 | + b = ScalarBoolean.__new__(cls, boolval, anchor=anchor) |
| 24 | + b.original_bool_repr = origrepr |
| 25 | + return b |
| 26 | + |
| 27 | + |
| 28 | +#def represent_list2(self, data): |
| 29 | +# dedede |
| 30 | +# RoundTripRepresenter.add_representer(CommentedSeq, represent_list2) |
| 31 | + |
| 32 | + |
| 33 | +class BetterRoundTripConstructor(RoundTripConstructor): |
| 34 | + keep_bool_repr = True |
| 35 | + |
| 36 | + |
| 37 | +class BetterRoundTripRepresenter(RoundTripRepresenter): |
| 38 | + def represent_bool(self, data, anchor=None): |
| 39 | + ret = self.represent_scalar('tag:yaml.org,2002:bool', data.original_bool_repr, anchor=anchor) |
| 40 | + return ret |
| 41 | + |
| 42 | + def TOTO_represent_sequence(self, tag, data): |
| 43 | + node = super().represent_sequence(tag, data) |
| 44 | + print(44, self, tag, data, node) |
| 45 | + # print(45, data.lc, data.lc.data) |
| 46 | + local_block_seq_ident = -1 |
| 47 | + |
| 48 | + |
| 49 | +#BetterRoundTripConstructor.add_constructor('tag:yaml.org,2002:bool', BetterRoundTripConstructor.construct_yaml_bool) |
| 50 | +#BetterRoundTripRepresenter.add_representer(ScalarBooleanStable, BetterRoundTripRepresenter.represent_bool) |
| 51 | + |
| 52 | + |
| 53 | +def round_trip_stabler( |
| 54 | + inp, |
| 55 | + outp=None, |
| 56 | +): |
| 57 | + if outp is None: |
| 58 | + outp = inp |
| 59 | + doutp = dedent(outp) |
| 60 | + yaml = ruyaml.YAML() |
| 61 | + yaml.preserve_quotes = True |
| 62 | + yaml.preserve_block_seqs_indents = True |
| 63 | + data = yaml.load(doutp) |
| 64 | + buf = io.StringIO() |
| 65 | + yaml.dump(data, stream=buf) |
| 66 | + res = buf.getvalue() |
| 67 | + assert res == doutp |
| 68 | + |
| 69 | + |
| 70 | +class TestStability: |
| 71 | + |
| 72 | + def test_blockseq1(self): |
| 73 | + round_trip( |
| 74 | + """ |
| 75 | + a: |
| 76 | + - a1 |
| 77 | + - a2 |
| 78 | + """ |
| 79 | + ) |
| 80 | + |
| 81 | + @pytest.mark.xfail(strict=True) |
| 82 | + def test_blockseq2(self): |
| 83 | + round_trip( |
| 84 | + """ |
| 85 | + a: |
| 86 | + - a1 |
| 87 | + - a2 |
| 88 | + """ |
| 89 | + ) |
| 90 | + |
| 91 | + @pytest.mark.xfail(strict=True) |
| 92 | + def test_blockseq3(self): |
| 93 | + round_trip( |
| 94 | + """ |
| 95 | + a: |
| 96 | + - a1 |
| 97 | + - a2 |
| 98 | + b: |
| 99 | + - b1 |
| 100 | + - b2 |
| 101 | + """ |
| 102 | + ) |
| 103 | + |
| 104 | +class TestStabilityStabler: |
| 105 | + |
| 106 | + def test_blockseq1(self): |
| 107 | + round_trip_stabler( |
| 108 | + """ |
| 109 | + a: |
| 110 | + - a1 |
| 111 | + - a2 |
| 112 | + """ |
| 113 | + ) |
| 114 | + |
| 115 | + def test_blockseq2(self): |
| 116 | + round_trip_stabler( |
| 117 | + """ |
| 118 | + a: |
| 119 | + - a1 |
| 120 | + - a2 |
| 121 | + """ |
| 122 | + ) |
| 123 | + |
| 124 | + def test_blockseq3(self): |
| 125 | + round_trip_stabler( |
| 126 | + """ |
| 127 | + a: |
| 128 | + - a1 |
| 129 | + - a2 |
| 130 | + b: |
| 131 | + - b1 |
| 132 | + - b2 |
| 133 | + """ |
| 134 | + ) |
0 commit comments