1
0

auto_gen_output.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import {SemanticAnalyser} from './../processor/semantic/semanticAnalyser';
  2. import {IVProgProcessor} from './../processor/ivprogProcessor';
  3. import { InputTest } from './inputTest';
  4. import { OutputTest } from './outputTest';
  5. import { LocalizedStrings } from './../services/localizedStringsService'
  6. export function autoGenerateTestCaseOutput (program_text, testCases) {
  7. let copyTestCases = testCases.map((test) => Object.assign({}, test));
  8. try {
  9. const program = SemanticAnalyser.analyseFromSource(program_text);
  10. const resultList = testCases.map((test, id) => {
  11. const input = new InputTest(test.input);
  12. const output = new OutputTest();
  13. const exec = new IVProgProcessor(program);
  14. exec.registerInput(input);
  15. exec.registerOutput(output);
  16. return exec.interpretAST().then(_ => {
  17. return {id: id, program: exec};
  18. });
  19. });
  20. return Promise.all(resultList).then(result_final => {
  21. for(let i = 0; i < result_final.length; ++i) {
  22. const result = result_final[i];
  23. const output = result.program.output.list;
  24. const input = result.program.input;
  25. if(input.index != input.inputList.length) {
  26. window.showAlert(LocalizedStrings.getMessage("testcase_autogen_unused_input", [result.id+1]));
  27. return Promise.resolve(false);
  28. }
  29. if(output.length == 0) {
  30. window.showAlert(LocalizedStrings.getMessage("testcase_autogen_empty", [result.id+1]));
  31. }
  32. copyTestCases[result.id].output = output;
  33. }
  34. window.outputGenerated(copyTestCases);
  35. return Promise.resolve(true);
  36. });
  37. }catch (error) {
  38. return Promise.reject(error)
  39. }
  40. }