Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7a070b4

Browse files
authoredJun 22, 2022
Merge pull request #333 from harshanarayana/tests/GIT-275-add-additional-test-for-kv
GIT-275: add tests for int and struct keys
2 parents 265aa25 + 280fc18 commit 7a070b4

File tree

5 files changed

+101
-2
lines changed

5 files changed

+101
-2
lines changed
 

‎.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ jobs:
44
test:
55
strategy:
66
matrix:
7-
go-version: [1.15, 1.16, 1.17]
7+
go-version: [1.15, 1.16, 1.17, 1.18]
88
platform: [ubuntu-latest, macos-latest, windows-latest]
99
runs-on: ${{ matrix.platform }}
1010
steps:

‎examples/benchmarks/benchmarks_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,33 @@ func BenchmarkOutput(b *testing.B) {
9191
"fail-verbosity-check": func(value interface{}) {
9292
klog.V(verbosityThreshold+1).InfoS("test", "key", value)
9393
},
94+
"non-standard-int-key-check": func(value interface{}) {
95+
klog.InfoS("test", 1, value)
96+
},
97+
"non-standard-struct-key-check": func(value interface{}) {
98+
klog.InfoS("test", struct{ key string }{"test"}, value)
99+
},
100+
"non-standard-map-key-check": func(value interface{}) {
101+
klog.InfoS("test", map[string]bool{"key": true}, value)
102+
},
103+
"pass-verbosity-non-standard-int-key-check": func(value interface{}) {
104+
klog.V(verbosityThreshold).InfoS("test", 1, value)
105+
},
106+
"pass-verbosity-non-standard-struct-key-check": func(value interface{}) {
107+
klog.V(verbosityThreshold).InfoS("test", struct{ key string }{"test"}, value)
108+
},
109+
"pass-verbosity-non-standard-map-key-check": func(value interface{}) {
110+
klog.V(verbosityThreshold).InfoS("test", map[string]bool{"key": true}, value)
111+
},
112+
"fail-verbosity-non-standard-int-key-check": func(value interface{}) {
113+
klog.V(verbosityThreshold+1).InfoS("test", 1, value)
114+
},
115+
"fail-verbosity-non-standard-struct-key-check": func(value interface{}) {
116+
klog.V(verbosityThreshold+1).InfoS("test", struct{ key string }{"test"}, value)
117+
},
118+
"fail-verbosity-non-standard-map-key-check": func(value interface{}) {
119+
klog.V(verbosityThreshold+1).InfoS("test", map[string]bool{"key": true}, value)
120+
},
94121
}
95122

96123
for name, config := range configs {

‎examples/output_test/output_test.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func TestTextloggerOutput(t *testing.T) {
6363
})
6464
}
6565

66-
// TestTextloggerOutput tests the zapr, directly and as backend.
66+
// TestZaprOutput tests the zapr, directly and as backend.
6767
func TestZaprOutput(t *testing.T) {
6868
newLogger := func(out io.Writer, v int, vmodule string) logr.Logger {
6969
return newZaprLogger(out, v)
@@ -121,6 +121,18 @@ func TestKlogrStackZapr(t *testing.T) {
121121

122122
`I output.go:<LINE>] "both odd" basekey1="basevar1" basekey2="(MISSING)" akey="avalue" akey2="(MISSING)"
123123
`: `{"caller":"test/output.go:<LINE>","msg":"both odd","v":0,"basekey1":"basevar1","basekey2":"(MISSING)","akey":"avalue","akey2":"(MISSING)"}
124+
`,
125+
`I output.go:<LINE>] "integer keys" %!s(int=1)="value" %!s(int=2)="value2" akey="avalue" akey2="(MISSING)"
126+
`: `{"caller":"test/output.go:<LINE>","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":1}
127+
{"caller":"test/output.go:<LINE>","msg":"integer keys","v":0}
128+
`,
129+
`I output.go:<LINE>] "struct keys" {name}="value" test="other value" key="val"
130+
`: `{"caller":"test/output.go:<LINE>","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":{}}
131+
{"caller":"test/output.go:<LINE>","msg":"struct keys","v":0}
132+
`,
133+
`I output.go:<LINE>] "map keys" map[test:%!s(bool=true)]="test"
134+
`: `{"caller":"test/output.go:<LINE>","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":{"test":true}}
135+
{"caller":"test/output.go:<LINE>","msg":"map keys","v":0}
124136
`,
125137
} {
126138
mapping[key] = value
@@ -172,6 +184,18 @@ func TestKlogrInternalStackZapr(t *testing.T) {
172184

173185
`I output.go:<LINE>] "both odd" basekey1="basevar1" basekey2="(MISSING)" akey="avalue" akey2="(MISSING)"
174186
`: `{"caller":"test/output.go:<LINE>","msg":"both odd","v":0,"basekey1":"basevar1","basekey2":"(MISSING)","akey":"avalue","akey2":"(MISSING)"}
187+
`,
188+
`I output.go:<LINE>] "integer keys" %!s(int=1)="value" %!s(int=2)="value2" akey="avalue" akey2="(MISSING)"
189+
`: `{"caller":"test/output.go:<LINE>","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":1}
190+
{"caller":"test/output.go:<LINE>","msg":"integer keys","v":0}
191+
`,
192+
`I output.go:<LINE>] "struct keys" {name}="value" test="other value" key="val"
193+
`: `{"caller":"test/output.go:<LINE>","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":{}}
194+
{"caller":"test/output.go:<LINE>","msg":"struct keys","v":0}
195+
`,
196+
`I output.go:<LINE>] "map keys" map[test:%!s(bool=true)]="test"
197+
`: `{"caller":"test/output.go:<LINE>","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":{"test":true}}
198+
{"caller":"test/output.go:<LINE>","msg":"map keys","v":0}
175199
`,
176200
} {
177201
mapping[key] = value

‎test/output.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,27 @@ I output.go:<LINE>] "test" firstKey=1 secondKey=3
365365
text: "marshaler recursion",
366366
values: []interface{}{"obj", recursiveMarshaler{}},
367367
expectedOutput: `I output.go:<LINE>] "marshaler recursion" obj={}
368+
`,
369+
},
370+
"handle integer keys": {
371+
withValues: []interface{}{1, "value", 2, "value2"},
372+
text: "integer keys",
373+
values: []interface{}{"akey", "avalue", "akey2"},
374+
expectedOutput: `I output.go:<LINE>] "integer keys" %!s(int=1)="value" %!s(int=2)="value2" akey="avalue" akey2="(MISSING)"
375+
`,
376+
},
377+
"struct keys": {
378+
withValues: []interface{}{struct{ name string }{"name"}, "value", "test", "other value"},
379+
text: "struct keys",
380+
values: []interface{}{"key", "val"},
381+
expectedOutput: `I output.go:<LINE>] "struct keys" {name}="value" test="other value" key="val"
382+
`,
383+
},
384+
"map keys": {
385+
withValues: []interface{}{},
386+
text: "map keys",
387+
values: []interface{}{map[string]bool{"test": true}, "test"},
388+
expectedOutput: `I output.go:<LINE>] "map keys" map[test:%!s(bool=true)]="test"
368389
`,
369390
},
370391
}

‎test/zapr.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,21 @@ I output.go:<LINE>] "odd WithValues" keyWithoutValue="(MISSING)"
217217
// klog.V(1).InfoS
218218
`I output.go:<LINE>] "hello" what="one world"
219219
`: `{"caller":"test/output.go:<LINE>","msg":"hello","v":1,"what":"one world"}
220+
`,
221+
222+
`I output.go:<LINE>] "integer keys" %!s(int=1)="value" %!s(int=2)="value2" akey="avalue" akey2="(MISSING)"
223+
`: `{"caller":"test/output.go:<WITH-VALUES>","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":1}
224+
{"caller":"test/output.go:<LINE>","msg":"odd number of arguments passed as key-value pairs for logging","ignored key":"akey2"}
225+
{"caller":"test/output.go:<LINE>","msg":"integer keys","v":0,"akey":"avalue"}
226+
`,
227+
228+
`I output.go:<LINE>] "struct keys" {name}="value" test="other value" key="val"
229+
`: `{"caller":"test/output.go:<WITH-VALUES>","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":{}}
230+
{"caller":"test/output.go:<LINE>","msg":"struct keys","v":0,"key":"val"}
231+
`,
232+
`I output.go:<LINE>] "map keys" map[test:%!s(bool=true)]="test"
233+
`: `{"caller":"test/output.go:<LINE>","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":{"test":true}}
234+
{"caller":"test/output.go:<LINE>","msg":"map keys","v":0}
220235
`,
221236
}
222237
}
@@ -291,6 +306,18 @@ I output.go:<LINE>] "test" firstKey=1 secondKey=3
291306
{"caller":"test/output.go:<LINE>","msg":"test","v":0,"firstKey":1,"secondKey":2}
292307
{"caller":"test/output.go:<LINE>","msg":"test","v":0,"firstKey":1}
293308
{"caller":"test/output.go:<LINE>","msg":"test","v":0,"firstKey":1,"secondKey":3}
309+
`,
310+
`I output.go:<LINE>] "integer keys" %!s(int=1)="value" %!s(int=2)="value2" akey="avalue" akey2="(MISSING)"
311+
`: `{"caller":"test/output.go:<LINE>","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":1}
312+
{"caller":"test/output.go:<LINE>","msg":"integer keys","v":0}
313+
`,
314+
`I output.go:<LINE>] "struct keys" {name}="value" test="other value" key="val"
315+
`: `{"caller":"test/output.go:<LINE>","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":{}}
316+
{"caller":"test/output.go:<LINE>","msg":"struct keys","v":0}
317+
`,
318+
`I output.go:<LINE>] "map keys" map[test:%!s(bool=true)]="test"
319+
`: `{"caller":"test/output.go:<LINE>","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":{"test":true}}
320+
{"caller":"test/output.go:<LINE>","msg":"map keys","v":0}
294321
`,
295322
} {
296323
mapping[key] = value

0 commit comments

Comments
 (0)