[go-nuts] Re: go 1.9: New Helper() method in testing package does not work with testing.TB interface

2017-08-25 Thread lukas.malkmus via golang-nuts
I'm sorry, I think I made a huge mistake. The malicious output only occurs 
when running go test with the -race flag.

Am Freitag, 25. August 2017 22:35:21 UTC+2 schrieb lukas@googlemail.com:
>
> I'm using helper functions in my test, for example this one:
>
> // assert fails the test if the condition is false.
> func assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
>  if !condition {
>  _, file, line, _ := runtime.Caller(1)
>  fmt.Printf("\033[31m%s:%d: "+msg+"\033[39m\n\n", append([]interface{}{
> filepath.Base(file), line}, v...)...)
>  tb.FailNow()
>  }
> }
>
> I hoped that I could improve these helpers with the new (*B).Helper() and 
> (*T).Helper() methods:
>
> // assert fails the test if the condition is false.
> func assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
> tb.Helper()
> if !condition {
> tb.Fatalf("\033[31m "+msg+"\033[39m\n\n", v...)
> }
> }
>
> I expected Go to print the file and line number of the failing test, 
> followed by a red message. Instead, the file and line number is of form "
> :1:"
>
> It works if I replace tb testing.TB with t testing.T. Is there a reason 
> the new Helper() method is part of the TB interface but not working as 
> expected? Or could it be a bug?
>
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: go 1.9: New Helper() method in testing package does not work with testing.TB interface

2017-08-25 Thread lukas.malkmus via golang-nuts
package bug

// Pow returns x raised to the power of y i.e. x^y.
func Pow(x, y int) int {
return x ^ y
}

package bug_test

import (
"fmt"
"testing"

"github.com/lukasmalkmus/bug"
)

func TestPow(t *testing.T) {
tests := []struct {
x, y, z int
}{
{0, 0, 0},
{1, 0, 1},
{0, 1, 11},
}

for _, tt := range tests {
t.Run("", func(t *testing.T) {
z := bug.Pow(tt.x, tt.y)
assert(t, tt.z == z, fmt.Sprintf("exp: %d, got: %d", tt.z, z))
})
}
}

// assert fails the test if the condition is false.
func assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
tb.Helper()
if !condition {
tb.Fatalf("\033[31m "+msg+"\033[39m\n\n", v...)
}
}

This will error on test case 3.

I expected this output:

--- FAIL: TestPow (0.00s)

--- FAIL: TestPow/#02 (0.00s)

bug_test.go:22:  exp: 11, got: 1

 

FAIL

coverage: 100.0% of statements

exit status 1

FAIL github.com/lukasmalkmus/bug 0.008s

I got this:

--- FAIL: TestPow (0.00s)

--- FAIL: TestPow/#02 (0.00s)

:1:  exp: 11, got: 1



FAIL

coverage: 100.0% of statements

FAILgithub.com/lukasmalkmus/bug 0.015s



Am Freitag, 25. August 2017 22:35:21 UTC+2 schrieb lukas@googlemail.com:
>
> I'm using helper functions in my test, for example this one:
>
> // assert fails the test if the condition is false.
> func assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
>  if !condition {
>  _, file, line, _ := runtime.Caller(1)
>  fmt.Printf("\033[31m%s:%d: "+msg+"\033[39m\n\n", append([]interface{}{
> filepath.Base(file), line}, v...)...)
>  tb.FailNow()
>  }
> }
>
> I hoped that I could improve these helpers with the new (*B).Helper() and 
> (*T).Helper() methods:
>
> // assert fails the test if the condition is false.
> func assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
> tb.Helper()
> if !condition {
> tb.Fatalf("\033[31m "+msg+"\033[39m\n\n", v...)
> }
> }
>
> I expected Go to print the file and line number of the failing test, 
> followed by a red message. Instead, the file and line number is of form "
> :1:"
>
> It works if I replace tb testing.TB with t testing.T. Is there a reason 
> the new Helper() method is part of the TB interface but not working as 
> expected? Or could it be a bug?
>
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: go 1.9: New Helper() method in testing package does not work with testing.TB interface

2017-08-25 Thread lukas.malkmus via golang-nuts
It is part of the interface...

Am Freitag, 25. August 2017 22:35:21 UTC+2 schrieb lukas@googlemail.com:
>
> I'm using helper functions in my test, for example this one:
>
> // assert fails the test if the condition is false.
> func assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
>  if !condition {
>  _, file, line, _ := runtime.Caller(1)
>  fmt.Printf("\033[31m%s:%d: "+msg+"\033[39m\n\n", append([]interface{}{
> filepath.Base(file), line}, v...)...)
>  tb.FailNow()
>  }
> }
>
> I hoped that I could improve these helpers with the new (*B).Helper() and 
> (*T).Helper() methods:
>
> // assert fails the test if the condition is false.
> func assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
> tb.Helper()
> if !condition {
> tb.Fatalf("\033[31m "+msg+"\033[39m\n\n", v...)
> }
> }
>
> I expected Go to print the file and line number of the failing test, 
> followed by a red message. Instead, the file and line number is of form "
> :1:"
>
> It works if I replace tb testing.TB with t testing.T. Is there a reason 
> the new Helper() method is part of the TB interface but not working as 
> expected? Or could it be a bug?
>
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] go 1.9: New Helper() method in testing package does not work with testing.TB interface

2017-08-25 Thread lukas.malkmus via golang-nuts
I'm using helper functions in my test, for example this one:

// assert fails the test if the condition is false.
func assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
 if !condition {
 _, file, line, _ := runtime.Caller(1)
 fmt.Printf("\033[31m%s:%d: "+msg+"\033[39m\n\n", append([]interface{}{
filepath.Base(file), line}, v...)...)
 tb.FailNow()
 }
}

I hoped that I could improve these helpers with the new (*B).Helper() and 
(*T).Helper() methods:

// assert fails the test if the condition is false.
func assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
tb.Helper()
if !condition {
tb.Fatalf("\033[31m "+msg+"\033[39m\n\n", v...)
}
}

I expected Go to print the file and line number of the failing test, 
followed by a red message. Instead, the file and line number is of form "
:1:"

It works if I replace tb testing.TB with t testing.T. Is there a reason the 
new Helper() method is part of the TB interface but not working as 
expected? Or could it be a bug?

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.