Skip to content

Commit

Permalink
Outdent if/else logic with returns (#895)
Browse files Browse the repository at this point in the history
  • Loading branch information
HaraldNordgren authored Aug 1, 2024
1 parent 1f35a1a commit 7e09ba4
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 108 deletions.
205 changes: 102 additions & 103 deletions internal/build/cmd/generate/commands/gentests/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,85 +576,86 @@ default:
match = re.Match(body)` + "\n")
b.WriteString(` if !match` + " {\n")
return b.String()
} else {
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 1b. Is the expected value a literal value
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// TODO: Match on literal value
// output = fmt.Sprintf("if true { // TODO, MatchBodyLiteral: %s => %v\n", a.operation, a.payload)
output = `if strings.HasPrefix(string(body), "[") {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 1b. Is the expected value a literal value?
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// TODO: Match on literal value
// output = fmt.Sprintf("if true { // TODO, MatchBodyLiteral: %s => %v\n", a.operation, a.payload)
output = `if strings.HasPrefix(string(body), "[") {
match = !reflect.DeepEqual(fmt.Sprintf("%#v", slic), ` + fmt.Sprintf("%#v", val) + `)
} else {
match = !reflect.DeepEqual(fmt.Sprintf("%#v", mapi), ` + fmt.Sprintf("%#v", val) + `)
}
if !match {` + "\n"
return output
}
return output

// panic(fmt.Sprintf("MatchUnknown: %q => %v\n", a.operation, a.payload))
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 2. Match on JSON/YAML field or numbered item
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} else {
subject = expand(key)
expected = strings.TrimSpace(fmt.Sprintf("%v", val))

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 2a. Is the expected value a regex pattern?
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if strings.HasPrefix(expected, "/") {
var pattern string
pattern = expected
pattern = strings.Replace(pattern, "/", "", -1)
pattern = strings.Replace(pattern, "\n", "", -1)
pattern = strings.Replace(pattern, " ", `\s*`, -1)
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 2. Match on JSON/YAML field or numbered item
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

b.WriteString(`re, err = regexp.Compile("(?m)" + ` + "`" + pattern + "`" + `)
subject = expand(key)
expected = strings.TrimSpace(fmt.Sprintf("%v", val))

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 2a. Is the expected value a regex pattern?
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if strings.HasPrefix(expected, "/") {
var pattern string
pattern = expected
pattern = strings.Replace(pattern, "/", "", -1)
pattern = strings.Replace(pattern, "\n", "", -1)
pattern = strings.Replace(pattern, " ", `\s*`, -1)

b.WriteString(`re, err = regexp.Compile("(?m)" + ` + "`" + pattern + "`" + `)
if err != nil {
t.Fatalf("Regex compile error: %s", err)
}
match = re.MatchString(fmt.Sprintf("%v", ` + escape(subject) + `))` + "\n")
b.WriteString(` if !match` + " {\n")
return b.String()
b.WriteString(` if !match` + " {\n")
return b.String()
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 2b. Is the value a regular value?
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} else {
// fmt.Printf("subject: %s, val: %T, %v \n", subject, val, val)

nilGuard := catchnil(subject)
switch val.(type) {

// --------------------------------------------------------------------------------
case nil:
output += ` if ` + escape(subject) + ` != nil` + " {\n"

// --------------------------------------------------------------------------------
case bool:
output += ` if ` +
nilGuard +
" || \n" +
`fmt.Sprintf("%v", ` + escape(subject) + `) != ` +
`fmt.Sprintf("%v", ` + expected + `)` +
" {\n"
case int, float64:
// Date range special case
// From & to are set in yaml tests as integer
// but ES returns a scientific notation float
var specialCase string
if strings.HasSuffix(key, "from") || strings.HasSuffix(key, "to") {
specialCase = `else if floatValue, err := actual.(encjson.Number).Float64(); err == nil {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 2b. Is the value a regular value?
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// fmt.Printf("subject: %s, val: %T, %v \n", subject, val, val)

nilGuard := catchnil(subject)
switch val.(type) {

// --------------------------------------------------------------------------------
case nil:
output += ` if ` + escape(subject) + ` != nil` + " {\n"

// --------------------------------------------------------------------------------
case bool:
output += ` if ` +
nilGuard +
" || \n" +
`fmt.Sprintf("%v", ` + escape(subject) + `) != ` +
`fmt.Sprintf("%v", ` + expected + `)` +
" {\n"
case int, float64:
// Date range special case
// From & to are set in yaml tests as integer
// but ES returns a scientific notation float
var specialCase string
if strings.HasSuffix(key, "from") || strings.HasSuffix(key, "to") {
specialCase = `else if floatValue, err := actual.(encjson.Number).Float64(); err == nil {
actual = strconv.FormatFloat(floatValue, 'f', -1, 64)
} `
}
}

r := strings.NewReplacer(`"`, "", `\`, "")
rkey := r.Replace(key)
output += ` if ` + nilGuard + ` { t.Error("Expected [` + rkey + `] to not be nil") }
r := strings.NewReplacer(`"`, "", `\`, "")
rkey := r.Replace(key)
output += ` if ` + nilGuard + ` { t.Error("Expected [` + rkey + `] to not be nil") }
actual = ` + escape(subject) + `
if intValue, ok := actual.(int); ok {
actual = fmt.Sprint(intValue)
Expand All @@ -675,53 +676,51 @@ default:
if !assertion {
t.Logf("%v != %v", actual, expected)` + "\n"

// --------------------------------------------------------------------------------
case string:
output += ` if ` +
nilGuard +
" || \n" +
`strings.TrimSpace(fmt.Sprintf("%s", ` + escape(subject) + `)) != `
if strings.HasPrefix(expected, "$") {
// Remove brackets if we compare to a stashed value replaced in the body.
expected = strings.NewReplacer("{", "", "}", "").Replace(expected)
output += `strings.TrimSpace(fmt.Sprintf("%s", ` + `stash["` + expected + `"]` + `))`
} else {
output += `strings.TrimSpace(fmt.Sprintf("%s", ` + strconv.Quote(expected) + `))`
}
output += " {\n"

// --------------------------------------------------------------------------------
case map[interface{}]interface{}, map[string]interface{}:
// We cannot reliably serialize to json and compare the json outputs: YAML responses are parsed as
// a map[interface{}]interface{} that encoding/json fails to marshall
// See https://play.golang.org/p/jhcXwg5dIrn
expectedOutput := flattenPayload(val)
expectedPayload := fmt.Sprintf("%#v", expectedOutput)

expectedPayload = strings.ReplaceAll(expectedPayload, "map[interface {}]interface {}", "map[string]interface {}")
output = ` actual = fmt.Sprintf("%v",` + escape(subject) + `)
// --------------------------------------------------------------------------------
case string:
output += ` if ` +
nilGuard +
" || \n" +
`strings.TrimSpace(fmt.Sprintf("%s", ` + escape(subject) + `)) != `
if strings.HasPrefix(expected, "$") {
// Remove brackets if we compare to a stashed value replaced in the body.
expected = strings.NewReplacer("{", "", "}", "").Replace(expected)
output += `strings.TrimSpace(fmt.Sprintf("%s", ` + `stash["` + expected + `"]` + `))`
} else {
output += `strings.TrimSpace(fmt.Sprintf("%s", ` + strconv.Quote(expected) + `))`
}
output += " {\n"

// --------------------------------------------------------------------------------
case map[interface{}]interface{}, map[string]interface{}:
// We cannot reliably serialize to json and compare the json outputs: YAML responses are parsed as
// a map[interface{}]interface{} that encoding/json fails to marshall
// See https://play.golang.org/p/jhcXwg5dIrn
expectedOutput := flattenPayload(val)
expectedPayload := fmt.Sprintf("%#v", expectedOutput)

expectedPayload = strings.ReplaceAll(expectedPayload, "map[interface {}]interface {}", "map[string]interface {}")
output = ` actual = fmt.Sprintf("%v",` + escape(subject) + `)
expected = fmt.Sprintf("%v",` + expectedPayload + `)
if actual != expected {` + "\n"

// --------------------------------------------------------------------------------
case []interface{}:
expectedPayload := fmt.Sprintf("%#v", val)
expectedPayload = strings.ReplaceAll(expectedPayload, "map[interface {}]interface {}", "map[string]interface {}")
output = ` actual, _ = encjson.Marshal(` + escape(subject) + `)
// --------------------------------------------------------------------------------
case []interface{}:
expectedPayload := fmt.Sprintf("%#v", val)
expectedPayload = strings.ReplaceAll(expectedPayload, "map[interface {}]interface {}", "map[string]interface {}")
output = ` actual, _ = encjson.Marshal(` + escape(subject) + `)
expected, _ = encjson.Marshal(` + expectedPayload + `)
if fmt.Sprintf("%s", actual) != fmt.Sprintf("%s", expected) {` + "\n"

// --------------------------------------------------------------------------------
default:
output += ` if true { // TODO, MatchMissingType: ` + fmt.Sprintf("<%[1]T>%[1]v", val) + "\n"
output += ` // Subject: ` + subject + "\n"
output += ` // Value: ` + fmt.Sprintf("%#v", val) + "\n"
}

return output
}
// --------------------------------------------------------------------------------
default:
output += ` if true { // TODO, MatchMissingType: ` + fmt.Sprintf("<%[1]T>%[1]v", val) + "\n"
output += ` // Subject: ` + subject + "\n"
output += ` // Value: ` + fmt.Sprintf("%#v", val) + "\n"
}

return output

// ================================================================================
default:
return fmt.Sprintf("if true { // TODO, Unimplemented: %q => %v\n", a.operation, a.payload)
Expand Down Expand Up @@ -768,9 +767,8 @@ func (a Assertion) Error() string {

if strings.HasPrefix(expected, "/") {
return `t.Errorf("Expected [$body] to match pattern: %s", re)`
} else {
output = `Expected [$body] to match value: ` + escape(expected)
}
output = `Expected [$body] to match value: ` + escape(expected)

} else { // 2. Match on JSON/YAML field or numbered item
if key == "" {
Expand All @@ -782,9 +780,10 @@ func (a Assertion) Error() string {
expected = strings.Replace(expected, `/`, `|`, -1)

return `t.Errorf("Expected [` + strings.Trim(strconv.Quote(escape(key)), `"`) + `] to match pattern: %s", re)`
} else { // 2b. Is the value to match a regular value?
output = `Expected [` + strings.Trim(strconv.Quote(escape(key)), `"`) + `] to match '` + escape(expected) + `'`
}

// 2b. Is the value to match a regular value?
output = `Expected [` + strings.Trim(strconv.Quote(escape(key)), `"`) + `] to match '` + escape(expected) + `'`
}
}

Expand Down
6 changes: 1 addition & 5 deletions internal/build/utils/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,15 @@ import (
var resVersion = regexp.MustCompile(`(\d+\.(x|\d+.\d+)).*`)

// EsVersion returns the Elasticsearch from environment variable, Java property file, or an error.
//
func EsVersion(fpath string) (string, error) {
if envEsVersion := os.Getenv("ELASTICSEARCH_BUILD_VERSION"); envEsVersion != "" {
splitVersion := resVersion.FindStringSubmatch(envEsVersion)
return splitVersion[1], nil
} else {
return "", fmt.Errorf("ELASTICSEARCH_BUILD_VERSION is empty")
}
return "", fmt.Errorf("ELASTICSEARCH_BUILD_VERSION is empty")
}

// GitCommit returns the Git commit from environment variable or parsing information from fpath, or an error.
//
func GitCommit(fpath string) (string, error) {
if esBuildHash := os.Getenv("ELASTICSEARCH_BUILD_HASH"); esBuildHash != "" {
return esBuildHash[:7], nil
Expand All @@ -49,7 +46,6 @@ func GitCommit(fpath string) (string, error) {
}

// GitTag returns the Git tag for fpath if available, or an error.
//
func GitTag(fpath string) (string, error) {
basePath, err := basePathFromFilepath(fpath)
if err != nil {
Expand Down

0 comments on commit 7e09ba4

Please sign in to comment.