| 1 | package jl |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/stretchr/testify/assert" |
| 9 | "github.com/stretchr/testify/require" |
| 10 | ) |
| 11 | |
| 12 | func TestLogfmtPrinter_Print(t *testing.T) { |
| 13 | tests := []struct { |
| 14 | name string |
| 15 | json string |
| 16 | formatted string |
| 17 | color bool |
| 18 | }{{ |
| 19 | name: "basic", |
| 20 | json: `{"timestamp":"2019-01-01 15:23:45","level":"INFO","thread":"truck-manager","logger":"TruckRepairServiceOverlordManager","message":"There are 7 more trucks in the garage to fix. Get to work."}`, |
| 21 | formatted: "timestamp=2019-01-01 15:23:45 level=INFO thread=truck-manager logger=TruckRepairServiceOverlordManager message=There are 7 more trucks in the garage to fix. Get to work.\n", |
| 22 | }, { |
| 23 | name: "color", |
| 24 | json: `{"timestamp":"2019-01-01 15:23:45","level":"INFO","thread":"truck-manager","logger":"TruckRepairServiceOverlordManager","message":"There are 7 more trucks in the garage to fix. Get to work."}`, |
| 25 | color: true, |
| 26 | formatted: "\x1b[32mtimestamp\x1b[0m=2019-01-01 15:23:45 \x1b[32mlevel\x1b[0m=INFO \x1b[32mthread\x1b[0m=truck-manager \x1b[32mlogger\x1b[0m=TruckRepairServiceOverlordManager \x1b[32mmessage\x1b[0m=There are 7 more trucks in the garage to fix. Get to work.\n", |
| 27 | }} |
| 28 | for _, test := range tests { |
| 29 | t.Run(test.name, func(t *testing.T) { |
| 30 | buf := &bytes.Buffer{} |
| 31 | printer := NewLogfmtPrinter(buf) |
| 32 | printer.DisableColor = !test.color |
| 33 | entry := &Entry{ |
| 34 | Raw: []byte(test.json), |
| 35 | } |
| 36 | require.NoError(t, json.Unmarshal([]byte(test.json), &entry.Partials)) |
| 37 | printer.Print(entry) |
| 38 | assert.Equal(t, test.formatted, buf.String()) |
| 39 | }) |
| 40 | } |
| 41 | } |