Skip to content

Commit

Permalink
✅ test: cflag,cli,fs - add more unit tests and fix some test error
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 30, 2023
1 parent b05e3ed commit 530d6c5
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 16 deletions.
33 changes: 20 additions & 13 deletions cflag/ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ func NewIntVar(checkFn comdef.IntCheckFunc) IntVar {
return IntVar{CheckFn: checkFn}
}

// Get value string
// Get value
func (o *IntVar) Get() any {
return o.str
return o.val
}

// Set new value
Expand Down Expand Up @@ -102,7 +102,7 @@ type String string

// Get value
func (s *String) Get() any {
return s
return string(*s)
}

// Set value
Expand Down Expand Up @@ -224,7 +224,7 @@ type Ints []int

// String to string
func (s *Ints) String() string {
return fmt.Sprintf("%v", *s)
return arrutil.ToString(*s)
}

// Get value
Expand All @@ -251,9 +251,8 @@ func (s *Ints) IsRepeatable() bool {
return true
}

// Strings The string flag list, repeatable
//
// implemented flag.Value interface
// Strings The string flag list, repeatable.
// eg: --names tom --names john
type Strings []string

// String input value to string
Expand All @@ -263,7 +262,7 @@ func (s *Strings) String() string {

// Get value
func (s *Strings) Get() any {
return s
return []string(*s)
}

// Set new value
Expand All @@ -272,18 +271,28 @@ func (s *Strings) Set(value string) error {
return nil
}

// Strings value
func (s *Strings) Strings() []string {
return *s
}

// IsRepeatable on input
func (s *Strings) IsRepeatable() bool {
return true
}

// Booleans The bool flag list, repeatable
// implemented flag.Value interface
// Booleans The bool flag list, repeatable.
// eg: -v -v => []bool{true, true}
type Booleans []bool

// String input value to string
func (s *Booleans) String() string {
return fmt.Sprintf("%v", *s)
return arrutil.ToString(*s)
}

// Bools value
func (s *Booleans) Bools() []bool {
return *s
}

// Set new value
Expand Down Expand Up @@ -424,8 +433,6 @@ func (s *KVString) IsRepeatable() bool {

// ConfString The config-string flag, INI format, like nginx-config.
//
// Implemented the flag.Value interface.
//
// Example:
//
// --config 'k0=val0;k1=val1' => string map {k0:val0, k1:val1}
Expand Down
33 changes: 33 additions & 0 deletions cflag/ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ func TestString_methods(t *testing.T) {
assert.NoErr(t, s.Set("val"))
assert.Eq(t, "val", s.Get())
assert.Eq(t, "val", s.String())

assert.NoErr(t, s.Set("val1,val2"))
assert.Eq(t, []string{"val1", "val2"}, s.Strings())

assert.NoErr(t, s.Set("23,34"))
assert.Eq(t, []int{23, 34}, s.Ints(","))
}

func TestStrVar_methods(t *testing.T) {
Expand Down Expand Up @@ -101,6 +107,10 @@ func TestInts_methods(t *testing.T) {
assert.NotEmpty(t, its.Get())
assert.Eq(t, []int{23}, its.Ints())
assert.Eq(t, "[23]", its.String())

assert.True(t, its.IsRepeatable())
assert.NoErr(t, its.Set("34"))
assert.Eq(t, "[23,34]", its.String())
}

func TestIntsString_methods(t *testing.T) {
Expand Down Expand Up @@ -129,3 +139,26 @@ func TestIntsString_methods(t *testing.T) {
}
assert.Err(t, its.Set("45"))
}

func TestStrings_methods(t *testing.T) {
ss := cflag.Strings{}

assert.NoErr(t, ss.Set("val"))
assert.Eq(t, []string{"val"}, ss.Get())
assert.Eq(t, []string{"val"}, ss.Strings())

assert.NoErr(t, ss.Set("val2"))
assert.Eq(t, "val,val2", ss.String())
assert.True(t, ss.IsRepeatable())
}

func TestBooleans_methods(t *testing.T) {
bs := cflag.Booleans{}

assert.NoErr(t, bs.Set("true"))
assert.Eq(t, []bool{true}, bs.Bools())

assert.NoErr(t, bs.Set("false"))
assert.Eq(t, "[true,false]", bs.String())
assert.True(t, bs.IsRepeatable())
}
5 changes: 5 additions & 0 deletions cliutil/cliutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,8 @@ func TestShellQuote(t *testing.T) {
assert.Eq(t, `'ab"s'`, cliutil.ShellQuote(`ab"s`))
assert.Eq(t, "abs", cliutil.ShellQuote("abs"))
}

func TestOutputLines(t *testing.T) {
assert.Empty(t, cliutil.OutputLines("\n"))
assert.Eq(t, []string{"a", "b"}, cliutil.OutputLines("a\nb"))
}
6 changes: 3 additions & 3 deletions comdef/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ type SortedType interface {
Int | Uint | Float | ~string
}

// Compared type. alias of constraints.ScalarType
// Compared type. alias of constraints.SortedType
//
// TODO: use type alias, will error on go1.18 Error: types.go:50: interface contains type constraints
// type Compared = ScalarType
// type Compared = SortedType
type Compared interface {
Int | Uint | Float | ~string | ~bool
Int | Uint | Float | ~string
}

// ScalarType interface type.
Expand Down
55 changes: 55 additions & 0 deletions fsutil/operate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,61 @@ func TestQuickOpenFile(t *testing.T) {
assert.NoErr(t, fsutil.Remove(file.Name()))
}

func TestMustOpenFile(t *testing.T) {
assert.Panics(t, func() {
fsutil.MustOpenFile("/path-not-exists", os.O_RDONLY, 0666)
})
}

func TestOpenAppendFile(t *testing.T) {
fpath := "./testdata/open-append-file.txt"
assert.NoErr(t, fsutil.RmFileIfExist(fpath))

file, err := fsutil.OpenAppendFile(fpath)
assert.NoErr(t, err)
assert.Eq(t, fpath, file.Name())

_, err = file.WriteString("hello")
assert.NoErr(t, err)
assert.NoErr(t, file.Close())

// reopen for write
file, err = fsutil.OpenAppendFile(fpath)
assert.NoErr(t, err)

_, err = file.WriteString(" world")
assert.NoErr(t, err)
assert.NoErr(t, file.Close())

// read all
s := fsutil.ReadString(fpath)
assert.Eq(t, "hello world", s)
}

func TestOpenTruncFile(t *testing.T) {
fpath := "./testdata/open-trunc-file.txt"
assert.NoErr(t, fsutil.RmFileIfExist(fpath))

file, err := fsutil.OpenTruncFile(fpath)
assert.NoErr(t, err)
assert.Eq(t, fpath, file.Name())

_, err = file.WriteString("hello")
assert.NoErr(t, err)
assert.NoErr(t, file.Close())

// reopen for write
file, err = fsutil.OpenTruncFile(fpath)
assert.NoErr(t, err)

_, err = file.WriteString(" world")
assert.NoErr(t, err)

// read all
s := fsutil.ReadString(fpath)
assert.Eq(t, " world", s)
}

func TestMustRemove(t *testing.T) {
assert.Panics(t, func() {
fsutil.MustRm("/path-not-exist")
Expand Down

0 comments on commit 530d6c5

Please sign in to comment.