Skip to content

Commit

Permalink
Fix channel directions when using experimental modelgen
Browse files Browse the repository at this point in the history
  • Loading branch information
petergtz authored and Peter Goetz committed Mar 5, 2019
1 parent f35d310 commit cab79a9
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
7 changes: 7 additions & 0 deletions dsl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,13 @@ var _ = Describe("MockDisplay", func() {
})
})

Context("using send-/receive-only channels", func() {
It("generates the mock method with correct channel directions", func() {
var stringReadChan <-chan string
var errorWriteChan chan<- error
display.ChanParams(stringReadChan, errorWriteChan)
})
})
})

func flattenStringSliceOfSlices(sliceOfSlices [][]string) (result []string) {
Expand Down
9 changes: 8 additions & 1 deletion mockgen/mockgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,14 @@ func spaceSeparatedNameFor(t model.Type, packageMap map[string]string) string {
case *model.MapType:
return "map of " + spaceSeparatedNameFor(typedType.Key, packageMap) + " to " + spaceSeparatedNameFor(typedType.Value, packageMap)
case *model.ChanType:
return "chan of " + spaceSeparatedNameFor(typedType.Type, packageMap)
switch typedType.Dir {
case model.RecvDir:
return "recv chan of " + spaceSeparatedNameFor(typedType.Type, packageMap)
case model.SendDir:
return "send chan of " + spaceSeparatedNameFor(typedType.Type, packageMap)
default:
return "chan of " + spaceSeparatedNameFor(typedType.Type, packageMap)
}
// TODO:
// case *model.FuncType:
default:
Expand Down
8 changes: 7 additions & 1 deletion mockgen/mockgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var _ = Describe("Mockgen", func() {
_, matcherSourceCodes := mockgen.GenerateOutput(ast, "irrelevant", "MockDisplay", "test_package", "")

Expect(matcherSourceCodes).To(SatisfyAll(
HaveLen(7),
HaveLen(9),
HaveKeyWithValue("http_request", SatisfyAll(
ContainSubstring("http \"net/http\""),
ContainSubstring("func AnyHttpRequest() http.Request"),
Expand All @@ -42,6 +42,12 @@ var _ = Describe("Mockgen", func() {
ContainSubstring("time \"time\""),
ContainSubstring("func AnyTimeTime() time.Time"),
)),
HaveKeyWithValue("recv_chan_of_string", SatisfyAll(
ContainSubstring("func AnyRecvChanOfString() <-chan string"),
)),
HaveKeyWithValue("send_chan_of_error", SatisfyAll(
ContainSubstring("func AnySendChanOfError() chan<- error"),
)),
))
})
})
Expand Down
11 changes: 10 additions & 1 deletion modelgen/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,17 @@ func (g *modelGenerator) modelTypeFrom(typesType types.Type) model.Type {
Value: g.modelTypeFrom(typedTyp.Elem()),
}
case *types.Chan:
var dir model.ChanDir
switch typedTyp.Dir() {
case types.SendOnly:
dir = model.SendDir
case types.RecvOnly:
dir = model.RecvDir
default:
dir = 0
}
return &model.ChanType{
Dir: model.ChanDir(typedTyp.Dir()),
Dir: dir,
Type: g.modelTypeFrom(typedTyp.Elem()),
}
case *types.Named:
Expand Down
1 change: 1 addition & 0 deletions test_interface/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ type Display interface {
CamelCaseTypeParam(camelCaseParam io.ReadCloser)
MapOfStringToInterfaceParam(m map[string]interface{})
UseTime(t time.Time)
ChanParams(<-chan string, chan<- error)
}

0 comments on commit cab79a9

Please sign in to comment.