github.com/astaxie/beego@v1.12.3/cache/conv_test.go (about) 1 // Copyright 2014 beego Author. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package cache 16 17 import ( 18 "testing" 19 ) 20 21 func TestGetString(t *testing.T) { 22 var t1 = "test1" 23 if "test1" != GetString(t1) { 24 t.Error("get string from string error") 25 } 26 var t2 = []byte("test2") 27 if "test2" != GetString(t2) { 28 t.Error("get string from byte array error") 29 } 30 var t3 = 1 31 if "1" != GetString(t3) { 32 t.Error("get string from int error") 33 } 34 var t4 int64 = 1 35 if "1" != GetString(t4) { 36 t.Error("get string from int64 error") 37 } 38 var t5 = 1.1 39 if "1.1" != GetString(t5) { 40 t.Error("get string from float64 error") 41 } 42 43 if "" != GetString(nil) { 44 t.Error("get string from nil error") 45 } 46 } 47 48 func TestGetInt(t *testing.T) { 49 var t1 = 1 50 if 1 != GetInt(t1) { 51 t.Error("get int from int error") 52 } 53 var t2 int32 = 32 54 if 32 != GetInt(t2) { 55 t.Error("get int from int32 error") 56 } 57 var t3 int64 = 64 58 if 64 != GetInt(t3) { 59 t.Error("get int from int64 error") 60 } 61 var t4 = "128" 62 if 128 != GetInt(t4) { 63 t.Error("get int from num string error") 64 } 65 if 0 != GetInt(nil) { 66 t.Error("get int from nil error") 67 } 68 } 69 70 func TestGetInt64(t *testing.T) { 71 var i int64 = 1 72 var t1 = 1 73 if i != GetInt64(t1) { 74 t.Error("get int64 from int error") 75 } 76 var t2 int32 = 1 77 if i != GetInt64(t2) { 78 t.Error("get int64 from int32 error") 79 } 80 var t3 int64 = 1 81 if i != GetInt64(t3) { 82 t.Error("get int64 from int64 error") 83 } 84 var t4 = "1" 85 if i != GetInt64(t4) { 86 t.Error("get int64 from num string error") 87 } 88 if 0 != GetInt64(nil) { 89 t.Error("get int64 from nil") 90 } 91 } 92 93 func TestGetFloat64(t *testing.T) { 94 var f = 1.11 95 var t1 float32 = 1.11 96 if f != GetFloat64(t1) { 97 t.Error("get float64 from float32 error") 98 } 99 var t2 = 1.11 100 if f != GetFloat64(t2) { 101 t.Error("get float64 from float64 error") 102 } 103 var t3 = "1.11" 104 if f != GetFloat64(t3) { 105 t.Error("get float64 from string error") 106 } 107 108 var f2 float64 = 1 109 var t4 = 1 110 if f2 != GetFloat64(t4) { 111 t.Error("get float64 from int error") 112 } 113 114 if 0 != GetFloat64(nil) { 115 t.Error("get float64 from nil error") 116 } 117 } 118 119 func TestGetBool(t *testing.T) { 120 var t1 = true 121 if !GetBool(t1) { 122 t.Error("get bool from bool error") 123 } 124 var t2 = "true" 125 if !GetBool(t2) { 126 t.Error("get bool from string error") 127 } 128 if GetBool(nil) { 129 t.Error("get bool from nil error") 130 } 131 } 132 133 func byteArrayEquals(a []byte, b []byte) bool { 134 if len(a) != len(b) { 135 return false 136 } 137 for i, v := range a { 138 if v != b[i] { 139 return false 140 } 141 } 142 return true 143 }