Skip to content

Commit 89f5f83

Browse files
aQuaaQua
aQua
authored and
aQua
committed
helper 整理了 *_test.go 文件模板。顺手整理了 helper 的代码。
1 parent ed4b793 commit 89f5f83

File tree

6 files changed

+70
-56
lines changed

6 files changed

+70
-56
lines changed

README_TAIL.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## helper
2-
[helper](./helper.v3)会帮助处理大部分琐碎的工作。
2+
[helper](./helper.v4)会帮助处理大部分琐碎的工作。
33

44
## notes
55
[notes](./notes)记录了我答题过程中,对知识点的总结。

helper.v4/data.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ func diff(new, old *leetcode) {
7171
// 对比 已完成的问题
7272
lenNew := len(new.Problems)
7373
lenOld := len(old.Problems)
74+
// TODO: 处理数据错位问题
75+
// isOrderChanged := false
7476
isChanged := false
7577

7678
i := 0
@@ -95,7 +97,7 @@ func diff(new, old *leetcode) {
9597
}
9698

9799
for i < lenNew {
98-
log.Printf("新题 %d %s", new.Problems[i].ID, new.Problems[i].Title)
100+
log.Printf("出现新题: %d %s", new.Problems[i].ID, new.Problems[i].Title)
99101
i++
100102
}
101103
}

helper.v4/leetcode.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ type leetcode struct {
1616
Problems problems
1717
}
1818

19-
// TODO: 去除 username 和 ranking 的全局变量
2019
func newLeetCode() *leetcode {
2120
return &leetcode{
2221
Username: cfg.Login,

helper.v4/main.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@ package main
33
import (
44
"fmt"
55
"log"
6-
"net/http"
76
"os"
87
"strconv"
98

109
"github.com/BurntSushi/toml"
11-
"github.com/mozillazg/request"
1210
)
1311

1412
// 程序辅助设置
1513
const (
16-
VERSION = "4.0.0"
14+
VERSION = "4.1.0"
1715
USAGE = `使用方法:
1816
1. 运行 helper 会重新生成项目的README.md。
1917
2. 运行 helper 123 会生成第123题的答题文件夹。`
@@ -31,8 +29,6 @@ func init() {
3129
}
3230
log.Printf("Hi, %s. \n", cfg.Login)
3331

34-
// 对 req 赋值
35-
req = request.NewRequest(new(http.Client))
3632
}
3733

3834
func main() {
@@ -52,7 +48,9 @@ func main() {
5248
}
5349
}
5450

55-
// 下载题目资料不需要登录 leetcode
51+
// 由于网络原因,有时候 signin 比较慢
52+
signin()
53+
5654
if problemNum > 0 {
5755
lc, err := readLeetCodeRecord()
5856
if err != nil {
@@ -62,8 +60,6 @@ func main() {
6260
return
6361
}
6462

65-
signin()
66-
6763
categories := []string{
6864
"Algorithms",
6965
// "database",

helper.v4/net.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io/ioutil"
77
"log"
8+
"net/http"
89
"regexp"
910
"strings"
1011

@@ -21,7 +22,9 @@ var req *request.Request
2122
// 登录 leetcode
2223
func signin() {
2324
log.Println("正在登录中...")
24-
25+
26+
// 对 req 赋值
27+
req = request.NewRequest(new(http.Client))
2528
// 配置request
2629
req.Headers = map[string]string{
2730
"Accept-Encoding": "",

helper.v4/problem.go

Lines changed: 58 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,34 @@ func makeProblemDir(ps problems, problemNum int) {
3131
return
3232
}
3333

34-
pb.build()
34+
build(pb)
3535
}
3636

37-
func (p problem) build() {
37+
func build(p problem) {
3838
if p.IsAccepted && GoKit.Exist(p.Dir) {
3939
log.Fatalf("第 %d 题已经accepted,请**删除**或**重命名** %s 文件夹后,再尝试。", p.ID, p.Dir)
4040
}
4141

42+
// 对于没有 accepted 的题目,直接删除重建
43+
if err := os.RemoveAll(p.Dir); err != nil {
44+
log.Fatalln("无法删除目录", p.Dir)
45+
}
46+
4247
mask := syscall.Umask(0)
4348
defer syscall.Umask(mask)
4449

4550
err := os.Mkdir(p.Dir, 0755)
4651
if err != nil {
47-
log.Fatalf("无法创建目录: %s", p.Dir)
52+
log.Fatalf("无法创建目录,%s :%s", p.Dir, err)
4853
}
49-
fc, fcHead, para, ans := getFunction(p.link())
5054

51-
log.Printf("开始创建 %d.%s 的文件夹...\n", p.ID, p.Title)
55+
log.Printf("开始创建 %d %s 的文件夹...\n", p.ID, p.Title)
5256

5357
creatREADME(p)
58+
59+
fc, fcName, para, ans := getFunction(p.link())
5460
creatGo(p, fc)
55-
creatGoTest(p, fcHead, para, ans)
61+
creatGoTest(p, fcName, para, ans)
5662

5763
log.Printf("%d.%s 的文件夹,创建完毕。\n", p.ID, p.Title)
5864
}
@@ -82,15 +88,18 @@ func creatREADME(p problem) {
8288

8389
func getQuestionDescription(URL string) string {
8490
doc, err := goquery.NewDocument(URL)
91+
8592
if err != nil {
8693
log.Fatal(err)
8794
}
95+
8896
return strings.TrimSpace(doc.Find("div.question-description").Text())
8997
}
9098

91-
func getFunction(URL string) (fc, fcHead, p, a string) {
99+
func getFunction(URL string) (fc, fcName, para, ansType string) {
92100
data := getRaw(URL)
93101
str := string(data)
102+
94103
i := strings.Index(str, "codeDefinition:")
95104
j := i + strings.Index(str[i:], "enableTestMode:")
96105
str = str[i:j]
@@ -111,23 +120,24 @@ func getFunction(URL string) (fc, fcHead, p, a string) {
111120
k := 0
112121
i0 := strings.Index(fc, " ")
113122
i = strings.Index(fc, "(")
114-
fcHead = fc[i0+1 : i]
123+
fcName = fc[i0+1 : i]
115124

116125
j = strings.Index(fc, ")")
117126
k = strings.Index(fc, "{")
118-
p = strings.Replace(fc[i+1:j], ",", "\n", -1)
119-
a = fc[j+1 : k]
127+
para = strings.Replace(fc[i+1:j], ",", "\n", -1)
128+
ansType = fc[j+1 : k]
120129

121130
fc = fc[:k] + "{\n\n}"
131+
122132
return
123133
}
124134

125-
func creatGo(p problem, fc string) {
135+
func creatGo(p problem, function string) {
126136
fileFormat := `package %s
127137
128138
%s
129139
`
130-
content := fmt.Sprintf(fileFormat, p.packageName(), fc)
140+
content := fmt.Sprintf(fileFormat, p.packageName(), function)
131141
filename := fmt.Sprintf("%s/%s.go", p.Dir, p.TitleSlug)
132142

133143
err := ioutil.WriteFile(filename, []byte(content), 0755)
@@ -136,57 +146,41 @@ func creatGo(p problem, fc string) {
136146
}
137147
}
138148

139-
func creatGoTest(p problem, fcHead, para, ans string) {
149+
func creatGoTest(p problem, fcName, para, ansType string) {
140150
fileFormat := `package %s
141151
142152
import (
143-
"testing"
144153
"fmt"
154+
"testing"
145155
146156
"github.com/stretchr/testify/assert"
147157
)
148158
149-
type question struct {
150-
para
151-
ans
152-
}
153-
154-
// para 是参数
155-
type para struct {
156-
%s
157-
}
158-
159-
// ans 是答案
160-
type ans struct {
161-
one %s
162-
}
163-
164159
func Test_%s(t *testing.T) {
165160
ast := assert.New(t)
166161
167-
qs := []question{
162+
// tcs is testcase slice
163+
tcs := []struct {
164+
%s
165+
ans %s
166+
}{
167+
168+
{
169+
,
168170
169-
question{
170-
para{
171-
,
172-
},
173-
ans{
174-
,
175-
},
176171
},
177-
178-
// 如需多个测试,可以复制上方元素。
179172
}
180173
181-
for _, q := range qs {
182-
a, p := q.ans, q.para
183-
fmt.Printf("~~%s~~\n", p)
174+
for _, tc := range tcs {
175+
fmt.Printf("~~%s~~\n", tc)
184176
185-
ast.Equal(a.one, %s(p. ), "输入:%s", p)
177+
ast.Equal(tc.ans, %s(%s), "输入:%s", tc)
186178
}
187179
}
188180
`
189-
content := fmt.Sprintf(fileFormat, p.packageName(), para, ans, p.packageName(), `%v`, fcHead, `%v`)
181+
tcPara := getTcPara(para)
182+
183+
content := fmt.Sprintf(fileFormat, p.packageName(), p.packageName(), para, ansType, `%v`, fcName, tcPara, `%v`)
190184
filename := fmt.Sprintf("%s/%s_test.go", p.Dir, p.TitleSlug)
191185

192186
err := ioutil.WriteFile(filename, []byte(content), 0755)
@@ -195,6 +189,26 @@ func Test_%s(t *testing.T) {
195189
}
196190
}
197191

192+
// 把 函数的参数 变成 tc 的参数
193+
func getTcPara(para string) string {
194+
// 把 para 按行切分
195+
paras := strings.Split(para, `\n`)
196+
197+
// 把单个参数按空格,切分成参数名和参数类型
198+
temp := make([][]string, len(paras))
199+
for i := range paras {
200+
temp[i] = strings.Split(paras[i], ` `)
201+
}
202+
203+
// 在参数名称前添加 "tc." 并组合在一起
204+
res := "tc." + temp[0][0]
205+
for i := 1; i < len(temp); i++ {
206+
res += "," + " tc." + temp[i][0]
207+
}
208+
209+
return res
210+
}
211+
198212
func (p problem) packageName() string {
199213
return fmt.Sprintf("Problem%04d", p.ID)
200214
}

0 commit comments

Comments
 (0)