-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use regular expressions for testdiff replacements (#2151)
## Changes Replacement was split between the type `ReplacementContext` and the `ReplaceOutput` function. The latter also ran a couple of regular expressions. This change consolidates them such that it is up to the caller to compose the set of replacements to use. This change is required to accommodate UUID replacement in #2146.
- Loading branch information
Showing
7 changed files
with
269 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package testdiff | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
type key int | ||
|
||
const ( | ||
replacementsMapKey = key(1) | ||
) | ||
|
||
func WithReplacementsMap(ctx context.Context) (context.Context, *ReplacementsContext) { | ||
value := ctx.Value(replacementsMapKey) | ||
if value != nil { | ||
if existingMap, ok := value.(*ReplacementsContext); ok { | ||
return ctx, existingMap | ||
} | ||
} | ||
|
||
newMap := &ReplacementsContext{} | ||
ctx = context.WithValue(ctx, replacementsMapKey, newMap) | ||
return ctx, newMap | ||
} | ||
|
||
func GetReplacementsMap(ctx context.Context) *ReplacementsContext { | ||
value := ctx.Value(replacementsMapKey) | ||
if value != nil { | ||
if existingMap, ok := value.(*ReplacementsContext); ok { | ||
return existingMap | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package testdiff | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetReplacementsMap_Nil(t *testing.T) { | ||
ctx := context.Background() | ||
repls := GetReplacementsMap(ctx) | ||
assert.Nil(t, repls) | ||
} | ||
|
||
func TestGetReplacementsMap_NotNil(t *testing.T) { | ||
ctx := context.Background() | ||
ctx, _ = WithReplacementsMap(ctx) | ||
repls := GetReplacementsMap(ctx) | ||
assert.NotNil(t, repls) | ||
} | ||
|
||
func TestWithReplacementsMap_UseExisting(t *testing.T) { | ||
ctx := context.Background() | ||
ctx, r1 := WithReplacementsMap(ctx) | ||
ctx, r2 := WithReplacementsMap(ctx) | ||
repls := GetReplacementsMap(ctx) | ||
assert.Equal(t, r1, repls) | ||
assert.Equal(t, r2, repls) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.