1
1
package main
2
2
3
3
import (
4
+ "fmt"
4
5
"path/filepath"
5
6
"runtime"
6
7
"strconv"
@@ -10,11 +11,12 @@ import (
10
11
type F map [TagField ]string
11
12
12
13
var testCases = []struct {
13
- filename string
14
- relative bool
15
- basepath string
16
- minversion string
17
- tags []Tag
14
+ filename string
15
+ relative bool
16
+ basepath string
17
+ minversion string
18
+ tags []Tag
19
+ excludePrivate bool
18
20
}{
19
21
{filename : "tests/const.go-src" , tags : []Tag {
20
22
tag ("Test" , 1 , "p" , F {}),
@@ -95,6 +97,51 @@ var testCases = []struct {
95
97
tag ("fmt" , 3 , "i" , F {}),
96
98
tag ("main" , 5 , "f" , F {"access" : "private" , "signature" : "()" }),
97
99
}},
100
+ // excludePrivate tests
101
+ {filename : "tests/func.go-src" , excludePrivate : true , tags : []Tag {
102
+ tag ("Test" , 1 , "p" , F {}),
103
+ tag ("Function1" , 3 , "f" , F {"access" : "public" , "signature" : "()" , "type" : "string" }),
104
+ }},
105
+ {filename : "tests/interface.go-src" , excludePrivate : true , tags : []Tag {
106
+ tag ("Test" , 1 , "p" , F {}),
107
+ tag ("InterfaceMethod" , 4 , "m" , F {"access" : "public" , "signature" : "(int)" , "ntype" : "Interface" , "type" : "string" }),
108
+ tag ("OtherMethod" , 5 , "m" , F {"access" : "public" , "signature" : "()" , "ntype" : "Interface" }),
109
+ tag ("io.Reader" , 6 , "e" , F {"access" : "public" , "ntype" : "Interface" }),
110
+ tag ("Interface" , 3 , "n" , F {"access" : "public" , "type" : "interface" }),
111
+ }},
112
+ {filename : "tests/struct.go-src" , excludePrivate : true , tags : []Tag {
113
+ tag ("Test" , 1 , "p" , F {}),
114
+ tag ("Field1" , 4 , "w" , F {"access" : "public" , "ctype" : "Struct" , "type" : "int" }),
115
+ tag ("Field2" , 4 , "w" , F {"access" : "public" , "ctype" : "Struct" , "type" : "int" }),
116
+ tag ("Struct" , 3 , "t" , F {"access" : "public" , "type" : "struct" }),
117
+ tag ("Struct" , 20 , "e" , F {"access" : "public" , "ctype" : "TestEmbed" , "type" : "Struct" }),
118
+ tag ("*io.Writer" , 21 , "e" , F {"access" : "public" , "ctype" : "TestEmbed" , "type" : "*io.Writer" }),
119
+ tag ("TestEmbed" , 19 , "t" , F {"access" : "public" , "type" : "struct" }),
120
+ tag ("Struct2" , 27 , "t" , F {"access" : "public" , "type" : "struct" }),
121
+ tag ("Connection" , 36 , "t" , F {"access" : "public" , "type" : "struct" }),
122
+ tag ("NewStruct" , 9 , "f" , F {"access" : "public" , "ctype" : "Struct" , "signature" : "()" , "type" : "*Struct" }),
123
+ tag ("F1" , 13 , "m" , F {"access" : "public" , "ctype" : "Struct" , "signature" : "()" , "type" : "[]bool, [2]*string" }),
124
+ tag ("F2" , 16 , "m" , F {"access" : "public" , "ctype" : "Struct" , "signature" : "()" , "type" : "bool" }),
125
+ tag ("NewTestEmbed" , 24 , "f" , F {"access" : "public" , "ctype" : "TestEmbed" , "signature" : "()" , "type" : "TestEmbed" }),
126
+ tag ("NewStruct2" , 30 , "f" , F {"access" : "public" , "ctype" : "Struct2" , "signature" : "()" , "type" : "*Struct2, error" }),
127
+ tag ("Dial" , 33 , "f" , F {"access" : "public" , "ctype" : "Connection" , "signature" : "()" , "type" : "*Connection, error" }),
128
+ tag ("Dial2" , 39 , "f" , F {"access" : "public" , "ctype" : "Connection" , "signature" : "()" , "type" : "*Connection, *Struct2" }),
129
+ tag ("Dial3" , 42 , "f" , F {"access" : "public" , "signature" : "()" , "type" : "*Connection, *Connection" }),
130
+ }},
131
+ {filename : "tests/type.go-src" , excludePrivate : true , tags : []Tag {
132
+ tag ("Test" , 1 , "p" , F {}),
133
+ }},
134
+ {filename : "tests/var.go-src" , excludePrivate : true , tags : []Tag {
135
+ tag ("Test" , 1 , "p" , F {}),
136
+ tag ("A" , 7 , "v" , F {"access" : "public" }),
137
+ tag ("B" , 8 , "v" , F {"access" : "public" }),
138
+ tag ("C" , 8 , "v" , F {"access" : "public" }),
139
+ tag ("D" , 9 , "v" , F {"access" : "public" }),
140
+ }},
141
+ {filename : "tests/range.go-src" , excludePrivate : true , minversion : "go1.4" , tags : []Tag {
142
+ tag ("main" , 1 , "p" , F {}),
143
+ tag ("fmt" , 3 , "i" , F {}),
144
+ }},
98
145
}
99
146
100
147
func TestParse (t * testing.T ) {
@@ -110,14 +157,21 @@ func TestParse(t *testing.T) {
110
157
continue
111
158
}
112
159
113
- tags , err := Parse (testCase .filename , testCase .relative , basepath )
160
+ tags , err := Parse (testCase .filename , testCase .relative , basepath , testCase . excludePrivate )
114
161
if err != nil {
115
162
t .Errorf ("[%s] Parse error: %s" , testCase .filename , err )
116
163
continue
117
164
}
118
165
119
166
if len (tags ) != len (testCase .tags ) {
120
- t .Errorf ("[%s] len(tags) == %d, want %d" , testCase .filename , len (tags ), len (testCase .tags ))
167
+ msg := fmt .Sprintf ("[%s] len(tags) == %d, want %d" ,
168
+ testCase .filename ,
169
+ len (tags ),
170
+ len (testCase .tags ))
171
+ if testCase .excludePrivate {
172
+ msg += " (exclude private)"
173
+ }
174
+ t .Error (msg )
121
175
continue
122
176
}
123
177
0 commit comments