github.com/april1989/origin-go-tools@v0.0.32/internal/lsp/cache/parse_test.go (about)

     1  // Copyright 2019 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  
     5  package cache
     6  
     7  import (
     8  	"go/ast"
     9  	"go/parser"
    10  	"testing"
    11  )
    12  
    13  func TestArrayLength(t *testing.T) {
    14  	tests := []struct {
    15  		expr   string
    16  		length int
    17  	}{
    18  		{`[...]int{0,1,2,3,4,5,6,7,8,9}`, 10},
    19  		{`[...]int{9:0}`, 10},
    20  		{`[...]int{19-10:0}`, 10},
    21  		{`[...]int{19-10:0, 17-10:0, 18-10:0}`, 10},
    22  	}
    23  
    24  	for _, tt := range tests {
    25  		expr, err := parser.ParseExpr(tt.expr)
    26  		if err != nil {
    27  			t.Fatal(err)
    28  		}
    29  		l, ok := arrayLength(expr.(*ast.CompositeLit))
    30  		if !ok {
    31  			t.Errorf("arrayLength did not recognize expression %#v", expr)
    32  		}
    33  		if l != tt.length {
    34  			t.Errorf("arrayLength(%#v) = %v, want %v", expr, l, tt.length)
    35  		}
    36  	}
    37  }