Source file src/go/ast/commentmap_test.go

     1  // Copyright 2012 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  // To avoid a cyclic dependency with go/parser, this file is in a separate package.
     6  
     7  package ast_test
     8  
     9  import (
    10  	"fmt"
    11  	. "go/ast"
    12  	"go/parser"
    13  	"go/token"
    14  	"sort"
    15  	"strings"
    16  	"testing"
    17  )
    18  
    19  const src = `
    20  // the very first comment
    21  
    22  // package p
    23  package p /* the name is p */
    24  
    25  // imports
    26  import (
    27  	"bytes"     // bytes
    28  	"fmt"       // fmt
    29  	"go/ast"
    30  	"go/parser"
    31  )
    32  
    33  // T
    34  type T struct {
    35  	a, b, c int // associated with a, b, c
    36  	// associated with x, y
    37  	x, y float64    // float values
    38  	z    complex128 // complex value
    39  }
    40  // also associated with T
    41  
    42  // x
    43  var x = 0 // x = 0
    44  // also associated with x
    45  
    46  // f1
    47  func f1() {
    48  	/* associated with s1 */
    49  	s1()
    50  	// also associated with s1
    51  
    52  	// associated with s2
    53  
    54  	// also associated with s2
    55  	s2() // line comment for s2
    56  }
    57  // associated with f1
    58  // also associated with f1
    59  
    60  // associated with f2
    61  
    62  // f2
    63  func f2() {
    64  }
    65  
    66  func f3() {
    67  	i := 1 /* 1 */ + 2 // addition
    68  	_ = i
    69  }
    70  
    71  // the very last comment
    72  `
    73  
    74  // res maps a key of the form "line number: node type"
    75  // to the associated comments' text.
    76  var res = map[string]string{
    77  	" 5: *ast.File":       "the very first comment\npackage p\n",
    78  	" 5: *ast.Ident":      " the name is p\n",
    79  	" 8: *ast.GenDecl":    "imports\n",
    80  	" 9: *ast.ImportSpec": "bytes\n",
    81  	"10: *ast.ImportSpec": "fmt\n",
    82  	"16: *ast.GenDecl":    "T\nalso associated with T\n",
    83  	"17: *ast.Field":      "associated with a, b, c\n",
    84  	"19: *ast.Field":      "associated with x, y\nfloat values\n",
    85  	"20: *ast.Field":      "complex value\n",
    86  	"25: *ast.GenDecl":    "x\nx = 0\nalso associated with x\n",
    87  	"29: *ast.FuncDecl":   "f1\nassociated with f1\nalso associated with f1\n",
    88  	"31: *ast.ExprStmt":   " associated with s1\nalso associated with s1\n",
    89  	"37: *ast.ExprStmt":   "associated with s2\nalso associated with s2\nline comment for s2\n",
    90  	"45: *ast.FuncDecl":   "associated with f2\nf2\n",
    91  	"49: *ast.AssignStmt": "addition\n",
    92  	"49: *ast.BasicLit":   " 1\n",
    93  	"50: *ast.Ident":      "the very last comment\n",
    94  }
    95  
    96  func ctext(list []*CommentGroup) string {
    97  	var buf strings.Builder
    98  	for _, g := range list {
    99  		buf.WriteString(g.Text())
   100  	}
   101  	return buf.String()
   102  }
   103  
   104  func TestCommentMap(t *testing.T) {
   105  	fset := token.NewFileSet()
   106  	f, err := parser.ParseFile(fset, "", src, parser.ParseComments)
   107  	if err != nil {
   108  		t.Fatal(err)
   109  	}
   110  	cmap := NewCommentMap(fset, f, f.Comments)
   111  
   112  	// very correct association of comments
   113  	for n, list := range cmap {
   114  		key := fmt.Sprintf("%2d: %T", fset.Position(n.Pos()).Line, n)
   115  		got := ctext(list)
   116  		want := res[key]
   117  		if got != want {
   118  			t.Errorf("%s: got %q; want %q", key, got, want)
   119  		}
   120  	}
   121  
   122  	// verify that no comments got lost
   123  	if n := len(cmap.Comments()); n != len(f.Comments) {
   124  		t.Errorf("got %d comment groups in map; want %d", n, len(f.Comments))
   125  	}
   126  
   127  	// support code to update test:
   128  	// set genMap to true to generate res map
   129  	const genMap = false
   130  	if genMap {
   131  		out := make([]string, 0, len(cmap))
   132  		for n, list := range cmap {
   133  			out = append(out, fmt.Sprintf("\t\"%2d: %T\":\t%q,", fset.Position(n.Pos()).Line, n, ctext(list)))
   134  		}
   135  		sort.Strings(out)
   136  		for _, s := range out {
   137  			fmt.Println(s)
   138  		}
   139  	}
   140  }
   141  
   142  func TestFilter(t *testing.T) {
   143  	fset := token.NewFileSet()
   144  	f, err := parser.ParseFile(fset, "", src, parser.ParseComments)
   145  	if err != nil {
   146  		t.Fatal(err)
   147  	}
   148  	cmap := NewCommentMap(fset, f, f.Comments)
   149  
   150  	// delete variable declaration
   151  	for i, decl := range f.Decls {
   152  		if gen, ok := decl.(*GenDecl); ok && gen.Tok == token.VAR {
   153  			copy(f.Decls[i:], f.Decls[i+1:])
   154  			f.Decls = f.Decls[:len(f.Decls)-1]
   155  			break
   156  		}
   157  	}
   158  
   159  	// check if comments are filtered correctly
   160  	cc := cmap.Filter(f)
   161  	for n, list := range cc {
   162  		key := fmt.Sprintf("%2d: %T", fset.Position(n.Pos()).Line, n)
   163  		got := ctext(list)
   164  		want := res[key]
   165  		if key == "25: *ast.GenDecl" || got != want {
   166  			t.Errorf("%s: got %q; want %q", key, got, want)
   167  		}
   168  	}
   169  }
   170  

View as plain text