2015-01-22 14:17:02 +08:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-08-17 06:32:21 +08:00
|
|
|
"os"
|
|
|
|
"reflect"
|
2018-01-11 10:34:14 +08:00
|
|
|
"runtime"
|
2015-02-12 21:34:00 +08:00
|
|
|
"strings"
|
2015-01-22 14:17:02 +08:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestReadlines(t *testing.T) {
|
|
|
|
ret, err := ReadLines("common_test.go")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2015-02-12 21:34:00 +08:00
|
|
|
if !strings.Contains(ret[0], "package common") {
|
2015-01-22 14:17:02 +08:00
|
|
|
t.Error("could not read correctly")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadLinesOffsetN(t *testing.T) {
|
|
|
|
ret, err := ReadLinesOffsetN("common_test.go", 2, 1)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
fmt.Println(ret[0])
|
2015-02-12 21:34:00 +08:00
|
|
|
if !strings.Contains(ret[0], `import (`) {
|
2015-01-22 14:17:02 +08:00
|
|
|
t.Error("could not read correctly")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIntToString(t *testing.T) {
|
|
|
|
src := []int8{65, 66, 67}
|
2021-11-06 17:53:56 +08:00
|
|
|
dst := IntToString(src)
|
|
|
|
if dst != "ABC" {
|
2015-01-22 14:17:02 +08:00
|
|
|
t.Error("could not convert")
|
|
|
|
}
|
|
|
|
}
|
2021-12-23 05:54:41 +08:00
|
|
|
|
2015-01-22 14:17:02 +08:00
|
|
|
func TestByteToString(t *testing.T) {
|
|
|
|
src := []byte{65, 66, 67}
|
|
|
|
dst := ByteToString(src)
|
|
|
|
if dst != "ABC" {
|
|
|
|
t.Error("could not convert")
|
|
|
|
}
|
|
|
|
|
|
|
|
src = []byte{0, 65, 66, 67}
|
|
|
|
dst = ByteToString(src)
|
|
|
|
if dst != "ABC" {
|
|
|
|
t.Error("could not convert")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-07 19:24:17 +08:00
|
|
|
func TestHexToUint32(t *testing.T) {
|
|
|
|
if HexToUint32("FFFFFFFF") != 4294967295 {
|
|
|
|
t.Error("Could not convert")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-02 21:18:57 +08:00
|
|
|
func TestMustParseInt32(t *testing.T) {
|
2021-11-06 17:53:56 +08:00
|
|
|
ret := mustParseInt32("11111")
|
|
|
|
if ret != int32(11111) {
|
2015-01-22 14:17:02 +08:00
|
|
|
t.Error("could not parse")
|
|
|
|
}
|
|
|
|
}
|
2021-12-23 05:54:41 +08:00
|
|
|
|
2020-10-02 21:18:57 +08:00
|
|
|
func TestMustParseUint64(t *testing.T) {
|
2015-01-22 14:17:02 +08:00
|
|
|
ret := mustParseUint64("11111")
|
|
|
|
if ret != uint64(11111) {
|
|
|
|
t.Error("could not parse")
|
|
|
|
}
|
|
|
|
}
|
2021-12-23 05:54:41 +08:00
|
|
|
|
2020-10-02 21:18:57 +08:00
|
|
|
func TestMustParseFloat64(t *testing.T) {
|
2015-01-22 14:17:02 +08:00
|
|
|
ret := mustParseFloat64("11111.11")
|
|
|
|
if ret != float64(11111.11) {
|
|
|
|
t.Error("could not parse")
|
|
|
|
}
|
|
|
|
ret = mustParseFloat64("11111")
|
|
|
|
if ret != float64(11111) {
|
|
|
|
t.Error("could not parse")
|
|
|
|
}
|
|
|
|
}
|
2021-12-23 05:54:41 +08:00
|
|
|
|
2015-07-21 14:25:04 +08:00
|
|
|
func TestStringsContains(t *testing.T) {
|
2015-01-22 14:17:02 +08:00
|
|
|
target, err := ReadLines("common_test.go")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2015-07-21 14:25:04 +08:00
|
|
|
if !StringsContains(target, "func TestStringsContains(t *testing.T) {") {
|
2015-01-22 14:17:02 +08:00
|
|
|
t.Error("cloud not test correctly")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPathExists(t *testing.T) {
|
|
|
|
if !PathExists("common_test.go") {
|
|
|
|
t.Error("exists but return not exists")
|
|
|
|
}
|
|
|
|
if PathExists("should_not_exists.go") {
|
|
|
|
t.Error("not exists but return exists")
|
|
|
|
}
|
|
|
|
}
|
2016-03-13 22:42:06 +08:00
|
|
|
|
2022-07-15 20:20:19 +08:00
|
|
|
func TestPathExistsWithContents(t *testing.T) {
|
|
|
|
if !PathExistsWithContents("common_test.go") {
|
|
|
|
t.Error("exists but return not exists")
|
|
|
|
}
|
|
|
|
if PathExistsWithContents("should_not_exists.go") {
|
|
|
|
t.Error("not exists but return exists")
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.CreateTemp("", "empty_test.txt")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("CreateTemp failed, %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(f.Name()) // clean up
|
|
|
|
|
|
|
|
if PathExistsWithContents(f.Name()) {
|
|
|
|
t.Error("exists but no content file return true")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-13 22:42:06 +08:00
|
|
|
func TestHostEtc(t *testing.T) {
|
2018-01-11 10:34:14 +08:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("windows doesn't have etc")
|
|
|
|
}
|
2021-11-06 17:53:56 +08:00
|
|
|
p := HostEtc("mtab")
|
|
|
|
if p != "/etc/mtab" {
|
2016-03-13 22:42:06 +08:00
|
|
|
t.Errorf("invalid HostEtc, %s", p)
|
|
|
|
}
|
|
|
|
}
|
2017-08-17 06:32:21 +08:00
|
|
|
|
|
|
|
func TestGetSysctrlEnv(t *testing.T) {
|
|
|
|
// Append case
|
|
|
|
env := getSysctrlEnv([]string{"FOO=bar"})
|
|
|
|
if !reflect.DeepEqual(env, []string{"FOO=bar", "LC_ALL=C"}) {
|
|
|
|
t.Errorf("unexpected append result from getSysctrlEnv: %q", env)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace case
|
|
|
|
env = getSysctrlEnv([]string{"FOO=bar", "LC_ALL=en_US.UTF-8"})
|
|
|
|
if !reflect.DeepEqual(env, []string{"FOO=bar", "LC_ALL=C"}) {
|
|
|
|
t.Errorf("unexpected replace result from getSysctrlEnv: %q", env)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test against real env
|
|
|
|
env = getSysctrlEnv(os.Environ())
|
|
|
|
found := false
|
|
|
|
for _, v := range env {
|
|
|
|
if v == "LC_ALL=C" {
|
|
|
|
found = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(v, "LC_ALL") {
|
|
|
|
t.Fatalf("unexpected LC_ALL value: %q", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Errorf("unexpected real result from getSysctrlEnv: %q", env)
|
|
|
|
}
|
|
|
|
}
|