testConsole.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { DOMConsole } from "../io/domConsole";
  2. import { getInstance } from "../services/localizedStringsService";
  3. const LocalizedStrings = getInstance();
  4. export class TestConsole {
  5. constructor (inputList) {
  6. this.index = 0;
  7. this.inputList = inputList;
  8. this.list = [];
  9. }
  10. write (text) {
  11. this._appendText(text, DOMConsole.USER);
  12. }
  13. info (text) {
  14. this._appendText(text, DOMConsole.INFO);
  15. }
  16. err (text) {
  17. this._appendText(text, DOMConsole.ERR);
  18. }
  19. _appendText (text) {
  20. this.list.push(text);
  21. }
  22. getClassForType (type) {
  23. switch (type) {
  24. case DOMConsole.USER:
  25. return "ivprog-term-userText";
  26. case DOMConsole.INFO:
  27. return "ivprog-term-info";
  28. case DOMConsole.ERR:
  29. return "ivprog-term-error";
  30. }
  31. }
  32. requestInput () {
  33. const promise = new Promise( (resolve, reject) => {
  34. if(this.index < this.inputList.length) {
  35. resolve(this.inputList[this.index]);
  36. this.index++;
  37. } else {
  38. reject(new Error(LocalizedStrings.getError("exceeded_input_request")));
  39. }
  40. });
  41. return promise
  42. }
  43. sendOutput (text) {
  44. const output = ""+text;
  45. output.split("\n").forEach(t => {
  46. t = t.replace(/\t/g,'&#9;');
  47. this.write(t)
  48. });
  49. }
  50. }