assessment_result.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import StringDiff from "./../../util/string_diff";
  2. import { LocalizedStrings } from "./../../services/localizedStringsService";
  3. export class OutputAssessmentResult {
  4. static get PAGE_TEMPLATE () {
  5. return `<!DOCTYPE html>
  6. <html>
  7. <head>
  8. <meta http-equiv='content-type' content='text/html; charset=UTF-8'>
  9. <link rel='stylesheet' href='css/ivprog-assessment.css' type='text/css'/>
  10. </head>
  11. <body>
  12. :assessment-result:
  13. </body>
  14. </html>`;
  15. }
  16. static get DETAIL_TEMPLATE () {
  17. return `<div class='details-body'>
  18. <div class='details-header'>
  19. <h2>:test-name:</h2>
  20. <p>:time-label:: <span>:time:ms</span></p>
  21. <p>:grade-label:: <span>:grade:%</span></p>
  22. </div>
  23. <div>
  24. <h3>:input-label:</h3>
  25. <ul>
  26. <li>:input-list:</li>
  27. </ul>
  28. </div>
  29. <div>
  30. <h3>:output-label:</h3>
  31. :output-result:
  32. </div>
  33. </div>`;
  34. }
  35. static get OUPUT_TABLE_TEMPLATE () {
  36. return `<div class='detaisl-div-table'>
  37. <table class='assessment-output-table'>
  38. <tr>
  39. <th>:expected-label:</th>
  40. <th>:generated-label:</th>
  41. <th>:result-label:</th>
  42. </tr>
  43. :results:
  44. </table>
  45. </div>`;
  46. }
  47. static get OUTPUT_TEMPLATE () {
  48. return `<tr><td class=':class-expected:'>$0</td>
  49. <td class=':class-generated:'>$1</td>
  50. <td class=':class-result:'>$2</td></tr>`;
  51. }
  52. static get EMPTY_OUTPUT_TEMPLATE () {
  53. return `<div class='assessment-popup'><img class='assessment-empty-output' src='img/empty.svg'>
  54. <span class='assessment-popuptext'>$0</span></div>`;
  55. }
  56. static get FAILED_TEMPLATE () {
  57. return `<p class='assessment-failed-execution'><span class='assessment-failed-case'>✗</span>$0</p>`;
  58. }
  59. static get INPUT_INFO_TEMPLATE () {
  60. return `<span class='$0'>$1</span>`;
  61. }
  62. // Status code - it is not grade related!
  63. // 0 - Succesful execution
  64. // 1 - failed execution
  65. constructor (
  66. name,
  67. status,
  68. inputs,
  69. result,
  70. store,
  71. time,
  72. error_msg = "",
  73. error_id
  74. ) {
  75. this.name = name;
  76. this.status = status;
  77. this.inputs = inputs;
  78. this.results = result;
  79. this.store = store;
  80. this.time = time;
  81. this.error_msg = error_msg;
  82. this.error_id = error_id;
  83. }
  84. get grade () {
  85. if (this.results == null) {
  86. return 0;
  87. }
  88. return (
  89. this.results.reduce((prev, val) => prev + val.grade, 0) /
  90. this.results.length
  91. );
  92. }
  93. prepareResults () {
  94. let template = OutputAssessmentResult.DETAIL_TEMPLATE;
  95. const grade = (this.grade * 100).toFixed(2);
  96. const time = this.time || "-";
  97. template = template.replace(
  98. ":test-name:",
  99. LocalizedStrings.getUI("assessment-detail-title", [this.name + 1])
  100. );
  101. template = template.replace(
  102. ":time-label:",
  103. LocalizedStrings.getUI("assessment-detail-time-label")
  104. );
  105. template = template.replace(":time:", time);
  106. template = template.replace(
  107. ":grade-label:",
  108. LocalizedStrings.getUI("assessment-detail-grade-label")
  109. );
  110. template = template.replace(":grade:", grade);
  111. const input_spans = this.prepareInputList(this.inputs);
  112. template = template.replace(
  113. ":input-label:",
  114. LocalizedStrings.getUI("assessment-detail-input-label")
  115. );
  116. template = template.replace(":input-list:", input_spans);
  117. template = template.replace(
  118. ":output-label:",
  119. LocalizedStrings.getUI("assessment-detail-output-label")
  120. );
  121. if (this.status == 0) {
  122. const output_rows = this.results.map((result) => {
  123. if (result.type == "string") {
  124. return this.formatString(result);
  125. } else if (result.type == "number") {
  126. return this.formatNumber(result);
  127. } else {
  128. return this.formatBool(result);
  129. }
  130. }, this);
  131. template = template.replace(
  132. ":output-result:",
  133. this.prepareOutputTable(output_rows)
  134. );
  135. } else {
  136. let failed_text = OutputAssessmentResult.FAILED_TEMPLATE;
  137. failed_text = failed_text.replace("$0", this.error_msg);
  138. template = template.replace(":output-result:", failed_text);
  139. }
  140. return template;
  141. }
  142. prepareInputList (input_list) {
  143. const list = input_list.map((input) => {
  144. let template = OutputAssessmentResult.INPUT_INFO_TEMPLATE;
  145. template = template.replace("$1", input.value);
  146. if (input.read) {
  147. template = template.replace("$0", "assessment-input-read");
  148. } else {
  149. template = template.replace("$0", "assessment-input-unread");
  150. }
  151. return template;
  152. }, this);
  153. return list.join(LocalizedStrings.getUI("text_join_assessment_outputs"));
  154. }
  155. prepareOutputTable (output_rows) {
  156. let template = OutputAssessmentResult.OUPUT_TABLE_TEMPLATE;
  157. template = template.replace(
  158. ":expected-label:",
  159. LocalizedStrings.getUI("assessment-detail-expected-label")
  160. );
  161. template = template.replace(
  162. ":generated-label:",
  163. LocalizedStrings.getUI("assessment-detail-generated-label")
  164. );
  165. template = template.replace(
  166. ":result-label:",
  167. LocalizedStrings.getUI("assessment-detail-result-label")
  168. );
  169. template = template.replace(":results:", output_rows.join(""));
  170. return template;
  171. }
  172. generateOutput () {
  173. const assessment_result = this.prepareResults();
  174. let page = OutputAssessmentResult.PAGE_TEMPLATE;
  175. page = page.replace(":assessment-result:", assessment_result);
  176. page = page.replace(/(\r|\n|\t)/gm, "").replace(/> *</g, "><");
  177. return page;
  178. }
  179. formatNumber (result) {
  180. const result_class =
  181. result.grade == 1
  182. ? "assessment-number-result"
  183. : "assessment-number-result-failed";
  184. const template = this.formatOutput(
  185. "assessment-number-expected",
  186. "assessment-number-generated",
  187. result_class,
  188. result
  189. );
  190. return template;
  191. }
  192. formatBool (result) {
  193. const result_class =
  194. result.grade == 1
  195. ? "assessment-bool-result"
  196. : "assessment-bool-result-failed";
  197. const template = this.formatOutput(
  198. "assessment-bool-expected",
  199. "assessment-bool-generated",
  200. result_class,
  201. result
  202. );
  203. return template;
  204. }
  205. formatOutput (expected_class, generated_class, result_class, result) {
  206. let template = OutputAssessmentResult.OUTPUT_TEMPLATE;
  207. template = template.replace(":class-expected:", expected_class);
  208. template = template.replace(":class-generated:", generated_class);
  209. template = template.replace(":class-result:", result_class);
  210. let expected_tmpl = result.expected;
  211. let generated_tmpl = result.generated;
  212. if (expected_tmpl == null) {
  213. expected_tmpl = OutputAssessmentResult.EMPTY_OUTPUT_TEMPLATE.replace(
  214. "$0",
  215. LocalizedStrings.getMessage("assessment-empty-expected-tooltip")
  216. );
  217. } else if (generated_tmpl == null) {
  218. generated_tmpl = OutputAssessmentResult.EMPTY_OUTPUT_TEMPLATE.replace(
  219. "$0",
  220. LocalizedStrings.getMessage("assessment-empty-generated-tooltip")
  221. );
  222. }
  223. template = template.replace("$0", expected_tmpl);
  224. template = template.replace("$1", generated_tmpl);
  225. const final_result = result.grade == 1 ? "✓" : "✗";
  226. template = template.replace("$2", final_result);
  227. return template;
  228. }
  229. formatString (result) {
  230. const expected_class = "assessment-string-expected";
  231. const generated_class = "assessment-string-generated";
  232. //const result_class = 'assessment-string-result';
  233. let template = OutputAssessmentResult.OUTPUT_TEMPLATE;
  234. template = template.replace(":class-expected:", expected_class);
  235. template = template.replace(":class-generated:", generated_class);
  236. //template = template.replace(":class-result:", result_class);
  237. const g_string = result.generated || "";
  238. const e_string = result.expected || "";
  239. // console.log("generated: ", g_string,"expected: ", e_string);
  240. let g_string_tmpl = g_string;
  241. let e_string_tmpl = e_string;
  242. if (result.generated == null) {
  243. g_string_tmpl = OutputAssessmentResult.EMPTY_OUTPUT_TEMPLATE.replace(
  244. "$0",
  245. LocalizedStrings.getMessage("assessment-empty-generated-tooltip")
  246. );
  247. } else if (result.expected == null) {
  248. e_string_tmpl = OutputAssessmentResult.EMPTY_OUTPUT_TEMPLATE.replace(
  249. "$0",
  250. LocalizedStrings.getMessage("assessment-empty-expected-tooltip")
  251. );
  252. }
  253. template = template.replace("$0", e_string_tmpl);
  254. template = template.replace("$1", g_string_tmpl);
  255. if (result.grade == 1) {
  256. template = template.replace("$2", "✓");
  257. template = template.replace(":class-result:", "assessment-string-result");
  258. } else {
  259. const diff = StringDiff(g_string, e_string);
  260. // console.log(diff);
  261. const diff_vec = diff.map(
  262. (part) => this.getDiffStringStyle(part[1], part[0]),
  263. this
  264. );
  265. const diff_string = diff_vec.reduce((prev, actual) => prev + actual, "");
  266. template = template.replace(
  267. "$2",
  268. "<span class='assessment-failed-case'>✗</span>" + diff_string
  269. );
  270. template = template.replace(":class-result:", "assessment-string-diff");
  271. }
  272. return template;
  273. }
  274. getDiffStringStyle (text, action) {
  275. const template = "<span class='$0'>$1</span>";
  276. // Fix missing whitespace when its a single element
  277. text = text.replace(/\s/g, "&#160;");
  278. switch (action) {
  279. case StringDiff.INSERT:
  280. return template.replace("$0", "stringdiff-insert").replace("$1", text);
  281. case StringDiff.DELETE:
  282. return template.replace("$0", "stringdiff-delete").replace("$1", text);
  283. case StringDiff.EQUAL:
  284. return template.replace("$0", "stringdiff-equal").replace("$1", text);
  285. }
  286. }
  287. }