uber-go / mock

GoMock is a mocking framework for the Go programming language.
Apache License 2.0
1.85k stars 106 forks source link

Provide a new flag that makes the generated constructor function private #42

Open sakuradon99 opened 1 year ago

sakuradon99 commented 1 year ago

In my code, there are some private interfaces that should only be accessed within the current package. However, when I use gomock to generate mock code, other packages can call NewmockXXX to create a mock of those interfaces, which is not what we expect. I would like to achieve a functionality similar to the following:

...
    private            = flag.Bool("private", false, "Generate private constructor functions for all mocks")
...
...
    newStr := "New"
    if *private {
        newStr = "new"
    }
    g.p("// "+newStr+"%v creates a new mock instance.", mockType)
    g.p("func "+newStr+"%v%v(ctrl *gomock.Controller) *%v%v {", mockType, longTp, mockType, shortTp)
    g.in()
...
tulzke commented 8 months ago

I see three use cases:

  1. Generate public mocks for all interfaces.
  2. Generate private mocks for all interfaces
  3. Generate public mocks for public interfaces, private for private ones.

Maybe it's better to make "private" a flag with multiple string values, like "exlude_interfaces" or "mock_names"?