Skip to content

Commit 2dd1d94

Browse files
committed
Jinja templates and render functions on feedback.
1 parent 3f3cf84 commit 2dd1d94

11 files changed

+123
-6
lines changed

Diff for: templates/generic/generic.pl

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@
2222

2323
# définition de la procédure de validation
2424
grader==
25-
return (0, "<l'exercice n'a pas défini de procédure de validation>")
25+
return (0, "L'exercice n'a pas défini de procédure de validation.")
2626
==

Diff for: templates/generic/grader.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# TODO: check whether other stuff should be mocked (notably stderr) and possibly
1212
# use a new patch-decorated function
1313
# TODO: implement comparison facilities with trusted code
14+
# TODO: add comments to individual tests
1415

1516
import sys
1617

@@ -55,4 +56,4 @@ def get_student_code(exercise_context: dict):
5556
"contacter un enseignant.", file=sys.stderr)
5657
raise e
5758

58-
sandboxio.output(0, str(r.tests))
59+
sandboxio.output(0, r.render_tests())

Diff for: templates/generic/jinja/testgroup.html

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{% if testgroup.status == stat.PASS %}
2+
<div style="border:1px solid black;padding:0%;margin:1%;background-color:LightGreen;border-radius:4px;">
3+
<button type="button"
4+
class="btn btn-block btn-success"
5+
data-toggle="collapse"
6+
data-target="#{{ testgroup.make_id() }}">
7+
{{ testgroup.title }}
8+
</button>
9+
<div id="{{ testgroup.make_id() }}" class="collapse">
10+
<div class="card card-success;" style="background-color: LightGreen;">
11+
{% else %}
12+
<div style="border:1px solid black;padding:1%;margin:1%;background-color:Tomato;border-radius:4px;">
13+
<button type="button"
14+
class="btn btn-block btn-danger"
15+
data-toggle="collapse"
16+
data-target="#{{ testgroup.make_id() }}"
17+
aria-expanded="true"
18+
aria-controls="{{ testgroup.make_id() }}">
19+
{{ testgroup.title }}
20+
</button>
21+
<div id="{{ testgroup.make_id() }}" class="show">
22+
<div class="card card-danger;" style="background-color: Tomato;">
23+
{% endif %}
24+
{% for test in testgroup.tests %}
25+
{{ test.render() }}
26+
{% endfor %}
27+
</div>
28+
</div>
29+
</div>

Diff for: templates/generic/jinja/testitem.html

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{% if test.status == stat.FAIL %}
2+
<div style="border:1px solid black;padding:1px;margin:1px;background-color:Tomato;border-radius:4px;">
3+
<button type="button"
4+
class="btn btn-block btn-danger"
5+
aria-expanded="true"
6+
aria-controls="{{ test.make_id() }}"
7+
data-toggle="collapse"
8+
data-target="#{{ test.make_id() }}">
9+
{{ test.__str__() }}
10+
</button>
11+
<div id="test-{{num}}" class="show">
12+
<div class="card card-danger;"
13+
style="background-color: Tomato;">
14+
<p>
15+
{{ test.__str__() }}
16+
</p>
17+
</div>
18+
</div>
19+
</div>
20+
{% else %}
21+
<div style="border:1px white;padding:1px;margin:1px;background-color:LightGreen;border-radius:4px;">
22+
<button type="button"
23+
class="btn btn-block btn-success"
24+
data-toggle="collapse"
25+
data-target="#{{ test.make_id() }}">
26+
{{ test.__str__() }}
27+
</button>
28+
<div id="{{ test.make_id() }}" class="collapse">
29+
<div class="card card-success;"
30+
style="background-color: LightGreen;">
31+
<p>
32+
{{ test.__str__() }}
33+
</p>
34+
</div>
35+
</div>
36+
</div>
37+
{% endif %}

Diff for: templates/generic/tests/test_idiot.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ def f(n):
3131
# 5 - display test results
3232
# TODO: needs work
3333
for test in runner.tests:
34-
print(test)
34+
print(test.render())

Diff for: templates/generic/tests/tri_de_trois.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,4 @@
5656
g.end_test_group()
5757

5858
for test in g.tests:
59-
print(test)
59+
print(test.render())

Diff for: templates/generic/utils/coderunner.py

+13
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,14 @@ def end_test_group(self) -> NoReturn:
102102
def record_test(self, test: fb.TestFeedback) -> NoReturn:
103103
if self.current_test_group:
104104
self.current_test_group.append(test)
105+
if test.status == fb.FAIL:
106+
self.current_test_group.status = fb.FAIL
105107
else:
106108
self.tests.append(test)
107109

110+
def render_tests(self):
111+
return "\n".join(test.render() for test in self.tests)
112+
108113
"""Code execution."""
109114

110115
def summarize_changes(self):
@@ -209,3 +214,11 @@ def assert_no_global_change(self):
209214
added, deleted, modified, _ = self.summarize_changes()
210215
status = fb.FAIL if added or deleted or modified else fb.PASS
211216
self.record_test(fb.NoGlobalChangeTestFeedback(status))
217+
218+
def assert_no_exception(self):
219+
status = self.exception is None
220+
self.record_test(fb.NoExceptionTestFeedback(status))
221+
222+
def assert_exception(self, exception_type):
223+
status = isinstance(self.exception, exception_type)
224+
self.record_test(fb.ExceptionTestFeedback(status, exception_type))

Diff for: templates/generic/utils/testfeedback.py

+38-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
1-
FAIL, PASS, INFO, EXEC, EVAL = range(5)
1+
import jinja2
2+
import string
3+
import unicodedata
4+
5+
FAIL, PASS, INFO, EXEC, EVAL, GROUP = range(6)
6+
7+
# TODO: change this, ugly
8+
stat_dict = {
9+
"FAIL": FAIL,
10+
"PASS": PASS,
11+
"INFO": INFO,
12+
"EXEC": EXEC,
13+
"EVAL": EVAL
14+
}
215

316
_prefix = {
417
FAIL: "[erreur] ",
@@ -15,6 +28,8 @@
1528

1629

1730
class TestFeedback:
31+
num = 0
32+
1833
def __init__(self, status, params):
1934
self.status = status
2035
self.params = _default_params.copy()
@@ -23,6 +38,16 @@ def __init__(self, status, params):
2338
def __str__(self):
2439
return _prefix[self.status]
2540

41+
def render(self):
42+
with open('templates/generic/jinja/testitem.html', "r") as tempfile:
43+
templatestring = tempfile.read()
44+
template = jinja2.Template(templatestring)
45+
return template.render(test=self, stat=stat_dict)
46+
47+
def make_id(self):
48+
TestFeedback.num += 1
49+
return TestFeedback.num
50+
2651

2752
class OutputTestFeedback(TestFeedback):
2853
def __init__(self, status, expected, **params):
@@ -238,6 +263,7 @@ def __str__(self):
238263
class TestGroup:
239264
def __init__(self, title: str):
240265
self.title = title
266+
self.status = GROUP
241267
self.tests = []
242268

243269
def __str__(self):
@@ -246,3 +272,14 @@ def __str__(self):
246272

247273
def append(self, test: TestFeedback):
248274
self.tests.append(test)
275+
276+
def make_id(self):
277+
return 'group-' + ''.join(
278+
x for x in unicodedata.normalize('NFKD', self.title) if x.isalnum()
279+
).lower()
280+
281+
def render(self):
282+
with open('templates/generic/jinja/testgroup.html',"r") as tempfile:
283+
templatestring = tempfile.read()
284+
template = jinja2.Template(templatestring)
285+
return template.render(testgroup=self, stat=stat_dict)
File renamed without changes.
File renamed without changes.

Diff for: unused/ap1_grader_old.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from typing import Callable, TypeVar
2-
from unused.ap1_feedback import Feedback
2+
from unused.ap1_feedback_old import Feedback
33

44
T = TypeVar('T')
55

0 commit comments

Comments
 (0)