Skip to content

Pretty formating #10068

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
SimonSapin opened this issue Oct 25, 2013 · 5 comments
Closed

Pretty formating #10068

SimonSapin opened this issue Oct 25, 2013 · 5 comments

Comments

@SimonSapin
Copy link
Contributor

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

@klutzy
Copy link
Contributor

klutzy commented Oct 15, 2014

Reflection is going to removed. What could be done for this?

@sfackler
Copy link
Member

Seems reasonable to leave it open, as we'd presumably want the same thing for #[deriving(Show)] or whatever new thing is added for debug output.

@SimonSapin
Copy link
Contributor Author

This issue will soon be one year old. Meanwhile, assert_eq! does not use {:?} anymore but relies on the compared type implementing Show. So… what @sfackler said.

@tomjakubowski
Copy link
Contributor

Visiting after the fmt::Show/fmt::String split. Now that Show is exclusively for debugging output, uses {:?}, and should be derived and therefore have a predictable structure, a debugging Show pretty printer would be more useful than ever!

@SimonSapin
Copy link
Contributor Author

Fixed in #23162 (RFC #640). Use with {:#?}.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants