GoPLS Viewer

Home|gopls/go/expect/expect_test.go
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 expect_test
6
7import (
8    "bytes"
9    "go/token"
10    "io/ioutil"
11    "testing"
12
13    "golang.org/x/tools/go/expect"
14)
15
16func TestMarker(t *testing.T) {
17    for _tt := range []struct {
18        filename      string
19        expectNotes   int
20        expectMarkers map[string]string
21        expectChecks  map[string][]interface{}
22    }{
23        {
24            filename:    "testdata/test.go",
25            expectNotes13,
26            expectMarkers: map[string]string{
27                "αSimpleMarker""α",
28                "OffsetMarker":  "β",
29                "RegexMarker":   "γ",
30                "εMultiple":     "ε",
31                "ζMarkers":      "ζ",
32                "ηBlockMarker":  "η",
33                "Declared":      "η",
34                "Comment":       "ι",
35                "LineComment":   "someFunc",
36                "NonIdentifier""+",
37                "StringMarker":  "\"hello\"",
38            },
39            expectChecks: map[string][]interface{}{
40                "αSimpleMarker"nil,
41                "StringAndInt":  {"Number %d"int64(12)},
42                "Bool":          {true},
43            },
44        },
45        {
46            filename:    "testdata/go.mod",
47            expectNotes2,
48            expectMarkers: map[string]string{
49                "αMarker""αfake1α",
50                "βMarker""require golang.org/modfile v0.0.0",
51            },
52        },
53    } {
54        t.Run(tt.filename, func(t *testing.T) {
55            contenterr := ioutil.ReadFile(tt.filename)
56            if err != nil {
57                t.Fatal(err)
58            }
59            readFile := func(string) ([]byteerror) { return contentnil }
60
61            markers := make(map[string]token.Pos)
62            for nametok := range tt.expectMarkers {
63                offset := bytes.Index(content, []byte(tok))
64                markers[name] = token.Pos(offset + 1)
65                end := bytes.Index(content[offset:], []byte(tok))
66                if end > 0 {
67                    markers[name+"@"] = token.Pos(offset + end + 2)
68                }
69            }
70
71            fset := token.NewFileSet()
72            noteserr := expect.Parse(fsettt.filenamecontent)
73            if err != nil {
74                t.Fatalf("Failed to extract notes: %v"err)
75            }
76            if len(notes) != tt.expectNotes {
77                t.Errorf("Expected %v notes, got %v"tt.expectNoteslen(notes))
78            }
79            for _n := range notes {
80                switch {
81                case n.Args == nil:
82                    // A //@foo note associates the name foo with the position of the
83                    // first match of "foo" on the current line.
84                    checkMarker(tfsetreadFilemarkersn.Posn.Namen.Name)
85                case n.Name == "mark":
86                    // A //@mark(name, "pattern") note associates the specified name
87                    // with the position on the first match of pattern on the current line.
88                    if len(n.Args) != 2 {
89                        t.Errorf("%v: expected 2 args to mark, got %v"fset.Position(n.Pos), len(n.Args))
90                        continue
91                    }
92                    identok := n.Args[0].(expect.Identifier)
93                    if !ok {
94                        t.Errorf("%v: identifier, got %T"fset.Position(n.Pos), n.Args[0])
95                        continue
96                    }
97                    checkMarker(tfsetreadFilemarkersn.Posstring(ident), n.Args[1])
98
99                case n.Name == "check":
100                    // A //@check(args, ...) note specifies some hypothetical action to
101                    // be taken by the test driver and its expected outcome.
102                    // In this test, the action is to compare the arguments
103                    // against expectChecks.
104                    if len(n.Args) < 1 {
105                        t.Errorf("%v: expected 1 args to check, got %v"fset.Position(n.Pos), len(n.Args))
106                        continue
107                    }
108                    identok := n.Args[0].(expect.Identifier)
109                    if !ok {
110                        t.Errorf("%v: identifier, got %T"fset.Position(n.Pos), n.Args[0])
111                        continue
112                    }
113                    argsok := tt.expectChecks[string(ident)]
114                    if !ok {
115                        t.Errorf("%v: unexpected check %v"fset.Position(n.Pos), ident)
116                        continue
117                    }
118                    if len(n.Args) != len(args)+1 {
119                        t.Errorf("%v: expected %v args to check, got %v"fset.Position(n.Pos), len(args)+1len(n.Args))
120                        continue
121                    }
122                    for igot := range n.Args[1:] {
123                        if args[i] != got {
124                            t.Errorf("%v: arg %d expected %v, got %v"fset.Position(n.Pos), iargs[i], got)
125                        }
126                    }
127                default:
128                    t.Errorf("Unexpected note %v at %v"n.Namefset.Position(n.Pos))
129                }
130            }
131        })
132    }
133}
134
135func checkMarker(t *testing.Tfset *token.FileSetreadFile expect.ReadFilemarkers map[string]token.Pospos token.Posname stringpattern interface{}) {
136    startenderr := expect.MatchBefore(fsetreadFilepospattern)
137    if err != nil {
138        t.Errorf("%v: MatchBefore failed: %v"fset.Position(pos), err)
139        return
140    }
141    if start == token.NoPos {
142        t.Errorf("%v: Pattern %v did not match"fset.Position(pos), pattern)
143        return
144    }
145    expectStartok := markers[name]
146    if !ok {
147        t.Errorf("%v: unexpected marker %v"fset.Position(pos), name)
148        return
149    }
150    if start != expectStart {
151        t.Errorf("%v: Expected %v got %v"fset.Position(pos), fset.Position(expectStart), fset.Position(start))
152    }
153    if expectEndok := markers[name+"@"]; ok && end != expectEnd {
154        t.Errorf("%v: Expected end %v got %v"fset.Position(pos), fset.Position(expectEnd), fset.Position(end))
155    }
156}
157
MembersX
checkMarker
checkMarker.pos
TestMarker
TestMarker.RangeStmt_305.BlockStmt.BlockStmt.RangeStmt_1467.tok
TestMarker.RangeStmt_305.BlockStmt.BlockStmt.err
TestMarker.RangeStmt_305.BlockStmt.BlockStmt.RangeStmt_1467.BlockStmt.offset
TestMarker.RangeStmt_305.BlockStmt.BlockStmt.RangeStmt_1467.BlockStmt.end
TestMarker.RangeStmt_305.BlockStmt.BlockStmt.fset
checkMarker.readFile
checkMarker.markers
checkMarker.name
TestMarker.RangeStmt_305.tt
checkMarker.err
checkMarker.pattern
TestMarker.RangeStmt_305.BlockStmt.BlockStmt.RangeStmt_2011.n
TestMarker.RangeStmt_305.BlockStmt.BlockStmt.RangeStmt_2011.BlockStmt.BlockStmt.RangeStmt_3685.i
checkMarker.t
TestMarker.RangeStmt_305.BlockStmt.BlockStmt.content
checkMarker.fset
expect
TestMarker.t
TestMarker.RangeStmt_305.BlockStmt.BlockStmt.markers
TestMarker.RangeStmt_305.BlockStmt.BlockStmt.notes
TestMarker.RangeStmt_305.BlockStmt.BlockStmt.RangeStmt_2011.BlockStmt.BlockStmt.RangeStmt_3685.got
checkMarker.start
testing
TestMarker.RangeStmt_305.BlockStmt.BlockStmt.RangeStmt_1467.name
checkMarker.end
ioutil
Members
X