github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/framework/cache/conv_test.go (about)

     1  // The package is migrated from beego, you can get from following link:
     2  // import(
     3  //   "github.com/beego/beego/v2/client/cache"
     4  // )
     5  // Copyright 2023. All Rights Reserved.
     6  //
     7  // Licensed under the Apache License, Version 2.0 (the "License");
     8  // you may not use this file except in compliance with the License.
     9  // You may obtain a copy of the License at
    10  //
    11  //      http://www.apache.org/licenses/LICENSE-2.0
    12  //
    13  // Unless required by applicable law or agreed to in writing, software
    14  // distributed under the License is distributed on an "AS IS" BASIS,
    15  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  // See the License for the specific language governing permissions and
    17  // limitations under the License.
    18  
    19  package cache
    20  
    21  import (
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func TestGetString(t *testing.T) {
    28  	t1 := "test1"
    29  
    30  	assert.Equal(t, "test1", GetString(t1))
    31  	t2 := []byte("test2")
    32  	assert.Equal(t, "test2", GetString(t2))
    33  	t3 := 1
    34  	assert.Equal(t, "1", GetString(t3))
    35  	var t4 int64 = 1
    36  	assert.Equal(t, "1", GetString(t4))
    37  	t5 := 1.1
    38  	assert.Equal(t, "1.1", GetString(t5))
    39  	assert.Equal(t, "", GetString(nil))
    40  }
    41  
    42  func TestGetInt(t *testing.T) {
    43  	t1 := 1
    44  	assert.Equal(t, 1, GetInt(t1))
    45  	var t2 int32 = 32
    46  	assert.Equal(t, 32, GetInt(t2))
    47  
    48  	var t3 int64 = 64
    49  	assert.Equal(t, 64, GetInt(t3))
    50  	t4 := "128"
    51  
    52  	assert.Equal(t, 128, GetInt(t4))
    53  	assert.Equal(t, 0, GetInt(nil))
    54  }
    55  
    56  func TestGetInt64(t *testing.T) {
    57  	var i int64 = 1
    58  	t1 := 1
    59  	assert.Equal(t, i, GetInt64(t1))
    60  	var t2 int32 = 1
    61  
    62  	assert.Equal(t, i, GetInt64(t2))
    63  	var t3 int64 = 1
    64  	assert.Equal(t, i, GetInt64(t3))
    65  	t4 := "1"
    66  	assert.Equal(t, i, GetInt64(t4))
    67  	assert.Equal(t, int64(0), GetInt64(nil))
    68  }
    69  
    70  func TestGetFloat64(t *testing.T) {
    71  	f := 1.11
    72  	var t1 float32 = 1.11
    73  	assert.Equal(t, f, GetFloat64(t1))
    74  	t2 := 1.11
    75  	assert.Equal(t, f, GetFloat64(t2))
    76  	t3 := "1.11"
    77  	assert.Equal(t, f, GetFloat64(t3))
    78  
    79  	var f2 float64 = 1
    80  	t4 := 1
    81  	assert.Equal(t, f2, GetFloat64(t4))
    82  
    83  	assert.Equal(t, float64(0), GetFloat64(nil))
    84  }
    85  
    86  func TestGetBool(t *testing.T) {
    87  	t1 := true
    88  	assert.True(t, GetBool(t1))
    89  	t2 := "true"
    90  	assert.True(t, GetBool(t2))
    91  
    92  	assert.False(t, GetBool(nil))
    93  }