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

load range and instant resource

上级 89e192e3
......@@ -8,6 +8,7 @@ require (
github.com/360EntSecGroup-Skylar/excelize/v2 v2.2.0
github.com/emirpasic/gods v1.12.0
github.com/fatih/color v1.9.0
github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a
github.com/mattn/go-runewidth v0.0.9
github.com/mattn/go-sqlite3 v2.0.3+incompatible
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5
......
......@@ -6,6 +6,7 @@ import (
"fmt"
"github.com/easysoft/zendata/src/gen"
"github.com/easysoft/zendata/src/model"
constant "github.com/easysoft/zendata/src/utils/const"
logUtils "github.com/easysoft/zendata/src/utils/log"
stringUtils "github.com/easysoft/zendata/src/utils/string"
"github.com/easysoft/zendata/src/utils/vari"
......@@ -19,13 +20,12 @@ import (
func Generate(def string, total int, fieldsToExportStr string, out string, format string, table string) {
startTime := time.Now().Unix()
vari.InputDir = filepath.Dir(def) + string(os.PathSeparator)
constant.Total = total
fieldsToExport := strings.Split(fieldsToExportStr, ",")
rangeClsFields, instClsFields := gen.LoadClsDef(def, fieldsToExport)
rangeClsFields = rangeClsFields
instClsFields = instClsFields
referFieldValueMap := gen.LoadClsDef(def, fieldsToExport)
referFieldValueMap = referFieldValueMap
rows, colTypes := gen.GenerateForDefinition(total, fieldsToExport)
content := Print(rows, format, table, colTypes, fieldsToExport)
......
......@@ -11,20 +11,20 @@ import (
"strings"
)
func LoadClsDef(file string, fieldsToExport []string) []model.FieldValue {
referFieldValues := make([]model.FieldValue, 0)
func LoadClsDef(file string, fieldsToExport []string) map[string]map[string][]string {
referFieldValueMap := map[string]map[string][]string{}
yamlContent, err := ioutil.ReadFile(file)
if err != nil {
logUtils.Screen("fail to read " + file)
return referFieldValues
return referFieldValueMap
}
def := model.DefData{}
err = yaml.Unmarshal(yamlContent, &def)
if err != nil {
logUtils.Screen("fail to parse " + file)
return referFieldValues
return referFieldValueMap
}
constant.RootDef = def
......@@ -32,21 +32,21 @@ func LoadClsDef(file string, fieldsToExport []string) []model.FieldValue {
for _, field := range def.Fields {
if !stringUtils.FindInArr(field.Field, fieldsToExport) { continue }
loadClsField(&field, &referFieldValues)
loadClsField(&field, &referFieldValueMap)
}
return referFieldValues
return referFieldValueMap
}
func loadClsField(field *model.DefField, referFieldValues *[]model.FieldValue) {
func loadClsField(field *model.DefField, referFieldValueMap *map[string]map[string][]string) {
if len(field.Fields) > 0 {
for _, child := range field.Fields {
loadClsField(&child, referFieldValues)
loadClsField(&child, referFieldValueMap)
}
} else if field.From != "" {
referFile, referType, tableName := getReferProp(field.From)
fieldValue := getReferFieldValue(referFile, referType, tableName)
*referFieldValues = append(*referFieldValues, fieldValue)
values := getReferFieldValue(referFile, referType, tableName)
(*referFieldValueMap)[field.Field] = values
}
}
......@@ -63,6 +63,7 @@ func getReferProp(from string) (string, string, string) {
left = strings.ReplaceAll(left, ".", sep)
referFile = left + ".yaml"
referType = "yaml"
} else { // excel, system.address.china
index = strings.LastIndex(from, ".")
......@@ -71,6 +72,7 @@ func getReferProp(from string) (string, string, string) {
referFile = left + ".xlsx"
tableName = from[index:]
referType = "excel"
}
if strings.Index(referFile, "system") > -1 {
......@@ -80,10 +82,80 @@ func getReferProp(from string) (string, string, string) {
return referFile, referType, tableName
}
func getReferFieldValue(referFile string, referType string, tableName string) model.FieldValue {
fieldValue := model.FieldValue{}
func getReferFieldValue(referFile string, referType string, tableName string) map[string][]string {
values := map[string][]string{}
if referType == "yaml" {
values = getReferFieldValueForYaml(referFile)
} else if referType == "excel" {
values = getReferFieldValueForExcel(referFile, tableName)
}
return values
}
func getReferFieldValueForYaml(referFile string) map[string][]string {
valueMap := map[string][]string{}
ranges := model.ClsRanges{}
yamlContent, err := ioutil.ReadFile(referFile)
if err != nil {
logUtils.Screen("fail to read " + referFile)
return valueMap
}
err = yaml.Unmarshal(yamlContent, &ranges)
if err != nil || ranges.Ranges == nil || len(ranges.Ranges) == 0 {
logUtils.Screen("fail to parse ClsRanges " + referFile + ", try to parse as ClsInsts")
insts := model.ClsInsts{}
yamlContent, err := ioutil.ReadFile(referFile)
if err != nil {
logUtils.Screen("fail to read " + referFile)
return valueMap
}
err = yaml.Unmarshal(yamlContent, &insts)
if err != nil {
return valueMap
} else {
valueMap = getReferFieldValueForInstances(insts)
}
} else {
valueMap = getReferFieldValueForRanges(ranges)
}
return valueMap
}
func getReferFieldValueForRanges(ranges model.ClsRanges) map[string][]string {
values := map[string][]string{}
for name, exp := range ranges.Ranges {
// convert ranges refer to standard field
tempField := model.DefField{Field: ranges.Field, Range: exp}
values[name] = GenerateFieldItemsFromDefinition(&tempField, constant.Total)
}
return values
}
func getReferFieldValueForInstances(referFile model.ClsInsts) map[string][]string {
values := map[string][]string{}
return values
}
func getReferFieldValueForExcel(referFile string, tableName string) map[string][]string {
values := map[string][]string{}
return fieldValue
return values
}
\ No newline at end of file
......@@ -57,12 +57,12 @@ func GenerateFieldValuesFromExcel(field *model.DefField, fieldValue *model.Field
continue
}
fieldValue.Values = append(fieldValue.Values, item)
fieldValue.Values["all"] = append(fieldValue.Values["all"], item)
index = index + 1
}
if len(fieldValue.Values) == 0 {
fieldValue.Values = append(fieldValue.Values, "N/A")
fieldValue.Values["all"] = append(fieldValue.Values["all"], "N/A")
}
}
......
......@@ -51,7 +51,7 @@ func GenerateForField(field *model.DefField, total int) []string {
values := make([]string, 0)
if len(field.Fields) > 0 { // sub fields
arr := make([][]string, 0)
arr := make([][]string, 0) // 2 dimension arr for child, [ [a,b,c], [1,2,3] ]
for _, child := range field.Fields {
childValues := GenerateForField(&child, total)
arr = append(arr, childValues)
......@@ -60,7 +60,7 @@ func GenerateForField(field *model.DefField, total int) []string {
for i := 0; i < total; i++ {
concat := ""
for _, row := range arr {
concat = concat + row[i]
concat = concat + row[i] // a1 or b2
}
concat = field.Prefix + concat + field.Postfix
......@@ -132,7 +132,7 @@ func GenerateFieldVal(field model.DefField, fieldValue model.FieldValue, index *
// 叶节点
idx := *index % len(fieldValue.Values)
val := fieldValue.Values[idx]
val := fieldValue.Values["all"][idx]
str = GetFieldValStr(field, val)
return str
......@@ -185,7 +185,7 @@ func LoopSubFields(field *model.DefField, oldValues []string, total int) []strin
fieldValue := model.FieldValue{}
for _, val := range oldValues {
fieldValue.Values = append(fieldValue.Values, val)
fieldValue.Values["all"] = append(fieldValue.Values["all"], val)
}
index := 0
......
......@@ -19,19 +19,14 @@ func GenerateListField(field *model.DefField, fieldValue *model.FieldValue, leve
fieldValue.Precision = field.Precision
if len(field.Fields) > 0 {
GenerateFieldChildren(field, fieldValue, level)
for _, child := range field.Fields {
childValue := model.FieldValue{}
GenerateListField(&child, &childValue, level + 1)
}
} else {
GenerateFieldValues(field, fieldValue, level)
}
}
func GenerateFieldChildren(field *model.DefField, fieldValue *model.FieldValue, level int) {
for _, child := range field.Fields {
childValue := model.FieldValue{}
GenerateListField(&child, &childValue, level + 1)
fieldValue.Children = append(fieldValue.Children, childValue)
}
}
func GenerateFieldValues(field *model.DefField, fieldValue *model.FieldValue, level int) {
if strings.Index(field.Range, ".txt") > -1 {
......@@ -88,12 +83,12 @@ func GenerateFieldValuesFromList(field *model.DefField, fieldValue *model.FieldV
}
}
fieldValue.Values = append(fieldValue.Values, items...)
fieldValue.Values["all"] = append(fieldValue.Values["all"], items...)
index = index + len(items)
}
if len(fieldValue.Values) == 0 {
fieldValue.Values = append(fieldValue.Values, "N/A")
fieldValue.Values["all"] = append(fieldValue.Values["all"], "N/A")
}
}
......
......@@ -31,7 +31,7 @@ func GenerateFieldValuesFromText(field *model.DefField, fieldValue *model.FieldV
if err != nil {
logUtils.Screen("fail to read " + relaPath + ", will return")
fieldValue.Values = append(fieldValue.Values, "N/A")
fieldValue.Values["all"] = append(fieldValue.Values["all"], "N/A")
return
} else {
logUtils.Screen("success to read " + relaPath)
......@@ -65,11 +65,11 @@ func GenerateFieldValuesFromText(field *model.DefField, fieldValue *model.FieldV
if index >= constant.MaxNumb { break }
if strings.TrimSpace(item) == "" { continue }
fieldValue.Values = append(fieldValue.Values, item)
fieldValue.Values["all"] = append(fieldValue.Values["all"], item)
index = index + 1
}
if len(fieldValue.Values) == 0 {
fieldValue.Values = append(fieldValue.Values, "N/A")
fieldValue.Values["all"] = append(fieldValue.Values["all"], "N/A")
}
}
\ No newline at end of file
......@@ -61,6 +61,5 @@ type FieldValue struct {
FieldBase
Field string `yaml:"field"`
Values []interface{}
Children []FieldValue
Values map[string][]interface{}
}
\ No newline at end of file
......@@ -25,6 +25,7 @@ var (
CmdViewHeight = 10
Total = 10
MaxNumb = 100000 // max number in array
RootDef model.DefData = model.DefData{}
......
package scriptUtils
import (
"fmt"
"github.com/easysoft/zendata/src/model"
fileUtils "github.com/easysoft/zendata/src/utils/file"
zentaoUtils "github.com/easysoft/zendata/src/utils/zentao"
"github.com/emirpasic/gods/maps"
"github.com/emirpasic/gods/maps/linkedhashmap"
"regexp"
"strconv"
"strings"
)
func GetStepAndExpectMap(file string) (maps.Map, maps.Map, maps.Map) {
if fileUtils.FileExist(file) {
txt := fileUtils.ReadFile(file)
_, content := zentaoUtils.ReadCaseInfo(txt)
lines := strings.Split(content, "\n")
groupBlockArr := getGroupBlockArr(lines)
groupArr := getStepNestedArr(groupBlockArr)
_, stepMap, stepTypeMap, expectMap := getSortedTextFromNestedSteps(groupArr)
return stepMap, stepTypeMap, expectMap
}
return nil, nil, nil
}
func SortFile(file string) {
stepsTxt := ""
if fileUtils.FileExist(file) {
txt := fileUtils.ReadFile(file)
info, content := zentaoUtils.ReadCaseInfo(txt)
lines := strings.Split(content, "\n")
groupBlockArr := getGroupBlockArr(lines)
groupArr := getStepNestedArr(groupBlockArr)
stepsTxt, _, _, _ = getSortedTextFromNestedSteps(groupArr)
// replace info
re, _ := regexp.Compile(`(?s)\[case\].*\[esac\]`)
script := re.ReplaceAllString(txt, "[case]\n"+info+"\n"+stepsTxt+"\n\n[esac]")
fileUtils.WriteFile(file, script)
}
}
func getGroupBlockArr(lines []string) [][]string {
groupBlockArr := make([][]string, 0)
idx := 0
for true {
if idx >= len(lines) {
break
}
var groupContent []string
line := strings.TrimSpace(lines[idx])
if isGroup(line) { // must match a group
groupContent = make([]string, 0)
groupContent = append(groupContent, line)
idx++
for true {
if idx >= len(lines) {
groupBlockArr = append(groupBlockArr, groupContent)
break
}
line = strings.TrimSpace(lines[idx])
if isGroup(line) {
groupBlockArr = append(groupBlockArr, groupContent)
break
} else if line != "" && !isGroup(line) {
groupContent = append(groupContent, line)
}
idx++
}
} else {
idx++
}
}
return groupBlockArr
}
func getStepNestedArr(blocks [][]string) []model.TestStep {
ret := make([]model.TestStep, 0)
for _, block := range blocks {
name := block[0]
group := model.TestStep{Desc: name}
if isStepsIdent(block[1]) { // muti line
group.MutiLine = true
childs := loadMutiLineSteps(block[1:])
group.Children = append(group.Children, childs...)
} else {
childs := loadSingleLineSteps(block[1:])
group.Children = append(group.Children, childs...)
}
ret = append(ret, group)
}
return ret
}
func loadMutiLineSteps(arr []string) []model.TestStep {
childs := make([]model.TestStep, 0)
child := model.TestStep{}
idx := 0
for true {
if idx >= len(arr) {
if child.Desc != "" {
childs = append(childs, child)
}
break
}
line := arr[idx]
line = strings.TrimSpace(line)
if isStepsIdent(line) {
if idx > 0 {
childs = append(childs, child)
}
child = model.TestStep{}
idx++
stp := ""
for true { // retrieve next lines
if idx >= len(arr) || hasBrackets(arr[idx]) {
child.Desc = stp
break
}
stp += arr[idx] + "\n"
idx++
}
}
if isExpectsIdent(line) {
idx++
exp := ""
for true { // retrieve next lines
if idx >= len(arr) || hasBrackets(arr[idx]) {
child.Expect = exp
break
}
temp := strings.TrimSpace(arr[idx])
if temp == ">>" {
temp = "pending"
}
exp += temp + "\n"
idx++
}
}
}
return childs
}
func loadSingleLineSteps(arr []string) []model.TestStep {
childs := make([]model.TestStep, 0)
for _, line := range arr {
line = strings.TrimSpace(line)
sections := strings.Split(line, ">>")
expect := ""
if len(sections) > 1 { // has expect
expect = strings.TrimSpace(sections[1])
if expect == "" { // in independent file
expect = "pending"
}
}
child := model.TestStep{Desc: sections[0], Expect: expect}
childs = append(childs, child)
}
return childs
}
func isGroupIdent(str string) bool {
pass, _ := regexp.MatchString(`(?i)\[\s*group\s*\]`, str)
return pass
}
func isStepsIdent(str string) bool {
pass, _ := regexp.MatchString(`(?i)\[.*steps\.*\]`, str)
return pass
}
func isExpectsIdent(str string) bool {
pass, _ := regexp.MatchString(`(?i)\[.*expects\.*\]`, str)
return pass
}
func hasBrackets(str string) bool {
pass, _ := regexp.MatchString(`(?i)()\[.*\]`, str)
return pass
}
func isGroup(str string) bool {
ret := strings.Index(str, ">>") < 0 && hasBrackets(str) && !isStepsIdent(str) && !isExpectsIdent(str)
return ret
}
func getSortedTextFromNestedSteps(groups []model.TestStep) (string, maps.Map, maps.Map, maps.Map) {
ret := make([]string, 0)
stepMap := linkedhashmap.New()
stepTypeMap := linkedhashmap.New()
expectMap := linkedhashmap.New()
groupNumb := 1
for _, group := range groups {
desc := group.Desc
if desc == "[group]" {
ret = append(ret, "\n"+desc)
for idx, child := range group.Children { // level 1 item
numbStr := getNumbStr(groupNumb, -1)
stepTypeMap.Put(numbStr, "item")
if group.MutiLine {
// steps
tag := replaceNumb("[steps]", groupNumb, -1, true)
ret = append(ret, " "+tag)
stepTxt := printMutiStepOrExpect(child.Desc)
ret = append(ret, stepTxt)
stepMap.Put(numbStr, stepTxt)
// expects
tag = replaceNumb("[expects]", groupNumb, -1, true)
ret = append(ret, " "+tag)
expectTxt := printMutiStepOrExpect(child.Expect)
ret = append(ret, expectTxt)
if idx < len(group.Children)-1 {
ret = append(ret, "")
}
expectMap.Put(numbStr, expectTxt)
} else {
stepTxt := strings.TrimSpace(child.Desc)
stepTxtWithNumb := replaceNumb(stepTxt, groupNumb, -1, false)
stepMap.Put(numbStr, stepTxt)
expectTxt := child.Expect
expectTxt = strings.TrimSpace(expectTxt)
expectMap.Put(numbStr, expectTxt)
if expectTxt != "" {
expectTxt = ">> " + expectTxt
}
ret = append(ret, fmt.Sprintf(" %s %s", stepTxtWithNumb, expectTxt))
}
groupNumb++
}
} else {
desc = replaceNumb(group.Desc, groupNumb, -1, true)
ret = append(ret, "\n"+desc)
numbStr := getNumbStr(groupNumb, -1)
stepMap.Put(numbStr, getGroupName(group.Desc))
stepTypeMap.Put(numbStr, "group")
expectMap.Put(numbStr, "")
childNumb := 1
for _, child := range group.Children {
numbStr := getNumbStr(groupNumb, childNumb)
stepTypeMap.Put(numbStr, "item")
if group.MutiLine {
// steps
tag := replaceNumb("[steps]", groupNumb, childNumb, true)
ret = append(ret, " "+tag)
stepTxt := printMutiStepOrExpect(child.Desc)
ret = append(ret, stepTxt)
stepMap.Put(numbStr, stepTxt)
// expects
tag = replaceNumb("[expects]", groupNumb, childNumb, true)
ret = append(ret, " "+tag)
expectTxt := printMutiStepOrExpect(child.Expect)
ret = append(ret, expectTxt)
expectMap.Put(numbStr, expectTxt)
} else {
stepTxt := strings.TrimSpace(child.Desc)
stepMap.Put(numbStr, stepTxt)
expectTxt := child.Expect
expectTxt = strings.TrimSpace(expectTxt)
expectMap.Put(numbStr, expectTxt)
if expectTxt != "" {
expectTxt = ">> " + expectTxt
}
ret = append(ret, fmt.Sprintf(" %s %s", stepTxt, expectTxt))
}
childNumb++
}
groupNumb++
}
}
return strings.Join(ret, "\n"), stepMap, stepTypeMap, expectMap
}
func replaceNumb(str string, groupNumb int, childNumb int, withBrackets bool) string {
numb := getNumbStr(groupNumb, childNumb)
reg := `[\d\.\s]*(.*)`
repl := numb + " ${1}"
if withBrackets {
reg = `\[` + reg + `\]`
repl = `[` + repl + `]`
}
regx, _ := regexp.Compile(reg)
str = regx.ReplaceAllString(str, repl)
return str
}
func getNumbStr(groupNumb int, childNumb int) string {
numb := strconv.Itoa(groupNumb) + "."
if childNumb != -1 {
numb += strconv.Itoa(childNumb) + "."
}
return numb
}
func getGroupName(str string) string {
reg := `\[\d\.\s]*(.*)\]`
repl := "${1}"
regx, _ := regexp.Compile(reg)
str = regx.ReplaceAllString(str, repl)
return str
}
func printMutiStepOrExpect(str string) string {
str = strings.TrimSpace(str)
ret := make([]string, 0)
for _, line := range strings.Split(str, "\n") {
line = strings.TrimSpace(line)
ret = append(ret, fmt.Sprintf("%s%s", strings.Repeat(" ", 4), line))
}
return strings.Join(ret, "\r\n")
}
func GetExpectMapFromIndependentFile(expectMap maps.Map, content string, withEmptyExpect bool) maps.Map {
retMap := linkedhashmap.New()
expectArr := zentaoUtils.ReadExpectIndependentArr(content)
idx := 0
for _, keyIfs := range expectMap.Keys() {
valueIfs, _ := expectMap.Get(keyIfs)
key := strings.TrimSpace(keyIfs.(string))
value := strings.TrimSpace(valueIfs.(string))
if value == "pending" {
retMap.Put(key, strings.Join(expectArr[idx], "\r\n"))
idx++
} else {
if withEmptyExpect {
retMap.Put(key, "")
}
}
}
return retMap
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册