-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser_test.go
94 lines (79 loc) · 2.08 KB
/
parser_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"go/ast"
"go/parser"
"go/token"
"testing"
)
func TestSorter(t *testing.T) {
fset := token.NewFileSet()
pkgs, err := parser.ParseDir(fset, "testdata", nil, parser.ParseComments)
if err != nil {
t.Fatal(err)
}
if len(pkgs) != 1 {
t.Fatalf("Expected len(pkgs) == 1, got %d", len(pkgs))
}
expected := []string{"doc.go", "a.go", "testdata.go", "z.go"}
sorted := sortedFiles(pkgs["testdata"])
if len(sorted) != len(expected) {
t.Fatalf("Expected %d, got %d", len(expected), len(sorted))
}
for i := 0; i < len(sorted); i++ {
if sorted[i].name != expected[i] {
t.Errorf("Expected %s, got %s", expected[i], sorted[i].name)
}
}
}
func TestExtractComment(t *testing.T) {
tests := []struct {
comment string
expected bool
}{
{"//+extract", true},
{"// +extract", true},
{"// +extract\n", true},
{"/*+extract*/", true},
{"/* +extract */", true},
{"/* +extract */", true},
{"/*\n+extract */", true},
{"/* +extract\nfoo */", true},
{"/* foo\n+extract */", false},
{"// extract", false},
{"// foo +extract", false},
}
for i, test := range tests {
var c ast.Comment
c.Text = test.comment
cgrp := &ast.CommentGroup{List: []*ast.Comment{&c}}
_, ok := extractComment(cgrp)
if ok != test.expected {
t.Fatalf("Iteration %d: Expected %t, got %t: %q", i, test.expected, ok, test.comment)
}
}
}
func TestParsePackage(t *testing.T) {
fset := token.NewFileSet()
pkgs, err := parser.ParseDir(fset, "testdata", nil, parser.ParseComments)
if err != nil {
t.Fatal(err)
}
if len(pkgs) != 1 {
t.Fatalf("Expected len(pkgs) == 1, got %d", len(pkgs))
}
comments := extractPackageComments(pkgs["testdata"])
expected := []string{
"More text, in doc.go\n",
"Here's a comment in a.go\n",
"A comment inside testdata.go\n",
"An interesting\nmulti-line\ncomment inside\nz.go\n",
}
if len(comments) != len(expected) {
t.Fatalf("Expected %#v, got %#v", expected, comments)
}
for i := 0; i < len(comments); i++ {
if comments[i] != expected[i] {
t.Errorf("Expected %s, got %s", expected[i], comments[i])
}
}
}