Skip to content

Commit 9725cc9

Browse files
committed
add test script and simple a+b task
0 parents  commit 9725cc9

File tree

6 files changed

+95
-0
lines changed

6 files changed

+95
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This is a repository, containing solutions for tasks from leetcode.com
2+
Different folders contains solutions on different languages
3+
Also there is some simple code for testing

py3/sum/sum.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def main():
2+
a, b = input().split()
3+
print(int(a) + abs(int(b)))
4+
5+
6+
if __name__ == "__main__":
7+
main()

py3/sum/test/test1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"input": "1 2",
3+
"output": "3"
4+
}

py3/sum/test/test2

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"input": "888 -111",
3+
"output": "777"
4+
}

py3/sum/test/test3

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"input": "-10000 100",
3+
"output": "-9900"
4+
}

py3/test.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/python3
2+
3+
import os
4+
import io
5+
import re
6+
import sys
7+
import json
8+
import filecmp
9+
import subprocess
10+
11+
from collections import namedtuple as nt
12+
13+
14+
class bcolors:
15+
OKBLUE = '\033[94m'
16+
OKGREEN = '\033[92m'
17+
WARNING = '\033[93m'
18+
FAIL = '\033[91m'
19+
ENDC = '\033[0m'
20+
21+
22+
def print_green(text):
23+
print(f"{bcolors.OKGREEN}{text}{bcolors.ENDC}")
24+
25+
26+
def print_red(text):
27+
print(f"{bcolors.FAIL}{text}{bcolors.ENDC}")
28+
29+
30+
Test = nt("Test", "process output expected name")
31+
32+
33+
def check_result(test):
34+
out = test.output.decode("ascii").strip()
35+
if test.expected == out:
36+
print_green(f"Test: {test.name} is [OK]")
37+
else:
38+
print_red(f"Test: {test.name} is [FAIL]")
39+
print_green(test.expected)
40+
print_red(out)
41+
print("-" * 10)
42+
43+
44+
def run_test(program_path, test_path):
45+
with open(test_path, "r") as test:
46+
test_case = json.load(test)
47+
p = subprocess.Popen(["python3", program_path + "/" + program_path.split("/")[-1] + ".py"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
48+
outp, errs = p.communicate(str(test_case["input"]).encode("ascii"))
49+
if errs:
50+
print_red("[FAIL]")
51+
print(errs)
52+
return
53+
check_result(Test(p, outp, test_case["output"], test_path.split("/")[-1]))
54+
55+
56+
def main(argv):
57+
"""
58+
Assuming each program is in it's own folder containing "test" folder inside
59+
Each test is a json file with "input" and "output" string keys
60+
"""
61+
cwd = os.getcwd()
62+
program_path = os.path.join(cwd, argv[1])
63+
for test_path in os.listdir(os.path.join(program_path, "test")):
64+
if not re.match(test_path, "_res$") and not re.match(test_path, "_test_res$"):
65+
full_path = os.path.join(program_path, "test", test_path)
66+
run_test(program_path, full_path)
67+
68+
69+
if __name__ == "__main__":
70+
if len(sys.argv) < 2:
71+
print("Please select the program")
72+
exit(-1)
73+
main(sys.argv)

0 commit comments

Comments
 (0)