| 35 | } |
| 36 | |
| 37 | func TestQuoting(t *testing.T) { |
| 38 | tf := &TextFormatter{DisableColors: true} |
| 39 | |
| 40 | checkQuoting := func(q bool, value interface{}) { |
| 41 | b, _ := tf.Format(WithField("test", value)) |
| 42 | idx := bytes.Index(b, ([]byte)("test=")) |
| 43 | cont := bytes.Contains(b[idx+5:], []byte("\"")) |
| 44 | if cont != q { |
| 45 | if q { |
| 46 | t.Errorf("quoting expected for: %#v", value) |
| 47 | } else { |
| 48 | t.Errorf("quoting not expected for: %#v", value) |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | checkQuoting(false, "") |
| 54 | checkQuoting(false, "abcd") |
| 55 | checkQuoting(false, "v1.0") |
| 56 | checkQuoting(false, "1234567890") |
| 57 | checkQuoting(false, "/foobar") |
| 58 | checkQuoting(false, "foo_bar") |
| 59 | checkQuoting(false, "foo@bar") |
| 60 | checkQuoting(false, "foobar^") |
| 61 | checkQuoting(false, "+/-_^@f.oobar") |
| 62 | checkQuoting(true, "foo\n\rbar") |
| 63 | checkQuoting(true, "foobar$") |
| 64 | checkQuoting(true, "&foobar") |
| 65 | checkQuoting(true, "x y") |
| 66 | checkQuoting(true, "x,y") |
| 67 | checkQuoting(false, errors.New("invalid")) |
| 68 | checkQuoting(true, errors.New("invalid argument")) |
| 69 | |
| 70 | // Test for quoting empty fields. |
| 71 | tf.QuoteEmptyFields = true |
| 72 | checkQuoting(true, "") |
| 73 | checkQuoting(false, "abcd") |
| 74 | checkQuoting(true, "foo\n\rbar") |
| 75 | checkQuoting(true, errors.New("invalid argument")) |
| 76 | |
| 77 | // Test forcing quotes. |
| 78 | tf.ForceQuote = true |
| 79 | checkQuoting(true, "") |
| 80 | checkQuoting(true, "abcd") |
| 81 | checkQuoting(true, "foo\n\rbar") |
| 82 | checkQuoting(true, errors.New("invalid argument")) |
| 83 | |
| 84 | // Test forcing quotes when also disabling them. |
| 85 | tf.DisableQuote = true |
| 86 | checkQuoting(true, "") |
| 87 | checkQuoting(true, "abcd") |
| 88 | checkQuoting(true, "foo\n\rbar") |
| 89 | checkQuoting(true, errors.New("invalid argument")) |
| 90 | |
| 91 | // Test disabling quotes |
| 92 | tf.ForceQuote = false |
| 93 | tf.QuoteEmptyFields = false |
| 94 | checkQuoting(false, "") |