0

So from what I've read, you can't test if a function is equal in Go, but I'm trying to solve a test-case issue, so any help in refactoring this would be helpful.

I have a constructor and I'm passing it some configuration values. Based on those configs, it assigns another constructor function to a member of the struct. Later, in a different method, it calls that new constructor. I did it this way because it made it easier to test the methods on the struct, since I could now create a test constructor and reassign the struct member to it, before calling the methods I was testing. Similar to the approach here: Mock functions in Go

Now though, I'm trying to write a test case on the struct constructor and I'm having a hard time figuring out how to test it.

Here's an example:

type requestBuilder func(portFlipArgs, PortFlipConfig) portFlipRequest

type portFlip struct {
    config  PortFlipConfig
    args    portFlipArgs
    builder requestBuilder
}

func newPortFlip(args portFlipArgs, config PortFlipConfig) (*portFlip, error) {
    p := &portFlip{args: args, config: config}
    if p.netType() == "sdn" {
        p.builder = newSDNRequest
    } else if p.netType() == "legacy" {
        p.builder = newLegacyRequest
    } else {
        return nil, fmt.Errorf("Invalid or nil netType: %s", p.netType())
    }
    return p, nil
}

The 'newSDNRequest' and 'newLegacyRequest' are the new constructors. I can't figure out how to test the newPortFlip method to make sure that's it assigning the right method to the 'builder' member, since you can't test function equality.

My only thought at this point is to have a 'builderType string' member, and just assign it to the name of the new constructor and then I could just test that. Something like:

func newPortFlip(args portFlipArgs, config PortFlipConfig) (*portFlip, error) {
    p := &portFlip{args: args, config: config}
    if p.netType() == "sdn" {
        p.builderType = "newSDNRequest"
        p.builder = newSDNRequest
    } else if p.netType() == "legacy" {
        p.builderType = "newLegacyRequest"
        p.builder = newLegacyRequest
    } else {
        return nil, fmt.Errorf("Invalid or nil netType: %s", p.netType())
    }
    return p, nil
}

But that seemed rather frivolous, so I figured I should seek a better way before I did that.

Thoughts?

Community
  • 1
  • 1
sjmh
  • 3,330
  • 4
  • 23
  • 27

1 Answers1

0

Make portFlip an interface and have newPortFlip construct either an sdnPortFlip or a legacyPortFlip depending on the incoming type. In your test you can then check it's returning the correct concrete type using a type assertion.

If you embed the common type into the SDN and legacy types then you can directly call those methods.

type portFlip interface {
    build()
    ...
}

type portFlipCommon struct {
    config  PortFlipConfig
    args    portFlipArgs
}

type portFlipSdn struct {
    portFlipCommon
}

type portFlipLegacy struct {
    portFlipCommon
}

func (pf *portFlipCommon) netType() { ... }
func (pf *portFlipSdn)    build() { ... }
func (pf *portFlipLegacy) build() { ... }

func newPortFlip(args portFlipArgs, config PortFlipConfig) (portFlip, error) {
    var pf portFlip
    p := &portFlipCommon{args: args, config: config}
    if p.netType() == "sdn" {
        // Either build directly or define build on the sdn type
        pf = &portFlipSdn{*p}
    } else if p.netType() == "legacy" {
        // Either build directly or define build on the legacy type
        pf = &portFlipLegacy{*p}
    } else {
        return nil, fmt.Errorf("Invalid or nil netType: %s", p.netType())
    }
    return pf, nil
}
Andrew Johnson
  • 7,277
  • 4
  • 23
  • 27
  • Not sure I've ever seen the '&portFlipSdn(*p)' syntax before. Is that documented somewhere? I recognize the embedding of portFlipCommon into the portFlipSdn and portFlipLegacy struct's. – sjmh Nov 15 '15 at 05:15
  • Think maybe you meant pf := &portFlipSdn{*p} ? – sjmh Nov 15 '15 at 08:01
  • Yes, and I should have had '=' rather than ':=', otherwise pf is declared in the block, shadowing the one declared earlier. – Andrew Johnson Nov 15 '15 at 23:03