Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✅ test: all - add more unit test cases for some util func
- Loading branch information
Showing
12 changed files
with
260 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package fmtutil_test | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/gookit/goutil/fmtutil" | ||
"github.com/gookit/goutil/testutil/assert" | ||
) | ||
|
||
func TestStringOrJSON(t *testing.T) { | ||
t.Run("string input", func(t *testing.T) { | ||
input := "Hello, world!" | ||
expected := []byte(input) | ||
actual, err := fmtutil.StringOrJSON(input) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
if !bytes.Equal(expected, actual) { | ||
t.Errorf("expected %q, but got %q", expected, actual) | ||
} | ||
}) | ||
|
||
t.Run("JSON input", func(t *testing.T) { | ||
input := map[string]any{ | ||
"foo": "bar", | ||
"baz": 123, | ||
} | ||
|
||
actual, err := fmtutil.StringOrJSON(input) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
|
||
assert.StrContains(t, string(actual), `"foo": "bar"`) | ||
assert.StrContains(t, string(actual), `"baz": 123`) | ||
}) | ||
|
||
t.Run("invalid JSON input", func(t *testing.T) { | ||
input := make(chan int) // channel is not JSON-serializable | ||
_, err := fmtutil.StringOrJSON(input) | ||
if err == nil { | ||
t.Error("expected error, but got nil") | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package finder_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gookit/goutil/fsutil/finder" | ||
"github.com/gookit/goutil/testutil/assert" | ||
) | ||
|
||
func TestBasic_func(t *testing.T) { | ||
assert.Eq(t, finder.FlagBoth, finder.ToFlag("b")) | ||
assert.Eq(t, finder.FlagBoth, finder.ToFlag("both")) | ||
assert.Eq(t, finder.FlagFile, finder.ToFlag("")) | ||
assert.Eq(t, finder.FlagFile, finder.ToFlag("file")) | ||
assert.Eq(t, finder.FlagDir, finder.ToFlag("dir")) | ||
} | ||
|
||
func TestFinder_Config(t *testing.T) { | ||
ff := finder.NewEmpty().ConfigFn(func(c *finder.Config) { | ||
c.IncludeNames = []string{"name1"} | ||
}) | ||
|
||
ff.AddScan("/some/path/dir"). | ||
FileAndDir(). | ||
WithExts([]string{".go"}). | ||
WithFileExt(".mod"). | ||
WithoutFile(".keep"). | ||
CacheResult(false) | ||
|
||
assert.NotEmpty(t, ff.Config()) | ||
} | ||
|
||
func TestConfig_NewFinder(t *testing.T) { | ||
c := finder.NewConfig() | ||
c.IncludePaths = []string{"path/to"} | ||
c.ExcludePaths = []string{"exclude/path"} | ||
c.ExcludeDirs = []string{"dir1"} | ||
c.IncludeExts = []string{".go"} | ||
c.ExcludeFiles = []string{".keep"} | ||
|
||
ff := c.NewFinder() | ||
ff.UseAbsPath(true). | ||
CacheResult(true). | ||
WithoutDotFile(true) | ||
ff.IncludeExt(".txt") | ||
|
||
assert.NotEmpty(t, ff.Config()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.