提交 cc7d585b 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

fix sonar issues

上级 6e2c910c
......@@ -181,7 +181,7 @@ func ValidateStepResult(langType string, expectLines []string, actualLines []str
pass = MatchScene(expect[1:len(expect)-1], log, langType)
} else if len(expect) >= 2 && expect[:1] == "`" && expect[len(expect)-1:] == "`" {
expect = expect[1 : len(expect)-1]
pass = MatchString(expect, log, langType)
pass = Match(expect, log)
} else {
pass = strings.TrimSpace(log) == strings.TrimSpace(expect)
}
......@@ -200,33 +200,41 @@ func ValidateStepResult(langType string, expectLines []string, actualLines []str
}
func MatchString(expect string, actual string, langType string) bool {
expect = strings.TrimSpace(expect)
actual = strings.TrimSpace(actual)
expect = strings.Replace(expect, "%s", `.+?`, -1) // 字符串
expect = strings.Replace(expect, "%i", `[+\-]?[0-9]+`, -1) // 十进制数字,可有符号
expect = strings.Replace(expect, "%d", `[0-9]+`, -1) // 十进制数字,无符号
expect = strings.Replace(expect, "%x", `(0X|0x)?[0-9a-fA-F]+`, -1) // 十六进制数字
expect = strings.Replace(expect, "%f", `[+\-]?\.?[0-9]+\.?[0-9]*(E-?[0-9]+)?`, -1) // 十进制浮点数
expect = strings.Replace(expect, "%c", ".", -1) // 单个字符
pass, _ := regexp.MatchString(expect, actual)
return pass
}
func MatchScene(expect, actual, langType string) (pass bool) {
expect = strings.TrimSpace(expect)
actual = strings.TrimSpace(actual)
if len(expect) == 0 {
return actual == ""
pass = actual == ""
return
}
if len(expect) <= 2 {
return
}
if len(expect) > 2 {
// len(expect) > 2
scene := expect[:2]
expect = strings.TrimSpace(expect[2:])
switch scene {
case "f:":
return Contain(expect, actual, langType)
case "m:":
return Match(expect, actual)
case "c:":
return Compare(expect, actual)
case "l:":
return Logic(expect, actual, langType)
}
return
}
func Contain(expect, actual string, langType string) bool {
if strings.Contains(expect, "*") {
expectArr := strings.Split(expect, "*")
repeatCount, _ := strconv.Atoi(string(expectArr[1]))
......@@ -235,10 +243,26 @@ func MatchScene(expect, actual, langType string) (pass bool) {
if expect[0:1] == "(" && expect[len(expect)-1:] == ")" && strings.Contains(expect, ",") {
expect = fmt.Sprintf("^%s{1}$", strings.ReplaceAll(expect, ",", "|"))
}
return MatchString(expect, actual, langType)
case "m:":
return MatchString(expect, actual, langType)
case "c:":
return Match(expect, actual)
}
func Match(expect string, actual string) bool {
expect = strings.TrimSpace(expect)
actual = strings.TrimSpace(actual)
expect = strings.Replace(expect, "%s", `.+?`, -1) // 字符串
expect = strings.Replace(expect, "%i", `[+\-]?[0-9]+`, -1) // 十进制数字,可有符号
expect = strings.Replace(expect, "%d", `[0-9]+`, -1) // 十进制数字,无符号
expect = strings.Replace(expect, "%x", `(0X|0x)?[0-9a-fA-F]+`, -1) // 十六进制数字
expect = strings.Replace(expect, "%f", `[+\-]?\.?[0-9]+\.?[0-9]*(E-?[0-9]+)?`, -1) // 十进制浮点数
expect = strings.Replace(expect, "%c", ".", -1) // 单个字符
pass, _ := regexp.MatchString(expect, actual)
return pass
}
func Compare(expect string, actual string) bool {
if len(expect) > 2 && (expect[:2] == ">=" || expect[:2] == "<=" || expect[:2] == "<>" || expect[:2] == "!=") {
character := expect[:2]
expectFloot, err := strconv.ParseFloat(strings.TrimSpace(expect[2:]), 64)
......@@ -259,7 +283,11 @@ func MatchScene(expect, actual, langType string) (pass bool) {
case "!=":
return actualFloot != expectFloot
}
} else if strings.Contains(expect, "-") && strings.Count(expect, "-") == 1 {
return false
}
if strings.Contains(expect, "-") && strings.Count(expect, "-") == 1 {
rangeArr := strings.Split(expect, "-")
rangeFrom, err := strconv.ParseFloat(strings.TrimSpace(rangeArr[0]), 64)
if err != nil {
......@@ -274,7 +302,8 @@ func MatchScene(expect, actual, langType string) (pass bool) {
return false
}
return actualFloot >= rangeFrom && actualFloot <= rangeTo
} else {
}
character := expect[:1]
expectFloot, err := strconv.ParseFloat(strings.TrimSpace(expect[1:]), 64)
if err != nil {
......@@ -298,8 +327,11 @@ func MatchScene(expect, actual, langType string) (pass bool) {
expectMax, _ := strconv.ParseFloat(strings.TrimSpace(expectArr[1]), 64)
return actualFloot >= expectMin && actualFloot <= expectMax
}
}
case "l:":
return false
}
func Logic(expect, actual, langType string) bool {
openParenthesisCount, closeParenthesisCount := 0, 0
hasLogicCharacter := false
for index, v := range expect {
......@@ -331,8 +363,6 @@ func MatchScene(expect, actual, langType string) (pass bool) {
} else {
return MatchScene("l:"+expect, actual, langType)
}
}
}
return pass
return false
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册