trpc.group/trpc-go/trpc-cmdline@v1.0.9/util/apidocs/x/types_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 x
    11  
    12  import (
    13  	"testing"
    14  
    15  	protobuf "google.golang.org/protobuf/types/descriptorpb"
    16  )
    17  
    18  func TestGetFormatStr(t *testing.T) {
    19  	tests := []struct {
    20  		name  string
    21  		pType protobuf.FieldDescriptorProto_Type
    22  		want  string
    23  	}{
    24  		{"DOUBLE", protobuf.FieldDescriptorProto_TYPE_DOUBLE, "double"},
    25  		{"FLOAT", protobuf.FieldDescriptorProto_TYPE_FLOAT, "float"},
    26  		{"INT64", protobuf.FieldDescriptorProto_TYPE_INT64, "int64"},
    27  		{"UINT64", protobuf.FieldDescriptorProto_TYPE_UINT64, "uint64"},
    28  		{"INT32", protobuf.FieldDescriptorProto_TYPE_INT32, "int32"},
    29  		{"FIXED64", protobuf.FieldDescriptorProto_TYPE_FIXED64, "fixed64"},
    30  		{"FIXED32", protobuf.FieldDescriptorProto_TYPE_FIXED32, "fixed32"},
    31  		{"BOOL", protobuf.FieldDescriptorProto_TYPE_BOOL, ""},
    32  		{"STRING", protobuf.FieldDescriptorProto_TYPE_STRING, ""},
    33  		{"GROUP", protobuf.FieldDescriptorProto_TYPE_GROUP, "group"},
    34  		{"MESSAGE", protobuf.FieldDescriptorProto_TYPE_MESSAGE, "message"},
    35  		{"BYTES", protobuf.FieldDescriptorProto_TYPE_BYTES, "bytes"},
    36  		{"UINT32", protobuf.FieldDescriptorProto_TYPE_UINT32, "uint32"},
    37  		// Enum type format should be set to int32 (with type as integer)
    38  		{"ENUM", protobuf.FieldDescriptorProto_TYPE_ENUM, "int32"},
    39  		{"SFIXED32", protobuf.FieldDescriptorProto_TYPE_SFIXED32, "sfixed32"},
    40  		{"SFIXED64", protobuf.FieldDescriptorProto_TYPE_SFIXED64, "sfixed64"},
    41  		{"SINT32", protobuf.FieldDescriptorProto_TYPE_SINT32, "sint32"},
    42  		{"SINT64", protobuf.FieldDescriptorProto_TYPE_SINT64, "sint64"},
    43  		{"other set to string", 19, "string"},
    44  	}
    45  
    46  	for _, tt := range tests {
    47  		t.Run(tt.name, func(t *testing.T) {
    48  			if got := GetFormatStr(tt.pType); got != tt.want {
    49  				t.Errorf("GetFormatStr() = %v, want %v", got, tt.want)
    50  			}
    51  		})
    52  	}
    53  }
    54  
    55  func TestGetTypeStr(t *testing.T) {
    56  	tests := []struct {
    57  		name  string
    58  		pType protobuf.FieldDescriptorProto_Type
    59  		want  string
    60  	}{
    61  		{"bool", protobuf.FieldDescriptorProto_TYPE_BOOL, "boolean"},
    62  		{"double", protobuf.FieldDescriptorProto_TYPE_DOUBLE, "number"},
    63  		{"float", protobuf.FieldDescriptorProto_TYPE_FLOAT, "number"},
    64  		{"int64", protobuf.FieldDescriptorProto_TYPE_INT64, "string"},
    65  		{"int32", protobuf.FieldDescriptorProto_TYPE_INT32, "integer"},
    66  		{"uint64", protobuf.FieldDescriptorProto_TYPE_UINT64, "string"},
    67  		{"uint32", protobuf.FieldDescriptorProto_TYPE_UINT32, "integer"},
    68  		{"fixed64", protobuf.FieldDescriptorProto_TYPE_FIXED64, "string"},
    69  		{"fixed32", protobuf.FieldDescriptorProto_TYPE_FIXED32, "integer"},
    70  		{"sfixed64", protobuf.FieldDescriptorProto_TYPE_SFIXED64, "integer"},
    71  		{"sfixed32", protobuf.FieldDescriptorProto_TYPE_SFIXED32, "integer"},
    72  		{"sint64", protobuf.FieldDescriptorProto_TYPE_SINT64, "integer"},
    73  		{"sint32", protobuf.FieldDescriptorProto_TYPE_SINT32, "integer"},
    74  		// Enum type should be set to integer.
    75  		{"enum", protobuf.FieldDescriptorProto_TYPE_ENUM, "integer"},
    76  		{"string", protobuf.FieldDescriptorProto_TYPE_STRING, "string"},
    77  		{"bytes", protobuf.FieldDescriptorProto_TYPE_BYTES, "string"},
    78  		{"message", protobuf.FieldDescriptorProto_TYPE_MESSAGE, "object"},
    79  		{"other set to string", 19, "string"},
    80  	}
    81  	for _, tt := range tests {
    82  		t.Run(tt.name, func(t *testing.T) {
    83  			if got := GetTypeStr(tt.pType); got != tt.want {
    84  				t.Errorf("GetTypeStr() = %v, want %v", got, tt.want)
    85  			}
    86  		})
    87  	}
    88  }