@@ -5,7 +5,6 @@ package apidoc
55import (
66 "bytes"
77 "path/filepath"
8- "strconv"
98 "testing"
109 "time"
1110
@@ -14,15 +13,40 @@ import (
1413 "github.com/caixw/apidoc/v5/input"
1514 "github.com/caixw/apidoc/v5/internal/vars"
1615 "github.com/caixw/apidoc/v5/message"
16+ "github.com/caixw/apidoc/v5/static"
1717)
1818
19- func buildMessageHandle () (* bytes.Buffer , message.HandlerFunc ) {
20- buf := new (bytes.Buffer )
21-
22- return buf , func (msg * message.Message ) {
23- buf .WriteString (strconv .Itoa (int (msg .Type )))
24- buf .WriteString (msg .Message )
19+ func buildMessageHandle () (erro , succ * bytes.Buffer , h * message.Handler ) {
20+ erro = new (bytes.Buffer )
21+ succ = new (bytes.Buffer )
22+
23+ f := func (msg * message.Message ) {
24+ switch msg .Type {
25+ case message .Erro :
26+ erro .WriteString (msg .Message )
27+ default :
28+ succ .WriteString (msg .Message )
29+ }
2530 }
31+
32+ return erro , succ , message .NewHandler (f )
33+ }
34+
35+ func TestLoadConfig (t * testing.T ) {
36+ a := assert .New (t )
37+
38+ erro , succ , h := buildMessageHandle ()
39+ cfg := LoadConfig (h , "./" )
40+ a .NotNil (cfg ).
41+ Empty (erro .String ())
42+
43+ erro , succ , h = buildMessageHandle ()
44+ cfg = LoadConfig (h , "./docs" ) // 不存在 apidoc 的配置文件
45+
46+ h .Stop ()
47+ a .Nil (cfg ).
48+ NotEmpty (erro .String ()).
49+ Empty (succ .String ())
2650}
2751
2852func TestLoadFile (t * testing.T ) {
@@ -42,9 +66,9 @@ func TestDetect_Load(t *testing.T) {
4266 a .NotError (err ).NotEmpty (wd )
4367 a .NotError (Detect (wd , true ))
4468
45- out , f := buildMessageHandle ()
46- cfg := LoadConfig (message . NewHandler ( f ) , wd )
47- a .Empty (out .String ()).NotNil (cfg )
69+ erro , _ , h := buildMessageHandle ()
70+ cfg := LoadConfig (h , wd )
71+ a .Empty (erro .String ()).NotNil (cfg )
4872
4973 a .Equal (cfg .Version , vars .Version ()).
5074 Equal (cfg .Inputs [0 ].Lang , "go" )
@@ -78,21 +102,55 @@ func TestConfig_sanitize(t *testing.T) {
78102 Equal (err .Field , "output" )
79103}
80104
105+ func TestConfig_Test (t * testing.T ) {
106+ a := assert .New (t )
107+
108+ erro , succ , h := buildMessageHandle ()
109+ cfg := LoadConfig (h , "./docs/example" )
110+ a .NotNil (cfg )
111+ cfg .Test ()
112+
113+ h .Stop ()
114+ a .Empty (erro .String ()).
115+ NotEmpty (succ .String ()) // 有成功提示
116+ }
117+
118+ func TestConfig_Pack (t * testing.T ) {
119+ a := assert .New (t )
120+
121+ erro , succ , h := buildMessageHandle ()
122+ cfg := LoadConfig (h , "./docs/example" )
123+ a .NotNil (cfg )
124+ cfg .Pack ("testdata" , "Data" , "./.testdata" , "apidoc.xml" , "application/xml" , static .TypeAll )
125+
126+ h .Stop ()
127+ a .Empty (erro .String ()).
128+ Empty (succ .String ())
129+ }
130+
81131func TestConfig_Do (t * testing.T ) {
82132 a := assert .New (t )
83133
84- out , f := buildMessageHandle ()
85- h := message .NewHandler (f )
86- LoadConfig (h , "./docs/example" ).Do (time .Now ())
87- a .Empty (out .String ())
134+ erro , succ , h := buildMessageHandle ()
135+ cfg := LoadConfig (h , "./docs/example" )
136+ a .NotNil (cfg )
137+ cfg .Do (time .Now ())
138+
139+ h .Stop ()
140+ a .NotEmpty (succ .String ()). // 有成功提示
141+ Empty (erro .String ())
88142}
89143
90144func TestConfig_Buffer (t * testing.T ) {
91145 a := assert .New (t )
92146
93- out , f := buildMessageHandle ()
94- h := message .NewHandler (f )
95- buf := LoadConfig (h , "./docs/example" ).Buffer ()
96- a .Empty (out .String ()).
147+ erro , succ , h := buildMessageHandle ()
148+ cfg := LoadConfig (h , "./docs/example" )
149+ a .NotNil (cfg )
150+
151+ buf := cfg .Buffer ()
152+ h .Stop ()
153+ a .Empty (erro .String ()).
154+ Empty (succ .String ()).
97155 True (buf .Len () > 0 )
98156}
0 commit comments