github.com/wulonghui/docker@v1.8.0-rc2/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, 6, 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 110 func TestParseWithUnamedReturn(t *testing.T) { 111 _, err := Parse(testFixture, "Fooer4") 112 if !strings.HasSuffix(err.Error(), ErrBadReturn.Error()) { 113 t.Fatalf("expected ErrBadReturn, got %v", err) 114 } 115 } 116 117 func TestEmbeddedInterface(t *testing.T) { 118 pkg, err := Parse(testFixture, "Fooer5") 119 if err != nil { 120 t.Fatal(err) 121 } 122 123 assertName(t, "foo", pkg.Name) 124 assertNum(t, 2, len(pkg.Functions)) 125 126 f := pkg.Functions[0] 127 assertName(t, "Foo", f.Name) 128 assertNum(t, 0, len(f.Args)) 129 assertNum(t, 0, len(f.Returns)) 130 131 f = pkg.Functions[1] 132 assertName(t, "Boo", f.Name) 133 assertNum(t, 2, len(f.Args)) 134 assertNum(t, 2, len(f.Returns)) 135 136 arg := f.Args[0] 137 assertName(t, "a", arg.Name) 138 assertName(t, "string", arg.ArgType) 139 140 arg = f.Args[1] 141 assertName(t, "b", arg.Name) 142 assertName(t, "string", arg.ArgType) 143 144 arg = f.Returns[0] 145 assertName(t, "s", arg.Name) 146 assertName(t, "string", arg.ArgType) 147 148 arg = f.Returns[1] 149 assertName(t, "err", arg.Name) 150 assertName(t, "error", arg.ArgType) 151 } 152 153 func assertName(t *testing.T, expected, actual string) { 154 if expected != actual { 155 fatalOut(t, fmt.Sprintf("expected name to be `%s`, got: %s", expected, actual)) 156 } 157 } 158 159 func assertNum(t *testing.T, expected, actual int) { 160 if expected != actual { 161 fatalOut(t, fmt.Sprintf("expected number to be %d, got: %d", expected, actual)) 162 } 163 } 164 165 func fatalOut(t *testing.T, msg string) { 166 _, file, ln, _ := runtime.Caller(2) 167 t.Fatalf("%s:%d: %s", filepath.Base(file), ln, msg) 168 }