ivprogAssessment.js 3.6 KB

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