Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.Sign up
testing: leading spaces in Example output are dropped from godoc display #4487
If you write an example that generates a right-leaning triangle: // Output: // * // ** // *** Then it tests okay (I think the leading space is dropped from the target and the actual output) but it formats in the web godoc as: * ** *** instead of * ** *** That is, godoc eats the leading spaces. More generally I wonder if the comment processing in examples should be handed off to the standard go/doc comment processing and formatting. It already handles stripping an *equal* amount of indent from pre blocks.
This behavior is inherent to the Example parsing in go/doc -- it calls strings.TrimSpace on Output. This seems wrong to me not only for the purposes of displaying the Example but for testing as well. One might well construct a whitespace-sensitive example that doesn't fail as expected; for instance, the following actual outputs are accepted by the right-leaning triangle example output: * ** *** * ** *** The testing documentation isn't explicit about the behavior here. What if the output parsing were changed? Right now, doc.Examples removes all leading/trailing whitespace; what if instead it only removed /^[ ]*\n?/ ? I think that's a more intuitive behavior that works for both the single-line (e.g., '// Output: hi') and multi-line output targets. The trimming of the actual output would have to be adjusted correspondingly. I'm happy to make these changes but wanted some feedback first.