Skip to content

testing: example output whitespace trimming can be confusing #23542

Closed

Description

@josharian

Consider the Example for filepath.Split.

The code is:

fmt.Println(path.Split("static/myfile.css"))
fmt.Println(path.Split("myfile.css"))
fmt.Println(path.Split(""))

The output is:

static/ myfile.css
 myfile.css

Given that there are three fmt.Println calls, it seems to me that there really ought to be three lines of output.

(And if we decide that we don't want to or can't change example output whitespace trimming, we should at least tweak this example so that the last line of output contains non-whitespace, e.g. by prefixing each output line with an asterisk or number.)

Activity

  1. added
    NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.
    on Jan 24, 2018
  2. added this to the Go1.11 milestone on Jan 24, 2018
  3. mvdan commented on Jan 25, 2018

    @mvdan
    Member

    Perhaps another solution would be to not leave the empty line print at either end, so that it could not be trimmed. Leaving it in the middle, for example.

  4. added
    NeedsFixThe path to resolution is known, but the work has not been done.
    on Jan 25, 2018
  5. removed
    NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.
    on Jan 25, 2018
  6. added
    DocumentationIssues describing a change to documentation.
    NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.
    on Jan 25, 2018
  7. removed
    NeedsFixThe path to resolution is known, but the work has not been done.
    on Jan 25, 2018
  8. awoodbeck commented on Jan 29, 2018

    @awoodbeck
    Contributor

    I could take this one. I prefer mvdan's suggestion unless we'd like to be more explicit with the whitespace.

    Output would of course look like this:

    static/ myfile.css
     
     myfile.css
    

    Edit: Here's another proposal with explicit treatment of whitespace:

    The code:

    d, f := path.Split("static/myfile.css")
    fmt.Printf("path.Split(%q) = %q %q\n", "static/myfile.css", d, f)
    
    d, f = path.Split("myfile.css")
    fmt.Printf("path.Split(%q) = %q %q\n", "myfile.css", d, f)
    
    d, f = path.Split("")
    fmt.Printf("path.Split(%q) = %q %q\n", "", d, f)
    

    The output:

    path.Split("static/myfile.css") = "static/" "myfile.css"
    path.Split("myfile.css") = "" "myfile.css"
    path.Split("") = "" ""
    
  9. rsc commented on Jan 29, 2018

    @rsc
    Contributor

    It seems like the simplest fix is to swap the second and third line.

  10. added
    NeedsFixThe path to resolution is known, but the work has not been done.
    on Jan 29, 2018
  11. removed
    NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.
    on Jan 29, 2018
  12. awoodbeck commented on Jan 30, 2018

    @awoodbeck
    Contributor

    This simple change exposed a limitation in example output testing. The example works if the fmt.Println(path.Split("")) line is either the first or last line in the example, but not the middle line. Its single whitespace character and new line are stripped before comparison if it's on either end of the output, but not if it's in the middle (see the example output below).

    Adding a whitespace character in the example output doesn't help either because each line is trimmed before being joined with a new line character as best I can tell.

    --- FAIL: ExampleSplit (0.00s)                            
    got:                                                      
    "static/ myfile.css\n \n myfile.css"                      
    want:
    "static/ myfile.css\n\n myfile.css"
    
  13. josharian commented on Jan 30, 2018

    @josharian
    ContributorAuthor

    Possibly related: #6416

  14. 1 remaining item

  15. awoodbeck commented on Jan 30, 2018

    @awoodbeck
    Contributor

    Here's a proposed compromise:

    Code:

    fmt.Println(path.Split("static/myfile.css"))
    fmt.Println(path.Split("myfile.css"))
    fmt.Printf("%q", fmt.Sprintln(path.Split("")))
    

    Output:

    static/ myfile.css
    myfile.css
    " \n"
    

    This passes but it may convolute the last example.

  16. josharian commented on Feb 1, 2018

    @josharian
    ContributorAuthor

    I dunno. It'd be nice for all three to be constructed similarly. I tried this, but it's even worse:

    package main
    
    import (
    	"fmt"
    	"path"
    )
    
    func main() {
    	for _, s := range []string{"static/myfile.css", "myfile.css", ""} {
    		file, dir := path.Split(s)
    		fmt.Printf("path.Split(%q): file=%q dir=%q\n", s, file, dir)
    	}
    }

    I can't say I have any better ideas at this point.

  17. awoodbeck commented on Feb 5, 2018

    @awoodbeck
    Contributor

    Here's another suggestion that similarly constructs all 3 examples and passes.

    out := func(args ...interface{}) {
    	fmt.Printf("%q\n", fmt.Sprintln(args...))
    }
    
    out(path.Split("static/myfile.css"))
    out(path.Split("myfile.css"))
    out(path.Split(""))

    Output:

    "static/ myfile.css\n"
    " myfile.css\n"
    " \n"
    
  18. awoodbeck commented on Feb 12, 2018

    @awoodbeck
    Contributor

    Any opinion on my last example? I will submit it as a CL if it's received better than the current example.

  19. josharian commented on Feb 12, 2018

    @josharian
    ContributorAuthor

    On m phone, but: it is nice for examples to be copy-paste friendly. In particular, having a line that reads:

    file, sir := path.Split(x)

    with useful mnemonic variable names seems valuable. And then we could avoid nested fmt calls and a code golf flavor.

  20. irbekrm commented on Aug 22, 2019

    @irbekrm
    Contributor

    Hey folks, I just did a Go contributors' workshop at London GopherCon and was looking at this as potentially my first issue to try to contribute.
    Has there been any further decision about this issue?

  21. mvdan commented on Aug 22, 2019

    @mvdan
    Member

    @irbekrm I don't think there was a consensus so far; I'd say pick a solution you like (or your own solution), and we can always discuss it on the CL.

  22. irbekrm commented on Aug 22, 2019

    @irbekrm
    Contributor

    Thanks @mvdan

  23. gopherbot commented on Aug 22, 2019

    @gopherbot
    Contributor

    Change https://golang.org/cl/191397 mentions this issue: path: change the output format of ExampleSplit function

  24. added a commit that references this issue on Sep 5, 2019
    5b273f1
  25. locked and limited conversation to collaborators on Aug 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationIssues describing a change to documentation.FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.help wanted

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions