Skip to content

Commit ea8b3aa

Browse files
authored
Fix usage of exercise_key without ExerciseRegistry (#88)
Because the `exercise_key` is used for the `ExerciseRegistry` and the `CheckRegistry` we need to ensure the `CodeExercise` can be run with each individual one and both.
1 parent 2e42bff commit ea8b3aa

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/scwidgets/exercise/_widget_code_exercise.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,13 @@ def __init__(
176176
"code and params do no match: " + compatibility_result
177177
)
178178

179-
CheckableWidget.__init__(self, check_registry, exercise_key)
180-
ExerciseWidget.__init__(self, exercise_registry, exercise_key)
179+
name = kwargs.get("name", exercise_key)
180+
CheckableWidget.__init__(self, check_registry, name)
181+
if exercise_registry is not None:
182+
ExerciseWidget.__init__(self, exercise_registry, exercise_key)
183+
else:
184+
# otherwise ExerciseWidget constructor will raise an error
185+
ExerciseWidget.__init__(self, None, None)
181186

182187
self._code = code
183188
self._output = CueOutput()

tests/test_code.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,3 +355,21 @@ def test_figure(self):
355355
assert isinstance(code_ex.figure, Figure)
356356
assert code_ex.output.figure is code_ex.figure
357357
assert code_ex.outputs[0] is code_ex.output
358+
359+
def test_consrtuction_with_registries(self):
360+
"""Because the exercise key is used for the `ExerciseRegistry` and the
361+
`CheckRegistry` we need to ensure the `CodeExercise` can be run with
362+
each individual one and both"""
363+
CodeExercise(
364+
exercise_key="some_key",
365+
check_registry=CheckRegistry(),
366+
)
367+
CodeExercise(
368+
exercise_key="some_key",
369+
exercise_registry=ExerciseRegistry(),
370+
)
371+
CodeExercise(
372+
exercise_key="some_key",
373+
check_registry=CheckRegistry(),
374+
exercise_registry=ExerciseRegistry(),
375+
)

0 commit comments

Comments
 (0)