1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-13 01:02:28 +08:00

refactor: rewrite all unit test functions with assert

This commit is contained in:
dudaodong
2022-01-09 15:10:56 +08:00
parent 9cb9aa2f2f
commit 9da7115169

View File

@@ -2,72 +2,62 @@ package fileutil
import ( import (
"os" "os"
"reflect"
"testing" "testing"
"github.com/duke-git/lancet/internal" "github.com/duke-git/lancet/internal"
) )
func TestIsExist(t *testing.T) { func TestIsExist(t *testing.T) {
assert := internal.NewAssert(t, "TestIsExist")
cases := []string{"./", "./file.go", "./a.txt"} cases := []string{"./", "./file.go", "./a.txt"}
expected := []bool{true, true, false} expected := []bool{true, true, false}
for i := 0; i < len(cases); i++ { for i := 0; i < len(cases); i++ {
res := IsExist(cases[i]) actual := IsExist(cases[i])
if res != expected[i] { assert.Equal(expected[i], actual)
internal.LogFailedTestInfo(t, "IsExist", cases[i], expected[i], res)
t.FailNow()
}
} }
} }
func TestCreateFile(t *testing.T) { func TestCreateFile(t *testing.T) {
assert := internal.NewAssert(t, "TestCreateFile")
f := "./text.txt" f := "./text.txt"
if CreateFile(f) { if CreateFile(f) {
file, err := os.Open(f) file, err := os.Open(f)
if err != nil { assert.IsNil(err)
internal.LogFailedTestInfo(t, "CreateFile", f, f, "create file error: "+err.Error()) assert.Equal(f, file.Name())
t.FailNow()
}
if file.Name() != f {
internal.LogFailedTestInfo(t, "CreateFile", f, f, file.Name())
t.FailNow()
}
} else { } else {
internal.LogFailedTestInfo(t, "CreateFile", f, f, "create file error")
t.FailNow() t.FailNow()
} }
os.Remove(f) os.Remove(f)
} }
func TestIsDir(t *testing.T) { func TestIsDir(t *testing.T) {
assert := internal.NewAssert(t, "TestIsDir")
cases := []string{"./", "./a.txt"} cases := []string{"./", "./a.txt"}
expected := []bool{true, false} expected := []bool{true, false}
for i := 0; i < len(cases); i++ { for i := 0; i < len(cases); i++ {
res := IsDir(cases[i]) actual := IsDir(cases[i])
if res != expected[i] { assert.Equal(expected[i], actual)
internal.LogFailedTestInfo(t, "IsDir", cases[i], expected[i], res)
t.FailNow()
}
} }
} }
func TestRemoveFile(t *testing.T) { func TestRemoveFile(t *testing.T) {
assert := internal.NewAssert(t, "TestRemoveFile")
f := "./text.txt" f := "./text.txt"
if CreateFile(f) { if CreateFile(f) {
err := RemoveFile(f) err := RemoveFile(f)
if err != nil { assert.IsNil(err)
internal.LogFailedTestInfo(t, "RemoveFile", f, f, err.Error())
t.FailNow()
}
} else {
internal.LogFailedTestInfo(t, "RemoveFile", f, f, "create file error")
t.FailNow()
} }
} }
func TestCopyFile(t *testing.T) { func TestCopyFile(t *testing.T) {
assert := internal.NewAssert(t, "TestCopyFile")
srcFile := "./text.txt" srcFile := "./text.txt"
CreateFile(srcFile) CreateFile(srcFile)
@@ -76,126 +66,118 @@ func TestCopyFile(t *testing.T) {
err := CopyFile(srcFile, destFile) err := CopyFile(srcFile, destFile)
if err != nil { if err != nil {
file, err := os.Open(destFile) file, err := os.Open(destFile)
if err != nil { assert.IsNil(err)
internal.LogFailedTestInfo(t, "CopyFile", srcFile, destFile, "create file error: "+err.Error()) assert.Equal(destFile, file.Name())
t.FailNow()
}
if file.Name() != destFile {
internal.LogFailedTestInfo(t, "CopyFile", srcFile, destFile, file.Name())
t.FailNow()
}
} }
os.Remove(srcFile) os.Remove(srcFile)
os.Remove(destFile) os.Remove(destFile)
} }
func TestListFileNames(t *testing.T) { func TestListFileNames(t *testing.T) {
filesInCurrentPath, err := ListFileNames("../datetime/") assert := internal.NewAssert(t, "TestListFileNames")
if err != nil {
t.FailNow() filesInPath, err := ListFileNames("../datetime/")
} assert.IsNil(err)
expected := []string{"datetime.go", "datetime_test.go"} expected := []string{"datetime.go", "datetime_test.go"}
if !reflect.DeepEqual(filesInCurrentPath, expected) { assert.Equal(expected, filesInPath)
internal.LogFailedTestInfo(t, "ToChar", "./", expected, filesInCurrentPath)
t.FailNow()
}
} }
func TestReadFileToString(t *testing.T) { func TestReadFileToString(t *testing.T) {
assert := internal.NewAssert(t, "TestReadFileToString")
path := "./text.txt" path := "./text.txt"
CreateFile(path) CreateFile(path)
f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777) f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777)
f.WriteString("hello world") f.WriteString("hello world")
res, _ := ReadFileToString(path) content, _ := ReadFileToString(path)
if res != "hello world" { assert.Equal("hello world", content)
internal.LogFailedTestInfo(t, "ReadFileToString", path, "hello world", res)
t.FailNow()
}
os.Remove(path) os.Remove(path)
} }
func TestClearFile(t *testing.T) { func TestClearFile(t *testing.T) {
assert := internal.NewAssert(t, "TestClearFile")
path := "./text.txt" path := "./text.txt"
CreateFile(path) CreateFile(path)
f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777) f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777)
defer f.Close()
f.WriteString("hello world") f.WriteString("hello world")
err := ClearFile(path) err := ClearFile(path)
if err != nil { assert.IsNil(err)
t.Error("Clear file error: ", err)
} content, _ := ReadFileToString(path)
fileContent, _ := ReadFileToString(path) assert.Equal("", content)
if fileContent != "" {
internal.LogFailedTestInfo(t, "ClearFile", path, "", fileContent)
t.FailNow()
}
os.Remove(path) os.Remove(path)
} }
func TestReadFileByLine(t *testing.T) { func TestReadFileByLine(t *testing.T) {
assert := internal.NewAssert(t, "TestReadFileByLine")
path := "./text.txt" path := "./text.txt"
CreateFile(path) CreateFile(path)
f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777) f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777)
defer f.Close()
f.WriteString("hello\nworld") f.WriteString("hello\nworld")
expected := []string{"hello", "world"} expected := []string{"hello", "world"}
res, _ := ReadFileByLine(path) actual, _ := ReadFileByLine(path)
if !reflect.DeepEqual(res, expected) { assert.Equal(expected, actual)
internal.LogFailedTestInfo(t, "ReadFileByLine", path, expected, res)
t.FailNow()
}
os.Remove(path) os.Remove(path)
} }
func TestZipAndUnZip(t *testing.T) { func TestZipAndUnZip(t *testing.T) {
assert := internal.NewAssert(t, "TestZipAndUnZip")
srcFile := "./text.txt" srcFile := "./text.txt"
CreateFile(srcFile) CreateFile(srcFile)
file, _ := os.OpenFile(srcFile, os.O_WRONLY|os.O_TRUNC, 0777) file, _ := os.OpenFile(srcFile, os.O_WRONLY|os.O_TRUNC, 0777)
defer file.Close()
file.WriteString("hello\nworld") file.WriteString("hello\nworld")
zipFile := "./text.zip" zipFile := "./text.zip"
err := Zip(srcFile, zipFile) err := Zip(srcFile, zipFile)
if err != nil { assert.IsNil(err)
internal.LogFailedTestInfo(t, "Zip", srcFile, zipFile, err)
t.FailNow()
}
unZipPath := "./unzip" unZipPath := "./unzip"
err = UnZip(zipFile, unZipPath) err = UnZip(zipFile, unZipPath)
if err != nil { assert.IsNil(err)
internal.LogFailedTestInfo(t, "UnZip", srcFile, unZipPath, err)
t.FailNow()
}
unZipFile := "./unzip/text.txt" unZipFile := "./unzip/text.txt"
if !IsExist(unZipFile) { assert.Equal(true, IsExist(unZipFile))
internal.LogFailedTestInfo(t, "UnZip", zipFile, zipFile, err)
t.FailNow()
}
os.Remove(srcFile) os.Remove(srcFile)
os.Remove(zipFile) os.Remove(zipFile)
os.RemoveAll(unZipPath) os.RemoveAll(unZipPath)
} }
func TestFileMode(t *testing.T) { func TestFileMode(t *testing.T) {
assert := internal.NewAssert(t, "TestFileMode")
srcFile := "./text.txt" srcFile := "./text.txt"
CreateFile(srcFile) CreateFile(srcFile)
mode, err := FileMode(srcFile) mode, err := FileMode(srcFile)
if err != nil { assert.IsNil(err)
t.Fail()
}
t.Log(mode) t.Log(mode)
os.Remove(srcFile) os.Remove(srcFile)
} }
func TestIsLink(t *testing.T) { func TestIsLink(t *testing.T) {
assert := internal.NewAssert(t, "TestIsLink")
srcFile := "./text.txt" srcFile := "./text.txt"
CreateFile(srcFile) CreateFile(srcFile)
@@ -203,31 +185,18 @@ func TestIsLink(t *testing.T) {
if !IsExist(linkFile) { if !IsExist(linkFile) {
_ = os.Symlink(srcFile, linkFile) _ = os.Symlink(srcFile, linkFile)
} }
if !IsLink(linkFile) { assert.Equal(true, IsLink(linkFile))
internal.LogFailedTestInfo(t, "IsLink", linkFile, "true", "false")
t.FailNow() assert.Equal(false, IsLink("./file.go"))
}
if IsLink("./file.go") {
internal.LogFailedTestInfo(t, "IsLink", "./file.go", "false", "true")
t.FailNow()
}
os.Remove(srcFile) os.Remove(srcFile)
os.Remove(linkFile) os.Remove(linkFile)
} }
func TestMiMeType(t *testing.T) { func TestMiMeType(t *testing.T) {
mt1 := MiMeType("./file.go") assert := internal.NewAssert(t, "TestMiMeType")
expected := "text/plain; charset=utf-8"
if mt1 != expected {
internal.LogFailedTestInfo(t, "MiMeType", "./file.go", expected, mt1)
t.FailNow()
}
f, _ := os.Open("./file.go") f, _ := os.Open("./file.go")
mt2 := MiMeType(f) assert.Equal("text/plain; charset=utf-8", MiMeType(f))
if mt2 != expected { assert.Equal("text/plain; charset=utf-8", MiMeType("./file.go"))
internal.LogFailedTestInfo(t, "MiMeType", "./file.go", expected, mt2)
t.FailNow()
}
} }