Go Testing using func Example, and spaces

118 views
Skip to first unread message

Tong Sun

unread,
Jun 29, 2015, 12:07:32 AM6/29/15
to golan...@googlegroups.com
From http://golang.org/pkg/testing/, it says

The (testing) package also runs and verifies example code. ... line comment that begins with "Output:" and is compared with the standard output of the function when the tests are run. (The comparison ignores leading and trailing space.) 

Does the space here include tabs? 

This is my Example actual output:

        flags.Bool("debug", false, "Turn on debugging.")
        viper.BindPFlag("debug", flags.Lookup("debug"))

I.e., it has a leading tab in each line. 
This is my Example output comment section:

// Output:
// flags.Bool("debug", false, "Turn on debugging.")
// viper.BindPFlag("debug", flags.Lookup("debug"))

They are exactly from the actual output, character by character. The only differences are the leading and trailing spaces. But this is my test result:

=== RUN: ExampleCommandLineCobraViper
--- FAIL: ExampleCommandLineCobraViper (0.00s)
got:
flags.Bool("debug", false, "Turn on debugging.")
        viper.BindPFlag("debug", flags.Lookup("debug"))
want:
flags.Bool("debug", false, "Turn on debugging.")
viper.BindPFlag("debug", flags.Lookup("debug"))
FAIL
exit status 1

- How to make it working?
- If my actual output contains more lines, but have blank lines in between them, should I include the blank lines in the output comment or not? 

        flags.Bool("debug", false, "Turn on debugging.")
        viper.BindPFlag("debug", flags.Lookup("debug"))

        flags.String("addr", "localhost:5002", "Address of the service.")
        viper.BindPFlag("addr", flags.Lookup("addr"))

Thanks

Andrew Gerrand

unread,
Jun 29, 2015, 12:12:53 AM6/29/15
to Tong Sun, golang-nuts
The testing infrastructure does take into account leading tabs (with the exception of the first; spaces are trimmed from the start and end of the actual and expected output before comparison).

Can you just not print the tabs? Or include them in the "Output:" comment?

Andrew

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tong Sun

unread,
Jun 29, 2015, 12:17:21 AM6/29/15
to Andrew Gerrand, golang-nuts
Wow, thanks for answering -- didn't expecting any answers at this time of the night. 

I've change it to space, but still fails

func ExampleTestExample() {
  fmt.Println(`  flags.Bool("debug", false, "Turn on debugging.")`)
  fmt.Println(`  viper.BindPFlag("debug", flags.Lookup("debug"))`)
  // Output:
  // flags.Bool("debug", false, "Turn on debugging.")
  // viper.BindPFlag("debug", flags.Lookup("debug"))
}

=== RUN: ExampleTestExample
--- FAIL: ExampleTestExample (0.00s)
got:
flags.Bool("debug", false, "Turn on debugging.")
  viper.BindPFlag("debug", flags.Lookup("debug"))
want:
flags.Bool("debug", false, "Turn on debugging.")
viper.BindPFlag("debug", flags.Lookup("debug"))
=== RUN: ExampleCommandLineCobraViper
...

Looks like a bug? 

Tong Sun

unread,
Jun 29, 2015, 12:25:28 AM6/29/15
to Andrew Gerrand, golang-nuts
On Mon, Jun 29, 2015 at 12:16 AM, Tong Sun <sunto...@gmail.com> wrote:
Wow, thanks for answering -- didn't expecting any answers at this time of the night. 

I've change it to space, but still fails

func ExampleTestExample() {
  fmt.Println(`  flags.Bool("debug", false, "Turn on debugging.")`)
  fmt.Println(`  viper.BindPFlag("debug", flags.Lookup("debug"))`)
  // Output:
  // flags.Bool("debug", false, "Turn on debugging.")
  // viper.BindPFlag("debug", flags.Lookup("debug"))
}


yeah, definitely Looks like a bug, because having add two spaces into the output comment section, it is working now. 
 
=== RUN: ExampleTestExample
--- FAIL: ExampleTestExample (0.00s)
got:
flags.Bool("debug", false, "Turn on debugging.")
  viper.BindPFlag("debug", flags.Lookup("debug"))
want:
flags.Bool("debug", false, "Turn on debugging.")
viper.BindPFlag("debug", flags.Lookup("debug"))
=== RUN: ExampleCommandLineCobraViper
...

And also, having put the tab in the output comment section as you suggested, the ExampleCommandLineCobraViper PASSed now:

=== RUN: ExampleTestExample
--- PASS: ExampleTestExample (0.00s)
=== RUN: ExampleCommandLineCobraViper
--- PASS: ExampleCommandLineCobraViper (0.00s)
PASS

Thanks!

Andrew Gerrand

unread,
Jun 29, 2015, 12:36:58 AM6/29/15
to Tong Sun, golang-nuts
On 29 June 2015 at 14:16, Tong Sun <sunto...@gmail.com> wrote:
Wow, thanks for answering -- didn't expecting any answers at this time of the night. 

It's always daytime somewhere in the world. :-)
 
I've change it to space, but still fails

func ExampleTestExample() {
  fmt.Println(`  flags.Bool("debug", false, "Turn on debugging.")`)
  fmt.Println(`  viper.BindPFlag("debug", flags.Lookup("debug"))`)
  // Output:
  // flags.Bool("debug", false, "Turn on debugging.")
  // viper.BindPFlag("debug", flags.Lookup("debug"))
}

=== RUN: ExampleTestExample
--- FAIL: ExampleTestExample (0.00s)
got:
flags.Bool("debug", false, "Turn on debugging.")
  viper.BindPFlag("debug", flags.Lookup("debug"))
want:
flags.Bool("debug", false, "Turn on debugging.")
viper.BindPFlag("debug", flags.Lookup("debug"))
=== RUN: ExampleCommandLineCobraViper
...

Looks like a bug? 

The first space on the output comment is stripped by the comment parsing.

So you really need a comment like
  //<space>Output:
  //<space><tab>TEXT
to match
  fmt.Println("\tTEXT")


But I think the better approach is to avoid using tabs in your output.

Andrew

Tong Sun

unread,
Jun 29, 2015, 12:45:08 AM6/29/15
to Andrew Gerrand, golang-nuts
On Mon, Jun 29, 2015 at 12:36 AM, Andrew Gerrand <a...@golang.org> wrote:
The first space on the output comment is stripped by the comment parsing.

Oh, that's what the docu means. 
 
So you really need a comment like
  //<space>Output:
  //<space><tab>TEXT
to match
  fmt.Println("\tTEXT")

I also found that this works as well:

//<tab>TEXT

without the <space> after "//", as posted in my previous email. 

I've just pushed my success test file here -- 

Good night

Reply all
Reply to author
Forward
0 new messages