0

What is the right way to write following test?

To avoid writing inside the package itself

var _ Common = (*Stuff)(nil), 
var _ Common2 = (*Stuff)(nil)
var _ Common3 = (*Stuff)(nil)
...

``

package stuff

stuff/common.go

package stuff

type Common interface {
  String() string
}

stuff/common2.go

package stuff

type Common2 interface {
  String2() string
}

stuff/stuff_suite_test.go

package stuff

import (
  "testing"
  . "github.com/onsi/ginkgo"
  . "github.com/onsi/gomega"
)

func Test(t *testing.T) {
  RegisterFailHandler(Fail)
  RunSpecs(t, "Stuff")
}

stuff/stuff_test.go

package stuff

import (
  "testing"
  . "github.com/onsi/ginkgo"
  . "github.com/onsi/gomega"
}

var _ = Describe("Stuff", func() {
  It("implements Common", func() {
    var s interface{} = &Stuff{}
    _, ok := s.(Common)
    Expect(ok).To(Equal(true))
  })
})

It("implements Common 2", func() {
  var _ Common2 = (*Stuff)(nil) // ok
})

stuff/stuff.go

package stuff

type Stuff struct {}

func (s *Stuff) String() string {
  return ""
}

package lib

/lib/lib.go

import "stuff"

func GetCommon() *stuff.Stuff {
  return &stuff.Stuff{}
}

/lib/lib_suite_test.go

package lib

import (
  "testing"
  . "github.com/onsi/ginkgo"
  . "github.com/onsi/gomega"
)

func Test(t *testing.T) {
  RegisterFailHandler(Fail)
  RunSpecs(t, "Lib")
}

/lib/lib_test.go

package lib

import (
    . "github.com/onsi/ginkgo"
    . "github.com/onsi/gomega"
)

var _ = Describe("Lib", func() {
  It("can be created", func() {
    l := GetCommon()
    Expect(l).NotTo(BeNil())
  })
}
mkungla
  • 3,123
  • 1
  • 24
  • 37
  • 3
    No need to write tests for that, just use the compiler: `var _ Common = (*Stuff)(nil)`. You'll find this pattern in the standard library as well. – mkopriva Oct 02 '19 at 18:07
  • @mkopriva so just to expect compiler to fail e.g. `var _ Common = (*Stuff)(nil)`, `var _ Common2 = (*Stuff)(nil)` ... ??? – mkungla Oct 02 '19 at 18:15
  • 3
    yup, with that your program will *not* compile if `*Stuff` does not implement `Common`, and if your program compiles it means `*Stuff` *does* implement `Common`. – mkopriva Oct 02 '19 at 18:17
  • https://play.golang.com/p/G3YNlQ2MeAQ – mkopriva Oct 02 '19 at 18:21
  • Possible duplicate of https://stackoverflow.com/q/30367803/13860 – Flimzy Oct 02 '19 at 18:34
  • @mkopriva updated the question! What I mean is that `lib` package builds without knowing that stuff should implement `String2` – mkungla Oct 02 '19 at 18:48
  • 2
    @mkungla write the check where you need `*Stuff` to implement `Common`. However keep in mind that this is less useful outside of the declaring package. Why? Because most likely you are importing `stuff` so that you can use it and if you're using `*Stuff` where `Common` is expected, imported or not, the compiler will complain if the interface is not implemented, and keep quiet if it is. [example1](https://play.golang.com/p/FipYcC3evRT), [example2](https://play.golang.com/p/zduCZu2MJTc), [example3](https://play.golang.com/p/hbszAqouVmz) – mkopriva Oct 02 '19 at 19:13

0 Answers0