Skip to content

Commit 8317f08

Browse files
authored
Add a test to verify the test code in question and solution are identical (laike9m#46)
1 parent c731acd commit 8317f08

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed

challenges/advanced-overload-generic/question.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@ def foo(value: Foo):
1818

1919
## End of your code ##
2020

21-
2221
foo(Foo[int]()).bit_length()
2322
foo(Foo[str]()).upper()
2423
foo(Foo[list]()).a.append(1)
2524

26-
2725
foo(Foo[int]()).upper() # expect-type-error
2826
foo(Foo[str]()).bit_length() # expect-type-error
2927
foo(Foo[list]()).bit_length() # expect-type-error

challenges/advanced-overload-literal/solution.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ def foo(value: Any, flag: Any) -> Any:
3939
foo(True, 3).append(1)
4040
foo({}, "4").keys()
4141

42-
4342
foo("42", 1).upper() # expect-type-error
4443
foo(42, 2).append(1) # expect-type-error
4544
foo(True, 3).bit_length() # expect-type-error

challenges/extreme-constructor-parameter/question.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def func_pass(foo: Foo) -> list[Foo]:
3131

3232

3333
@constructor_parameter(Foo)
34-
def func_fail(foo: Foo) -> list:
34+
def func_fail(foo: Foo) -> list[Any]:
3535
...
3636

3737

challenges/intermediate-generic/solution.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ def add(a: T, b: T) -> T:
2323
assert_type(add(1, 2), int)
2424
assert_type(add("1", "2"), str)
2525
assert_type(add(["1"], ["2"]), List[str])
26+
assert_type(add(1, "2"), int) # expect-type-error

tests/test_identical.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
Make sure all test codes in questions and solutions are identical.
3+
"""
4+
5+
from pathlib import Path
6+
7+
import pytest
8+
9+
from views.utils import Challenge, Level
10+
11+
CHALLENGES_DIR = Path(__file__).parent.parent / "challenges"
12+
ALL_SOLUTIONS = list(CHALLENGES_DIR.glob("**/solution.py"))
13+
QUESTION = "question.py"
14+
15+
16+
@pytest.mark.parametrize("solution_file", ALL_SOLUTIONS, ids=lambda x: x.parent.name)
17+
def test_identical(solution_file: Path):
18+
level, challenge_name = solution_file.parent.name.split("-", maxsplit=1)
19+
with solution_file.open() as f:
20+
solution_code = f.read()
21+
solution_test = Challenge(
22+
name=challenge_name, level=Level(level), code=solution_code
23+
).test_code
24+
25+
question_file = solution_file.parent / QUESTION
26+
with question_file.open() as f:
27+
question_code = f.read()
28+
question_test = Challenge(
29+
name=challenge_name, level=Level(level), code=question_code
30+
).test_code
31+
assert solution_test.strip() == question_test.strip()

0 commit comments

Comments
 (0)