github.com/searKing/golang/go@v1.2.117/reflect/type_test.go (about)

     1  // Copyright 2020 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package reflect
     6  
     7  import (
     8  	"encoding/json"
     9  	"reflect"
    10  	"testing"
    11  )
    12  
    13  type inputType struct {
    14  	a      reflect.Type
    15  	expect string
    16  }
    17  
    18  func TestTypeDumpTypeInfoDFS(t *testing.T) {
    19  	var nilError *json.SyntaxError
    20  	ins := []inputType{
    21  		{
    22  			a:      reflect.TypeOf(nil),
    23  			expect: `<nil>`,
    24  		},
    25  		{
    26  			a:      reflect.TypeOf(true),
    27  			expect: `bool`,
    28  		},
    29  		{
    30  			a:      reflect.TypeOf(0),
    31  			expect: `int`,
    32  		},
    33  		{
    34  			a:      reflect.TypeOf(""),
    35  			expect: `string`,
    36  		},
    37  		{
    38  			a: reflect.TypeOf(json.SyntaxError{}),
    39  			expect: `json.SyntaxError
    40  	string
    41  	int64`,
    42  		},
    43  		{
    44  			a: reflect.TypeOf(nilError),
    45  			expect: `*json.SyntaxError
    46  	string
    47  	int64`,
    48  		},
    49  	}
    50  	for idx, in := range ins {
    51  		info := DumpTypeInfoDFS(in.a)
    52  		if info != in.expect {
    53  			t.Errorf("#%d expect\n[\n%s\n]\nactual[\n%s\n]", idx, in.expect, info)
    54  		}
    55  	}
    56  }
    57  
    58  func TestTypeDumpTypeInfoBFS(t *testing.T) {
    59  	var nilError *json.SyntaxError
    60  	ins := []inputType{
    61  		{
    62  			a:      reflect.TypeOf(nil),
    63  			expect: `<nil>`,
    64  		},
    65  		{
    66  			a:      reflect.TypeOf(true),
    67  			expect: `bool`,
    68  		},
    69  		{
    70  			a:      reflect.TypeOf(0),
    71  			expect: `int`,
    72  		},
    73  		{
    74  			a:      reflect.TypeOf(""),
    75  			expect: `string`,
    76  		},
    77  		{
    78  			a: reflect.TypeOf(json.SyntaxError{}),
    79  			expect: `json.SyntaxError
    80  	string
    81  	int64`,
    82  		},
    83  		{
    84  			a: reflect.TypeOf(nilError),
    85  			expect: `*json.SyntaxError
    86  	string
    87  	int64`,
    88  		},
    89  	}
    90  	for idx, in := range ins {
    91  		info := DumpTypeInfoBFS(in.a)
    92  		if info != in.expect {
    93  			t.Errorf("#%d expect\n[\n%s\n]\nactual[\n%s\n]", idx, in.expect, info)
    94  		}
    95  	}
    96  }