github.com/unionj-cloud/go-doudou@v1.3.8-0.20221011095552-0088008e5b31/toolkit/internal/common/common_test.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"reflect"
     7  	"runtime"
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  func TestReadlines(t *testing.T) {
    13  	ret, err := ReadLines("common_test.go")
    14  	if err != nil {
    15  		t.Error(err)
    16  	}
    17  	if !strings.Contains(ret[0], "package common") {
    18  		t.Error("could not read correctly")
    19  	}
    20  }
    21  
    22  func TestReadLinesOffsetN(t *testing.T) {
    23  	ret, err := ReadLinesOffsetN("common_test.go", 2, 1)
    24  	if err != nil {
    25  		t.Error(err)
    26  	}
    27  	fmt.Println(ret[0])
    28  	if !strings.Contains(ret[0], `import (`) {
    29  		t.Error("could not read correctly")
    30  	}
    31  }
    32  
    33  func TestIntToString(t *testing.T) {
    34  	src := []int8{65, 66, 67}
    35  	dst := IntToString(src)
    36  	if dst != "ABC" {
    37  		t.Error("could not convert")
    38  	}
    39  }
    40  
    41  func TestByteToString(t *testing.T) {
    42  	src := []byte{65, 66, 67}
    43  	dst := ByteToString(src)
    44  	if dst != "ABC" {
    45  		t.Error("could not convert")
    46  	}
    47  
    48  	src = []byte{0, 65, 66, 67}
    49  	dst = ByteToString(src)
    50  	if dst != "ABC" {
    51  		t.Error("could not convert")
    52  	}
    53  }
    54  
    55  func TestHexToUint32(t *testing.T) {
    56  	if HexToUint32("FFFFFFFF") != 4294967295 {
    57  		t.Error("Could not convert")
    58  	}
    59  }
    60  
    61  func TestMustParseInt32(t *testing.T) {
    62  	ret := mustParseInt32("11111")
    63  	if ret != int32(11111) {
    64  		t.Error("could not parse")
    65  	}
    66  }
    67  
    68  func TestMustParseUint64(t *testing.T) {
    69  	ret := mustParseUint64("11111")
    70  	if ret != uint64(11111) {
    71  		t.Error("could not parse")
    72  	}
    73  }
    74  
    75  func TestMustParseFloat64(t *testing.T) {
    76  	ret := mustParseFloat64("11111.11")
    77  	if ret != float64(11111.11) {
    78  		t.Error("could not parse")
    79  	}
    80  	ret = mustParseFloat64("11111")
    81  	if ret != float64(11111) {
    82  		t.Error("could not parse")
    83  	}
    84  }
    85  
    86  func TestStringsContains(t *testing.T) {
    87  	target, err := ReadLines("common_test.go")
    88  	if err != nil {
    89  		t.Error(err)
    90  	}
    91  	if !StringsContains(target, "func TestStringsContains(t *testing.T) {") {
    92  		t.Error("cloud not test correctly")
    93  	}
    94  }
    95  
    96  func TestPathExists(t *testing.T) {
    97  	if !PathExists("common_test.go") {
    98  		t.Error("exists but return not exists")
    99  	}
   100  	if PathExists("should_not_exists.go") {
   101  		t.Error("not exists but return exists")
   102  	}
   103  }
   104  
   105  func TestPathExistsWithContents(t *testing.T) {
   106  	if !PathExistsWithContents("common_test.go") {
   107  		t.Error("exists but return not exists")
   108  	}
   109  	if PathExistsWithContents("should_not_exists.go") {
   110  		t.Error("not exists but return exists")
   111  	}
   112  
   113  	f, err := os.CreateTemp("", "empty_test.txt")
   114  	if err != nil {
   115  		t.Errorf("CreateTemp failed, %s", err)
   116  	}
   117  	defer os.Remove(f.Name()) // clean up
   118  
   119  	if PathExistsWithContents(f.Name()) {
   120  		t.Error("exists but no content file return true")
   121  	}
   122  }
   123  
   124  func TestHostEtc(t *testing.T) {
   125  	if runtime.GOOS == "windows" {
   126  		t.Skip("windows doesn't have etc")
   127  	}
   128  	p := HostEtc("mtab")
   129  	if p != "/etc/mtab" {
   130  		t.Errorf("invalid HostEtc, %s", p)
   131  	}
   132  }
   133  
   134  func TestGetSysctrlEnv(t *testing.T) {
   135  	// Append case
   136  	env := getSysctrlEnv([]string{"FOO=bar"})
   137  	if !reflect.DeepEqual(env, []string{"FOO=bar", "LC_ALL=C"}) {
   138  		t.Errorf("unexpected append result from getSysctrlEnv: %q", env)
   139  	}
   140  
   141  	// Replace case
   142  	env = getSysctrlEnv([]string{"FOO=bar", "LC_ALL=en_US.UTF-8"})
   143  	if !reflect.DeepEqual(env, []string{"FOO=bar", "LC_ALL=C"}) {
   144  		t.Errorf("unexpected replace result from getSysctrlEnv: %q", env)
   145  	}
   146  
   147  	// Test against real env
   148  	env = getSysctrlEnv(os.Environ())
   149  	found := false
   150  	for _, v := range env {
   151  		if v == "LC_ALL=C" {
   152  			found = true
   153  			continue
   154  		}
   155  		if strings.HasPrefix(v, "LC_ALL") {
   156  			t.Fatalf("unexpected LC_ALL value: %q", v)
   157  		}
   158  	}
   159  	if !found {
   160  		t.Errorf("unexpected real result from getSysctrlEnv: %q", env)
   161  	}
   162  }