diff options
| author | Shulhan <ms@kilabit.info> | 2022-07-23 16:33:14 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2022-07-23 16:33:14 +0700 |
| commit | c80513b67f83f40f243c46c500d67a81f5b96df2 (patch) | |
| tree | ddf5701b9c82fd0abbb54c2b47bc58a5b7915382 | |
| parent | f6eeae8e1a4ae18c2e825db076b74531b4a343b6 (diff) | |
| download | pakakeh.go-c80513b67f83f40f243c46c500d67a81f5b96df2.tar.xz | |
lib/ini: convert the Get test using test.Data
| -rw-r--r-- | lib/ini/ini_test.go | 347 | ||||
| -rw-r--r-- | lib/ini/testdata/get.txt | 26 | ||||
| -rw-r--r-- | lib/ini/testdata/get_complex.txt | 235 |
3 files changed, 279 insertions, 329 deletions
diff --git a/lib/ini/ini_test.go b/lib/ini/ini_test.go index f404f136..639b97be 100644 --- a/lib/ini/ini_test.go +++ b/lib/ini/ini_test.go @@ -6,11 +6,9 @@ package ini import ( "bytes" - "fmt" "testing" "time" - "github.com/shuLhan/share/lib/debug" "github.com/shuLhan/share/lib/test" ) @@ -234,336 +232,53 @@ func TestAddSection(t *testing.T) { } } -func TestGet(t *testing.T) { +func TestIni_Get(t *testing.T) { var ( - err error - got string - ok bool + cfg *Ini + listTData []*test.Data + tdata *test.Data + got string + def string + tags []string + keys [][]byte + exps [][]byte + key []byte + err error + x int ) - inputIni, err := Open(testdataInputIni) + listTData, err = test.LoadDataDir("testdata/") if err != nil { t.Fatal(err) } - cases := []struct { - desc string - sec string - sub string - key string - expVal string - expOk bool - }{{ - desc: `With empty section`, - sub: "devel", - key: "remote", - }, { - desc: `With empty subsection`, - sec: "user", - key: "name", - expVal: "Shulhan", - expOk: true, - }, { - desc: `With empty key`, - sec: "user", - }, { - desc: `With invalid section`, - sec: "sectionnotexist", - key: "name", - }, { - desc: `With invalid subsection`, - sec: "branch", - sub: "notexist", - key: "remote", - }, { - desc: `With invalid key`, - sec: "branch", - sub: "devel", - key: "user", - }, { - desc: `With empty value`, - sec: "http", - key: "sslVerify", - }} - - for _, c := range cases { - t.Logf("%+v", c) - - got, ok = inputIni.Get(c.sec, c.sub, c.key, "") - if !ok { - test.Assert(t, "ok", c.expOk, ok) - continue + for _, tdata = range listTData { + cfg, err = Parse(tdata.Input["default"]) + if err != nil { + t.Fatal(err) } - test.Assert(t, "value", c.expVal, got) - } -} - -func TestGetInputIni(t *testing.T) { - inputIni, err := Open(testdataInputIni) - if err != nil { - t.Fatal(err) - } - - cases := []struct { - sec string - sub string - keys []string - expVals []string - }{{ - sec: "core", - keys: []string{ - "filemode", - "gitProxy", - "pager", - "editor", - "autocrlf", - }, - expVals: []string{ - "true", - "default-proxy", - "less -R", - "nvim", - "false", - }, - }, { - sec: "diff", - keys: []string{ - "external", - "renames", - }, - expVals: []string{ - "/usr/local/bin/diff-wrapper", - "true", - }, - }, { - sec: "user", - keys: []string{ - "name", - "email", - }, - expVals: []string{ - "Shulhan", - "ms@kilabit.info", - }, - }, { - sec: "http", - keys: []string{ - "sslVerify", - "cookiefile", - }, - expVals: []string{ - "", - "/home/ms/.gitcookies", - }, - }, { - sec: "http", - sub: "https://weak.example.com", - keys: []string{ - "sslVerify", - "cookiefile", - }, - expVals: []string{ - "false", - "/tmp/cookie.txt", - }, - }, { - sec: "branch", - sub: "devel", - keys: []string{ - "remote", - "merge", - }, - expVals: []string{ - "origin", - "refs/heads/devel", - }, - }, { - sec: "include", - keys: []string{ - "path", - }, - expVals: []string{ - "~/foo.inc", - }, - }, { - sec: "includeIf", - sub: "gitdir:/path/to/foo/.git", - keys: []string{ - "path", - }, - expVals: []string{ - "/path/to/foo.inc", - }, - }, { - sec: "includeIf", - sub: "gitdir:/path/to/group/", - keys: []string{ - "path", - }, - expVals: []string{ - "foo.inc", - }, - }, { - sec: "includeIf", - sub: "gitdir:~/to/group/", - keys: []string{ - "path", - }, - expVals: []string{ - "/path/to/foo.inc", - }, - }, { - sec: "color", - keys: []string{"ui"}, - expVals: []string{"true"}, - }, { - sec: "gui", - keys: []string{ - "fontui", - "fontdiff", - "diffcontext", - "spellingdictionary", - }, - expVals: []string{ - "-family \"xos4 Terminus\" -size 10 -weight normal -slant roman -underline 0 -overstrike 0", - "-family \"xos4 Terminus\" -size 10 -weight normal -slant roman -underline 0 -overstrike 0", - "4", - "none", - }, - }, { - sec: "svn", - keys: []string{ - "rmdir", - }, - expVals: []string{ - "true", - }, - }, { - sec: "alias", - keys: []string{ - "change", - "gofmt", - "mail", - "pending", - "submit", - "sync", - "tree", - "to-master", - "to-prod", - }, - expVals: []string{ - "codereview change", - "codereview gofmt", - "codereview mail", - "codereview pending", - "codereview submit", - "codereview sync", - `!git --no-pager log --graph --date=format:'%Y-%m-%d' --pretty=format:'%C(auto,dim)%ad %<(7,trunc) %an %Creset%m %h %s %Cgreen%d%Creset' --exclude=*/production --exclude=*/dev-* --all -n 20`, - `!git stash -u && git fetch origin && git rebase origin/master && git stash pop && git --no-pager log --graph --decorate --pretty=oneline --abbrev-commit origin/master~1..HEAD`, - `!git stash -u && git fetch origin && git rebase origin/production && git stash pop && git --no-pager log --graph --decorate --pretty=oneline --abbrev-commit origin/production~1..HEAD`, - }, - }, { - sec: "url", - sub: "git@github.com:", - keys: []string{ - "insteadOf", - }, - expVals: []string{ - "https://github.com/", - }, - }, { - sec: "last", - keys: []string{ - "valid0", - "valid1", - "valid2", - "valid3", - "valid4", - }, - expVals: []string{ - "", - "", - "", - "", - "", - }, - }} - - var ( - got string - ok bool - ) + keys = bytes.Split(tdata.Input["keys"], []byte("\n")) + exps = bytes.Split(tdata.Output["default"], []byte("\n")) - for _, c := range cases { - t.Log(c) - - if debug.Value >= 3 { - t.Logf("Section header: [%s %s]", c.sec, c.sub) - t.Logf(">>> keys: %s", c.keys) - t.Logf(">>> expVals: %s", c.expVals) + if len(keys) != len(exps) { + t.Fatalf("%s: input keys length %d does not match with output %d", + tdata.Name, len(keys), len(exps)) } - for x, k := range c.keys { - t.Log(" Get:", k) - - got, ok = inputIni.Get(c.sec, c.sub, k, "") - if !ok { - t.Logf("Get: %s > %s > %s", c.sec, c.sub, k) - test.Assert(t, "ok", true, ok) - t.FailNow() + for x, key = range keys { + if len(key) == 0 { + test.Assert(t, "Get", string(exps[x]), "") + continue } - test.Assert(t, "value", c.expVals[x], got) - } - } -} - -func TestIni_Get(t *testing.T) { - var ( - cfg *Ini - tdata *test.Data - got string - def string - tags []string - keys [][]byte - exps [][]byte - key []byte - err error - x int - ok bool - ) - - tdata, err = test.LoadData("testdata/get.txt") - if err != nil { - t.Fatal(err) - } - - cfg, err = Parse(tdata.Input["default"]) - if err != nil { - t.Fatal(err) - } - - keys = bytes.Split(tdata.Input["keys"], []byte("\n")) - exps = bytes.Split(tdata.Output["default"], []byte("\n")) + tags = parseTag(string(key)) + def = tags[3] - if len(keys) != len(exps) { - t.Fatalf("%s: input keys length %d does not match with output %d", - tdata.Name, len(keys), len(exps)) - } + got, _ = cfg.Get(tags[0], tags[1], tags[2], def) + got = got + "." - for x, key = range keys { - if len(key) == 0 { - test.Assert(t, "Get", string(exps[x]), "") - continue + test.Assert(t, "Get", string(exps[x]), got) } - - tags = parseTag(string(key)) - def = tags[3] - - got, ok = cfg.Get(tags[0], tags[1], tags[2], def) - got = fmt.Sprintf("%t %s.", ok, got) - - test.Assert(t, "Get", string(exps[x]), got) } } diff --git a/lib/ini/testdata/get.txt b/lib/ini/testdata/get.txt index db74f8ba..f8613473 100644 --- a/lib/ini/testdata/get.txt +++ b/lib/ini/testdata/get.txt @@ -39,19 +39,19 @@ sec3::test sec4::test <<< -false . +. -true 2. -true old value. -true new value. -false . -false default value. +2. +old value. +new value. +. +default value. -true !git --no-pager log --graph . -false . -false . -true . +!git --no-pager log --graph . +. +. +. -true !git --no-pager log --graph [sec4]. -true . -false . +!git --no-pager log --graph [sec4]. +. +. diff --git a/lib/ini/testdata/get_complex.txt b/lib/ini/testdata/get_complex.txt new file mode 100644 index 00000000..60929cfe --- /dev/null +++ b/lib/ini/testdata/get_complex.txt @@ -0,0 +1,235 @@ +kind: get + +Complex ini content. + +>>> +# +# This is the config file, and +# a '#' or ';' character indicates +# a comment +# + +; core variables +[core] + ; Don't trust file modes + filemode = false + +; Our diff algorithm +[diff] + external = /usr/local/bin/diff-wrapper + renames = true + +; Proxy settings +[core] + gitProxy="ssh" for "kernel.org" + gitProxy=default-proxy ; for the rest + +; User settings +[user] + name = ms + email = ms@localhost.localdomain + +; HTTP +[http] + sslVerify +[http "https://weak.example.com"] + sslVerify = false + cookieFile = /tmp/cookie.txt + +[branch "devel"] + remote = origin + merge = refs/heads/devel + +[include] + path = /path/to/foo.inc ; include by absolute path + path = foo.inc ; find "foo.inc" relative to the current file + path = ~/foo.inc ; find "foo.inc" in your `$HOME` directory + +; include if $GIT_DIR is /path/to/foo/.git +[includeIf "gitdir:/path/to/foo/.git"] + path = /path/to/foo.inc + +; include for all repositories inside /path/to/group +[includeIf "gitdir:/path/to/group/"] + path = /path/to/foo.inc + +; include for all repositories inside $HOME/to/group +[includeIf "gitdir:~/to/group/"] + path = /path/to/foo.inc + +; relative paths are always relative to the including +; file (if the condition is true); their location is not +; affected by the condition +[includeIf "gitdir:/path/to/group/"] + path = foo.inc + +[color] + ui = true +[core] + pager = less -R + editor = nvim + autocrlf = false + filemode = true +[gui] + fontui = -family \"xos4 Terminus\" -size 10 -weight normal -slant roman -underline 0 -overstrike 0 + fontdiff = -family \"xos4 Terminus\" -size 10 -weight normal -slant roman -underline 0 -overstrike 0 + diffcontext = 4 + spellingdictionary = none +[svn] + rmdir = true +[http] + cookiefile = /home/ms/.gitcookies + +[alias] + change = codereview change + gofmt = codereview gofmt + mail = codereview mail + pending = codereview pending + submit = codereview submit + sync = codereview sync + tree = !git --no-pager log --graph \ + --date=format:'%Y-%m-%d' \ + --pretty=format:'%C(auto,dim)%ad %<(7,trunc) %an %Creset%m %h %s %Cgreen%d%Creset' \ + --exclude="*/production" \ + --exclude="*/dev-*" \ + --all -n 20 + + to-master = !git stash -u \ + && git fetch origin \ + && git rebase origin/master \ + && git stash pop \ + && git --no-pager log --graph --decorate --pretty=oneline \ + --abbrev-commit origin/master~1..HEAD + + to-prod = !git stash -u \ + && git fetch origin \ + && git rebase origin/production \ + && git stash pop \ + && git --no-pager log --graph --decorate --pretty=oneline \ + --abbrev-commit origin/production~1..HEAD +[user] + name = Shulhan + email = ms@kilabit.info + +[url "git@github.com:"] # Replace HTTP URL with git+ssh + insteadOf = https://github.com/ + +[last] + valid0 + valid1 = + valid2 = # comment + valid3 = \ + ; comment + valid4 = + +>>> keys +core::filemode +core::gitProxy +core::pager +core::editor +core::autocrlf + +diff::external +diff::renames + +user +user::name +user::email + +http::sslVerify +http::cookiefile + +http:https\://weak.example.com:sslVerify +http:https\://weak.example.com:cookiefile + +branch:devel:remote +:devel:remote +branch:devel:merge + +include::path +includeIf:gitdir\:/path/to/foo/.git:path +includeIf:gitdir\:/path/to/group/:path +includeIf:gitdir\:~/to/group/:path + +color::ui + +gui::fontui +gui::fontdiff +gui::diffcontext +gui::spellingdictionary + +svn::rmdir + +alias::change +alias::gofmt +alias::mail +alias::pending +alias::submit +alias::sync +alias::tree +alias::to-master +alias::to-prod + +url:git@github.com\::insteadOf + +last::valid0 +last::valid1 +last::valid2 +last::valid3 +last::valid4 + +<<< +true. +default-proxy. +less -R. +nvim. +false. + +/usr/local/bin/diff-wrapper. +true. + +. +Shulhan. +ms@kilabit.info. + +. +/home/ms/.gitcookies. + +false. +/tmp/cookie.txt. + +origin. +. +refs/heads/devel. + +~/foo.inc. +/path/to/foo.inc. +foo.inc. +/path/to/foo.inc. + +true. + +-family "xos4 Terminus" -size 10 -weight normal -slant roman -underline 0 -overstrike 0. +-family "xos4 Terminus" -size 10 -weight normal -slant roman -underline 0 -overstrike 0. +4. +none. + +true. + +codereview change. +codereview gofmt. +codereview mail. +codereview pending. +codereview submit. +codereview sync. +!git --no-pager log --graph --date=format:'%Y-%m-%d' --pretty=format:'%C(auto,dim)%ad %<(7,trunc) %an %Creset%m %h %s %Cgreen%d%Creset' --exclude=*/production --exclude=*/dev-* --all -n 20. +!git stash -u && git fetch origin && git rebase origin/master && git stash pop && git --no-pager log --graph --decorate --pretty=oneline --abbrev-commit origin/master~1..HEAD. +!git stash -u && git fetch origin && git rebase origin/production && git stash pop && git --no-pager log --graph --decorate --pretty=oneline --abbrev-commit origin/production~1..HEAD. + +https://github.com/. + +. +. +. +. +. |
