Skip to content

Commit 67a82b3

Browse files
committed
feat: add lab8
1 parent 854dc2c commit 67a82b3

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed

.github/workflows/lab8.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# This workflow will build a golang project
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
3+
4+
name: lab8
5+
6+
on:
7+
push:
8+
paths:
9+
- 'lab8/**'
10+
11+
jobs:
12+
13+
build:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- uses: actions/checkout@v4
18+
19+
- uses: actions/setup-go@v4
20+
with:
21+
go-version-file: 'lab8/go.mod'
22+
cache: false
23+
24+
- name: Run
25+
working-directory: 'lab8'
26+
run: |
27+
go mod tidy
28+
python validate.py

lab8/lab8.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"github.com/gocolly/colly"
6+
)
7+
8+
func main() {
9+
10+
flag.Parse()
11+
12+
c := colly.NewCollector()
13+
14+
c.OnHTML("???", func(e *colly.HTMLElement) {
15+
})
16+
}

lab8/validate.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
2+
import difflib
3+
import os
4+
import sys
5+
6+
testcase = [
7+
(
8+
["go", "run", "lab8.go"],
9+
"1. 名字:Ommmmmm5566,留言: 哈哈哈哈哈ㄏㄏㄏㄏ哈哈哈哈哈哈,時間: 12/09 03:18\n"+\
10+
"2. 名字:Ommmmmm5566,留言: 笑死,時間: 12/09 03:18\n"+\
11+
"3. 名字:husky01,留言: 樓上…,時間: 12/09 03:27\n"+\
12+
"4. 名字:bakapika,留言: ㄏㄏ,時間: 12/09 06:25\n"+\
13+
"5. 名字:scmdwyam,留言: XDD,時間: 12/09 08:48\n"+\
14+
"6. 名字:L2e2o4,留言: ㄏㄏ,時間: 12/09 09:21\n"+\
15+
"7. 名字:NobleDino,留言: ㄏㄏ,時間: 12/09 09:34\n"+\
16+
"8. 名字:tottoko0908,留言: ㄏㄏ,時間: 12/09 09:45\n"+\
17+
"9. 名字:lmh911152,留言: ㄏㄏ,時間: 12/09 09:56\n"+\
18+
"10. 名字:monicamomo,留言: ㄏㄏ,時間: 12/09 10:25\n",
19+
False
20+
),
21+
(
22+
["go", "run", "lab8.go", "-max", "5"],
23+
"1. 名字:Ommmmmm5566,留言: 哈哈哈哈哈ㄏㄏㄏㄏ哈哈哈哈哈哈,時間: 12/09 03:18\n"+\
24+
"2. 名字:Ommmmmm5566,留言: 笑死,時間: 12/09 03:18\n"+\
25+
"3. 名字:husky01,留言: 樓上…,時間: 12/09 03:27\n"+\
26+
"4. 名字:bakapika,留言: ㄏㄏ,時間: 12/09 06:25\n"+\
27+
"5. 名字:scmdwyam,留言: XDD,時間: 12/09 08:48\n",
28+
False
29+
),
30+
(
31+
["go", "run", "lab8.go", "-hello"],
32+
"flag provided but not defined: -hello\n"+\
33+
"Usage of\n"+\
34+
" -max int\n"+\
35+
" Max number of comments to show (default 10)\n"+\
36+
"exit status 2\n",
37+
True
38+
),
39+
(
40+
["go", "run", "lab8.go", "-max"],
41+
"flag needs an argument: -max\n"+\
42+
"Usage of\n"+\
43+
" -max int\n"+\
44+
" Max number of comments to show (default 10)\n"+\
45+
"exit status 2\n",
46+
True
47+
),
48+
(
49+
["go", "run", "lab8.go", "-max", "hello"],
50+
'invalid value "hello" for flag -max: parse error\n'+\
51+
"Usage of\n"+\
52+
" -max int\n"+\
53+
" Max number of comments to show (default 10)\n"+\
54+
"exit status 2\n",
55+
True
56+
)
57+
]
58+
59+
def edits_string(first, second):
60+
61+
first = first.splitlines(1)
62+
second = second.splitlines(1)
63+
result = difflib.unified_diff(first, second)
64+
65+
colored_diff = []
66+
for line in result:
67+
if line.startswith(' '):
68+
colored_diff.append('\033[0m' + line)
69+
elif line.startswith('+'):
70+
colored_diff.append('\033[32m' + line)
71+
elif line.startswith('-'):
72+
colored_diff.append('\033[31m' + line)
73+
return ''.join(colored_diff) + '\033[0m'
74+
75+
def main():
76+
try:
77+
for i, (args, ans, isError) in enumerate(testcase):
78+
os.system(f"{' '.join(args)} > result_{i}.txt 2>&1")
79+
with open(f"result_{i}.txt", "r", encoding='utf-8') as file_:
80+
output = file_.read()
81+
if isError:
82+
uidx = output.find("Usage of")
83+
if uidx == -1:
84+
print(f"\033[1;31m[ERROR]\033[0;33m\nCommand:\033[0m\n{' '.join(args)}\n\033[0;33mResult:\033[0m")
85+
print(edits_string(ans, output))
86+
break
87+
tidx = output.index("\n", uidx)
88+
if tidx == -1:
89+
print(f"\033[1;31m[ERROR]\033[0;33m\nCommand:\033[0m\n{' '.join(args)}\n\033[0;33mResult:\033[0m")
90+
print(edits_string(ans, output))
91+
break
92+
output = output[:uidx + 8] + output[tidx:]
93+
94+
if output != ans:
95+
print(f"\033[1;31m[ERROR]\033[0;33m\nCommand:\033[0m\n{' '.join(args)}\n\033[0;33mResult:\033[0m")
96+
print(edits_string(ans, output))
97+
break
98+
else:
99+
return 0
100+
finally:
101+
for i in range(len(testcase)):
102+
if os.path.exists(f"result_{i}.txt"):
103+
os.remove(f"result_{i}.txt")
104+
return 1
105+
106+
if __name__ == "__main__":
107+
status = main()
108+
if status == 0:
109+
print("\033[1;32mPASS\033[0m")
110+
sys.exit(status)

0 commit comments

Comments
 (0)