franela / goblin

Minimal and Beautiful Go testing framework
MIT License
883 stars 79 forks source link

losing nested structs in some weird situations #33

Closed mattetti closed 10 years ago

mattetti commented 10 years ago

I don't have a reproduction for this bug yet, but here is a quick explanation that might make sense:

I have a nested multi describe test, in the last test of the tree, I do a for loop.

type expectation struct {
 Id int
 List []string
}

// construct an array of expectations

for i, exp := range expectations {
   g.It("each thing must be correct", func() {
    thing := things[i]
    t.Log(len(thing.List)) // always prints 0
    t.Log(len(exp.List)) // always print 0
  })
}

I'm very confused why as to why even tho I construct my array with filled up lists, I never get the content inside the scope of the test. However if I move the test function like that:

g.It("each thing must be correct", func() {
  for i, exp := range expectations {
    thing := things[i]
    t.Log(len(thing.List)) // prints the right value
    t.Log(len(exp.List)) // prints the right value
  }
})

I'm sure this is something dumb I should know about golang, but that's a very surprising behavior. Any idea what's going on?

marcosnils commented 10 years ago

Matt,

The reason why this is happening is because Goblin executes everything after all the tests are declared. This means that in your first example, the value of the variable "i" in every test declared inside the loop will be the same.

In order to get your example to work you must do something like this:

for i, exp := range expectations {
    thing := things[i]
    g.It("each thing must be correct", func() {
    t.Log(len(thing.List)) // always prints 0
    t.Log(len(exp.List)) // always print 0
  })
}

Let me know if that works for you.

mattetti commented 10 years ago

Sorry for the lack of feedback,

Quick question, can I do something like this (obviously the following code would fail:

for i, exp := range expectations {
   g.It("each thing must be correct", func(thing) {
    t.Log(len(thing.List))
    t.Log(len(exp.List))
  }(things[i]))
}

Basically, I'd like to send arguments to my expectation. This isn't required and I understand that the problem I encountered was a scoping bug in my code and I should have found it myself, but I'm curious to what extent, nested functions can cause more scoping issues.

marcosnils commented 10 years ago

@mattetti

Sorry for taking too long to respond. I get what you're trying to do, and I don't think we need to change goblin in order to accomplish that.

Let me know if this works for you:

g.Describe("test", func() {
  for i := 0; i < 5; i++ {
    (func(i int) {
        g.It("Is pending "+strconv.Itoa(i), func() {
            fmt.Println(i)
        })
     }(i))
  }
})

Marcos.

mattetti commented 10 years ago

Oh I see, it delays the running of the "It" function since it's nested the anonymous function. Smart!

On Mon, Dec 2, 2013 at 5:48 PM, Marcos Nils notifications@github.comwrote:

@mattetti https://github.com/mattetti

Sorry for taking too long to respond. I get what you're trying to do, and I don't think we need to change goblin in order to accomplish that.

Let me know if this works for you:

g.Describe("test", func() { for i := 0; i < 5; i++ { (func(i int) { g.It("Is pending "+strconv.Itoa(i), func() { fmt.Println(i) }) }(i)) }})

Marcos.

— Reply to this email directly or view it on GitHubhttps://github.com/franela/goblin/issues/33#issuecomment-29676801 .