gopkg.in/docker/docker.v23@v23.0.11/pkg/plugins/pluginrpc-gen/parser_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "path/filepath" 6 "runtime" 7 "strings" 8 "testing" 9 ) 10 11 const testFixture = "fixtures/foo.go" 12 13 func TestParseEmptyInterface(t *testing.T) { 14 pkg, err := Parse(testFixture, "Fooer") 15 if err != nil { 16 t.Fatal(err) 17 } 18 19 assertName(t, "foo", pkg.Name) 20 assertNum(t, 0, len(pkg.Functions)) 21 } 22 23 func TestParseNonInterfaceType(t *testing.T) { 24 _, err := Parse(testFixture, "wobble") 25 if _, ok := err.(errUnexpectedType); !ok { 26 t.Fatal("expected type error when parsing non-interface type") 27 } 28 } 29 30 func TestParseWithOneFunction(t *testing.T) { 31 pkg, err := Parse(testFixture, "Fooer2") 32 if err != nil { 33 t.Fatal(err) 34 } 35 36 assertName(t, "foo", pkg.Name) 37 assertNum(t, 1, len(pkg.Functions)) 38 assertName(t, "Foo", pkg.Functions[0].Name) 39 assertNum(t, 0, len(pkg.Functions[0].Args)) 40 assertNum(t, 0, len(pkg.Functions[0].Returns)) 41 } 42 43 func TestParseWithMultipleFuncs(t *testing.T) { 44 pkg, err := Parse(testFixture, "Fooer3") 45 if err != nil { 46 t.Fatal(err) 47 } 48 49 assertName(t, "foo", pkg.Name) 50 assertNum(t, 7, len(pkg.Functions)) 51 52 f := pkg.Functions[0] 53 assertName(t, "Foo", f.Name) 54 assertNum(t, 0, len(f.Args)) 55 assertNum(t, 0, len(f.Returns)) 56 57 f = pkg.Functions[1] 58 assertName(t, "Bar", f.Name) 59 assertNum(t, 1, len(f.Args)) 60 assertNum(t, 0, len(f.Returns)) 61 arg := f.Args[0] 62 assertName(t, "a", arg.Name) 63 assertName(t, "string", arg.ArgType) 64 65 f = pkg.Functions[2] 66 assertName(t, "Baz", f.Name) 67 assertNum(t, 1, len(f.Args)) 68 assertNum(t, 1, len(f.Returns)) 69 arg = f.Args[0] 70 assertName(t, "a", arg.Name) 71 assertName(t, "string", arg.ArgType) 72 arg = f.Returns[0] 73 assertName(t, "err", arg.Name) 74 assertName(t, "error", arg.ArgType) 75 76 f = pkg.Functions[3] 77 assertName(t, "Qux", f.Name) 78 assertNum(t, 2, len(f.Args)) 79 assertNum(t, 2, len(f.Returns)) 80 arg = f.Args[0] 81 assertName(t, "a", f.Args[0].Name) 82 assertName(t, "string", f.Args[0].ArgType) 83 arg = f.Args[1] 84 assertName(t, "b", arg.Name) 85 assertName(t, "string", arg.ArgType) 86 arg = f.Returns[0] 87 assertName(t, "val", arg.Name) 88 assertName(t, "string", arg.ArgType) 89 arg = f.Returns[1] 90 assertName(t, "err", arg.Name) 91 assertName(t, "error", arg.ArgType) 92 93 f = pkg.Functions[4] 94 assertName(t, "Wobble", f.Name) 95 assertNum(t, 0, len(f.Args)) 96 assertNum(t, 1, len(f.Returns)) 97 arg = f.Returns[0] 98 assertName(t, "w", arg.Name) 99 assertName(t, "*wobble", arg.ArgType) 100 101 f = pkg.Functions[5] 102 assertName(t, "Wiggle", f.Name) 103 assertNum(t, 0, len(f.Args)) 104 assertNum(t, 1, len(f.Returns)) 105 arg = f.Returns[0] 106 assertName(t, "w", arg.Name) 107 assertName(t, "wobble", arg.ArgType) 108 109 f = pkg.Functions[6] 110 assertName(t, "WiggleWobble", f.Name) 111 assertNum(t, 6, len(f.Args)) 112 assertNum(t, 6, len(f.Returns)) 113 expectedArgs := [][]string{ 114 {"a", "[]*wobble"}, 115 {"b", "[]wobble"}, 116 {"c", "map[string]*wobble"}, 117 {"d", "map[*wobble]wobble"}, 118 {"e", "map[string][]wobble"}, 119 {"f", "[]*otherfixture.Spaceship"}, 120 } 121 for i, arg := range f.Args { 122 assertName(t, expectedArgs[i][0], arg.Name) 123 assertName(t, expectedArgs[i][1], arg.ArgType) 124 } 125 expectedReturns := [][]string{ 126 {"g", "map[*wobble]wobble"}, 127 {"h", "[][]*wobble"}, 128 {"i", "otherfixture.Spaceship"}, 129 {"j", "*otherfixture.Spaceship"}, 130 {"k", "map[*otherfixture.Spaceship]otherfixture.Spaceship"}, 131 {"l", "[]otherfixture.Spaceship"}, 132 } 133 for i, ret := range f.Returns { 134 assertName(t, expectedReturns[i][0], ret.Name) 135 assertName(t, expectedReturns[i][1], ret.ArgType) 136 } 137 } 138 139 func TestParseWithUnnamedReturn(t *testing.T) { 140 _, err := Parse(testFixture, "Fooer4") 141 if !strings.HasSuffix(err.Error(), errBadReturn.Error()) { 142 t.Fatalf("expected ErrBadReturn, got %v", err) 143 } 144 } 145 146 func TestEmbeddedInterface(t *testing.T) { 147 pkg, err := Parse(testFixture, "Fooer5") 148 if err != nil { 149 t.Fatal(err) 150 } 151 152 assertName(t, "foo", pkg.Name) 153 assertNum(t, 2, len(pkg.Functions)) 154 155 f := pkg.Functions[0] 156 assertName(t, "Foo", f.Name) 157 assertNum(t, 0, len(f.Args)) 158 assertNum(t, 0, len(f.Returns)) 159 160 f = pkg.Functions[1] 161 assertName(t, "Boo", f.Name) 162 assertNum(t, 2, len(f.Args)) 163 assertNum(t, 2, len(f.Returns)) 164 165 arg := f.Args[0] 166 assertName(t, "a", arg.Name) 167 assertName(t, "string", arg.ArgType) 168 169 arg = f.Args[1] 170 assertName(t, "b", arg.Name) 171 assertName(t, "string", arg.ArgType) 172 173 arg = f.Returns[0] 174 assertName(t, "s", arg.Name) 175 assertName(t, "string", arg.ArgType) 176 177 arg = f.Returns[1] 178 assertName(t, "err", arg.Name) 179 assertName(t, "error", arg.ArgType) 180 } 181 182 func TestParsedImports(t *testing.T) { 183 cases := []string{"Fooer6", "Fooer7", "Fooer8", "Fooer9", "Fooer10", "Fooer11"} 184 for _, testCase := range cases { 185 pkg, err := Parse(testFixture, testCase) 186 if err != nil { 187 t.Fatal(err) 188 } 189 190 assertNum(t, 1, len(pkg.Imports)) 191 importPath := strings.Split(pkg.Imports[0].Path, "/") 192 assertName(t, "otherfixture\"", importPath[len(importPath)-1]) 193 assertName(t, "", pkg.Imports[0].Name) 194 } 195 } 196 197 func TestAliasedImports(t *testing.T) { 198 pkg, err := Parse(testFixture, "Fooer12") 199 if err != nil { 200 t.Fatal(err) 201 } 202 203 assertNum(t, 1, len(pkg.Imports)) 204 assertName(t, "aliasedio", pkg.Imports[0].Name) 205 } 206 207 func assertName(t *testing.T, expected, actual string) { 208 if expected != actual { 209 fatalOut(t, fmt.Sprintf("expected name to be `%s`, got: %s", expected, actual)) 210 } 211 } 212 213 func assertNum(t *testing.T, expected, actual int) { 214 if expected != actual { 215 fatalOut(t, fmt.Sprintf("expected number to be %d, got: %d", expected, actual)) 216 } 217 } 218 219 func fatalOut(t *testing.T, msg string) { 220 _, file, ln, _ := runtime.Caller(2) 221 t.Fatalf("%s:%d: %s", filepath.Base(file), ln, msg) 222 }