Skip to content

Commit 4d93afc

Browse files
committed
Moved string coloring code to its own function
1 parent 4515f9f commit 4d93afc

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

tests/test_all_solutions.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ class colors:
1515
BOLD = '\033[1m'
1616
UNDERLINE = '\033[4m'
1717

18+
def color_string(color, string):
19+
return color + string + colors.ENDC
20+
1821
def test_all_solutions(directory_name, compiler_name, expected_solutions):
1922
files = glob.glob(directory_name + "/Problem***.cpp")
2023

2124
if not files:
22-
print(colors.FAIL + "Error: No files were found!" + colors.ENDC)
25+
print(color_string(colors.FAIL, "Error: No files were found!"))
2326
return 1
2427

2528
files.sort()
@@ -34,21 +37,20 @@ def test_all_solutions(directory_name, compiler_name, expected_solutions):
3437
try:
3538
dir_util.copy_tree(directory_name + "/data/", "./data/") # Copy the data folder that some solutions need
3639
except dir_util.DistutilsFileError:
37-
print(colors.WARNING + "The data folder was unable to be copied from " + directory_name + " to ./data!")
38-
print("Some solutions may not be able to compile!\n" + colors.ENDC)
40+
print(color_string(colors.WARNING, "The data folder was unable to be copied from " + directory_name + " to ./data!\nSome solutions may not be able to compile!\n"))
3941

4042
passed_tests = 0
4143

4244
for file in files:
4345
compile_command = compiler_name + " " + file + " -std=c++14 -O2"
4446
current_key = file[-7: -4]
4547

46-
print(colors.BOLD + "===============" + colors.ENDC)
47-
print(colors.OKBLUE + "TEST" + colors.ENDC + ": Problem " + str(current_key) + "\n")
48+
print(color_string(colors.BOLD, "==============="))
49+
print(color_string(colors.OKBLUE, "TEST") + ": Problem " + str(current_key) + "\n")
4850
print("Trying to compile " + file + " with " + compile_command)
4951

5052
if (subprocess.call(compile_command.split(), stdout = subprocess.DEVNULL, stderr = subprocess.STDOUT) != 0): # Compile the current file
51-
print("Result: " + colors.FAIL + "FAILURE" + colors.ENDC + ": File was unable to be compiled!")
53+
print("Result: " + color_string(colors.FAIL, "FAILURE") + ": File was unable to be compiled!")
5254
else:
5355
try:
5456
print("File was successfully able to be compiled. Running executable with ./a.out\n")
@@ -62,16 +64,16 @@ def test_all_solutions(directory_name, compiler_name, expected_solutions):
6264
print("Expected output: " + str(expected_solution))
6365

6466
if current_output == expected_solution:
65-
print("\nResult: " + colors.OKGREEN + "SUCCESS" + colors.ENDC)
67+
print("\nResult: " + color_string(colors.OKGREEN, "SUCCESS"))
6668
passed_tests += 1
6769
else:
68-
print("\nResult: " + colors.FAIL + "FAILURE" + colors.ENDC + ": Current output " + str(current_output) + " does not match expected output " + str(expected_solution) + "!")
70+
print("\nResult: " + color_string(colors.FAIL, "FAILURE") + ": Current output " + str(current_output) + " does not match expected output " + str(expected_solution) + "!")
6971
else:
70-
print("Result: " + colors.FAIL + "FAILURE" + colors.ENDC + ": Expected solution to problem " + str(current_key) + " was not found!")
72+
print("Result: " + color_string(colors.FAIL, "FAILURE") + ": Expected solution to problem " + str(current_key) + " was not found!")
7173
except ValueError:
72-
print("Result: " + colors.FAIL + "FAILURE" + colors.ENDC + ": Output is of non-integer type!")
74+
print("Result: " + color_string(colors.FAIL, "FAILURE") + ": Output is of non-integer type!")
7375

74-
print(colors.BOLD + "===============" + colors.ENDC)
76+
print(color_string(colors.BOLD, "==============="))
7577

7678
if os.path.isfile('./a.out'): # Remove the new executable file
7779
os.remove('a.out')
@@ -83,13 +85,13 @@ def test_all_solutions(directory_name, compiler_name, expected_solutions):
8385

8486
num_tests = len(expected_solutions)
8587

86-
print(colors.BOLD + str(passed_tests) + " out of " + str(num_tests) + " tests were successful." + colors.ENDC)
88+
print(color_string(colors.BOLD, str(passed_tests) + " out of " + str(num_tests) + " tests were successful."))
8789

8890
if passed_tests == num_tests:
89-
print("Result: " + colors.OKGREEN + "SUCCESS" + colors.ENDC)
91+
print("Result: " + color_string(colors.OKGREEN, "SUCCESS"))
9092
return 0
9193
else:
92-
print("Result: " + colors.FAIL + "FAILURE" + colors.ENDC)
94+
print("Result: " + color_string(colors.FAIL, "SUCCESS"))
9395
return 1
9496

9597

0 commit comments

Comments
 (0)