diff options
Diffstat (limited to '_test/test_comments.py')
-rw-r--r-- | _test/test_comments.py | 401 |
1 files changed, 269 insertions, 132 deletions
diff --git a/_test/test_comments.py b/_test/test_comments.py index 921b193..7a43a97 100644 --- a/_test/test_comments.py +++ b/_test/test_comments.py @@ -27,15 +27,18 @@ class TestComments: round_trip(x, extra='a\n') def test_no_comments(self): - round_trip(""" + round_trip( + """ - europe: 10 - usa: - ohio: 2 - california: 9 - """) + """ + ) def test_round_trip_ordering(self): - round_trip(""" + round_trip( + """ a: 1 b: 2 c: 3 @@ -44,25 +47,32 @@ class TestComments: d: 4 e: 5 f: 6 - """) + """ + ) def test_complex(self): - round_trip(""" + round_trip( + """ - europe: 10 # top - usa: - ohio: 2 - california: 9 # o - """) + """ + ) def test_dropped(self): - round_trip(""" + round_trip( + """ # comment scalar ... - """, "scalar\n...\n") + """, + 'scalar\n...\n', + ) def test_main_mapping_begin_end(self): - round_trip(""" + round_trip( + """ # C start a # C start b abc: 1 @@ -70,7 +80,8 @@ class TestComments: klm: 3 # C end a # C end b - """) + """ + ) def test_reindent(self): x = """\ @@ -80,14 +91,17 @@ class TestComments: """ d = round_trip_load(x) y = round_trip_dump(d, indent=4) - assert y == dedent("""\ + assert y == dedent( + """\ a: b: # comment 1 c: 1 # comment 2 - """) + """ + ) def test_main_mapping_begin_end_items_post(self): - round_trip(""" + round_trip( + """ # C start a # C start b abc: 1 # abc comment @@ -95,10 +109,12 @@ class TestComments: klm: 3 # klm comment # C end a # C end b - """) + """ + ) def test_main_sequence_begin_end(self): - round_trip(""" + round_trip( + """ # C start a # C start b - abc @@ -106,10 +122,12 @@ class TestComments: - klm # C end a # C end b - """) + """ + ) def test_main_sequence_begin_end_items_post(self): - round_trip(""" + round_trip( + """ # C start a # C start b - abc # abc comment @@ -117,10 +135,12 @@ class TestComments: - klm # klm comment # C end a # C end b - """) + """ + ) def test_main_mapping_begin_end_complex(self): - round_trip(""" + round_trip( + """ # C start a # C start b abc: 1 @@ -130,10 +150,12 @@ class TestComments: 3b: beta # it is all greek to me # C end a # C end b - """) + """ + ) - def test_09(self): # 2.9 from the examples in the spec - round_trip(""" + def test_09(self): # 2.9 from the examples in the spec + round_trip( + """ hr: # 1998 hr ranking - Mark McGwire - Sammy Sosa @@ -141,10 +163,14 @@ class TestComments: # 1998 rbi ranking - Sammy Sosa - Ken Griffey - """, indent=4, block_seq_indent=2) + """, + indent=4, + block_seq_indent=2, + ) def test_09a(self): - round_trip(""" + round_trip( + """ hr: # 1998 hr ranking - Mark McGwire - Sammy Sosa @@ -152,36 +178,44 @@ class TestComments: # 1998 rbi ranking - Sammy Sosa - Ken Griffey - """) + """ + ) def test_simple_map_middle_comment(self): - round_trip(""" + round_trip( + """ abc: 1 # C 3a # C 3b ghi: 2 - """) + """ + ) def test_map_in_map_0(self): - round_trip(""" + round_trip( + """ map1: # comment 1 # comment 2 map2: key1: val1 - """) + """ + ) def test_map_in_map_1(self): # comment is moved from value to key - round_trip(""" + round_trip( + """ map1: # comment 1 map2: key1: val1 - """) + """ + ) def test_application_arguments(self): # application configur - round_trip(""" + round_trip( + """ args: username: anthon passwd: secret @@ -190,7 +224,8 @@ class TestComments: session-name: test loop: wait: 10 - """) + """ + ) def test_substitute(self): x = """ @@ -210,7 +245,8 @@ class TestComments: assert round_trip_dump(data) == dedent(x) def test_set_comment(self): - round_trip(""" + round_trip( + """ !!set # the beginning ? a @@ -218,29 +254,35 @@ class TestComments: ? b # You see? Promised you. ? c # this is the end - """) + """ + ) def test_omap_comment_roundtrip(self): - round_trip(""" + round_trip( + """ !!omap - a: 1 - b: 2 # two - c: 3 # three - d: 4 - """) + """ + ) def test_omap_comment_roundtrip_pre_comment(self): - round_trip(""" + round_trip( + """ !!omap - a: 1 - b: 2 # two - c: 3 # three # last one - d: 4 - """) + """ + ) def test_non_ascii(self): - round_trip(""" + round_trip( + """ verbosity: 1 # 0 is minimal output, -1 none base_url: http://gopher.net special_indices: [1, 5, 8] @@ -259,34 +301,43 @@ class TestComments: <<: *asia_europe Spain: Madrid Italy: Rome - """) + """ + ) def test_dump_utf8(self): import ruamel.yaml # NOQA - x = dedent("""\ + + x = dedent( + """\ ab: - x # comment - y # more comment - """) + """ + ) data = round_trip_load(x) dumper = ruamel.yaml.RoundTripDumper for utf in [True, False]: - y = ruamel.yaml.dump(data, default_flow_style=False, Dumper=dumper, - allow_unicode=utf) + y = ruamel.yaml.dump( + data, default_flow_style=False, Dumper=dumper, allow_unicode=utf + ) assert y == x def test_dump_unicode_utf8(self): import ruamel.yaml # NOQA - x = dedent(u"""\ + + x = dedent( + u"""\ ab: - x # comment - y # more comment - """) + """ + ) data = round_trip_load(x) dumper = ruamel.yaml.RoundTripDumper for utf in [True, False]: - y = ruamel.yaml.dump(data, default_flow_style=False, Dumper=dumper, - allow_unicode=utf) + y = ruamel.yaml.dump( + data, default_flow_style=False, Dumper=dumper, allow_unicode=utf + ) assert y == x def test_mlget_00(self): @@ -327,7 +378,8 @@ class TestInsertPopList: d = round_trip_load(self.ins) d['ab'].insert(0, 'xyz') y = round_trip_dump(d, indent=2) - assert y == dedent("""\ + assert y == dedent( + """\ ab: - xyz - a # a @@ -338,13 +390,15 @@ class TestInsertPopList: de: - 1 - 2 - """) + """ + ) def test_insert_1(self): d = round_trip_load(self.ins) d['ab'].insert(4, 'xyz') y = round_trip_dump(d, indent=2) - assert y == dedent("""\ + assert y == dedent( + """\ ab: - a # a - b # b @@ -355,13 +409,15 @@ class TestInsertPopList: de: - 1 - 2 - """) + """ + ) def test_insert_2(self): d = round_trip_load(self.ins) d['ab'].insert(1, 'xyz') y = round_trip_dump(d, indent=2) - assert y == dedent("""\ + assert y == dedent( + """\ ab: - a # a - xyz @@ -372,14 +428,16 @@ class TestInsertPopList: de: - 1 - 2 - """) + """ + ) def test_pop_0(self): d = round_trip_load(self.ins) d['ab'].pop(0) y = round_trip_dump(d, indent=2) print(y) - assert y == dedent("""\ + assert y == dedent( + """\ ab: - b # b - c @@ -388,14 +446,16 @@ class TestInsertPopList: de: - 1 - 2 - """) + """ + ) def test_pop_1(self): d = round_trip_load(self.ins) d['ab'].pop(1) y = round_trip_dump(d, indent=2) print(y) - assert y == dedent("""\ + assert y == dedent( + """\ ab: - a # a - c @@ -404,14 +464,16 @@ class TestInsertPopList: de: - 1 - 2 - """) + """ + ) def test_pop_2(self): d = round_trip_load(self.ins) d['ab'].pop(2) y = round_trip_dump(d, indent=2) print(y) - assert y == dedent("""\ + assert y == dedent( + """\ ab: - a # a - b # b @@ -420,14 +482,16 @@ class TestInsertPopList: de: - 1 - 2 - """) + """ + ) def test_pop_3(self): d = round_trip_load(self.ins) d['ab'].pop(3) y = round_trip_dump(d, indent=2) print(y) - assert y == dedent("""\ + assert y == dedent( + """\ ab: - a # a - b # b @@ -435,7 +499,8 @@ class TestInsertPopList: de: - 1 - 2 - """) + """ + ) # inspired by demux' question on stackoverflow @@ -451,45 +516,52 @@ class TestInsertInMapping: def test_insert_at_pos_1(self): d = round_trip_load(self.ins) - d.insert(1, 'last name', 'Vandelay', comment="new key") + d.insert(1, 'last name', 'Vandelay', comment='new key') y = round_trip_dump(d) print(y) - assert y == dedent("""\ + assert y == dedent( + """\ first_name: Art last name: Vandelay # new key occupation: Architect # This is an occupation comment about: Art Vandelay is a fictional character that George invents... - """) + """ + ) def test_insert_at_pos_0(self): d = round_trip_load(self.ins) - d.insert(0, 'last name', 'Vandelay', comment="new key") + d.insert(0, 'last name', 'Vandelay', comment='new key') y = round_trip_dump(d) print(y) - assert y == dedent("""\ + assert y == dedent( + """\ last name: Vandelay # new key first_name: Art occupation: Architect # This is an occupation comment about: Art Vandelay is a fictional character that George invents... - """) + """ + ) def test_insert_at_pos_3(self): # much more simple if done with appending. d = round_trip_load(self.ins) - d.insert(3, 'last name', 'Vandelay', comment="new key") + d.insert(3, 'last name', 'Vandelay', comment='new key') y = round_trip_dump(d) print(y) - assert y == dedent("""\ + assert y == dedent( + """\ first_name: Art occupation: Architect # This is an occupation comment about: Art Vandelay is a fictional character that George invents... last name: Vandelay # new key - """) + """ + ) class TestCommentedMapMerge: def test_in_operator(self): - data = round_trip_load(""" + data = round_trip_load( + """ x: &base a: 1 b: 2 @@ -498,31 +570,36 @@ class TestCommentedMapMerge: <<: *base k: 4 l: 5 - """) + """ + ) assert data['x']['a'] == 1 assert 'a' in data['x'] assert data['y']['a'] == 1 assert 'a' in data['y'] def test_issue_60(self): - data = round_trip_load(""" + data = round_trip_load( + """ x: &base a: 1 y: <<: *base - """) + """ + ) assert data['x']['a'] == 1 assert data['y']['a'] == 1 assert str(data['y']) == """ordereddict([('a', 1)])""" def test_issue_60_1(self): - data = round_trip_load(""" + data = round_trip_load( + """ x: &base a: 1 y: <<: *base b: 2 - """) + """ + ) assert data['x']['a'] == 1 assert data['y']['a'] == 1 assert str(data['y']) == """ordereddict([('b', 2), ('a', 1)])""" @@ -531,7 +608,8 @@ class TestCommentedMapMerge: class TestEmptyLines: # prompted by issue 46 from Alex Harvey def test_issue_46(self): - yaml_str = dedent("""\ + yaml_str = dedent( + """\ --- # Please add key/value pairs in alphabetical order @@ -540,13 +618,15 @@ class TestEmptyLines: jenkins_ad_credentials: bind_name: 'CN=svc-AAA-BBB-T,OU=Example,DC=COM,DC=EXAMPLE,DC=Local' bind_pass: 'xxxxyyyy{' - """) + """ + ) d = round_trip_load(yaml_str, preserve_quotes=True) y = round_trip_dump(d, explicit_start=True) assert yaml_str == y def test_multispace_map(self): - round_trip(""" + round_trip( + """ a: 1x b: 2x @@ -558,11 +638,13 @@ class TestEmptyLines: d: 4x - """) + """ + ) @pytest.mark.xfail(strict=True) def test_multispace_map_initial(self): - round_trip(""" + round_trip( + """ a: 1x @@ -575,27 +657,33 @@ class TestEmptyLines: d: 4x - """) + """ + ) def test_embedded_map(self): - round_trip(""" + round_trip( + """ - a: 1y b: 2y c: 3y - """) + """ + ) def test_toplevel_seq(self): - round_trip("""\ + round_trip( + """\ - 1 - 2 - 3 - """) + """ + ) def test_embedded_seq(self): - round_trip(""" + round_trip( + """ a: b: - 1 @@ -604,7 +692,8 @@ class TestEmptyLines: - 3 - """) + """ + ) def test_line_with_only_spaces(self): # issue 54 @@ -629,12 +718,14 @@ class TestEmptyLines: assert stripped == y def test_issue_54_not_ok(self): - yaml_str = dedent("""\ + yaml_str = dedent( + """\ toplevel: # some comment sublevel: 300 - """) + """ + ) d = round_trip_load(yaml_str) print(d.ca) y = round_trip_dump(d, indent=4) @@ -642,50 +733,61 @@ class TestEmptyLines: assert yaml_str == y def test_issue_54_ok(self): - yaml_str = dedent("""\ + yaml_str = dedent( + """\ toplevel: # some comment sublevel: 300 - """) + """ + ) d = round_trip_load(yaml_str) y = round_trip_dump(d, indent=4) assert yaml_str == y def test_issue_93(self): - round_trip("""\ + round_trip( + """\ a: b: - c1: cat # a1 # my comment on catfish - c2: catfish # a2 - """) + """ + ) def test_issue_93_00(self): - round_trip("""\ + round_trip( + """\ a: - - c1: cat # a1 # my comment on catfish - c2: catfish # a2 - """) + """ + ) def test_issue_93_01(self): - round_trip("""\ + round_trip( + """\ - - c1: cat # a1 # my comment on catfish - c2: catfish # a2 - """) + """ + ) def test_issue_93_02(self): # never failed as there is no indent - round_trip("""\ + round_trip( + """\ - c1: cat # my comment on catfish - c2: catfish - """) + """ + ) def test_issue_96(self): # inserted extra line on trailing spaces - round_trip("""\ + round_trip( + """\ a: b: c: c_val @@ -693,14 +795,15 @@ class TestEmptyLines: e: g: g_val - """) + """ + ) class TestUnicodeComments: - - @pytest.mark.skipif(sys.version_info < (2, 7), reason="wide unicode") + @pytest.mark.skipif(sys.version_info < (2, 7), reason='wide unicode') def test_issue_55(self): # reported by Haraguroicha Hsu - round_trip("""\ + round_trip( + """\ name: TEST description: test using author: Harguroicha @@ -715,87 +818,111 @@ class TestUnicodeComments: - :no: 05338777 # 〇〇啓 - :no: 05273867 # 〇 - :no: 05205786 # 〇𤦌 - """) + """ + ) class TestEmptyValueBeforeComments: def test_issue_25a(self): - round_trip("""\ + round_trip( + """\ - a: b c: d d: # foo - e: f - """) + """ + ) def test_issue_25a1(self): - round_trip("""\ + round_trip( + """\ - a: b c: d d: # foo e: f - """) + """ + ) def test_issue_25b(self): - round_trip("""\ + round_trip( + """\ var1: #empty var2: something #notempty - """) + """ + ) def test_issue_25c(self): - round_trip("""\ + round_trip( + """\ params: a: 1 # comment a b: # comment b c: 3 # comment c - """) + """ + ) def test_issue_25c1(self): - round_trip("""\ + round_trip( + """\ params: a: 1 # comment a b: # comment b # extra c: 3 # comment c - """) + """ + ) def test_issue_25_00(self): - round_trip("""\ + round_trip( + """\ params: a: 1 # comment a b: # comment b - """) + """ + ) def test_issue_25_01(self): - round_trip("""\ + round_trip( + """\ a: # comment 1 # comment 2 - b: # comment 3 c: 1 # comment 4 - """) + """ + ) def test_issue_25_02(self): - round_trip("""\ + round_trip( + """\ a: # comment 1 # comment 2 - b: 2 # comment 3 - """) + """ + ) def test_issue_25_03(self): - round_trip("""\ + round_trip( + """\ a: # comment 1 # comment 2 - b: 2 # comment 3 - """, indent=4, block_seq_indent=2) + """, + indent=4, + block_seq_indent=2, + ) def test_issue_25_04(self): - round_trip("""\ + round_trip( + """\ a: # comment 1 # comment 2 b: 1 # comment 3 - """) + """ + ) def test_flow_seq_within_seq(self): - round_trip("""\ + round_trip( + """\ # comment 1 - a - b @@ -807,7 +934,8 @@ class TestEmptyValueBeforeComments: - f # comment 4 - [] - """) + """ + ) test_block_scalar_commented_line_template = """\ @@ -824,8 +952,17 @@ class TestBlockScalarWithComments: # issue 99 reported by Colm O'Connor def test_scalar_with_comments(self): import ruamel.yaml # NOQA - for x in ['', '\n', '\n# Another comment\n', '\n\n', '\n\n# abc\n#xyz\n', - '\n\n# abc\n#xyz\n', '# abc\n\n#xyz\n', '\n\n # abc\n #xyz\n']: + + for x in [ + "", + '\n', + '\n# Another comment\n', + '\n\n', + '\n\n# abc\n#xyz\n', + '\n\n# abc\n#xyz\n', + '# abc\n\n#xyz\n', + '\n\n # abc\n #xyz\n', + ]: commented_line = test_block_scalar_commented_line_template.format(x) data = ruamel.yaml.round_trip_load(commented_line) |