File: formatwriter_plain_test.go

package info (click to toggle)
golang-github-cactus-mlog 1.0.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 272 kB
  • sloc: makefile: 2
file content (35 lines) | stat: -rw-r--r-- 986 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Copyright (c) 2012-2016 Eli Janssen
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

package mlog

import (
	"bytes"
	"testing"

	"gotest.tools/v3/assert"
	is "gotest.tools/v3/assert/cmp"
)

func TestFormatWriterPlainEncodeString(t *testing.T) {
	var stringTests = map[string]struct {
		input  string
		output string
	}{
		"generic":           {`test`, `test`},
		"quote":             {`"this"`, `"this"`},
		"r&n":               {"te\r\nst", `te\r\nst`},
		"tab":               {"\t what", `\t what`},
		"weird chars":       {"\u2028 \u2029", "\u2028 \u2029"},
		"other weird chars": {`"\u003c\u0026\u003e"`, `"\u003c\u0026\u003e"`},
		"invalid utf8":      {"\xff\xff\xffhello", `\ufffd\ufffd\ufffdhello`},
	}

	b := &bytes.Buffer{}
	for name, tt := range stringTests {
		b.Truncate(0)
		encodeStringPlain(b, tt.input)
		assert.Check(t, is.DeepEqual([]byte(tt.output), b.Bytes()), "%s: did not match expectation", name)
	}
}