Skip to content

Commit 967bf82

Browse files
Tests: extract flattening method to allow for recursive usage (#669) (#670)
Co-authored-by: Laurent Saint-Félix <[email protected]>
1 parent a492630 commit 967bf82

File tree

1 file changed

+17
-11
lines changed
  • internal/build/cmd/generate/commands/gentests

1 file changed

+17
-11
lines changed

internal/build/cmd/generate/commands/gentests/model.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -695,17 +695,7 @@ default:
695695
// We cannot reliably serialize to json and compare the json outputs: YAML responses are parsed as
696696
// a map[interface{}]interface{} that encoding/json fails to marshall
697697
// See https://play.golang.org/p/jhcXwg5dIrn
698-
expectedPayload := func(val interface{}) string {
699-
expectedOutput := make(map[string]interface{})
700-
if cast, ok := val.(map[interface{}]interface{}); ok {
701-
for k, v := range cast {
702-
expectedOutput[fmt.Sprintf("%v", k)] = v
703-
}
704-
} else {
705-
expectedOutput = val.(map[string]interface{})
706-
}
707-
return fmt.Sprintf("%#v", expectedOutput)
708-
}(val)
698+
expectedPayload := flattenPayload(val)
709699

710700
expectedPayload = strings.ReplaceAll(expectedPayload, "map[interface {}]interface {}", "map[string]interface {}")
711701
output = ` actual = fmt.Sprintf("%v",` + escape(subject) + `)
@@ -948,3 +938,19 @@ func skipVersion(minmax string) bool {
948938

949939
return false
950940
}
941+
942+
// flattenPayload serializes the expected payload as a map tree to compare within tests.
943+
func flattenPayload(val interface{}) string {
944+
expectedOutput := make(map[string]interface{})
945+
if cast, ok := val.(map[interface{}]interface{}); ok {
946+
for k, v := range cast {
947+
if _, ok := v.(map[interface{}]interface{}); ok {
948+
v = flattenPayload(v)
949+
}
950+
expectedOutput[fmt.Sprintf("%v", k)] = v
951+
}
952+
} else {
953+
expectedOutput = val.(map[string]interface{})
954+
}
955+
return fmt.Sprintf("%#v", expectedOutput)
956+
}

0 commit comments

Comments
 (0)