diff options
| author | Shulhan <ms@kilabit.info> | 2021-07-18 01:10:58 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2021-07-18 01:10:58 +0700 |
| commit | e5140bdb30b44aad7a47731748f14141f280cb43 (patch) | |
| tree | e6285b37b5a08ef05928391e8c90d548c4ccc509 /lib/memfs/memfs_test.go | |
| parent | c8bc9abb01cb69280c5fd132533985942a737d47 (diff) | |
| download | pakakeh.go-e5140bdb30b44aad7a47731748f14141f280cb43.tar.xz | |
lib/memfs: reorder the test functions by names
While at it, rename the test functions by the function or method that
they tests.
Diffstat (limited to 'lib/memfs/memfs_test.go')
| -rw-r--r-- | lib/memfs/memfs_test.go | 271 |
1 files changed, 135 insertions, 136 deletions
diff --git a/lib/memfs/memfs_test.go b/lib/memfs/memfs_test.go index bebe2cf1..c7b57fbb 100644 --- a/lib/memfs/memfs_test.go +++ b/lib/memfs/memfs_test.go @@ -15,7 +15,139 @@ var ( _testWD string ) -func TestAddFile(t *testing.T) { +func TestMain(m *testing.M) { + var err error + _testWD, err = os.Getwd() + if err != nil { + log.Fatal(err) + } + + err = os.MkdirAll(filepath.Join(_testWD, "testdata/exclude/dir"), 0700) + if err != nil { + perr, ok := err.(*os.PathError) + if !ok { + log.Fatal("!ok:", err) + } + if perr.Err != os.ErrExist { + log.Fatalf("perr: %+v %+v\n", perr.Err, os.ErrExist) + } + } + + err = os.MkdirAll(filepath.Join(_testWD, "testdata/include/dir"), 0700) + if err != nil { + perr, ok := err.(*os.PathError) + if !ok { + log.Fatal(err) + } + if perr.Err != os.ErrExist { + log.Fatal(err) + } + } + + os.Exit(m.Run()) +} + +func TestNew(t *testing.T) { + afile := filepath.Join(_testWD, "testdata/index.html") + + cases := []struct { + desc string + opts Options + expErr string + expMapKeys []string + }{{ + desc: "With empty dir", + expErr: "open : no such file or directory", + expMapKeys: make([]string, 0), + }, { + desc: "With file", + opts: Options{ + Root: afile, + }, + expErr: fmt.Sprintf("memfs.New: mount: %q must be a directory", afile), + }, { + desc: "With directory", + opts: Options{ + Root: filepath.Join(_testWD, "testdata"), + Excludes: []string{ + "memfs_generate.go$", + "direct$", + }, + }, + expMapKeys: []string{ + "/", + "/exclude", + "/exclude/index.css", + "/exclude/index.html", + "/exclude/index.js", + "/include", + "/include/index.css", + "/include/index.html", + "/include/index.js", + "/index.css", + "/index.html", + "/index.js", + "/plain", + }, + }, { + desc: "With excludes", + opts: Options{ + Root: filepath.Join(_testWD, "testdata"), + Excludes: []string{ + `.*\.js$`, + "memfs_generate.go$", + "direct$", + }, + }, + expMapKeys: []string{ + "/", + "/exclude", + "/exclude/index.css", + "/exclude/index.html", + "/include", + "/include/index.css", + "/include/index.html", + "/index.css", + "/index.html", + "/plain", + }, + }, { + desc: "With includes", + opts: Options{ + Root: filepath.Join(_testWD, "testdata"), + Includes: []string{ + `.*\.js$`, + }, + Excludes: []string{ + "memfs_generate.go$", + "direct$", + }, + }, + expMapKeys: []string{ + "/", + "/exclude", + "/exclude/index.js", + "/include", + "/include/index.js", + "/index.js", + }, + }} + + for _, c := range cases { + t.Log(c.desc) + + mfs, err := New(&c.opts) + if err != nil { + test.Assert(t, "error", c.expErr, err.Error()) + continue + } + + gotListNames := mfs.ListNames() + test.Assert(t, "names", c.expMapKeys, gotListNames) + } +} + +func TestMemFS_AddFile(t *testing.T) { cases := []struct { desc string intPath string @@ -103,8 +235,7 @@ func TestAddFile(t *testing.T) { } } -func TestGet(t *testing.T) { - +func TestMemFS_Get(t *testing.T) { cases := []struct { path string expV []byte @@ -213,107 +344,7 @@ func TestGet(t *testing.T) { } } -func TestMemFS_mount(t *testing.T) { - afile := filepath.Join(_testWD, "testdata/index.html") - - cases := []struct { - desc string - opts Options - expErr string - expMapKeys []string - }{{ - desc: "With empty dir", - expErr: "open : no such file or directory", - expMapKeys: make([]string, 0), - }, { - desc: "With file", - opts: Options{ - Root: afile, - }, - expErr: fmt.Sprintf("memfs.New: mount: %q must be a directory", afile), - }, { - desc: "With directory", - opts: Options{ - Root: filepath.Join(_testWD, "testdata"), - Excludes: []string{ - "memfs_generate.go$", - "direct$", - }, - }, - expMapKeys: []string{ - "/", - "/exclude", - "/exclude/index.css", - "/exclude/index.html", - "/exclude/index.js", - "/include", - "/include/index.css", - "/include/index.html", - "/include/index.js", - "/index.css", - "/index.html", - "/index.js", - "/plain", - }, - }, { - desc: "With excludes", - opts: Options{ - Root: filepath.Join(_testWD, "testdata"), - Excludes: []string{ - `.*\.js$`, - "memfs_generate.go$", - "direct$", - }, - }, - expMapKeys: []string{ - "/", - "/exclude", - "/exclude/index.css", - "/exclude/index.html", - "/include", - "/include/index.css", - "/include/index.html", - "/index.css", - "/index.html", - "/plain", - }, - }, { - desc: "With includes", - opts: Options{ - Root: filepath.Join(_testWD, "testdata"), - Includes: []string{ - `.*\.js$`, - }, - Excludes: []string{ - "memfs_generate.go$", - "direct$", - }, - }, - expMapKeys: []string{ - "/", - "/exclude", - "/exclude/index.js", - "/include", - "/include/index.js", - "/index.js", - }, - }} - - for _, c := range cases { - t.Log(c.desc) - - mfs, err := New(&c.opts) - if err != nil { - test.Assert(t, "error", c.expErr, err.Error()) - continue - } - - gotListNames := mfs.ListNames() - test.Assert(t, "names", c.expMapKeys, gotListNames) - } -} - -func TestFilter(t *testing.T) { +func TestMemFS_isIncluded(t *testing.T) { cases := []struct { desc string inc []string @@ -447,38 +478,6 @@ func TestFilter(t *testing.T) { } } -func TestMain(m *testing.M) { - var err error - _testWD, err = os.Getwd() - if err != nil { - log.Fatal(err) - } - - err = os.MkdirAll(filepath.Join(_testWD, "testdata/exclude/dir"), 0700) - if err != nil { - perr, ok := err.(*os.PathError) - if !ok { - log.Fatal("!ok:", err) - } - if perr.Err != os.ErrExist { - log.Fatalf("perr: %+v %+v\n", perr.Err, os.ErrExist) - } - } - - err = os.MkdirAll(filepath.Join(_testWD, "testdata/include/dir"), 0700) - if err != nil { - perr, ok := err.(*os.PathError) - if !ok { - log.Fatal(err) - } - if perr.Err != os.ErrExist { - log.Fatal(err) - } - } - - os.Exit(m.Run()) -} - func TestMerge(t *testing.T) { optsDirect := &Options{ Root: "testdata/direct", |
