File: query_test.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.5.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports
  • size: 16,592 kB
  • sloc: javascript: 2,011; asm: 1,635; sh: 192; yacc: 155; makefile: 52; ansic: 8
file content (80 lines) | stat: -rw-r--r-- 1,949 bytes parent folder | download | duplicates (2)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2017 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 pointer

import (
	"reflect"
	"testing"

	"golang.org/x/tools/go/loader"
)

func TestParseExtendedQuery(t *testing.T) {
	const myprog = `
package pkg

import "reflect"

type T []*int

var V1 *int
var V2 **int
var V3 []*int
var V4 chan []*int
var V5 struct {F1, F2 chan *int}
var V6 [1]chan *int
var V7 int
var V8 T
var V9 reflect.Value
`
	tests := []struct {
		in    string
		out   []interface{}
		v     string
		valid bool
	}{
		{`x`, []interface{}{"x"}, "V1", true},
		{`x`, []interface{}{"x"}, "V9", true},
		{`*x`, []interface{}{"x", "load"}, "V2", true},
		{`x[0]`, []interface{}{"x", "sliceelem"}, "V3", true},
		{`x[0]`, []interface{}{"x", "sliceelem"}, "V8", true},
		{`<-x`, []interface{}{"x", "recv"}, "V4", true},
		{`(<-x)[0]`, []interface{}{"x", "recv", "sliceelem"}, "V4", true},
		{`<-x.F2`, []interface{}{"x", "field", 1, "recv"}, "V5", true},
		{`<-x[0]`, []interface{}{"x", "arrayelem", "recv"}, "V6", true},
		{`x`, nil, "V7", false},
		{`y`, nil, "V1", false},
		{`x; x`, nil, "V1", false},
		{`x()`, nil, "V1", false},
		{`close(x)`, nil, "V1", false},
	}

	var conf loader.Config
	f, err := conf.ParseFile("file.go", myprog)
	if err != nil {
		t.Fatal(err)
	}
	conf.CreateFromFiles("main", f)
	lprog, err := conf.Load()
	if err != nil {
		t.Fatal(err)
	}
	pkg := lprog.Created[0].Pkg

	for _, test := range tests {
		typ := pkg.Scope().Lookup(test.v).Type()
		ops, _, err := parseExtendedQuery(typ, test.in)
		if test.valid && err != nil {
			t.Errorf("parseExtendedQuery(%q) = %s, expected no error", test.in, err)
		}
		if !test.valid && err == nil {
			t.Errorf("parseExtendedQuery(%q) succeeded, expected error", test.in)
		}

		if !reflect.DeepEqual(ops, test.out) {
			t.Errorf("parseExtendedQuery(%q) = %#v, want %#v", test.in, ops, test.out)
		}
	}
}