-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: add StructEqual() for verifying Equal method of structs
This PR adds StructEqual which is helpful for verifying that modifying the fields of a struct will cause the Equal method of the struct to no longer return true.
- Loading branch information
Showing
8 changed files
with
236 additions
and
1 deletion.
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,10 @@ | ||
package util | ||
|
||
// CloneSliceFunc creates a copy of A by first applying convert to each element. | ||
func CloneSliceFunc[A, B any](original []A, convert func(item A) B) []B { | ||
clone := make([]B, len(original)) | ||
for i := 0; i < len(original); i++ { | ||
clone[i] = convert(original[i]) | ||
} | ||
return clone | ||
} |
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,36 @@ | ||
package util | ||
|
||
import ( | ||
"strconv" | ||
"testing" | ||
) | ||
|
||
func TestCloneSliceFunc(t *testing.T) { | ||
t.Run("empty", func(t *testing.T) { | ||
result := CloneSliceFunc([]int{}, func(i int) string { | ||
return strconv.Itoa(i) | ||
}) | ||
if len(result) > 0 { | ||
t.Fatal("expected empty slice") | ||
} | ||
}) | ||
|
||
t.Run("non empty", func(t *testing.T) { | ||
original := []int{1, 4, 5} | ||
result := CloneSliceFunc(original, func(i int) string { | ||
return strconv.Itoa(i) | ||
}) | ||
if len(result) != 3 { | ||
t.Fatal("expected length of 3") | ||
} | ||
if result[0] != "1" { | ||
t.Fatal("expected result[0] == 1") | ||
} | ||
if result[1] != "4" { | ||
t.Fatal("expected result[1] == 4") | ||
} | ||
if result[2] != "5" { | ||
t.Fatal("expected result[2] == 5") | ||
} | ||
}) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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