@@ -4,93 +4,94 @@ package msg_test
44
55import (
66 "errors"
7- "log"
7+ "strings"
8+ "testing"
89
910 "github.com/rokath/trice/pkg/msg"
11+ "github.com/rokath/trice/pkg/tst"
1012)
1113
12- func ExampleInfo () {
13- msg .Info ("code issue" )
14- // Output:
15- // Error in msg_blackbox_test.go:14: func 'github.com/rokath/trice/pkg/msg_test.ExampleInfo' -> code issue
14+ func TestInfoOutput (t * testing.T ) {
15+ out := tst .CaptureStdOut (func () {
16+ msg .Info ("code issue" )
17+ })
18+ if ! strings .Contains (out , "code issue" ) {
19+ t .Fatalf ("missing message in output: %q" , out )
20+ }
21+ if ! strings .Contains (out , "msg_blackbox_test.go" ) {
22+ t .Fatalf ("missing file marker in output: %q" , out )
23+ }
1624}
1725
18- func ExampleOnErr () {
19- var e error
20- msg .OnErr (e )
21- e = errors .New ("s.th. went wrong" )
22- msg .OnErr (e )
23- // Output:
24- // Error in msg_blackbox_test.go:23: func 'github.com/rokath/trice/pkg/msg_test.ExampleOnErr' -> s.th. went wrong
26+ func TestOnErrOutput (t * testing.T ) {
27+ out := tst .CaptureStdOut (func () {
28+ msg .OnErr (nil )
29+ msg .OnErr (errors .New ("s.th. went wrong" ))
30+ })
31+ if ! strings .Contains (out , "s.th. went wrong" ) {
32+ t .Fatalf ("missing error string in output: %q" , out )
33+ }
2534}
2635
27- func ExampleFatalOnErr () {
28- log .SetFlags (0 )
29- var e error
30- msg .FatalOnErr (e )
31- // Output:
36+ func TestInfoOnErrOutput (t * testing.T ) {
37+ out := tst .CaptureStdOut (func () {
38+ msg .InfoOnErr (nil , "just in case" )
39+ msg .InfoOnErr (errors .New ("s.th. went wrong" ), "just in case" )
40+ })
41+ if ! strings .Contains (out , "just in case" ) {
42+ t .Fatalf ("missing info line in output: %q" , out )
43+ }
44+ if ! strings .Contains (out , "s.th. went wrong" ) {
45+ t .Fatalf ("missing error line in output: %q" , out )
46+ }
3247}
3348
34- func ExampleInfoOnErr () {
35- var e error
36- msg .InfoOnErr (e , "just in case" )
37- e = errors .New ("s.th. went wrong" )
38- msg .InfoOnErr (e , "just in case" )
39- // Output:
40- // just in case
41- // Error in msg_blackbox_test.go:39: func 'github.com/rokath/trice/pkg/msg_test.ExampleInfoOnErr' -> s.th. went wrong
49+ func TestOnTrueAndOnFalseOutput (t * testing.T ) {
50+ outTrue := tst .CaptureStdOut (func () {
51+ msg .OnTrue (false )
52+ msg .OnTrue (true )
53+ })
54+ if ! strings .Contains (outTrue , "<nil>" ) {
55+ t .Fatalf ("missing OnTrue marker in output: %q" , outTrue )
56+ }
57+
58+ outFalse := tst .CaptureStdOut (func () {
59+ msg .OnFalse (true )
60+ msg .OnFalse (false )
61+ })
62+ if ! strings .Contains (outFalse , "<nil>" ) {
63+ t .Fatalf ("missing OnFalse marker in output: %q" , outFalse )
64+ }
4265}
4366
44- func ExampleFatalInfoOnErr () {
45- var e error
46- msg .FatalInfoOnErr (e , "just in case" )
47- // Output:
67+ func TestInfoOnTrueAndInfoOnFalseOutput (t * testing.T ) {
68+ outTrue := tst .CaptureStdOut (func () {
69+ msg .InfoOnTrue (false , "just in case" )
70+ msg .InfoOnTrue (true , "just in case" )
71+ })
72+ if ! strings .Contains (outTrue , "just in case" ) {
73+ t .Fatalf ("missing InfoOnTrue text in output: %q" , outTrue )
74+ }
75+
76+ outFalse := tst .CaptureStdOut (func () {
77+ msg .InfoOnFalse (true , "just in case" )
78+ msg .InfoOnFalse (false , "just in case" )
79+ })
80+ if ! strings .Contains (outFalse , "just in case" ) {
81+ t .Fatalf ("missing InfoOnFalse text in output: %q" , outFalse )
82+ }
4883}
4984
50- func ExampleOnTrue () {
51- msg .OnTrue (false )
52- msg .OnTrue (true )
53- // Output:
54- // Error in msg_blackbox_test.go:53: func 'github.com/rokath/trice/pkg/msg_test.ExampleOnTrue' -> <nil>
55- }
56-
57- func ExampleFatalOnTrue () {
58- msg .FatalOnTrue (false )
59- // Output:
60- }
61-
62- func ExampleInfoOnTrue () {
63- msg .InfoOnTrue (false , "just in case" )
64- msg .InfoOnTrue (true , "just in case" )
65- // Output:
66- // Error in msg_blackbox_test.go:65: func 'github.com/rokath/trice/pkg/msg_test.ExampleInfoOnTrue' -> just in case
67- }
68-
69- func ExampleFatalInfoOnTrue () {
70- msg .FatalInfoOnTrue (false , "just in case" )
71- // Output:
72- }
73-
74- func ExampleOnFalse () {
75- msg .OnFalse (true )
76- msg .OnFalse (false )
77- // Output:
78- // Error in msg_blackbox_test.go:77: func 'github.com/rokath/trice/pkg/msg_test.ExampleOnFalse' -> <nil>
79- }
80-
81- func ExampleFatalOnFalse () {
82- msg .FatalOnFalse (true )
83- // Output:
84- }
85-
86- func ExampleInfoOnFalse () {
87- msg .InfoOnFalse (true , "just in case" )
88- msg .InfoOnFalse (false , "just in case" )
89- // Output:
90- // Error in msg_blackbox_test.go:89: func 'github.com/rokath/trice/pkg/msg_test.ExampleInfoOnFalse' -> just in case
91- }
92-
93- func ExampleFatalInfoOnFalse () {
94- msg .FatalInfoOnFalse (true , "just in case" )
95- // Output:
85+ func TestFatalFunctionsNoopPaths (t * testing.T ) {
86+ out := tst .CaptureStdOut (func () {
87+ msg .FatalOnErr (nil )
88+ msg .FatalInfoOnErr (nil , "just in case" )
89+ msg .FatalOnTrue (false )
90+ msg .FatalInfoOnTrue (false , "just in case" )
91+ msg .FatalOnFalse (true )
92+ msg .FatalInfoOnFalse (true , "just in case" )
93+ })
94+ if out != "" {
95+ t .Fatalf ("expected no output for noop fatal paths, got: %q" , out )
96+ }
9697}
0 commit comments