Black Lives Matter. Support the Equal Justice Initiative.

Source file src/go/ast/issues_test.go

Documentation: go/ast

     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 ast_test
     6  
     7  import (
     8  	"go/ast"
     9  	"go/parser"
    10  	"go/token"
    11  	"testing"
    12  )
    13  
    14  func TestIssue33649(t *testing.T) {
    15  	for _, src := range []string{
    16  		`package p; func _()`,
    17  		`package p; func _() {`,
    18  		`package p; func _() { _ = 0`,
    19  		`package p; func _() { _ = 0 }`,
    20  	} {
    21  		fset := token.NewFileSet()
    22  		f, _ := parser.ParseFile(fset, "", src, parser.AllErrors)
    23  		if f == nil {
    24  			panic("invalid test setup: parser didn't return an AST")
    25  		}
    26  
    27  		// find corresponding token.File
    28  		var tf *token.File
    29  		fset.Iterate(func(f *token.File) bool {
    30  			tf = f
    31  			return true
    32  		})
    33  		tfEnd := tf.Base() + tf.Size()
    34  
    35  		fd := f.Decls[len(f.Decls)-1].(*ast.FuncDecl)
    36  		fdEnd := int(fd.End())
    37  
    38  		if fdEnd != tfEnd {
    39  			t.Errorf("%q: got fdEnd = %d; want %d (base = %d, size = %d)", src, fdEnd, tfEnd, tf.Base(), tf.Size())
    40  		}
    41  	}
    42  }
    43  

View as plain text