trpc.group/trpc-go/trpc-cmdline@v1.0.9/util/apidocs/info_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 apidocs 11 12 import ( 13 "fmt" 14 "path/filepath" 15 "reflect" 16 "testing" 17 18 "github.com/agiledragon/gomonkey" 19 20 "trpc.group/trpc-go/trpc-cmdline/descriptor" 21 ) 22 23 func TestNewInfo(t *testing.T) { 24 type args struct { 25 fd *descriptor.FileDescriptor 26 } 27 tests := []struct { 28 name string 29 args args 30 want InfoStruct 31 wantErr bool 32 absError error 33 }{ 34 { 35 name: "case1-file_path_abs_error", 36 args: args{ 37 fd: &descriptor.FileDescriptor{}, 38 }, 39 want: InfoStruct{}, 40 wantErr: true, 41 absError: fmt.Errorf("error"), 42 }, 43 { 44 name: "case2-success", 45 args: args{ 46 fd: &descriptor.FileDescriptor{ 47 FilePath: "user.proto", 48 }, 49 }, 50 want: InfoStruct{ 51 Title: "user", 52 Description: "The api document of user.proto", 53 Version: "2.0", 54 }, 55 wantErr: false, 56 }, 57 } 58 for _, tt := range tests { 59 t.Run(tt.name, func(t *testing.T) { 60 p := gomonkey.ApplyFunc(filepath.Abs, 61 func(path string) (string, error) { 62 return tt.args.fd.FilePath, tt.absError 63 }) 64 defer p.Reset() 65 66 got, err := NewInfo(tt.args.fd) 67 if (err != nil) != tt.wantErr { 68 t.Errorf("NewInfo() error = %v, wantErr %v", err, tt.wantErr) 69 return 70 } 71 if !reflect.DeepEqual(got, tt.want) { 72 t.Errorf("NewInfo() got = %v, want %v", got, tt.want) 73 } 74 }) 75 } 76 }