File: fmt_unexported_test.go

package info (click to toggle)
golang-golang-x-xerrors 0.0~git20191204.9bdfabe-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, bullseye-backports
  • size: 156 kB
  • sloc: makefile: 2
file content (51 lines) | stat: -rw-r--r-- 1,348 bytes parent folder | download | duplicates (5)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package xerrors

import "testing"

func TestParsePrintfVerb(t *testing.T) {
	for _, test := range []struct {
		in       string
		wantSize int
		wantW    bool
	}{
		{"", 0, false},
		{"%", 1, false},
		{"%3.1", 4, false},
		{"%w", 2, true},
		{"%v", 2, false},
		{"%3.*[4]d", 8, false},
	} {
		gotSize, gotW := parsePrintfVerb(test.in)
		if gotSize != test.wantSize || gotW != test.wantW {
			t.Errorf("parsePrintfVerb(%q) = (%d, %t), want (%d, %t)",
				test.in, gotSize, gotW, test.wantSize, test.wantW)
		}
	}
}

func TestParsePercentW(t *testing.T) {
	for _, test := range []struct {
		in         string
		wantIdx    int
		wantFormat string
		wantOK     bool
	}{
		{"", -1, "", true},
		{"%", -1, "%", true},
		{"%w", 0, "%v", true},
		{"%w%w", 0, "%v%v", false},
		{"%3.2s %+q %% %w %#v", 2, "%3.2s %+q %% %v %#v", true},
		{"%3.2s %w %% %w %#v", 1, "%3.2s %v %% %v %#v", false},
	} {
		gotIdx, gotFormat, gotOK := parsePercentW(test.in)
		if gotIdx != test.wantIdx || gotFormat != test.wantFormat || gotOK != test.wantOK {
			t.Errorf("parsePercentW(%q) = (%d, %q, %t), want (%d, %q, %t)",
				test.in, gotIdx, gotFormat, gotOK, test.wantIdx, test.wantFormat, test.wantOK)

		}
	}
}