gitee.com/quant1x/gox@v1.21.2/util/internal/util_string_test.go (about)

     1  // Copyright (c) 2015, Emir Pasic. 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 internal
     6  
     7  import (
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  func TestToStringInts(t *testing.T) {
    13  	var value interface{}
    14  
    15  	value = int8(1)
    16  	if actualValue, expectedValue := ToString(value), "1"; actualValue != expectedValue {
    17  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
    18  	}
    19  
    20  	value = int16(1)
    21  	if actualValue, expectedValue := ToString(value), "1"; actualValue != expectedValue {
    22  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
    23  	}
    24  
    25  	value = int32(1)
    26  	if actualValue, expectedValue := ToString(value), "1"; actualValue != expectedValue {
    27  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
    28  	}
    29  
    30  	value = int64(1)
    31  	if actualValue, expectedValue := ToString(value), "1"; actualValue != expectedValue {
    32  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
    33  	}
    34  
    35  	value = rune(1)
    36  	if actualValue, expectedValue := ToString(value), "1"; actualValue != expectedValue {
    37  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
    38  	}
    39  }
    40  
    41  func TestToStringUInts(t *testing.T) {
    42  	var value interface{}
    43  
    44  	value = uint8(1)
    45  	if actualValue, expectedValue := ToString(value), "1"; actualValue != expectedValue {
    46  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
    47  	}
    48  
    49  	value = uint16(1)
    50  	if actualValue, expectedValue := ToString(value), "1"; actualValue != expectedValue {
    51  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
    52  	}
    53  
    54  	value = uint32(1)
    55  	if actualValue, expectedValue := ToString(value), "1"; actualValue != expectedValue {
    56  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
    57  	}
    58  
    59  	value = uint64(1)
    60  	if actualValue, expectedValue := ToString(value), "1"; actualValue != expectedValue {
    61  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
    62  	}
    63  
    64  	value = byte(1)
    65  	if actualValue, expectedValue := ToString(value), "1"; actualValue != expectedValue {
    66  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
    67  	}
    68  }
    69  
    70  func TestToStringFloats(t *testing.T) {
    71  	var value interface{}
    72  
    73  	value = float32(1.123456)
    74  	if actualValue, expectedValue := ToString(value), "1.123456"; !strings.HasPrefix(actualValue, expectedValue) {
    75  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
    76  	}
    77  	value = float32(1.123456)
    78  	if actualValue, expectedValue := ToString(value), "1.123456"; !strings.HasPrefix(actualValue, expectedValue) {
    79  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
    80  	}
    81  }
    82  
    83  func TestToStringOther(t *testing.T) {
    84  	var value interface{}
    85  
    86  	value = "abc"
    87  	if actualValue, expectedValue := ToString(value), "abc"; actualValue != expectedValue {
    88  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
    89  	}
    90  
    91  	value = true
    92  	if actualValue, expectedValue := ToString(value), "true"; actualValue != expectedValue {
    93  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
    94  	}
    95  
    96  	type T struct {
    97  		id   int
    98  		name string
    99  	}
   100  
   101  	if actualValue, expectedValue := ToString(T{1, "abc"}), "{id:1 name:abc}"; actualValue != expectedValue {
   102  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   103  	}
   104  }