1// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package xerrors
6
7import "testing"
8
9func TestParsePrintfVerb(t *testing.T) {
10	for _, test := range []struct {
11		in       string
12		wantSize int
13		wantW    bool
14	}{
15		{"", 0, false},
16		{"%", 1, false},
17		{"%3.1", 4, false},
18		{"%w", 2, true},
19		{"%v", 2, false},
20		{"%3.*[4]d", 8, false},
21	} {
22		gotSize, gotW := parsePrintfVerb(test.in)
23		if gotSize != test.wantSize || gotW != test.wantW {
24			t.Errorf("parsePrintfVerb(%q) = (%d, %t), want (%d, %t)",
25				test.in, gotSize, gotW, test.wantSize, test.wantW)
26		}
27	}
28}
29
30func TestParsePercentW(t *testing.T) {
31	for _, test := range []struct {
32		in         string
33		wantIdx    int
34		wantFormat string
35		wantOK     bool
36	}{
37		{"", -1, "", true},
38		{"%", -1, "%", true},
39		{"%w", 0, "%v", true},
40		{"%w%w", 0, "%v%v", false},
41		{"%3.2s %+q %% %w %#v", 2, "%3.2s %+q %% %v %#v", true},
42		{"%3.2s %w %% %w %#v", 1, "%3.2s %v %% %v %#v", false},
43	} {
44		gotIdx, gotFormat, gotOK := parsePercentW(test.in)
45		if gotIdx != test.wantIdx || gotFormat != test.wantFormat || gotOK != test.wantOK {
46			t.Errorf("parsePercentW(%q) = (%d, %q, %t), want (%d, %q, %t)",
47				test.in, gotIdx, gotFormat, gotOK, test.wantIdx, test.wantFormat, test.wantOK)
48
49		}
50	}
51}
52