Optimize model naming (#910)
* add unit test * fix #907 * format code * format code * format code Co-authored-by: anqiansong <anqiansong@xiaoheiban.cn>master
parent
a1fe8bf6cd
commit
b2fea65faa
@ -0,0 +1,96 @@
|
||||
package env
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/vars"
|
||||
)
|
||||
|
||||
func TestLookUpGo(t *testing.T) {
|
||||
xGo, err := LookUpGo()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
assert.True(t, util.FileExists(xGo))
|
||||
output, errOutput, err := execCommand(xGo, "version")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if len(errOutput) > 0 {
|
||||
return
|
||||
}
|
||||
assert.Equal(t, wrapVersion(), output)
|
||||
}
|
||||
|
||||
func TestLookUpProtoc(t *testing.T) {
|
||||
xProtoc, err := LookUpProtoc()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
assert.True(t, util.FileExists(xProtoc))
|
||||
output, errOutput, err := execCommand(xProtoc, "--version")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if len(errOutput) > 0 {
|
||||
return
|
||||
}
|
||||
assert.True(t, len(output) > 0)
|
||||
}
|
||||
|
||||
func TestLookUpProtocGenGo(t *testing.T) {
|
||||
xProtocGenGo, err := LookUpProtocGenGo()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
assert.True(t, util.FileExists(xProtocGenGo))
|
||||
}
|
||||
|
||||
func TestLookPath(t *testing.T) {
|
||||
xGo, err := LookPath("go")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
assert.True(t, util.FileExists(xGo))
|
||||
}
|
||||
|
||||
func TestCanExec(t *testing.T) {
|
||||
canExec := runtime.GOOS != vars.OsJs && runtime.GOOS != vars.OsIOS
|
||||
assert.Equal(t, canExec, CanExec())
|
||||
}
|
||||
|
||||
func execCommand(cmd string, arg ...string) (stdout string, stderr string, err error) {
|
||||
output := bytes.NewBuffer(nil)
|
||||
errOutput := bytes.NewBuffer(nil)
|
||||
c := exec.Command(cmd, arg...)
|
||||
c.Stdout = output
|
||||
c.Stderr = errOutput
|
||||
err = c.Run()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if errOutput.Len() > 0 {
|
||||
stderr = errOutput.String()
|
||||
return
|
||||
}
|
||||
stdout = strings.TrimSpace(output.String())
|
||||
return
|
||||
}
|
||||
|
||||
func wrapVersion() string {
|
||||
version := runtime.Version()
|
||||
os := runtime.GOOS
|
||||
arch := runtime.GOARCH
|
||||
return fmt.Sprintf("go version %s %s/%s", version, os, arch)
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type data struct {
|
||||
input string
|
||||
expected string
|
||||
}
|
||||
|
||||
func TestTitle(t *testing.T) {
|
||||
list := []*data{
|
||||
{input: "_", expected: "_"},
|
||||
{input: "abc", expected: "Abc"},
|
||||
{input: "ABC", expected: "ABC"},
|
||||
{input: "", expected: ""},
|
||||
{input: " abc", expected: " abc"},
|
||||
}
|
||||
for _, e := range list {
|
||||
assert.Equal(t, e.expected, Title(e.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestUntitle(t *testing.T) {
|
||||
list := []*data{
|
||||
{input: "_", expected: "_"},
|
||||
{input: "Abc", expected: "abc"},
|
||||
{input: "ABC", expected: "aBC"},
|
||||
{input: "", expected: ""},
|
||||
{input: " abc", expected: " abc"},
|
||||
}
|
||||
|
||||
for _, e := range list {
|
||||
assert.Equal(t, e.expected, Untitle(e.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIndex(t *testing.T) {
|
||||
list := []string{"a", "b", "c"}
|
||||
assert.Equal(t, 1, Index(list, "b"))
|
||||
assert.Equal(t, -1, Index(list, "d"))
|
||||
}
|
||||
|
||||
func TestSafeString(t *testing.T) {
|
||||
list := []*data{
|
||||
{input: "_", expected: "_"},
|
||||
{input: "a-b-c", expected: "a_b_c"},
|
||||
{input: "123abc", expected: "_123abc"},
|
||||
{input: "汉abc", expected: "_abc"},
|
||||
{input: "汉a字", expected: "_a_"},
|
||||
{input: "キャラクターabc", expected: "______abc"},
|
||||
{input: "-a_B-C", expected: "_a_B_C"},
|
||||
{input: "a_B C", expected: "a_B_C"},
|
||||
{input: "A#B#C", expected: "A_B_C"},
|
||||
{input: "_123", expected: "_123"},
|
||||
{input: "", expected: ""},
|
||||
{input: "\t", expected: "_"},
|
||||
{input: "\n", expected: "_"},
|
||||
}
|
||||
for _, e := range list {
|
||||
assert.Equal(t, e.expected, SafeString(e.input))
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue