ivprogAssessment.js 3.0 KB

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