Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit 7ad7e0a

Browse files
committed
♻️ Refactor getting settings from code
1 parent 330f54e commit 7ad7e0a

File tree

2 files changed

+24
-27
lines changed

2 files changed

+24
-27
lines changed

runestone/hparsons/js/SQLFeedback.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ export default class SQLFeedback extends HParsonsFeedback {
247247
}
248248

249249
// Now handle autograding
250-
if (this.hparsons.suffix) {
250+
if (this.hparsons.unittest) {
251251
this.testResult = this.autograde(
252252
this.results[this.results.length - 1]
253253
);
@@ -291,7 +291,7 @@ export default class SQLFeedback extends HParsonsFeedback {
291291

292292
// might move to base class if used by multiple execution based feedback
293293
autograde(result_table) {
294-
var tests = this.hparsons.suffix.split(/\n/);
294+
var tests = this.hparsons.unittest.split(/\n/);
295295
this.passed = 0;
296296
this.failed = 0;
297297
// Tests should be of the form

runestone/hparsons/js/hparsons.js

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ if (hpList === undefined) hpList = {};
1414
export default class HParsons extends RunestoneBase {
1515
constructor(opts) {
1616
super(opts);
17-
// copied from activecode
18-
var suffStart;
1917
// getting settings
2018
var orig = $(opts.orig).find("textarea")[0];
2119
this.reuse = $(orig).data("reuse") ? true : false;
@@ -37,16 +35,7 @@ export default class HParsons extends RunestoneBase {
3735
this.loadButton = null;
3836
this.outerDiv = null;
3937
this.controlDiv = null;
40-
let prefixEnd = this.code.indexOf("^^^^");
41-
if (prefixEnd > -1) {
42-
this.prefix = this.code.substring(0, prefixEnd);
43-
this.code = this.code.substring(prefixEnd + 5);
44-
}
45-
suffStart = this.code.indexOf("--unittest--");
46-
if (suffStart > -1) {
47-
this.suffix = this.code.substring(suffStart + 5);
48-
this.code = this.code.substring(0, suffStart);
49-
}
38+
this.processContent(this.code)
5039

5140
// Change to factory when more execution based feedback is included
5241
if (this.isBlockGrading) {
@@ -73,6 +62,26 @@ export default class HParsons extends RunestoneBase {
7362
this.feedbackController.init();
7463
}
7564

65+
processContent(code) {
66+
// todo: add errors when blocks are nonexistent (maybe in python)?
67+
this.originalBlocks = this.processSingleContent(code, '--blocks--').split('\n').slice(1,-1);
68+
this.unittest = this.processSingleContent(code, '--unittest--');
69+
}
70+
71+
processSingleContent(code, delimitier) {
72+
let index = code.indexOf(delimitier);
73+
if (index > -1) {
74+
let content = code.substring(index + delimitier.length);
75+
let endIndex = content.indexOf("\n--");
76+
content =
77+
endIndex > -1
78+
? content.substring(0, endIndex + 1)
79+
: content;
80+
return content;
81+
}
82+
return undefined;
83+
}
84+
7685
// copied from activecode, already modified to add parsons
7786
createEditor() {
7887
this.outerDiv = document.createElement("div");
@@ -82,24 +91,12 @@ export default class HParsons extends RunestoneBase {
8291
this.logHorizontalParsonsEvent(ev.detail);
8392
this.feedbackController.clearFeedback();
8493
});
85-
let blocks = [];
86-
let blockIndex = this.code.indexOf("--blocks--");
87-
if (blockIndex > -1) {
88-
let blocksString = this.code.substring(blockIndex + 10);
89-
let endIndex = blocksString.indexOf("\n--");
90-
blocksString =
91-
endIndex > -1
92-
? blocksString.substring(0, endIndex)
93-
: blocksString;
94-
blocks = blocksString.split("\n");
95-
}
96-
this.originalBlocks = blocks.slice(1, -1);
9794
const props = {
9895
selector: `#${this.divid}-container`,
9996
id: `${this.divid}-hparsons`,
10097
reuse: this.reuse,
10198
randomize: this.randomize,
102-
parsonsBlocks: blocks.slice(1, -1),
99+
parsonsBlocks: [...this.originalBlocks],
103100
language: this.language
104101
}
105102
InitMicroParsons(props);

0 commit comments

Comments
 (0)