gitee.com/gricks/utils@v1.0.8/misc_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  	"os"
     7  	"testing"
     8  	"time"
     9  
    10  	. "github.com/smartystreets/goconvey/convey"
    11  )
    12  
    13  type stru struct {
    14  	JTime JsonTime
    15  }
    16  
    17  func Test_JsonTime(t *testing.T) {
    18  	Convey("TestJsonTime", t, func() {
    19  		t := time.Unix(1546272000, 0)
    20  		stru := &stru{}
    21  		stru.JTime = JsonTime(t)
    22  		b, err := json.Marshal(stru)
    23  		So(err, ShouldEqual, nil)
    24  		So(string(b), ShouldEqual, `{"JTime":"2019-01-01 00:00:00"}`)
    25  		err = json.Unmarshal(b, stru)
    26  		So(err, ShouldEqual, nil)
    27  		So(time.Time(stru.JTime).Unix(), ShouldEqual, t.Unix())
    28  	})
    29  }
    30  
    31  func Test_Column2Number(t *testing.T) {
    32  	Convey("TestColumn2Number", t, func() {
    33  		datas := []struct {
    34  			Num int
    35  			Col string
    36  		}{
    37  			{26, "Z"},
    38  			{51, "AY"},
    39  			{52, "AZ"},
    40  			{80, "CB"},
    41  			{676, "YZ"},
    42  			{702, "ZZ"},
    43  			{705, "AAC"},
    44  		}
    45  		for _, data := range datas {
    46  			So(Column2Number(data.Col), ShouldEqual, data.Num)
    47  			So(Number2Column(data.Num), ShouldEqual, data.Col)
    48  		}
    49  	})
    50  }
    51  
    52  func Test_Filehash(t *testing.T) {
    53  	Convey("TestFilehash ", t, func() {
    54  		file, text := "_test_file_hash", []byte("xxx")
    55  		err := ioutil.WriteFile(file, text, 0644)
    56  		So(err, ShouldBeNil)
    57  		defer os.Remove(file)
    58  		str, err := Filehash(file)
    59  		So(err, ShouldBeNil)
    60  		So(str, ShouldEqual, "f561aaf6ef0bf14d4208bb46a4ccb3ad")
    61  	})
    62  }