github.com/lovishpuri/go-40569/src@v0.0.0-20230519171745-f8623e7c56cf/go/ast/issues_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 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 44 // TestIssue28089 exercises the IsGenerated function. 45 func TestIssue28089(t *testing.T) { 46 for i, test := range []struct { 47 src string 48 want bool 49 }{ 50 // No file comments. 51 {`package p`, false}, 52 // Irrelevant file comments. 53 {`// Package p doc. 54 package p`, false}, 55 // Special comment misplaced after package decl. 56 {`// Package p doc. 57 package p 58 // Code generated by gen. DO NOT EDIT. 59 `, false}, 60 // Special comment appears inside string literal. 61 {`// Package p doc. 62 package p 63 const c = "` + "`" + ` 64 // Code generated by gen. DO NOT EDIT. 65 ` + "`" + ` 66 `, false}, 67 // Special comment appears properly. 68 {`// Copyright 2019 The Go Authors. All rights reserved. 69 // Use of this source code is governed by a BSD-style 70 // license that can be found in the LICENSE file. 71 72 // Package p doc comment goes here. 73 // 74 // Code generated by gen. DO NOT EDIT. 75 package p 76 77 ... `, true}, 78 // Special comment is indented. 79 // 80 // Strictly, the indent should cause IsGenerated to 81 // yield false, but we cannot detect the indent 82 // without either source text or a token.File. 83 // In other words, the function signature cannot 84 // implement the spec. Let's brush this under the 85 // rug since well-formatted code has no indent. 86 {`// Package p doc comment goes here. 87 // 88 // Code generated by gen. DO NOT EDIT. 89 package p 90 91 ... `, true}, 92 // Special comment has unwanted spaces after "DO NOT EDIT." 93 {`// Copyright 2019 The Go Authors. All rights reserved. 94 // Use of this source code is governed by a BSD-style 95 // license that can be found in the LICENSE file. 96 97 // Package p doc comment goes here. 98 // 99 // Code generated by gen. DO NOT EDIT. 100 package p 101 102 ... `, false}, 103 // Special comment has rogue interior space. 104 {`// Code generated by gen. DO NOT EDIT. 105 package p 106 `, false}, 107 // Special comment lacks the middle portion. 108 {`// Code generated DO NOT EDIT. 109 package p 110 `, false}, 111 // Special comment (incl. "//") appears within a /* block */ comment, 112 // an obscure corner case of the spec. 113 {`/* start of a general comment 114 115 // Code generated by tool; DO NOT EDIT. 116 117 end of a general comment */ 118 119 // +build !dev 120 121 // Package comment. 122 package p 123 124 // Does match even though it's inside general comment (/*-style). 125 `, true}, 126 } { 127 fset := token.NewFileSet() 128 f, err := parser.ParseFile(fset, "", test.src, parser.PackageClauseOnly|parser.ParseComments) 129 if f == nil { 130 t.Fatalf("parse %d failed to return AST: %v", i, err) 131 } 132 133 got := ast.IsGenerated(f) 134 if got != test.want { 135 t.Errorf("%d: IsGenerated on <<%s>> returned %t", i, test.src, got) 136 } 137 } 138 139 }