88 "bytes"
99 "go/token"
1010 "os"
11+ "reflect"
12+ "slices"
1113 "testing"
1214
1315 "golang.org/x/tools/internal/expect"
@@ -18,11 +20,13 @@ func TestMarker(t *testing.T) {
1820 filename string
1921 expectNotes int
2022 expectMarkers map [string ]string
21- expectChecks map [string ][]interface {}
23+ expectChecks map [string ][]any
24+ // expectChecks holds {"id": values} for each call check(id, values...).
25+ // Any named k=v arguments become a final map[string]any argument.
2226 }{
2327 {
2428 filename : "testdata/test.go" ,
25- expectNotes : 13 ,
29+ expectNotes : 14 ,
2630 expectMarkers : map [string ]string {
2731 "αSimpleMarker" : "α" ,
2832 "OffsetMarker" : "β" ,
@@ -36,10 +40,15 @@ func TestMarker(t *testing.T) {
3640 "NonIdentifier" : "+" ,
3741 "StringMarker" : "\" hello\" " ,
3842 },
39- expectChecks : map [string ][]interface {} {
43+ expectChecks : map [string ][]any {
4044 "αSimpleMarker" : nil ,
4145 "StringAndInt" : {"Number %d" , int64 (12 )},
4246 "Bool" : {true },
47+ "NamedArgs" : {int64 (1 ), true , expect .Identifier ("a" ), map [string ]any {
48+ "b" : int64 (1 ),
49+ "c" : "3" ,
50+ "d" : true ,
51+ }},
4352 },
4453 },
4554 {
@@ -79,7 +88,7 @@ func TestMarker(t *testing.T) {
7988 fset := token .NewFileSet ()
8089 notes , err := expect .Parse (fset , tt .filename , content )
8190 if err != nil {
82- t .Fatalf ("Failed to extract notes: %v" , err )
91+ t .Fatalf ("Failed to extract notes:\n %v" , err )
8392 }
8493 if len (notes ) != tt .expectNotes {
8594 t .Errorf ("Expected %v notes, got %v" , tt .expectNotes , len (notes ))
@@ -99,7 +108,7 @@ func TestMarker(t *testing.T) {
99108 }
100109 ident , ok := n .Args [0 ].(expect.Identifier )
101110 if ! ok {
102- t .Errorf ("%v: identifier, got %T " , fset .Position (n .Pos ), n .Args [0 ])
111+ t .Errorf ("%v: got %v (%T), want identifier " , fset .Position (n .Pos ), n . Args [ 0 ] , n .Args [0 ])
103112 continue
104113 }
105114 checkMarker (t , fset , readFile , markers , n .Pos , string (ident ), n .Args [1 ])
@@ -115,21 +124,27 @@ func TestMarker(t *testing.T) {
115124 }
116125 ident , ok := n .Args [0 ].(expect.Identifier )
117126 if ! ok {
118- t .Errorf ("%v: identifier, got %T " , fset .Position (n .Pos ), n .Args [0 ])
127+ t .Errorf ("%v: got %v (%T), want identifier " , fset .Position (n .Pos ), n . Args [ 0 ] , n .Args [0 ])
119128 continue
120129 }
121- args , ok := tt .expectChecks [string (ident )]
130+ wantArgs , ok := tt .expectChecks [string (ident )]
122131 if ! ok {
123132 t .Errorf ("%v: unexpected check %v" , fset .Position (n .Pos ), ident )
124133 continue
125134 }
126- if len (n .Args ) != len (args )+ 1 {
127- t .Errorf ("%v: expected %v args to check, got %v" , fset .Position (n .Pos ), len (args )+ 1 , len (n .Args ))
135+ gotArgs := n .Args [1 :]
136+ if n .NamedArgs != nil {
137+ // Clip to avoid mutating Args' array.
138+ gotArgs = append (slices .Clip (gotArgs ), n .NamedArgs )
139+ }
140+
141+ if len (gotArgs ) != len (wantArgs ) {
142+ t .Errorf ("%v: expected %v args to check, got %v" , fset .Position (n .Pos ), len (wantArgs ), len (gotArgs ))
128143 continue
129144 }
130- for i , got := range n . Args [ 1 :] {
131- if args [i ] != got {
132- t .Errorf ("%v: arg %d expected %v, got %v" , fset .Position (n .Pos ), i , args [i ], got )
145+ for i := range gotArgs {
146+ if ! reflect . DeepEqual ( wantArgs [i ], gotArgs [ i ]) {
147+ t .Errorf ("%v: arg %d: expected %# v, got %# v" , fset .Position (n .Pos ), i + 1 , wantArgs [i ], gotArgs [ i ] )
133148 }
134149 }
135150 default :
0 commit comments