ivprogAssessment.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import line_i18n from 'line-i18n'
  2. import { IVProgProcessor } from "./../processor/ivprogProcessor";
  3. import { DOMConsole} from "./../io/domConsole";
  4. import * as LocalizedStringsService from "../services/localizedStringsService";
  5. import { OutputMatching } from './output_matching/output_matching';
  6. const LocalizedStrings = LocalizedStringsService.getInstance();
  7. const StringTypes = line_i18n.StringTypes;
  8. export class IVProgAssessment {
  9. constructor (ast_code, testCases, domConsole) {
  10. this.ast_code = ast_code;
  11. this.testCases = testCases;
  12. this.domConsole = domConsole;
  13. }
  14. runTest () {
  15. const outerRef = this;
  16. try {
  17. // loop test cases and show messages through domconsole
  18. const partialTests = this.testCases.map( (t, name) => {
  19. return new OutputMatching(new IVProgProcessor(outerRef.ast_code), t.input, t.output, name);
  20. });
  21. const testResult = partialTests.map(om => om.eval());
  22. return Promise.all(testResult).then(results => {
  23. let grade = 0;
  24. for(let i = 0; i < results.length; ++i) {
  25. const result = results[i];
  26. grade += result.grade;
  27. if(result.grade == 1) {
  28. outerRef.writeToConsole(DOMConsole.INFO, StringTypes.MESSAGE,'test_case_success',
  29. result.name + 1, result.generateOutput());
  30. } else if (result.status == 1) {
  31. outerRef.writeToConsole(DOMConsole.ERR, StringTypes.ERROR,'test_case_failed_exception',
  32. result.name + 1, result.error_msg, result.generateOutput());
  33. } else {
  34. outerRef.writeToConsole(DOMConsole.ERR, StringTypes.ERROR,'test_case_failed',
  35. result.name + 1, result.generateOutput());
  36. }
  37. }
  38. grade /= results.length;
  39. const channel = grade == 1 ? DOMConsole.INFO : DOMConsole.ERR;
  40. outerRef.writeToConsole(channel, StringTypes.MESSAGE, "test_suite_grade", (grade * 100).toFixed(2));
  41. return grade;
  42. });
  43. // return testResult.then(function (total) {
  44. // const grade = total / outerRef.testCases.length;
  45. // const channel = grade == 1 ? DOMConsole.INFO : DOMConsole.ERR;
  46. // outerRef.writeToConsole(channel, StringTypes.MESSAGE, "test_suite_grade", (grade * 100).toFixed(2));
  47. // return Promise.resolve(grade)
  48. // }).catch(err => {
  49. // outerRef.domConsole.err("Erro inesperado durante o cálculo da nota.");// try and show error messages through domconsole
  50. // outerRef.domConsole.err(err.message);
  51. // return Promise.resolve(0);
  52. // });
  53. } catch (error) {
  54. outerRef.domConsole.err("Erro inesperado durante a execução do programa");// try and show error messages through domconsole
  55. outerRef.domConsole.err(error.message);
  56. return Promise.resolve(0);
  57. }
  58. }
  59. showErrorMessage (errorID, ...args) {
  60. this.domConsole.err(LocalizedStrings.getError(errorID, args));
  61. }
  62. showInfoMessage (msgID, ...args) {
  63. this.domConsole.info(LocalizedStrings.getMessage(msgID, args));
  64. }
  65. writeToConsole (channel, msgType, msgID, ...args) {
  66. let msg = LocalizedStrings.getString(msgID, msgType);
  67. msg = LocalizedStrings.processString(msg, args);
  68. switch(channel) {
  69. case DOMConsole.ERR: {
  70. this.domConsole.err(msg);
  71. break;
  72. }
  73. case DOMConsole.INFO: {
  74. this.domConsole.info(msg);
  75. break;
  76. }
  77. case DOMConsole.USER: {
  78. this.domConsole.write(msg);
  79. break;
  80. }
  81. }
  82. }
  83. }