ivprogAssessment.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import line_i18n from "line-i18n";
  2. import { IVProgProcessor } from "./../processor/ivprogProcessor";
  3. import { DOMConsole } from "./../io/domConsole";
  4. import { OutputMatching } from "./output_matching/output_matching";
  5. import { Config } from "../util/config";
  6. import { Assessment } from "./assessment";
  7. import { Assistant } from "../assistant/assistant";
  8. const StringTypes = line_i18n.StringTypes;
  9. export class IVProgAssessment extends Assessment {
  10. constructor (ast_code, exerciseType, testCases, domConsole) {
  11. super(ast_code, exerciseType, testCases, domConsole);
  12. }
  13. runTest () {
  14. try {
  15. // loop test cases and show messages through domconsole
  16. const partialTests = this.testCases.map((t, name) => {
  17. return new OutputMatching(
  18. new IVProgProcessor(this.ast_code),
  19. t.input,
  20. t.output,
  21. name,
  22. t.tag
  23. );
  24. });
  25. const testResult = partialTests.map((om) => om.eval());
  26. return Promise.all(testResult).then((results) => {
  27. let grade = 0;
  28. for (let i = 0; i < results.length; i += 1) {
  29. const result = results[i];
  30. grade += result.grade;
  31. if (result.grade == 1) {
  32. this.writeToConsole(
  33. DOMConsole.INFO,
  34. StringTypes.MESSAGE,
  35. "test_case_success",
  36. result.name + 1,
  37. result.generateOutput()
  38. );
  39. } else if (result.status == 1) {
  40. this.writeToConsole(
  41. DOMConsole.ERR,
  42. StringTypes.ERROR,
  43. "test_case_failed_exception",
  44. result.name + 1,
  45. result.error_msg,
  46. result.generateOutput()
  47. );
  48. } else {
  49. this.writeToConsole(
  50. DOMConsole.ERR,
  51. StringTypes.ERROR,
  52. "test_case_failed",
  53. result.name + 1,
  54. result.generateOutput()
  55. );
  56. }
  57. }
  58. grade /= results.length;
  59. const channel = grade == 1 ? DOMConsole.INFO : DOMConsole.ERR;
  60. this.writeToConsole(
  61. channel,
  62. StringTypes.MESSAGE,
  63. "test_suite_grade",
  64. (grade * 100).toFixed(2)
  65. );
  66. const ivprog_assistant = new Assistant();
  67. let textArray = ivprog_assistant.analise("http://127.0.0.1:81/moodle/mod/iassign/endpoint.php",this.exerciseType,results);
  68. Promise.resolve(textArray).then((Text) => {
  69. this.writeToConsole(
  70. DOMConsole.INFO,
  71. StringTypes.MESSAGE,
  72. "assistant_content",
  73. results[0].generateAssistantOutput(Text[0],Text[1])
  74. );
  75. });
  76. return grade;
  77. });
  78. // return testResult.then(function (total) {
  79. // const grade = total / this.testCases.length;
  80. // const channel = grade == 1 ? DOMConsole.INFO : DOMConsole.ERR;
  81. // this.writeToConsole(channel, StringTypes.MESSAGE, "test_suite_grade", (grade * 100).toFixed(2));
  82. // return Promise.resolve(grade)
  83. // }).catch(err => {
  84. // this.domConsole.err("Erro inesperado durante o cálculo da nota.");// try and show error messages through domconsole
  85. // this.domConsole.err(err.message);
  86. // return Promise.resolve(0);
  87. // });
  88. } catch (error) {
  89. this.showErrorMessage(
  90. DOMConsole.ERR,
  91. StringTypes.MESSAGE,
  92. "unexpected_execution_error"
  93. );
  94. this.domConsole.err(error.message);
  95. return Promise.resolve(0);
  96. } finally {
  97. Config.setConfig(this.old_config);
  98. }
  99. }
  100. }