gf/g/os/gfile/gfile_contents_test.go

313 lines
5.4 KiB
Go
Raw Normal View History

package gfile
2019-04-10 22:16:16 +08:00
import (
"github.com/gogf/gf/g/test/gtest"
"io/ioutil"
2019-04-10 22:16:16 +08:00
"strings"
"testing"
)
func TestGetContents(t *testing.T) {
gtest.Case(t,func(){
var(
2019-04-11 15:49:00 +08:00
filepaths string= "./testfile/dirfiles/t1.txt"
)
2019-04-11 15:49:00 +08:00
gtest.Assert(GetContents(filepaths),"my name is jroam")
2019-04-10 17:54:28 +08:00
gtest.Assert(GetContents(""),"")
})
}
func TestGetBinContents(t *testing.T) {
gtest.Case(t , func() {
var(
2019-04-11 15:49:00 +08:00
filepaths1 string="./testfile/dirfiles/t1.txt" //存在文件
filepaths2 string="./testfile/dirfiles/t1_no.txt" //不存大文件
readcontent []byte
)
readcontent=GetBinContents(filepaths1)
2019-04-11 15:49:00 +08:00
gtest.Assert(readcontent,[]byte("my name is jroam"))
readcontent=GetBinContents(filepaths2)
2019-04-10 17:54:28 +08:00
gtest.Assert(readcontent,nil)
2019-04-09 23:25:20 +08:00
//if readcontent!=nil{
// t.Error("文件应不存在")
//}
gtest.Assert(GetBinContents(filepaths2),nil)
})
}
2019-04-10 13:39:36 +08:00
//截断文件为指定的大小
func TestTruncate(t *testing.T) {
gtest.Case(t , func() {
2019-04-10 13:39:36 +08:00
var(
filepaths1 string="./testfile/havefile1/GetContents.txt" //存在文件
err error
)
err=Truncate(filepaths1,200)
gtest.Assert(err,nil)
2019-04-10 17:54:28 +08:00
err=Truncate("",200)
gtest.AssertNE(err,nil)
})
}
func TestPutContents(t *testing.T) {
gtest.Case(t , func() {
var(
filepaths string="./testfile/havefile1/PutContents.txt"
err error
readcontent []byte
)
err=PutContents(filepaths,"test!")
gtest.Assert(err,nil)
//==================判断是否真正写入
readcontent, err=ioutil.ReadFile(filepaths)
gtest.Assert(err,nil)
gtest.Assert(string(readcontent),"test!")
2019-04-10 17:54:28 +08:00
err=PutContents("","test!")
gtest.AssertNE(err,nil)
})
}
2019-04-10 17:54:28 +08:00
func TestPutContentsAppend(t *testing.T) {
gtest.Case(t , func() {
var(
filepaths string="./testfile/havefile1/PutContents.txt"
err error
readcontent []byte
)
err=PutContentsAppend(filepaths,"hello")
gtest.Assert(err,nil)
//==================判断是否真正写入
readcontent, err=ioutil.ReadFile(filepaths)
gtest.Assert(err,nil)
gtest.Assert(string(readcontent),"test!hello")
2019-04-10 17:54:28 +08:00
err=PutContentsAppend("","hello")
gtest.AssertNE(err,nil)
})
}
func TestPutBinContents(t *testing.T){
gtest.Case(t , func() {
var(
filepaths string="./testfile/havefile1/PutContents.txt"
err error
readcontent []byte
)
err=PutBinContents(filepaths,[]byte("test!!"))
gtest.Assert(err,nil)
//==================判断是否真正写入
readcontent, err=ioutil.ReadFile(filepaths)
gtest.Assert(err,nil)
gtest.Assert(string(readcontent),"test!!")
2019-04-10 17:54:28 +08:00
err=PutBinContents("",[]byte("test!!"))
gtest.AssertNE(err,nil)
})
}
func TestPutBinContentsAppend(t *testing.T) {
gtest.Case(t , func() {
var(
filepaths string="./testfile/havefile1/PutContents.txt" //原文件内容: yy
err error
readcontent []byte
)
err=PutBinContentsAppend(filepaths,[]byte("word"))
gtest.Assert(err,nil)
//==================判断是否真正写入
readcontent, err=ioutil.ReadFile(filepaths)
gtest.Assert(err,nil)
gtest.Assert(string(readcontent),"test!!word")
2019-04-10 17:54:28 +08:00
err=PutBinContentsAppend("",[]byte("word"))
gtest.AssertNE(err,nil)
})
}
func TestGetBinContentsByTwoOffsetsByPath(t *testing.T) {
gtest.Case(t, func() {
var (
filepaths string = "./testfile/havefile1/GetContents.txt" //原文件内容: abcdefghijk
readcontent []byte
)
readcontent = GetBinContentsByTwoOffsetsByPath(filepaths, 2, 5)
gtest.Assert(string(readcontent), "cde")
2019-04-10 17:54:28 +08:00
readcontent = GetBinContentsByTwoOffsetsByPath("", 2, 5)
gtest.Assert(len(readcontent),0)
})
}
2019-04-10 13:39:36 +08:00
func TestGetNextCharOffsetByPath(t *testing.T) {
gtest.Case(t, func() {
var (
filepaths string = "./testfile/havefile1/GetContents.txt" //原文件内容: abcdefghijk
localindex int64
)
localindex = GetNextCharOffsetByPath(filepaths,'d', 1)
gtest.Assert(localindex, 3)
2019-04-10 17:54:28 +08:00
localindex = GetNextCharOffsetByPath("",'d', 1)
gtest.Assert(localindex, -1)
2019-04-10 13:39:36 +08:00
})
}
2019-04-10 22:16:16 +08:00
func TestGetNextCharOffset(t *testing.T) {
gtest.Case(t, func() {
var (
localindex int64
)
reader:=strings.NewReader("helloword")
localindex = GetNextCharOffset(reader,'w', 1)
gtest.Assert(localindex,5)
localindex = GetNextCharOffset(reader,'j', 1)
gtest.Assert(localindex,-1)
})
}
func TestGetBinContentsByTwoOffsets(t *testing.T) {
gtest.Case(t, func() {
var (
reads []byte
)
reader:=strings.NewReader("helloword")
reads = GetBinContentsByTwoOffsets(reader,1, 3)
gtest.Assert(string(reads),"el")
reads = GetBinContentsByTwoOffsets(reader,10, 30)
gtest.Assert(string(reads),"")
})
}
func TestGetBinContentsTilChar(t *testing.T) {
gtest.Case(t, func() {
var (
reads []byte
indexs int64
)
reader:=strings.NewReader("helloword")
reads,_ = GetBinContentsTilChar(reader,'w', 2)
gtest.Assert(string(reads),"llow")
_,indexs = GetBinContentsTilChar(reader,'w', 20)
gtest.Assert(indexs,-1)
})
}
func TestGetBinContentsTilCharByPath(t *testing.T) {
gtest.Case(t, func() {
var (
reads []byte
indexs int64
filepaths string = "./testfile/havefile1/GetContents.txt"
)
reads,_ = GetBinContentsTilCharByPath(filepaths,'c',2)
gtest.Assert(string(reads),"c")
reads,_ = GetBinContentsTilCharByPath(filepaths,'y',1)
gtest.Assert(string(reads),"")
_,indexs = GetBinContentsTilCharByPath(filepaths,'x',1)
gtest.Assert(indexs,-1)
})
}
func TestHome(t *testing.T) {
gtest.Case(t, func() {
var (
reads string
err error
)
reads,err=Home()
gtest.Assert(err,nil)
gtest.AssertNE(reads,"")
})
}