github.com/songzhibin97/gkit@v1.2.13/distributed/task/result_test.go (about)

     1  package task
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestConvertResult(t *testing.T) {
     9  	type args struct {
    10  		result []*Result
    11  	}
    12  	tests := []struct {
    13  		name    string
    14  		args    args
    15  		want    []reflect.Value
    16  		wantErr bool
    17  	}{
    18  		{
    19  			name: "string",
    20  			args: args{result: []*Result{{
    21  				Type:  "string",
    22  				Value: "gkit",
    23  			}}},
    24  			want:    []reflect.Value{reflect.ValueOf("gkit")},
    25  			wantErr: false,
    26  		},
    27  		{
    28  			name: "mix",
    29  			args: args{result: []*Result{{
    30  				Type:  "string",
    31  				Value: "gkit",
    32  			}, {
    33  				Type:  "int",
    34  				Value: 1,
    35  			}, {
    36  				Type:  "[]string",
    37  				Value: []string{"6", "66", "666"},
    38  			}}},
    39  			want: []reflect.Value{reflect.ValueOf("gkit"), reflect.ValueOf(int(1)), reflect.ValueOf([]string{"6", "66", "666"})},
    40  		},
    41  	}
    42  	for _, tt := range tests {
    43  		t.Run(tt.name, func(t *testing.T) {
    44  			got, err := ConvertResult(tt.args.result)
    45  			if (err != nil) != tt.wantErr {
    46  				t.Errorf("ConvertResult() error = %v, wantErr %v", err, tt.wantErr)
    47  				return
    48  			}
    49  			if len(got) != len(tt.want) {
    50  				t.Errorf("got len no equal want: got len = %d  want len = %d", len(got), len(tt.want))
    51  
    52  			}
    53  			for i := range got {
    54  				if !reflect.DeepEqual(got[i].Interface(), tt.want[i].Interface()) {
    55  					t.Errorf("ConvertResult() got = %v, want %v", got, tt.want)
    56  				}
    57  			}
    58  			t.Log(FormatResult(got))
    59  		})
    60  	}
    61  }