This repository has been archived by the owner on Dec 22, 2018. It is now read-only.
/
parser_test.go
112 lines (94 loc) · 2.3 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"go/ast"
"go/parser"
"go/token"
"testing"
)
func TestStripExt(t *testing.T) {
tests := []struct {
input, expected string
}{
{"foo.bar", "foo"},
{"foo.bar.baz", "foo.bar"},
{"foo", "foo"},
{".asdf", ""},
}
for i, test := range tests {
s := stripExt(test.input)
if s != test.expected {
t.Fatalf("Iteration %d: Expected %s, got %s", i, test.expected, s)
}
}
}
func TestSorter(t *testing.T) {
fset := token.NewFileSet()
pkgs, err := parser.ParseDir(fset, "test", 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{"test", "doc", "a", "z"}
sorter := newFileSorter(pkgs["test"])
if len(sorter.names) != len(expected) {
t.Fatalf("Expected %#v, got %#v", expected, sorter.names)
}
for i := 0; i < len(sorter.names); i++ {
if sorter.names[i] != expected[i] {
t.Fatalf("Expected %s, got %s", expected, sorter.names)
}
}
}
func TestRSTComment(t *testing.T) {
tests := []struct {
comment string
expected bool
}{
{"//+rst", true},
{"// +rst", true},
{"// +rst\n", true},
{"/*+rst*/", true},
{"/* +rst */", true},
{"/* +rst */", true},
{"/*\n+rst */", true},
{"/* +rst\nfoo */", true},
{"/* foo\n+rst */", false},
{"// rst", false},
}
for i, test := range tests {
var c ast.Comment
c.Text = test.comment
cgrp := &ast.CommentGroup{List: []*ast.Comment{&c}}
_, ok := rstComment(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, "test", nil, parser.ParseComments)
if err != nil {
t.Fatal(err)
}
if len(pkgs) != 1 {
t.Fatalf("Expected len(pkgs) == 1, got %d", len(pkgs))
}
comments := parsePackage(pkgs["test"])
expected := []string{
"A comment inside test.go\n",
"More restructured text, in doc.go\n",
"Here's a comment in a.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.Fatalf("Expected %s, got %s", expected, comments)
}
}
}