test-terminal.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // Not sure where to begin with gtest?
  2. //
  3. // What can I test with gtest?
  4. //
  5. // googletest/googletest/docs/primer.md
  6. #include "terminal.h"
  7. #include "gtest/gtest.h"
  8. #include <string>
  9. /*
  10. struct console_details {
  11. int posx, posy;
  12. int savedx, savedy;
  13. char ansi[20]; // current ANSI command being processed.
  14. int in_ansi;
  15. int fgcolor; // 0-7 // not 0-15
  16. int bgcolor; // 0-7
  17. int status; // 0, 1 or 5 (Blink)
  18. };
  19. enum ANSI_TYPE { START, CURSOR, COLOR, CLEAR, OTHER };
  20. struct termchar {
  21. int in_ansi;
  22. ANSI_TYPE ansi; // undefined if in_ansi is false
  23. };
  24. void console_init(struct console_details *cdp);
  25. void ansi_color(struct console_details *cdp, int color);
  26. const char *color_restore(struct console_details *cdp);
  27. ANSI_TYPE console_ansi(struct console_details *cdp, const char *ansi);
  28. termchar console_char(struct console_details *cdp, char ch);
  29. void console_receive(struct console_details *cdp, std::string chars);
  30. // Ok, it's a problem testing this. :(
  31. class Terminal {
  32. private:
  33. int posx, posy;
  34. std::vector<std::pair<int, int>> saved_cursor_position;
  35. std::string ansi;
  36. int in_ansi;
  37. int fgcolor, bgcolor, status;
  38. enum ANSI_TYPE { START, CURSOR, COLOR, CLEAR, OTHER };
  39. struct termchar {
  40. int in_ansi;
  41. ANSI_TYPE ansi;
  42. };
  43. public:
  44. Terminal(void);
  45. void init(void);
  46. std::string color_restore(void);
  47. termchar putchar(char ch);
  48. void putstring(std::string text);
  49. private:
  50. void ansi_color(int color);
  51. ANSI_TYPE ansi_code(std::string ansi);
  52. };
  53. */
  54. console_details console;
  55. const std::string reset("\x1b[0m\x1b[2J\x1b[1;1H");
  56. #define GTEST_COUT std::cerr << "[ ] [ INFO ]"
  57. namespace {
  58. TEST(ConsoleInit, init) {
  59. console_init(&console);
  60. ASSERT_EQ(console.posx, 0);
  61. ASSERT_EQ(console.posy, 0);
  62. ASSERT_EQ(console.in_ansi, 0);
  63. ASSERT_EQ(console.ansi[0], 0);
  64. ASSERT_EQ(console.status, 0);
  65. ASSERT_EQ(console.bgcolor, 0);
  66. ASSERT_EQ(console.fgcolor, 7);
  67. // std::string reset("\x1b[0m\x1b[2J\x1b[1;1H");
  68. console_receive(&console, reset);
  69. ASSERT_EQ(console.posx, 0);
  70. ASSERT_EQ(console.posy, 0);
  71. ASSERT_EQ(console.in_ansi, 0);
  72. ASSERT_EQ(console.ansi[0], 0);
  73. ASSERT_EQ(console.status, 0);
  74. ASSERT_EQ(console.bgcolor, 0);
  75. ASSERT_EQ(console.fgcolor, 7);
  76. }
  77. TEST(ConsoleBasic, simpleString) {
  78. console_init(&console);
  79. std::string send("Hello");
  80. console_receive(&console, send);
  81. ASSERT_NE(console.posx, 0);
  82. ASSERT_EQ(console.posx, (int)send.size());
  83. ASSERT_EQ(console.posy, 0);
  84. console_receive(&console, reset);
  85. ASSERT_EQ(console.posx, 0);
  86. ASSERT_EQ(console.posy, 0);
  87. send = "Hello\r\n";
  88. console_receive(&console, send);
  89. ASSERT_EQ(console.posx, 0);
  90. ASSERT_EQ(console.posy, 1);
  91. }
  92. TEST(ConsolePosition, simplePositions) {
  93. console_init(&console);
  94. std::string send("\x1b[10;5H"); // Y;X
  95. console_receive(&console, send);
  96. ASSERT_EQ(console.posx, 4);
  97. ASSERT_EQ(console.posy, 9);
  98. std::string save_pos("\x1b[s");
  99. std::string restore_pos("\x1b[u");
  100. console_receive(&console, save_pos);
  101. ASSERT_EQ(console.posx, 4);
  102. ASSERT_EQ(console.posy, 9);
  103. send = "\x1b[5;10H";
  104. console_receive(&console, send);
  105. ASSERT_EQ(console.posx, 9);
  106. ASSERT_EQ(console.posy, 4);
  107. console_receive(&console, restore_pos);
  108. ASSERT_EQ(console.posx, 4);
  109. ASSERT_EQ(console.posy, 9);
  110. }
  111. TEST(ConsoleCRNL, NLCRTests) {
  112. console_init(&console);
  113. std::string send("Hello\r");
  114. console_receive(&console, send);
  115. ASSERT_EQ(console.posx, 0);
  116. ASSERT_EQ(console.posy, 0);
  117. send = "Hello\n";
  118. console_receive(&console, send);
  119. ASSERT_EQ(console.posy, 1);
  120. ASSERT_EQ(console.posx, 5);
  121. console_receive(&console, reset);
  122. send = "Hello\r\n";
  123. console_receive(&console, send);
  124. ASSERT_EQ(console.posx, 0);
  125. ASSERT_EQ(console.posy, 1);
  126. }
  127. TEST(Terminal, init) {
  128. Terminal term;
  129. ASSERT_EQ(term.getx(), 0);
  130. ASSERT_EQ(term.gety(), 0);
  131. ASSERT_EQ(term.inANSI(), 0);
  132. ASSERT_TRUE(term.ansiempty());
  133. ASSERT_EQ(term.getstatus(), 0);
  134. ASSERT_EQ(term.bg(), 0);
  135. ASSERT_EQ(term.fg(), 7);
  136. term.putstring(reset);
  137. ASSERT_EQ(term.getx(), 0);
  138. ASSERT_EQ(term.gety(), 0);
  139. ASSERT_EQ(term.inANSI(), 0);
  140. ASSERT_TRUE(term.ansiempty());
  141. ASSERT_EQ(term.getstatus(), 0);
  142. ASSERT_EQ(term.bg(), 0);
  143. ASSERT_EQ(term.fg(), 7);
  144. }
  145. TEST(TerminalBasic, simpleString) {
  146. Terminal term;
  147. std::string send("Hello");
  148. term.putstring(send);
  149. ASSERT_NE(term.getx(), 0);
  150. ASSERT_EQ(term.getx(), (int)send.size());
  151. ASSERT_EQ(term.gety(), 0);
  152. term.putstring(reset);
  153. ASSERT_EQ(term.getx(), 0);
  154. ASSERT_EQ(term.gety(), 0);
  155. send = "Hello\r\n";
  156. term.putstring(send);
  157. ASSERT_EQ(term.getx(), 0);
  158. ASSERT_EQ(term.gety(), 1);
  159. }
  160. TEST(TerminalPosition, simplePositions) {
  161. Terminal term;
  162. std::string send("\x1b[10;5H"); // Y;X
  163. term.putstring(send);
  164. ASSERT_EQ(term.getx(), 4);
  165. ASSERT_EQ(term.gety(), 9);
  166. std::string save_pos("\x1b[s");
  167. std::string restore_pos("\x1b[u");
  168. term.putstring(save_pos);
  169. ASSERT_EQ(term.getx(), 4);
  170. ASSERT_EQ(term.gety(), 9);
  171. send = "\x1b[5;10H";
  172. term.putstring(send);
  173. ASSERT_EQ(term.getx(), 9);
  174. ASSERT_EQ(term.gety(), 4);
  175. term.putstring(restore_pos);
  176. ASSERT_EQ(term.getx(), 4);
  177. ASSERT_EQ(term.gety(), 9);
  178. }
  179. TEST(TerminalCRNL, NLCRTests) {
  180. Terminal term;
  181. std::string send("Hello\r");
  182. term.putstring(send);
  183. ASSERT_EQ(term.getx(), 0);
  184. ASSERT_EQ(term.gety(), 0);
  185. send = "Hello\n";
  186. term.putstring(send);
  187. ASSERT_EQ(term.getx(), 5);
  188. ASSERT_EQ(term.gety(), 1);
  189. term.putstring(reset);
  190. send = "Hello\r\n";
  191. term.putstring(send);
  192. ASSERT_EQ(term.getx(), 0);
  193. ASSERT_EQ(term.gety(), 1);
  194. }
  195. } // namespace