1 Star 0 Fork 0

systechn/gopher-lua

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
testutils_test.go 2.06 KB
一键复制 编辑 原始数据 按行查看 历史
yuin 提交于 2015-04-30 20:58 . add auxlib tests
package lua
import (
"fmt"
"path/filepath"
"regexp"
"runtime"
"testing"
)
func positionString(level int) string {
_, file, line, _ := runtime.Caller(level + 1)
return fmt.Sprintf("%v:%v:", filepath.Base(file), line)
}
func errorIfNotEqual(t *testing.T, v1, v2 interface{}) {
if v1 != v2 {
t.Errorf("%v '%v' expected, but got '%v'", positionString(1), v1, v2)
}
}
func errorIfFalse(t *testing.T, cond bool, msg string, args ...interface{}) {
if !cond {
if len(args) > 0 {
t.Errorf("%v %v", positionString(1), fmt.Sprintf(msg, args...))
} else {
t.Errorf("%v %v", positionString(1), msg)
}
}
}
func errorIfNotNil(t *testing.T, v1 interface{}) {
if fmt.Sprint(v1) != "<nil>" {
t.Errorf("%v nil expected, but got '%v'", positionString(1), v1)
}
}
func errorIfNil(t *testing.T, v1 interface{}) {
if fmt.Sprint(v1) == "<nil>" {
t.Errorf("%v non-nil value expected, but got nil", positionString(1))
}
}
func errorIfScriptFail(t *testing.T, L *LState, script string) {
if err := L.DoString(script); err != nil {
t.Errorf("%v %v", positionString(1), err.Error())
}
}
func errorIfGFuncFail(t *testing.T, L *LState, f LGFunction) {
if err := L.GPCall(f, LNil); err != nil {
t.Errorf("%v %v", positionString(1), err.Error())
}
}
func errorIfScriptNotFail(t *testing.T, L *LState, script string, pattern string) {
if err := L.DoString(script); err != nil {
reg := regexp.MustCompile(pattern)
if len(reg.FindStringIndex(err.Error())) == 0 {
t.Errorf("%v error message '%v' does not contains given pattern string '%v'.", positionString(1), err.Error(), pattern)
return
}
return
}
t.Errorf("%v script should fail", positionString(1))
}
func errorIfGFuncNotFail(t *testing.T, L *LState, f LGFunction, pattern string) {
if err := L.GPCall(f, LNil); err != nil {
reg := regexp.MustCompile(pattern)
if len(reg.FindStringIndex(err.Error())) == 0 {
t.Errorf("%v error message '%v' does not contains given pattern string '%v'.", positionString(1), err.Error(), pattern)
return
}
return
}
t.Errorf("%v LGFunction should fail", positionString(1))
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/systechn/gopher-lua.git
git@gitee.com:systechn/gopher-lua.git
systechn
gopher-lua
gopher-lua
master

搜索帮助