...

Source file src/go/ast/ast_test.go

Documentation: go/ast

     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  package ast
     6  
     7  import (
     8  	"testing"
     9  )
    10  
    11  var comments = []struct {
    12  	list []string
    13  	text string
    14  }{
    15  	{[]string{"//"}, ""},
    16  	{[]string{"//   "}, ""},
    17  	{[]string{"//", "//", "//   "}, ""},
    18  	{[]string{"// foo   "}, "foo\n"},
    19  	{[]string{"//", "//", "// foo"}, "foo\n"},
    20  	{[]string{"// foo  bar  "}, "foo  bar\n"},
    21  	{[]string{"// foo", "// bar"}, "foo\nbar\n"},
    22  	{[]string{"// foo", "//", "//", "//", "// bar"}, "foo\n\nbar\n"},
    23  	{[]string{"// foo", "/* bar */"}, "foo\n bar\n"},
    24  	{[]string{"//", "//", "//", "// foo", "//", "//", "//"}, "foo\n"},
    25  
    26  	{[]string{"/**/"}, ""},
    27  	{[]string{"/*   */"}, ""},
    28  	{[]string{"/**/", "/**/", "/*   */"}, ""},
    29  	{[]string{"/* Foo   */"}, " Foo\n"},
    30  	{[]string{"/* Foo  Bar  */"}, " Foo  Bar\n"},
    31  	{[]string{"/* Foo*/", "/* Bar*/"}, " Foo\n Bar\n"},
    32  	{[]string{"/* Foo*/", "/**/", "/**/", "/**/", "// Bar"}, " Foo\n\nBar\n"},
    33  	{[]string{"/* Foo*/", "/*\n*/", "//", "/*\n*/", "// Bar"}, " Foo\n\nBar\n"},
    34  	{[]string{"/* Foo*/", "// Bar"}, " Foo\nBar\n"},
    35  	{[]string{"/* Foo\n Bar*/"}, " Foo\n Bar\n"},
    36  
    37  	{[]string{"// foo", "//go:noinline", "// bar", "//:baz"}, "foo\nbar\n:baz\n"},
    38  	{[]string{"// foo", "//lint123:ignore", "// bar"}, "foo\nbar\n"},
    39  }
    40  
    41  func TestCommentText(t *testing.T) {
    42  	for i, c := range comments {
    43  		list := make([]*Comment, len(c.list))
    44  		for i, s := range c.list {
    45  			list[i] = &Comment{Text: s}
    46  		}
    47  
    48  		text := (&CommentGroup{list}).Text()
    49  		if text != c.text {
    50  			t.Errorf("case %d: got %q; expected %q", i, text, c.text)
    51  		}
    52  	}
    53  }
    54  
    55  var isDirectiveTests = []struct {
    56  	in string
    57  	ok bool
    58  }{
    59  	{"abc", false},
    60  	{"go:inline", true},
    61  	{"Go:inline", false},
    62  	{"go:Inline", false},
    63  	{":inline", false},
    64  	{"lint:ignore", true},
    65  	{"lint:1234", true},
    66  	{"1234:lint", true},
    67  	{"go: inline", false},
    68  	{"go:", false},
    69  	{"go:*", false},
    70  	{"go:x*", true},
    71  	{"export foo", true},
    72  	{"extern foo", true},
    73  	{"expert foo", false},
    74  }
    75  
    76  func TestIsDirective(t *testing.T) {
    77  	for _, tt := range isDirectiveTests {
    78  		if ok := isDirective(tt.in); ok != tt.ok {
    79  			t.Errorf("isDirective(%q) = %v, want %v", tt.in, ok, tt.ok)
    80  		}
    81  	}
    82  }
    83  

View as plain text