Skip to content

Commit c3aa10d

Browse files
committed
Add support to specify test cases with different built-in Python types.
1 parent 99e36ef commit c3aa10d

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

examples/test_cases_01.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import judge0
2+
3+
4+
submissions = judge0.run(
5+
source_code="print(f'hello {input()}')",
6+
test_cases=[
7+
("tuple", "hello tuple"),
8+
{"input": "dict", "expected_output": "hello dict"},
9+
["list", "hello list"],
10+
],
11+
)
12+
13+
submissions = judge0.run(submissions=submissions)
14+
15+
for submission in submissions:
16+
print(submission.status)

src/judge0/api.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,12 @@ def create_submissions_from_test_cases(
165165
else:
166166
submissions_list = submissions
167167

168-
if isinstance(test_cases, list):
169-
test_cases_list = test_cases
170-
else:
168+
if isinstance(test_cases, TestCase) or test_cases is None:
171169
test_cases_list = [test_cases]
170+
else:
171+
test_cases_list = test_cases
172+
173+
test_cases_list = [TestCase.from_record(tc) for tc in test_cases_list]
172174

173175
all_submissions = []
174176
for submission in submissions_list:

src/judge0/base_types.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@
44
from typing import Optional, Union
55

66

7-
TestCases = Union[list["TestCase"], tuple["TestCase"]]
7+
TestCases = Union[
8+
list["TestCase"],
9+
tuple["TestCase"],
10+
list[dict],
11+
tuple[dict],
12+
list[list],
13+
list[tuple],
14+
tuple[list],
15+
tuple[tuple],
16+
]
817

918

1019
@dataclass(frozen=True)
@@ -15,6 +24,26 @@ class TestCase:
1524
input: Optional[str] = None
1625
expected_output: Optional[str] = None
1726

27+
@staticmethod
28+
def from_record(
29+
test_case: Optional[Union[tuple, list, dict, "TestCase"]] = None
30+
) -> "TestCase":
31+
if isinstance(test_case, (tuple, list)):
32+
test_case = {
33+
field: value
34+
for field, value in zip(("input", "expected_output"), test_case)
35+
}
36+
if isinstance(test_case, dict):
37+
return TestCase(
38+
input=test_case.get("input", None),
39+
expected_output=test_case.get("expected_output", None),
40+
)
41+
if isinstance(test_case, TestCase) or test_case is None:
42+
return test_case
43+
raise ValueError(
44+
f"Cannot create TestCase object from object of type {type(test_case)}."
45+
)
46+
1847

1948
class Encodeable(ABC):
2049
@abstractmethod

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy