Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2021-11-10 09:10:31 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2021-11-10 09:10:31 +0300
commitbef328bb8c0e287ecd68e21bae40bdb335971212 (patch)
tree3d867e67eb59e127e5bf7c80e34a6be6b5226428 /doc/development/go_guide
parentaaa0fba8208ac74b34c2f87dde13611656fe7df6 (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'doc/development/go_guide')
-rw-r--r--doc/development/go_guide/index.md72
1 files changed, 72 insertions, 0 deletions
diff --git a/doc/development/go_guide/index.md b/doc/development/go_guide/index.md
index c594c67728f..9bf8b7ef89a 100644
--- a/doc/development/go_guide/index.md
+++ b/doc/development/go_guide/index.md
@@ -450,6 +450,78 @@ The conventional Secure [analyzer](https://gitlab.com/gitlab-org/security-produc
If the scanner report is small, less than 35 lines, then feel free to [inline the report](https://gitlab.com/gitlab-org/security-products/analyzers/sobelow/-/blob/8bd2428a/convert/convert_test.go#L13-77) rather than use a `testdata` directory.
+#### Test Diffs
+
+The [go-cmp]<https://github.com/google/go-cmp> package should be used when comparing large structs in tests. It makes it possible to output a specific diff where the two structs differ, rather than seeing the whole of both structs printed out in the test logs. Here is a small example:
+
+```golang
+package main
+
+import (
+ "reflect"
+ "testing"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type Foo struct {
+ Desc Bar
+ Point Baz
+}
+
+type Bar struct {
+ A string
+ B string
+}
+
+type Baz struct {
+ X int
+ Y int
+}
+
+func TestHelloWorld(t *testing.T) {
+ want := Foo{
+ Desc: Bar{A: "a", B: "b"},
+ Point: Baz{X: 1, Y: 2},
+ }
+
+ got := Foo{
+ Desc: Bar{A: "a", B: "b"},
+ Point: Baz{X: 2, Y: 2},
+ }
+
+ t.Log("reflect comparison:")
+ if !reflect.DeepEqual(got, want) {
+ t.Errorf("Wrong result. want:\n%v\nGot:\n%v", want, got)
+ }
+
+ t.Log("cmp comparison:")
+ if diff := cmp.Diff(want, got); diff != "" {
+ t.Errorf("Wrong result. (-want +got):\n%s", diff)
+ }
+}
+```
+
+The output demonstrates why `go-cmp` is far superior when comparing large structs. Even though you could spot the difference with this small difference, it quickly gets unwieldy as the data grows.
+
+```plaintext
+ main_test.go:36: reflect comparison:
+ main_test.go:38: Wrong result. want:
+ {{a b} {1 2}}
+ Got:
+ {{a b} {2 2}}
+ main_test.go:41: cmp comparison:
+ main_test.go:43: Wrong result. (-want +got):
+ main.Foo{
+ Desc: {A: "a", B: "b"},
+ Point: main.Baz{
+ - X: 1,
+ + X: 2,
+ Y: 2,
+ },
+ }
+```
+
---
[Return to Development documentation](../index.md).