@@ -2,6 +2,7 @@ package is
2
2
3
3
import (
4
4
"strings"
5
+ "unicode"
5
6
6
7
"github.com/halimath/expect"
7
8
)
@@ -51,10 +52,22 @@ func StringWithSuffix(got, want string) expect.Expectation {
51
52
})
52
53
}
53
54
55
+ // Dedent is intended to be used as a transformer passed to [EqualToStringByLines].
56
+ // It removes any prefix whitespace from s thus dedenting each line. This is
57
+ // especially usefull if the expected value for a test is written in code as an
58
+ // indented multiline raw string literal but the actual lines are not indented.
59
+ func DedentLines (s string ) string {
60
+ return strings .TrimLeftFunc (s , unicode .IsSpace )
61
+ }
62
+
54
63
// EqualToStringByLines compares got and want line by line and reports different
55
64
// lines one at a time. This makes it easiert to understand failed expectations
56
65
// when comparing large strings.
57
- func EqualToStringByLines (got , want string ) expect.Expectation {
66
+ //
67
+ // transformers are applied to all lines, both those obtained from got and want.
68
+ // transformers are applied in order (iteratively) and the final transformation
69
+ // result is used for comparison.
70
+ func EqualToStringByLines (got , want string , transformers ... func (string ) string ) expect.Expectation {
58
71
return expect .ExpectFunc (func (t expect.TB ) {
59
72
t .Helper ()
60
73
@@ -70,7 +83,15 @@ func EqualToStringByLines(got, want string) expect.Expectation {
70
83
limit := min (len (gotLines ), len (wantLines ))
71
84
72
85
for i := 0 ; i < limit ; i ++ {
73
- if wantLines [i ] != gotLines [i ] {
86
+ gotLine := gotLines [i ]
87
+ wantLine := wantLines [i ]
88
+
89
+ for _ , transformer := range transformers {
90
+ gotLine = transformer (gotLine )
91
+ wantLine = transformer (wantLine )
92
+ }
93
+
94
+ if gotLine != wantLine {
74
95
t .Errorf ("at line %d: wanted\n %q\n but got\n %q" , i , wantLines [i ], gotLines [i ])
75
96
if lenghtsDiffer {
76
97
return
0 commit comments