gitee.com/quant1x/gox@v1.21.2/api/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 api 6 7 import ( 8 "fmt" 9 "strings" 10 "testing" 11 ) 12 13 func TestToStringInts(t *testing.T) { 14 var value interface{} 15 16 value = int8(1) 17 if actualValue, expectedValue := ToString(value), "1"; actualValue != expectedValue { 18 t.Errorf("Got %v expected %v", actualValue, expectedValue) 19 } 20 21 value = int16(1) 22 if actualValue, expectedValue := ToString(value), "1"; actualValue != expectedValue { 23 t.Errorf("Got %v expected %v", actualValue, expectedValue) 24 } 25 26 value = int32(1) 27 if actualValue, expectedValue := ToString(value), "1"; actualValue != expectedValue { 28 t.Errorf("Got %v expected %v", actualValue, expectedValue) 29 } 30 31 value = int64(1) 32 if actualValue, expectedValue := ToString(value), "1"; actualValue != expectedValue { 33 t.Errorf("Got %v expected %v", actualValue, expectedValue) 34 } 35 36 value = rune(1) 37 if actualValue, expectedValue := ToString(value), "1"; actualValue != expectedValue { 38 t.Errorf("Got %v expected %v", actualValue, expectedValue) 39 } 40 } 41 42 func TestToStringUInts(t *testing.T) { 43 var value interface{} 44 45 value = uint8(1) 46 if actualValue, expectedValue := ToString(value), "1"; actualValue != expectedValue { 47 t.Errorf("Got %v expected %v", actualValue, expectedValue) 48 } 49 50 value = uint16(1) 51 if actualValue, expectedValue := ToString(value), "1"; actualValue != expectedValue { 52 t.Errorf("Got %v expected %v", actualValue, expectedValue) 53 } 54 55 value = uint32(1) 56 if actualValue, expectedValue := ToString(value), "1"; actualValue != expectedValue { 57 t.Errorf("Got %v expected %v", actualValue, expectedValue) 58 } 59 60 value = uint64(1) 61 if actualValue, expectedValue := ToString(value), "1"; actualValue != expectedValue { 62 t.Errorf("Got %v expected %v", actualValue, expectedValue) 63 } 64 65 value = byte(1) 66 if actualValue, expectedValue := ToString(value), "1"; actualValue != expectedValue { 67 t.Errorf("Got %v expected %v", actualValue, expectedValue) 68 } 69 } 70 71 func TestToStringFloats(t *testing.T) { 72 var value interface{} 73 74 value = float32(1.123456) 75 if actualValue, expectedValue := ToString(value), "1.123456"; !strings.HasPrefix(actualValue, expectedValue) { 76 t.Errorf("Got %v expected %v", actualValue, expectedValue) 77 } 78 value = float32(1.123456) 79 if actualValue, expectedValue := ToString(value), "1.123456"; !strings.HasPrefix(actualValue, expectedValue) { 80 t.Errorf("Got %v expected %v", actualValue, expectedValue) 81 } 82 } 83 84 func TestToStringOther(t *testing.T) { 85 var value interface{} 86 87 value = "abc" 88 if actualValue, expectedValue := ToString(value), "abc"; actualValue != expectedValue { 89 t.Errorf("Got %v expected %v", actualValue, expectedValue) 90 } 91 92 value = true 93 if actualValue, expectedValue := ToString(value), "true"; actualValue != expectedValue { 94 t.Errorf("Got %v expected %v", actualValue, expectedValue) 95 } 96 97 type T struct { 98 id int 99 name string 100 } 101 102 if actualValue, expectedValue := ToString(T{1, "abc"}), "{id:1 name:abc}"; actualValue != expectedValue { 103 t.Errorf("Got %v expected %v", actualValue, expectedValue) 104 } 105 } 106 107 func TestToCamelCase(t *testing.T) { 108 kebabString := "border_left_color" 109 t.Log(kebabString) 110 camel := ToCamelCase(kebabString) 111 if camel != "borderLeftColor" { 112 t.Error(camel) 113 return 114 } 115 t.Log(camel) 116 } 117 118 func TestStringToBytes(t *testing.T) { 119 str := "12345哈哈abc" 120 bytes := String2Bytes(str) 121 fmt.Println(bytes) 122 fmt.Println(Bytes2String(bytes)) 123 }