11 Star 11 Fork 0

Gitee 极速下载/goa

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/goadesign/goa
克隆/下载
testing.go 2.23 KB
一键复制 编辑 原始数据 按行查看 历史
Nitin 提交于 2019-04-05 12:10 . Support custom errors gRPC (#2071)
package expr
import (
"io/ioutil"
"os"
"os/exec"
"strings"
"testing"
"github.com/sergi/go-diff/diffmatchpatch"
"goa.design/goa/eval"
)
// RunDSL returns the DSL root resulting from running the given DSL.
// Used only in tests.
func RunDSL(t *testing.T, dsl func()) *RootExpr {
setupDSLRun()
// run DSL (first pass)
if !eval.Execute(dsl, nil) {
t.Fatal(eval.Context.Error())
}
// run DSL (second pass)
if err := eval.RunDSL(); err != nil {
t.Fatal(err)
}
// return generated root
return Root
}
// RunInvalidDSL returns the error resulting from running the given DSL.
// It is used only in tests.
func RunInvalidDSL(t *testing.T, dsl func()) error {
setupDSLRun()
// run DSL (first pass)
if !eval.Execute(dsl, nil) {
return eval.Context.Errors
}
// run DSL (second pass)
if err := eval.RunDSL(); err != nil {
return err
}
// expected an error - didn't get one
t.Fatal("expected a DSL evaluation error - got none")
return nil
}
// Diff returns a diff between s1 and s2. It uses the diff tool if installed
// otherwise degrades to using the dmp package.
func Diff(t *testing.T, s1, s2 string) string {
_, err := exec.LookPath("diff")
supportsDiff := (err == nil)
if !supportsDiff {
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(s1, s2, false)
return dmp.DiffPrettyText(diffs)
}
left := CreateTempFile(t, s1)
right := CreateTempFile(t, s2)
defer os.Remove(left)
defer os.Remove(right)
cmd := exec.Command("diff", left, right)
diffb, _ := cmd.CombinedOutput()
return strings.Replace(string(diffb), "\t", " ␉ ", -1)
}
// CreateTempFile creates a temporary file and writes the given content.
// It is used only for testing.
func CreateTempFile(t *testing.T, content string) string {
f, err := ioutil.TempFile("", "")
if err != nil {
t.Fatal(err)
}
_, err = f.WriteString(content)
if err != nil {
os.Remove(f.Name())
t.Fatal(err)
}
if err := f.Close(); err != nil {
t.Fatal(err)
}
return f.Name()
}
func setupDSLRun() {
// reset all roots and codegen data structures
eval.Reset()
Root = new(RootExpr)
Root.GeneratedTypes = &GeneratedRoot{}
eval.Register(Root)
eval.Register(Root.GeneratedTypes)
Root.API = NewAPIExpr("test api", func() {})
Root.API.Servers = []*ServerExpr{Root.API.DefaultServer()}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors/goa.git
git@gitee.com:mirrors/goa.git
mirrors
goa
goa
v2.0.0

搜索帮助