| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package severity_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"context" |
| 11 |
"strings" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"golang.org/x/exp/event" |
| 15 |
"golang.org/x/exp/event/adapter/logfmt" |
| 16 |
"golang.org/x/exp/event/eventtest" |
| 17 |
"golang.org/x/exp/event/severity" |
| 18 |
) |
| 19 |
|
| 20 |
func TestPrint(t *testing.T) { |
| 21 |
ctx := context.Background() |
| 22 |
for _, test := range []struct { |
| 23 |
name string |
| 24 |
events func(context.Context) |
| 25 |
expect string |
| 26 |
}{{ |
| 27 |
name: "debug", |
| 28 |
events: func(ctx context.Context) { severity.Debug.Log(ctx, "a message") }, |
| 29 |
expect: `time="2020/03/05 14:27:48" level=debug msg="a message"`, |
| 30 |
}, { |
| 31 |
name: "info", |
| 32 |
events: func(ctx context.Context) { severity.Info.Log(ctx, "a message") }, |
| 33 |
expect: `time="2020/03/05 14:27:48" level=info msg="a message"`}, |
| 34 |
} { |
| 35 |
buf := &strings.Builder{} |
| 36 |
ctx := event.WithExporter(ctx, event.NewExporter(logfmt.NewHandler(buf), eventtest.ExporterOptions())) |
| 37 |
test.events(ctx) |
| 38 |
got := strings.TrimSpace(buf.String()) |
| 39 |
expect := strings.TrimSpace(test.expect) |
| 40 |
if got != expect { |
| 41 |
t.Errorf("%s failed\ngot : %q\nexpect: %q", test.name, got, expect) |
| 42 |
} |
| 43 |
} |
| 44 |
} |