@@ -99,31 +99,57 @@ func writeMainFile(problemID, defaultCode string) error {
9999}
100100
101101func writeTestFile (problemID , funcName string , sampleIns , sampleOuts [][]string ) error {
102- if funcName == "" {
103- return fmt .Errorf ("funcName is empty" )
104- }
105102 if len (sampleIns ) != len (sampleOuts ) {
106103 return fmt .Errorf ("len(sampleIns) != len(sampleOuts) : %d != %d" , len (sampleIns ), len (sampleOuts ))
107104 }
108-
109105 funcName = strings .TrimSpace (funcName )
110- sampleToStr := func (samples [][]string ) (s string ) {
111- for i , args := range samples {
112- if i > 0 {
113- s += ", "
106+
107+ var testStr string
108+ if funcName == "" {
109+ // 编写类
110+ sampleToStr := func (samples [][]string ) (s string ) {
111+ for _ , text := range samples {
112+ s += text [0 ]
114113 }
115- s += "{"
116- for j , arg := range args {
117- if j > 0 {
114+ return
115+ }
116+ testStr = fmt .Sprintf (`package main
117+
118+ import (
119+ "github.com/stretchr/testify/assert"
120+ "strings"
121+ "testing"
122+ )
123+
124+ func Test(t *testing.T) {
125+ t.Log("Current test is [%s]")
126+ // copy to the Custom Testcase
127+ const exampleIns = ` + "`\n %s`" + `
128+ exampleOuts := ` + "`\n %s`" + `
129+ // copy Your answer in the Run Code Result
130+ yourAnswers := ` + "`\n \n `" + `
131+ assert.Equal(t, strings.TrimSpace(exampleOuts), strings.TrimSpace(yourAnswers))
132+ }
133+ ` , problemID , sampleToStr (sampleIns ), sampleToStr (sampleOuts ))
134+ } else {
135+ // 编写函数
136+ sampleToStr := func (samples [][]string ) (s string ) {
137+ for i , args := range samples {
138+ if i > 0 {
118139 s += ", "
119140 }
120- s += "`" + arg + "`"
141+ s += "{"
142+ for j , arg := range args {
143+ if j > 0 {
144+ s += ", "
145+ }
146+ s += "`" + arg + "`"
147+ }
148+ s += "}"
121149 }
122- s += "}"
150+ return
123151 }
124- return
125- }
126- testStr := fmt .Sprintf (`// Code generated by generator_test.
152+ testStr = fmt .Sprintf (`// Code generated by generator_test.
127153package main
128154
129155import (
@@ -133,13 +159,18 @@ import (
133159
134160func Test(t *testing.T) {
135161 t.Log("Current test is [%s]")
136- sampleIns := [][]string{%s}
137- sampleOuts := [][]string{%s}
138- if err := testutil.RunLeetCodeFunc(t, %s, sampleIns, sampleOuts); err != nil {
162+ exampleIns := [][]string{%s}
163+ exampleOuts := [][]string{%s}
164+ // custom test cases or WA cases.
165+ //exampleIns = append(exampleIns, []string{` + "``" + `})
166+ //exampleOuts = append(exampleOuts, []string{` + "``" + `})
167+ if err := testutil.RunLeetCodeFunc(t, %s, exampleIns, exampleOuts); err != nil {
139168 t.Fatal(err)
140169 }
141170}
142171` , problemID , sampleToStr (sampleIns ), sampleToStr (sampleOuts ), funcName )
172+ }
173+
143174 filePath := contestDir + fmt .Sprintf ("%[1]s/%[1]s_test.go" , problemID )
144175 return ioutil .WriteFile (filePath , []byte (testStr ), 0644 )
145176}
@@ -179,8 +210,9 @@ func parseHTML(fileName string, htmlURL string) error {
179210 }
180211 }
181212
213+ // 解析模板(codeDefinition)
182214 var funcName string
183- var genTestFile bool
215+ var isFuncProblem bool // 编写函数还是类
184216 for o := bodyNode .FirstChild ; o != nil ; o = o .NextSibling {
185217 if o .Type == html .ElementNode && o .Data == "script" && o .FirstChild != nil {
186218 jsText := o .FirstChild .Data
@@ -201,7 +233,7 @@ func parseHTML(fileName string, htmlURL string) error {
201233
202234 for _ , e := range d {
203235 if e .Value == "golang" {
204- funcName , genTestFile = parseFuncName (e .DefaultCode )
236+ funcName , isFuncProblem = parseFuncName (e .DefaultCode )
205237 if err := writeMainFile (fileName , e .DefaultCode ); err != nil {
206238 return err
207239 }
@@ -213,10 +245,7 @@ func parseHTML(fileName string, htmlURL string) error {
213245 }
214246 }
215247
216- if ! genTestFile {
217- return nil
218- }
219-
248+ // 解析样例输入输出
220249 const (
221250 tokenInputZH = "输入:"
222251 tokenOutputZH = "输出:"
@@ -233,6 +262,9 @@ func parseHTML(fileName string, htmlURL string) error {
233262 text = ""
234263 for _ , l := range lines {
235264 text += strings .TrimSpace (l )
265+ if ! isFuncProblem {
266+ text += "\n "
267+ }
236268 }
237269
238270 if ! isASCII (text ) {
@@ -252,19 +284,24 @@ func parseHTML(fileName string, htmlURL string) error {
252284 sample = append (sample , strings .TrimSpace (s [:end ]))
253285 }
254286 sample = append (sample , strings .TrimSpace (splits [len (splits )- 1 ]))
287+
288+ if ! isFuncProblem {
289+ sample = []string {strings .Join (sample , "\n " ) + "\n " }
290+ }
291+
255292 return
256293 }
257294 var f func (* html.Node )
258295 f = func (o * html.Node ) {
259296 if o .Type == html .TextNode {
260- if o .Data == tokenInputZH {
297+ if strings . Contains ( o .Data , tokenInputZH ) {
261298 raw := o .Parent .NextSibling .Data
262299 sample := parseSampleText (raw , true )
263300 if sample == nil {
264301 fmt .Fprintf (os .Stderr , "错误的输入数据:%s\n " , raw )
265302 }
266303 sampleIns = append (sampleIns , sample )
267- } else if o .Data == tokenOutputZH {
304+ } else if strings . Contains ( o .Data , tokenOutputZH ) {
268305 raw := o .Parent .NextSibling .Data
269306 sample := parseSampleText (raw , true )
270307 if sample == nil {
0 commit comments