Skip to content

Commit

Permalink
separete viml helper funcs to vimlfunc.go and add test to improve
Browse files Browse the repository at this point in the history
coverage
  • Loading branch information
haya14busa committed Sep 29, 2016
1 parent a2767dd commit deedba1
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 40 deletions.
4 changes: 0 additions & 4 deletions go/stacktrace/histerrs.go
Expand Up @@ -134,10 +134,6 @@ func Histerrs(msghist string) []*Error {
return errors
}

var inputlist = func(cli *Vim, candidates []string) (int, error) {
return cli.callintfunc("inputlist", candidates)
}

// Fromhist returns selected stacktrace from errors in message history.
//
// vimdoc:func:
Expand Down
36 changes: 0 additions & 36 deletions go/stacktrace/stacktrace.go
Expand Up @@ -66,42 +66,6 @@ type Vim struct {
c *vim.Client
}

// func (cli *Vim) debug(msg interface{}) {
// cli.c.Ex("echom " + strconv.Quote(fmt.Sprintf("%+#v", msg)))
// }

func (cli *Vim) sfile() (string, error) {
return cli.callstrfunc("expand", "<sfile>")
}

func (cli *Vim) function(funcname string) (string, error) {
return cli.callstrfunc("execute", fmt.Sprintf(":verbose function %v", funcname))
}

func (cli *Vim) callstrfunc(f string, args ...interface{}) (string, error) {
ret, err := cli.c.Call(f, args...)
if err != nil {
return "", err
}
s, ok := ret.(string)
if ok {
return s, nil
}
return "", fmt.Errorf("%v(%v) is not string: %v", f, args, ret)
}

func (cli *Vim) callintfunc(f string, args ...interface{}) (int, error) {
ret, err := cli.c.Call(f, args...)
if err != nil {
return 0, err
}
s, ok := ret.(float64)
if ok {
return int(s), nil
}
return 0, fmt.Errorf("%v(%v) is not float64: %v", f, args, ret)
}

// Callstack returns current callstack.
//
// vimdoc:func:
Expand Down
43 changes: 43 additions & 0 deletions go/stacktrace/vimlfunc.go
@@ -0,0 +1,43 @@
package stacktrace

import "fmt"

// func (cli *Vim) debug(msg interface{}) {
// cli.c.Ex("echom " + strconv.Quote(fmt.Sprintf("%+#v", msg)))
// }

var inputlist = func(cli *Vim, candidates []string) (int, error) {
return cli.callintfunc("inputlist", candidates)
}

func (cli *Vim) sfile() (string, error) {
return cli.callstrfunc("expand", "<sfile>")
}

func (cli *Vim) function(funcname string) (string, error) {
return cli.callstrfunc("execute", fmt.Sprintf(":verbose function %v", funcname))
}

func (cli *Vim) callstrfunc(f string, args ...interface{}) (string, error) {
ret, err := cli.c.Call(f, args...)
if err != nil {
return "", err
}
s, ok := ret.(string)
if ok {
return s, nil
}
return "", fmt.Errorf("%v(%v) is not string: %v", f, args, ret)
}

func (cli *Vim) callintfunc(f string, args ...interface{}) (int, error) {
ret, err := cli.c.Call(f, args...)
if err != nil {
return 0, err
}
s, ok := ret.(float64)
if ok {
return int(s), nil
}
return 0, fmt.Errorf("%v(%v) is not float64: %v", f, args, ret)
}
76 changes: 76 additions & 0 deletions go/stacktrace/vimlfunc_test.go
@@ -0,0 +1,76 @@
package stacktrace

import "testing"

func TestVimSfile(t *testing.T) {
v := &Vim{c: cli}
got, err := v.sfile()
if err == nil {
t.Errorf("Vim.sfile() returns %v, want error", got)
}
}

func TestVimfunction(t *testing.T) {
v := &Vim{c: cli}
{
got, err := v.function("hoge")
if err == nil {
t.Errorf("Vim.function('hoge') returns %v, want error", got)
}
}
{
f := `
function! Hoge()
endfunction
`
v.c.Call("execute", f)

got, err := v.function("Hoge")
if err != nil {
t.Errorf("Vim.function('Hoge') got an error %v", err)
}
if got == "" {
t.Error("Vim.function('Hoge') == ''")
}
}
}

func TestVimCallstrfunc(t *testing.T) {
v := &Vim{c: cli}
{
got, err := v.callstrfunc("substitute", "vim-callstack", "callstack", "stacktrace", "g")
if err != nil {
t.Error(err)
}
if want := "vim-stacktrace"; got != want {
t.Errorf("substitute(...) = %v, want %v", got, want)
}
}

{
_, err := v.callstrfunc("pow", 3, 3)
if err == nil {
t.Error("Vim.callstrfunc('pow', 3, 3) want error, but got nil")
}
}
}

func TestVimCallintfunc(t *testing.T) {
v := &Vim{c: cli}
{
_, err := v.callintfunc("substitute", "vim-callstack", "callstack", "stacktrace", "g")
if err == nil {
t.Error("Vim.callintfunc('substitute', ...) want error, but got nil")
}
}

{
got, err := v.callintfunc("pow", 3, 3)
if err != nil {
t.Error(err)
}
if want := 27; got != want {
t.Error("Vim.callintfunc('pow', 3, 3) = %v, want %v", got, want)
}
}
}

0 comments on commit deedba1

Please sign in to comment.