github.com/go-kivik/kivik/v4@v4.3.2/mockdb/gen/parse_test.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 2 // use this file except in compliance with the License. You may obtain a copy of 3 // the License at 4 // 5 // http://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 // License for the specific language governing permissions and limitations under 11 // the License. 12 13 package main 14 15 import ( 16 "context" 17 "reflect" 18 "testing" 19 20 "gitlab.com/flimzy/testy" 21 22 kivik "github.com/go-kivik/kivik/v4" 23 "github.com/go-kivik/kivik/v4/driver" 24 ) 25 26 type testDriver interface { 27 WithCtx(context.Context) error 28 NoCtx(string) error 29 WithOptions(string, driver.Options) 30 } 31 32 type testClient struct{} 33 34 func (c *testClient) WithCtx(context.Context) error { return nil } 35 func (c *testClient) NoCtx(string) error { return nil } 36 func (c *testClient) WithOptions(string, ...kivik.Option) {} 37 38 func TestMethods(t *testing.T) { 39 type tst struct { 40 input interface{} 41 isClient bool 42 expected []*method 43 err string 44 } 45 tests := testy.NewTable() 46 tests.Add("non-struct", tst{ 47 input: 123, 48 err: "input must be struct", 49 }) 50 tests.Add("wrong field name", tst{ 51 input: struct{ Y int }{}, // nolint: unused 52 err: "wrapper struct must have a single field: X", 53 }) 54 tests.Add("non-interface", tst{ 55 input: struct{ X int }{}, // nolint: unused 56 err: "field X must be of type interface", 57 }) 58 tests.Add("testDriver", tst{ 59 input: struct{ X testDriver }{}, // nolint: unused 60 expected: []*method{ 61 { 62 Name: "NoCtx", 63 ReturnsError: true, 64 Accepts: []reflect.Type{typeString}, 65 }, 66 { 67 Name: "WithCtx", 68 AcceptsContext: true, 69 ReturnsError: true, 70 }, 71 { 72 Name: "WithOptions", 73 AcceptsOptions: true, 74 Accepts: []reflect.Type{typeString}, 75 }, 76 }, 77 }) 78 tests.Add("invalid client", tst{ 79 input: struct{ X int }{}, // nolint: unused 80 isClient: true, 81 err: "field X must be of type pointer to struct", 82 }) 83 tests.Add("testClient", tst{ 84 input: struct{ X testClient }{}, // nolint: unused 85 isClient: true, 86 err: "field X must be of type pointer to struct", 87 }) 88 tests.Add("*testClient", tst{ 89 input: struct{ X *testClient }{}, // nolint: unused 90 isClient: true, 91 expected: []*method{ 92 { 93 Name: "NoCtx", 94 ReturnsError: true, 95 Accepts: []reflect.Type{typeString}, 96 }, 97 { 98 Name: "WithCtx", 99 AcceptsContext: true, 100 ReturnsError: true, 101 }, 102 { 103 Name: "WithOptions", 104 AcceptsOptions: true, 105 Accepts: []reflect.Type{typeString}, 106 }, 107 }, 108 }) 109 110 tests.Run(t, func(t *testing.T, test tst) { 111 result, err := parseMethods(test.input, test.isClient, nil) 112 if !testy.ErrorMatches(test.err, err) { 113 t.Errorf("Unexpected error: %s", err) 114 } 115 if d := testy.DiffInterface(test.expected, result); d != nil { 116 t.Error(d) 117 } 118 }) 119 }