Skip to content

Commit 1d2980d

Browse files
Add test for dict insertion order preservation
Add test_dict_preserves_insertion_order to verify that dictionary keys maintain their insertion order in assertion output, not alphabetical order. This test ensures the fix for issue #13503 works correctly and prevents future regressions. Related to #13503
1 parent b8c5717 commit 1d2980d

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

testing/io/test_pprint.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,3 +406,36 @@ class DataclassWithTwoItems:
406406
)
407407
def test_consistent_pretty_printer(data: Any, expected: str) -> None:
408408
assert PrettyPrinter().pformat(data) == textwrap.dedent(expected).strip()
409+
410+
411+
def test_dict_preserves_insertion_order() -> None:
412+
"""Test that dictionary keys maintain insertion order, not alphabetical.
413+
414+
Relates to issue #13503 - dicts should preserve insertion order
415+
since Python 3.7+, not sort alphabetically.
416+
"""
417+
# Create dict with non-alphabetical insertion order
418+
d = {}
419+
d['z'] = 1
420+
d['a'] = 2
421+
d['m'] = 3
422+
423+
result = PrettyPrinter().pformat(d)
424+
425+
# Verify the keys appear in insertion order (z, a, m)
426+
z_pos = result.index("'z'")
427+
a_pos = result.index("'a'")
428+
m_pos = result.index("'m'")
429+
430+
# z should appear before a, and a before m
431+
assert z_pos < a_pos < m_pos, f"Expected insertion order z<a<m, got positions: z={z_pos}, a={a_pos}, m={m_pos}"
432+
433+
# Also verify expected format
434+
expected = textwrap.dedent("""
435+
{
436+
'z': 1,
437+
'a': 2,
438+
'm': 3,
439+
}
440+
""").strip()
441+
assert result == expected

0 commit comments

Comments
 (0)