Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 62ee7b7

Browse files
committed
Add --list-testcases flag.
1 parent ab93c13 commit 62ee7b7

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

‎README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ The problem names are automatically extracted from the folder names inside **dat
8989
## Usage
9090
```text
9191
$ python openleetcode.py --help
92-
usage: openleetcode.py [-h] [--language {cpp}] [--list-problems] [--problem problem_name]
93-
[--problem_builds_dir dir] [--verbose]
92+
usage: openleetcode.py [-h] [--language {cpp}] [--list-problems] [--list-testcases] [--problem problem_name] [--problem_builds_dir dir] [--testcase testcase_name] [--verbose]
9493
9594
OpenLeetCode problem builder. This script builds and tests a leetcode like problems locally. Currently, it only supports C++ language but it can be extended to support other languages.
9695
@@ -99,12 +98,13 @@ options:
9998
--language {cpp}, -l {cpp}
10099
The programming language.
101100
--list-problems List problems.
101+
--list-testcases List testcases for a problem specified with '--problem' option.
102102
--problem problem_name, -p problem_name
103-
Name of the problem to build and test. Default: TwoSum. Use --list-problems to
104-
list all problems.
103+
Name of the problem to build and test. Default: TwoSum. Use --list-problems to list all problems.
105104
--problem_builds_dir dir, -d dir
106-
Path to a directory with the problems. Usually ./problem_builds/ directory.
107-
Default: problem_builds.
105+
Path to a directory with the problems. Usually ./problem_builds/ directory. Default: problem_builds.
106+
--testcase testcase_name, -t testcase_name
107+
Name of the testcase to run. '--testcase All' will run all testcases. Default: All.
108108
--verbose, -v Print verbose output
109109
```
110110

‎src/app/openleetcode.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import json
55
import logger
66
import os
7+
import re
78
import shutil
89
import subprocess
910
import sys
@@ -12,13 +13,13 @@
1213
import functionextractor
1314
import resultsvalidator
1415

15-
# TODO:
16-
# test on cmd, PowerShell, Winddows Terminal, iOS Terminal
17-
# Add --list-testcases
18-
1916
TESTCAST_OUTPUT_DIR = "testcase_output"
2017
VALIDATION_SCHEMA_FILE_NAME = "results_validation_schema.json"
2118

19+
def naturalSort(s):
20+
return [int(text) if text.isdigit() else text.lower()
21+
for text in re.split('([0-9]+)', s)]
22+
2223
def run(command):
2324
result = subprocess.run(command,
2425
stdout=subprocess.PIPE,
@@ -54,13 +55,19 @@ def main():
5455
action="store_true",
5556
default=False,
5657
help="List problems.")
58+
parser.add_argument(
59+
"--list-testcases",
60+
action="store_true",
61+
default=False,
62+
help="List testcases for a problem specified with '--problem' option."
63+
)
5764
parser.add_argument(
5865
"--problem", "-p",
5966
metavar='problem_name',
6067
default="TwoSum",
6168
type=str,
6269
help="Name of the problem to build and test. Default: TwoSum. Use "
63-
"--list-problems to list all problems. '")
70+
"--list-problems to list all problems.")
6471
parser.add_argument(
6572
"--problem_builds_dir", "-d",
6673
default="problem_builds",
@@ -91,7 +98,6 @@ def main():
9198
f"does not exist."))
9299
sys.exit(1)
93100

94-
95101
problems_dir = os.path.abspath(os.path.join(problem_builds_dir, "problems"))
96102

97103
if not os.path.isdir(problems_dir):
@@ -104,10 +110,6 @@ def main():
104110
print(problem)
105111
sys.exit(1)
106112

107-
108-
print("Running OpenLeetCode on problem " + args.problem +
109-
" for testcase " + args.testcase + " in language " + args.language)
110-
111113
if not os.path.isdir(args.problem_builds_dir):
112114
print(logger.red(f"The build directory '{args.problem_builds_dir}' "
113115
f"does not exist."))
@@ -130,7 +132,6 @@ def main():
130132
f"argument."))
131133
sys.exit(1)
132134

133-
134135
src_dir = os.path.abspath(os.path.join(problem_dir, args.language))
135136
if not os.path.isdir(src_dir):
136137
print(logger.red(f"The source directory {src_dir} does not exist. This"
@@ -141,6 +142,12 @@ def main():
141142
build_dir = os.path.abspath(os.path.join(src_dir, "build"))
142143

143144
testcases_dir = os.path.abspath(os.path.join(src_dir, "../testcases"))
145+
146+
if (args.list_testcases):
147+
print("List of testcases:")
148+
for testcase in sorted(os.listdir(testcases_dir), key=naturalSort):
149+
print(testcase.replace(".test", ""))
150+
sys.exit(0)
144151

145152
if (args.testcase != "All"):
146153
testcase_file = os.path.join(testcases_dir, args.testcase + ".test")
@@ -149,6 +156,8 @@ def main():
149156
"exist."))
150157
sys.exit(1)
151158

159+
print("Running OpenLeetCode on problem " + args.problem +
160+
" for testcase " + args.testcase + " in language " + args.language)
152161
logger.log(f"Building the problem {args.problem} "
153162
f"in {args.language} language.")
154163
logger.log(f"Problem directory: {problem_dir}")

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /