Skip to content

Commit 3757e60

Browse files
author
Christoph Glaubitz
committed
Added parsing to objmetadata_test.TestCreateObjMetadata
This commit extends TestCreateObjMetadata. The test function does not just verify CreateObjMetadata, but also uses ParseObjMetadata to verify back and forth between both functions. I left specific testing of ParseObjMetadata in place, because there might be edge cases, that should be tested on its own. I'm aware, that the TestCreateObjMetadata now tests two things, but I think it makes life of maintainers and new contributors far easier. E.g. the change I introduced in #193 would have been catched by tests, rather than someone with a mental model of all of the code.
1 parent 7d23762 commit 3757e60

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

pkg/object/objmetadata_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package object
55

66
import (
7+
"strings"
78
"testing"
89

910
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -97,7 +98,22 @@ func TestCreateObjMetadata(t *testing.T) {
9798
if err != nil {
9899
t.Errorf("Error creating ObjMetadata when it should have worked.")
99100
} else if test.expected != inv.String() {
100-
t.Errorf("Expected inventory (%s) != created inventory(%s)\n", test.expected, inv.String())
101+
t.Errorf("Expected inventory\n(%s) != created inventory\n(%s)\n", test.expected, inv.String())
102+
}
103+
104+
// Parsing back the just created inventory string to ObjMetadata,
105+
// so that tests will catch any change to CreateObjMetadata that
106+
// would break ParseObjMetadata.
107+
expectedObjMetadata := &ObjMetadata{
108+
Namespace: strings.TrimSpace(test.namespace),
109+
Name: strings.TrimSpace(test.name),
110+
GroupKind: test.gk,
111+
}
112+
actual, err := ParseObjMetadata(inv.String())
113+
if err != nil {
114+
t.Errorf("Error parsing back ObjMetadata, when it should have worked.")
115+
} else if !expectedObjMetadata.Equals(actual) {
116+
t.Errorf("Expected inventory (%s) != parsed inventory (%s)\n", expectedObjMetadata, actual)
101117
}
102118
}
103119
if test.isError && err == nil {

0 commit comments

Comments
 (0)