Skip to content

Pretty formating #10068

Closed
Closed
@SimonSapin

Description

It would be helpful for debugging to have a string formatting "style" that is for "{:?}" what extra::json::Json::to_pretty_str is to extra::json::Json::to_str. That is, it does the same (uses introspection to dump the content of various data structures) but adds newlines and indentation as appropriate for readability.

This pretty formatting would be used by assert_eq!().

For example, I am writing tests like this for Selector parsing in Servo:

    assert_eq!(parse("e.foo #bar"), Some(~[Selector{
        compound_selectors: CompoundSelector {
            simple_selectors: ~[LocalNameSelector(~"e"),
                                ClassSelector(~"foo")],
            next: Some((~CompoundSelector {
                simple_selectors: ~[IDSelector(~"baz")],
                next: None,
            }, Descendant)),
        },
        pseudo_element: None,
        specificity: specificity(1, 1, 1),
    }]))

At the moment, a failing test gives this output:

task '<unnamed>' failed at 'assertion failed: `(left == right) && (right ==
 left)` (left: `Some(~[selectors::Selector{compound_selectors: selectors::C
ompoundSelector{simple_selectors: ~[IDSelector(~"bar")], next: Some((~selec
tors::CompoundSelector{simple_selectors: ~[LocalNameSelector(~"e"), ClassSe
lector(~"foo")], next: None}, Descendant))}, pseudo_element: None, specific
ity: 1049601u32}])`, right: `Some(~[selectors::Selector{compound_selectors:
 selectors::CompoundSelector{simple_selectors: ~[LocalNameSelector(~"e"), C
lassSelector(~"foo")], next: Some((~selectors::CompoundSelector{simple_sele
ctors: ~[IDSelector(~"bar")], next: None}, Descendant))}, pseudo_element: N
one, specificity: 1049601u32}])`)', /home/simon/projects/servo/src/componen
ts/style/selectors.rs:550

Not exactly easy to read. Ideally, the output should be closer to this:

task '<unnamed>' failed at 'assertion failed: `(left == right) && (right == left)`
left: `Some(~[
    selectors::Selector{
        compound_selectors: selectors::CompoundSelector{
            simple_selectors: ~[
                IDSelector(~"bar")
            ],
            next: Some((
                ~selectors::CompoundSelector{
                    simple_selectors: ~[
                        LocalNameSelector(~"e"),
                        ClassSelector(~"foo")
                    ],
                    next: None
                },
                Descendant
            ))
        },
        pseudo_element: None,
        specificity: 1049601u32
    }
])`,
right: `Some(~[
    selectors::Selector{
        compound_selectors: selectors::CompoundSelector{
            simple_selectors: ~[
                LocalNameSelector(~"e"),
                ClassSelector(~"foo")
            ],
            next: Some((
                ~selectors::CompoundSelector{
                    simple_selectors: ~[
                        IDSelector(~"bar")
                    ],
                    next: None
                },
                Descendant
            ))
        },
        pseudo_element: None,
        specificity: 1049601u32
    }
])`'
/home/simon/projects/servo/src/components/style/selectors.rs:550

See also Python’s pprint module: http://docs.python.org/3/library/pprint.html
http://hg.python.org/cpython/file/3.3/Lib/pprint.py

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions