1// Copyright 2020 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 completion
6
7import (
8	"fmt"
9	"testing"
10)
11
12func TestFormatOperandKind(t *testing.T) {
13	cases := []struct {
14		f    string
15		idx  int
16		kind objKind
17	}{
18		{"", 1, kindAny},
19		{"%", 1, kindAny},
20		{"%%%", 1, kindAny},
21		{"%[1", 1, kindAny},
22		{"%[?%s", 2, kindAny},
23		{"%[abc]v", 1, kindAny},
24
25		{"%v", 1, kindAny},
26		{"%T", 1, kindAny},
27		{"%t", 1, kindBool},
28		{"%d", 1, kindInt},
29		{"%c", 1, kindInt},
30		{"%o", 1, kindInt},
31		{"%O", 1, kindInt},
32		{"%U", 1, kindInt},
33		{"%e", 1, kindFloat | kindComplex},
34		{"%E", 1, kindFloat | kindComplex},
35		{"%f", 1, kindFloat | kindComplex},
36		{"%F", 1, kindFloat | kindComplex},
37		{"%g", 1, kindFloat | kindComplex},
38		{"%G", 1, kindFloat | kindComplex},
39		{"%b", 1, kindInt | kindFloat | kindComplex | kindBytes},
40		{"%q", 1, kindString | kindBytes | kindStringer | kindError},
41		{"%s", 1, kindString | kindBytes | kindStringer | kindError},
42		{"%x", 1, kindString | kindBytes | kindInt | kindFloat | kindComplex},
43		{"%X", 1, kindString | kindBytes | kindInt | kindFloat | kindComplex},
44		{"%p", 1, kindPtr | kindSlice},
45		{"%w", 1, kindError},
46
47		{"%1.2f", 1, kindFloat | kindComplex},
48		{"%*f", 1, kindInt},
49		{"%*f", 2, kindFloat | kindComplex},
50		{"%*.*f", 1, kindInt},
51		{"%*.*f", 2, kindInt},
52		{"%*.*f", 3, kindFloat | kindComplex},
53		{"%[3]*.[2]*[1]f", 1, kindFloat | kindComplex},
54		{"%[3]*.[2]*[1]f", 2, kindInt},
55		{"%[3]*.[2]*[1]f", 3, kindInt},
56
57		{"foo %% %d", 1, kindInt},
58		{"%#-12.34f", 1, kindFloat | kindComplex},
59		{"% d", 1, kindInt},
60
61		{"%s %[1]X %d", 1, kindString | kindBytes},
62		{"%s %[1]X %d", 2, kindInt},
63	}
64
65	for _, c := range cases {
66		t.Run(fmt.Sprintf("%q#%d", c.f, c.idx), func(t *testing.T) {
67			if got := formatOperandKind(c.f, c.idx); got != c.kind {
68				t.Errorf("expected %d (%[1]b), got %d (%[2]b)", c.kind, got)
69			}
70		})
71	}
72}
73