Skip to content

Commit

Permalink
GIT-275: add tests for int and struct keys
Browse files Browse the repository at this point in the history
GIT-275: add map keys for example

GIT-275: add map keys for example
  • Loading branch information
harshanarayana committed Jun 21, 2022
1 parent ca252b1 commit 3a8da04
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
26 changes: 25 additions & 1 deletion examples/output_test/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestTextloggerOutput(t *testing.T) {
})
}

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

`I output.go:<LINE>] "both odd" basekey1="basevar1" basekey2="(MISSING)" akey="avalue" akey2="(MISSING)"
`: `{"caller":"test/output.go:<LINE>","msg":"both odd","v":0,"basekey1":"basevar1","basekey2":"(MISSING)","akey":"avalue","akey2":"(MISSING)"}
`,
`I output.go:<LINE>] "integer keys" %!s(int=1)="value" %!s(int=2)="value2" akey="avalue" akey2="(MISSING)"
`: `{"caller":"test/output.go:<LINE>","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":1}
{"caller":"test/output.go:<LINE>","msg":"integer keys","v":0}
`,
`I output.go:<LINE>] "struct keys" {name}="value" test="other value" key="val"
`: `{"caller":"test/output.go:<LINE>","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":{}}
{"caller":"test/output.go:<LINE>","msg":"struct keys","v":0}
`,
`I output.go:<LINE>] "map keys" map[test:%!s(bool=true)]="test"
`: `{"caller":"test/output.go:<LINE>","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":{"test":true}}
{"caller":"test/output.go:<LINE>","msg":"map keys","v":0}
`,
} {
mapping[key] = value
Expand Down Expand Up @@ -172,6 +184,18 @@ func TestKlogrInternalStackZapr(t *testing.T) {

`I output.go:<LINE>] "both odd" basekey1="basevar1" basekey2="(MISSING)" akey="avalue" akey2="(MISSING)"
`: `{"caller":"test/output.go:<LINE>","msg":"both odd","v":0,"basekey1":"basevar1","basekey2":"(MISSING)","akey":"avalue","akey2":"(MISSING)"}
`,
`I output.go:<LINE>] "integer keys" %!s(int=1)="value" %!s(int=2)="value2" akey="avalue" akey2="(MISSING)"
`: `{"caller":"test/output.go:<LINE>","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":1}
{"caller":"test/output.go:<LINE>","msg":"integer keys","v":0}
`,
`I output.go:<LINE>] "struct keys" {name}="value" test="other value" key="val"
`: `{"caller":"test/output.go:<LINE>","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":{}}
{"caller":"test/output.go:<LINE>","msg":"struct keys","v":0}
`,
`I output.go:<LINE>] "map keys" map[test:%!s(bool=true)]="test"
`: `{"caller":"test/output.go:<LINE>","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":{"test":true}}
{"caller":"test/output.go:<LINE>","msg":"map keys","v":0}
`,
} {
mapping[key] = value
Expand Down
21 changes: 21 additions & 0 deletions test/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,27 @@ I output.go:<LINE>] "test" firstKey=1 secondKey=3
text: "marshaler recursion",
values: []interface{}{"obj", recursiveMarshaler{}},
expectedOutput: `I output.go:<LINE>] "marshaler recursion" obj={}
`,
},
"handle integer keys": {
withValues: []interface{}{1, "value", 2, "value2"},
text: "integer keys",
values: []interface{}{"akey", "avalue", "akey2"},
expectedOutput: `I output.go:<LINE>] "integer keys" %!s(int=1)="value" %!s(int=2)="value2" akey="avalue" akey2="(MISSING)"
`,
},
"struct keys": {
withValues: []interface{}{struct{ name string }{"name"}, "value", "test", "other value"},
text: "struct keys",
values: []interface{}{"key", "val"},
expectedOutput: `I output.go:<LINE>] "struct keys" {name}="value" test="other value" key="val"
`,
},
"map keys": {
withValues: []interface{}{},
text: "map keys",
values: []interface{}{map[string]bool{"test": true}, "test"},
expectedOutput: `I output.go:<LINE>] "map keys" map[test:%!s(bool=true)]="test"
`,
},
}
Expand Down
27 changes: 27 additions & 0 deletions test/zapr.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,21 @@ I output.go:<LINE>] "odd WithValues" keyWithoutValue="(MISSING)"
// klog.V(1).InfoS
`I output.go:<LINE>] "hello" what="one world"
`: `{"caller":"test/output.go:<LINE>","msg":"hello","v":1,"what":"one world"}
`,

`I output.go:<LINE>] "integer keys" %!s(int=1)="value" %!s(int=2)="value2" akey="avalue" akey2="(MISSING)"
`: `{"caller":"test/output.go:<WITH-VALUES>","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":1}
{"caller":"test/output.go:<LINE>","msg":"odd number of arguments passed as key-value pairs for logging","ignored key":"akey2"}
{"caller":"test/output.go:<LINE>","msg":"integer keys","v":0,"akey":"avalue"}
`,

`I output.go:<LINE>] "struct keys" {name}="value" test="other value" key="val"
`: `{"caller":"test/output.go:<WITH-VALUES>","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":{}}
{"caller":"test/output.go:<LINE>","msg":"struct keys","v":0,"key":"val"}
`,
`I output.go:<LINE>] "map keys" map[test:%!s(bool=true)]="test"
`: `{"caller":"test/output.go:<LINE>","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":{"test":true}}
{"caller":"test/output.go:<LINE>","msg":"map keys","v":0}
`,
}
}
Expand Down Expand Up @@ -291,6 +306,18 @@ I output.go:<LINE>] "test" firstKey=1 secondKey=3
{"caller":"test/output.go:<LINE>","msg":"test","v":0,"firstKey":1,"secondKey":2}
{"caller":"test/output.go:<LINE>","msg":"test","v":0,"firstKey":1}
{"caller":"test/output.go:<LINE>","msg":"test","v":0,"firstKey":1,"secondKey":3}
`,
`I output.go:<LINE>] "integer keys" %!s(int=1)="value" %!s(int=2)="value2" akey="avalue" akey2="(MISSING)"
`: `{"caller":"test/output.go:<LINE>","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":1}
{"caller":"test/output.go:<LINE>","msg":"integer keys","v":0}
`,
`I output.go:<LINE>] "struct keys" {name}="value" test="other value" key="val"
`: `{"caller":"test/output.go:<LINE>","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":{}}
{"caller":"test/output.go:<LINE>","msg":"struct keys","v":0}
`,
`I output.go:<LINE>] "map keys" map[test:%!s(bool=true)]="test"
`: `{"caller":"test/output.go:<LINE>","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":{"test":true}}
{"caller":"test/output.go:<LINE>","msg":"map keys","v":0}
`,
} {
mapping[key] = value
Expand Down

0 comments on commit 3a8da04

Please sign in to comment.