-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdodona.js
719 lines (483 loc) · 16 KB
/
dodona.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
//
// Message
//
class Message {
constructor(properties) {
this._properties = Object.assign(
// set default values
{
description: "",
format: "text"
},
// overwrite with specific values
properties
);
}
toJson() {
return this._properties;
}
}
//
// Test
//
class Test {
constructor(properties, parent) {
// TODO: check object properties using JSON schema
//
// base
// description: string or message
// messages: array of messages
// data: object
// acceptance
// accepted (required): boolean
// status (required): string
// score: float (>= 0.0)
// maximal_score: float (>= 0.0)
// outcome
// expected
// generated
// set parent
this._parent = parent || null;
// set object properties
this._properties = {}
// set default properties and overwrite with specific values
this.setStatus("unprocessed").setProperties(properties);
}
hasParent() {
return this._parent !== null;
}
get parent() {
return this._parent;
}
set parent(parent) {
// set parent
this._parent = parent;
// return object for chaining purposes
return this;
}
hasProperty(name) {
// check if object has a property with the given name
return this._properties.hasOwnProperty(name);
}
getProperty(name) {
// check if object has a property with the given name
if (this.hasProperty(name)) {
return this._properties[name];
}
// report that object has no property with the given name
throw new Error(`unknown property "${name}"`);
}
setAccepted(value) {
// set acceptance of current object
this._properties.accepted = value;
// recursively call function on parent
if (this.hasParent()) {
this.parent.setAccepted(value);
}
// return object for chaining purposes
return this;
}
setStatus(value) {
// define the order of severity of status codes
const statusCodes = [
"unprocessed",
"correct answer",
"wrong answer",
"runtime error",
"segmentation error",
"unexpected end of line",
"memory limit exceeded",
"time limit exceeded",
"compilation error",
];
// determine severity of given status
const severity = statusCodes.indexOf(value);
if (severity === -1) {
throw new Error(`invalid status "${value}"`);
}
// determine current severity
let currentSeverity = -1;
if (this.hasProperty("status")) {
currentSeverity = statusCodes.indexOf(this.getProperty("status"));
}
// update object status
// NOTE: object status is only updated in case it was not set before or
// in case it is more severe than the previous status
if (currentSeverity === -1 || currentSeverity < severity) {
this._properties.status = value;
}
// update object acceptance according to status
this.setAccepted(this.getProperty("status") === "correct answer");
// recursively call function on parent
if (this.hasParent()) {
this.parent.setStatus(value);
}
// return object for chaining purposes
return this;
}
setProperty(name, value) {
// delegate updating of properties status and accepted
if (name === "status") {
// set status
return this.setStatus(value);
} else if (name === "accepted") {
// set acceptance
return this.setAccepted(value);
}
// update object property
this._properties[name] = value;
// return object for chaining purposes
return this;
}
setProperties(properties) {
// set individual properties
for (let property in properties) {
this.setProperty(property, properties[property]);
}
// return object for chaining purposes
return this;
}
deleteProperty(name) {
if (this.hasProperty(name)) {
delete this._properties[name];
}
// return object for chaining purposes
return this;
}
hasMessages() {
// add groups property if not present
if (!this.hasProperty("messages")) {
return false;
}
return this.getProperty("messages").length !== 0;
}
addMessage(message) {
// add message property if not present
if (!this.hasProperty("messages")) {
// create new message property
this.setProperty("messages", [message]);
} else {
// add message to list of messages
this.getProperty("messages").push(message);
}
// return object for chaining purposes
return this;
}
toJson() {
// initialize JavaScript Object representation
let json = {};
// fill JavaScript Object with properties
for (let property in this._properties) {
if (["groups", "tests", "messages"].includes(property)) {
// recursively call on array elements
json[property] = this.getProperty(property).map(
element => element.toJson()
);
} else if (
property === "description" &&
typeof this.getProperty(property) === "object"
) {
// reversively call on object
json[property] = this.getProperty(property).toJson();
} else {
// add property as is
json[property] = this.getProperty(property);
}
}
// return JavaScript Object representation
return json;
}
toString() {
// stringify JavaScript Object representation
return JSON.stringify(this.toJson(), null, 4);
}
}
//
// TestGroup
//
class TestGroup extends Test {
constructor(properties, parent) {
// TODO: check object properties using JSON schema
//
// base
// description: string or message
// messages: array of messages
// data: object
// acceptance
// accepted (required): boolean
// status (required): string
// score: float (>= 0.0)
// maximal_score: float (>= 0.0)
// access
// permission: string (["student", "staff", "zeus", "admin"])
// runtime metrics
// wall_time: float (>= 0.0)
// peak_memory: float (>= 0.0)
// representation
// show_stacked
// show_merged
// show_accepted
// show_line_numbers
// toggle_accepted
// toggle_line_numbers
// substructures
// groups
// tests
// call super constructor
super(properties, parent);
}
hasGroups() {
// no groups if no groups property
if (!this.hasProperty("groups")) {
return false;
}
return this.groups.length !== 0;
}
get groups() {
// check if there are any groups
// NOTE: we cannot use hasGroups here because this also checks if the
// array of groups is empty
if (!this.hasProperty("groups")) {
throw new Error("no groups");
}
return this.getProperty("groups");
};
get lastGroup() {
// no last group if no groups
if (!this.hasGroups()) {
throw new Error("no groups");
}
// no last group if array of groups is empty
if (this.groups.length === 0) {
throw new Error("no groups")
}
// return last group
return this.groups[this.groups.length - 1];
}
addGroup(group) {
// set parent of group
group.parent = this;
if (!this.hasProperty("groups")) {
// add groups property if not present
this.setProperty("groups", [group]);
} else {
// append group to existing list of groups
this.groups.push(group);
}
// update status of test group according to status of group
if (group.hasProperty("status")) {
this.setStatus(group.getProperty("status"));
}
// update acceptance of test group according to acceptance of group
if (group.hasProperty("accepted")) {
this.setAccepted(group.getProperty("accepted"));
}
// return object for chaining purposes
return this;
}
clearGroups() {
if (this.hasProperty("groups")) {
this.groups.length = 0;
}
}
hasTests() {
// no tests if no tests property
if (!this.hasProperty("tests")) {
return false;
}
return this.tests.length !== 0;
}
get tests() {
// check if there are any tests
// NOTE: we cannot use hasTests here because this also checks if the
// array of tests is empty
if (!this.hasProperty("tests")) {
throw new Error("no tests");
}
return this.getProperty("tests");
};
get lastTest() {
// no last test if no tests
if (!this.hasTests()) {
throw new Error("no tests");
}
// get array of groups
const tests = this.tests;
// check if there are groups
if (tests.length === 0) {
throw new Error("no tests")
}
// return last test
return tests[tests.length - 1];
}
addTest(test) {
// set parent of test
test.parent = this;
if (!this.hasProperty("tests")) {
// add tests property if not present
this.setProperty("tests", [test]);
} else {
// append group to existing list of groups
this.tests.push(test);
}
// update status of test group according to status of group
if (test.hasProperty("status")) {
this.setStatus(test.getProperty("status"));
}
// update acceptance of test group according to acceptance of group
if (test.hasProperty("accepted")) {
this.setAccepted(test.getProperty("accepted"));
}
// return object for chaining purposes
return this;
}
clearTests() {
if (this.hasProperty("tests")) {
this.tests.length = 0;
}
}
[Symbol.iterator]() {
return {
next: function() {
if (this.index < this.array.length) {
this.index += 1;
return { value: this.array[this.index - 1], done: false };
} else {
return { done: true };
}
},
array: [].concat(this.hasGroups() ? this.groups : []),
index: 0
};
}
}
//
// TestCase
//
class TestCase extends TestGroup {
constructor(properties, parent) {
// call super constructor
super(properties, parent);
};
[Symbol.iterator]() {
return {
next: function() {
if (this.index < this.array.length) {
this.index += 1;
return { value: this.array[this.index - 1], done: false };
} else {
return { done: true };
}
},
array: [].concat(this.hasTests() ? this.tests : []),
index: 0
};
}
}
//
// Context
//
class Context extends TestGroup {
constructor(properties, parent) {
// call super constructor
super(properties, parent);
}
addTestCase(testcase) {
// return object for chaining purposes
return this.addGroup(testcase || new TestCase());
}
addTest(test) {
// make sure context contains at least one testcase
if (!this.hasGroups()) {
this.addTestCase();
}
// add test to last testcase
this.lastGroup.addTest(test);
// return object for chaining purposes
return this;
}
}
//
// Tab
//
class Tab extends TestGroup {
constructor(properties, parent) {
// call super constructor
super(properties, parent);
}
addContext(context) {
// return object for chaining purposes
return this.addGroup(context || new Context());
}
addTestCase(testcase) {
// make sure tab contains at least one context
if (!this.hasGroups()) {
this.addContext();
}
// add testcase to last context
this.lastGroup.addTestCase(testcase);
// return object for chaining purposes
return this;
}
addTest(test) {
// make sure tab contains at least one context
if (!this.hasGroups()) {
this.addContext();
}
// add test to last context
this.lastGroup.addTest(test);
// return object for chaining purposes
return this;
}
}
//
// Submission
//
class Submission extends TestGroup {
constructor(properties) {
// call super constructor
super(properties);
};
addTab(tab) {
// return object for chaining purposes
return this.addGroup(tab || new Tab());
}
addContext(context) {
// make sure submission contains at least one tab
if (!this.hasGroups()) {
this.addTab();
}
// add context to last group (tab)
this.lastGroup.addContext(context);
// return object for chaining purposes
return this;
}
addTestCase(testcase) {
// make sure submission contains at least one tab
if (!this.hasGroups()) {
this.addTab();
}
// add context to last group (tab)
this.lastGroup.addTestCase(testcase);
// return object for chaining purposes
return this;
}
addTest(test) {
// make sure submission contains at least one tab
if (!this.hasGroups()) {
this.addTab();
}
// add test to tab
this.lastGroup.addTest(test);
// return object for chaining purposes
return this;
}
}
module.exports = {
Message: Message,
Submission: Submission,
Tab: Tab,
Context: Context,
TestCase: TestCase,
Test: Test
};