trpc.group/trpc-go/trpc-cmdline@v1.0.9/util/apidocs/openapi/gen_test.go (about) 1 // Tencent is pleased to support the open source community by making tRPC available. 2 // 3 // Copyright (C) 2023 THL A29 Limited, a Tencent company. 4 // All rights reserved. 5 // 6 // If you have downloaded a copy of the tRPC source code from Tencent, 7 // please note that tRPC source code is licensed under the Apache 2.0 License, 8 // A copy of the Apache 2.0 License is included in this file. 9 10 package openapi 11 12 import ( 13 "fmt" 14 "testing" 15 16 "github.com/agiledragon/gomonkey" 17 18 "trpc.group/trpc-go/trpc-cmdline/descriptor" 19 "trpc.group/trpc-go/trpc-cmdline/params" 20 "trpc.group/trpc-go/trpc-cmdline/util/apidocs" 21 ) 22 23 func TestGenOpenAPI(t *testing.T) { 24 type args struct { 25 fd *descriptor.FileDescriptor 26 option *params.Option 27 } 28 tests := []struct { 29 name string 30 args args 31 wantErr bool 32 newErr error 33 }{ 34 { 35 name: "case1: new err", 36 args: args{ 37 fd: &descriptor.FileDescriptor{}, 38 option: ¶ms.Option{}, 39 }, 40 wantErr: true, 41 newErr: fmt.Errorf("err"), 42 }, 43 { 44 name: "case1: without err", 45 args: args{ 46 fd: &descriptor.FileDescriptor{}, 47 option: ¶ms.Option{}, 48 }, 49 wantErr: false, 50 newErr: nil, 51 }, 52 } 53 for _, tt := range tests { 54 t.Run(tt.name, func(t *testing.T) { 55 p := gomonkey.ApplyFunc( 56 apidocs.NewOpenAPIJSON, 57 func(fd *descriptor.FileDescriptor, option *params.Option) (*apidocs.OpenAPIJSON, error) { 58 return &apidocs.OpenAPIJSON{}, tt.newErr 59 }, 60 ).ApplyFunc( 61 apidocs.WriteJSON, 62 func(file string, data interface{}) error { 63 return nil 64 }, 65 ) 66 67 defer p.Reset() 68 69 if err := GenOpenAPI(tt.args.fd, tt.args.option); (err != nil) != tt.wantErr { 70 t.Errorf("GenOpenAPI() error = %v, wantErr %v", err, tt.wantErr) 71 } 72 }) 73 } 74 }