repogen/internal/testutils/multilines.go

33 lines
942 B
Go
Raw Normal View History

2021-01-23 13:03:16 +00:00
package testutils
import (
"fmt"
"strings"
)
// ExpectMultiLineString compares two multi-line strings and report the
// difference.
2021-01-23 13:03:16 +00:00
func ExpectMultiLineString(expected, actual string) error {
expectedLines := strings.Split(expected, "\n")
actualLines := strings.Split(actual, "\n")
numberOfComparableLines := len(expectedLines)
if len(actualLines) < numberOfComparableLines {
numberOfComparableLines = len(actualLines)
}
for i := 0; i < numberOfComparableLines; i++ {
if expectedLines[i] != actualLines[i] {
return fmt.Errorf("at line %d\nexpected: %v\nreceived: %v", i+1, expectedLines[i], actualLines[i])
2021-01-23 13:03:16 +00:00
}
}
if len(expectedLines) < len(actualLines) {
return fmt.Errorf("unexpected lines:\n%s", strings.Join(actualLines[len(expectedLines):], "\n"))
2021-01-23 13:03:16 +00:00
} else if len(expectedLines) > len(actualLines) {
return fmt.Errorf("missing lines:\n%s", strings.Join(expectedLines[len(actualLines):], "\n"))
2021-01-23 13:03:16 +00:00
}
return nil
}