test-terminal.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. */
  31. console_details console;
  32. const std::string reset("\x1b[0m\x1b[2J\x1b[1;1H");
  33. #define GTEST_COUT std::cerr << "[ ] [ INFO ]"
  34. namespace {
  35. TEST(ConsoleInit, init) {
  36. console_init(&console);
  37. ASSERT_EQ(console.posx, 0);
  38. ASSERT_EQ(console.posy, 0);
  39. ASSERT_EQ(console.in_ansi, 0);
  40. ASSERT_EQ(console.ansi[0], 0);
  41. ASSERT_EQ(console.status, 0);
  42. ASSERT_EQ(console.bgcolor, 0);
  43. ASSERT_EQ(console.fgcolor, 7);
  44. std::string reset("\x1b[0m\x1b[2J\x1b[1;1H");
  45. console_receive(&console, reset);
  46. ASSERT_EQ(console.posx, 0);
  47. ASSERT_EQ(console.posy, 0);
  48. ASSERT_EQ(console.in_ansi, 0);
  49. ASSERT_EQ(console.ansi[0], 0);
  50. ASSERT_EQ(console.status, 0);
  51. ASSERT_EQ(console.bgcolor, 0);
  52. ASSERT_EQ(console.fgcolor, 7);
  53. }
  54. TEST(ConsoleBasic, simpleString) {
  55. console_init(&console);
  56. std::string send("Hello");
  57. console_receive(&console, send);
  58. ASSERT_NE(console.posx, 0);
  59. ASSERT_EQ(console.posx, (int)send.size());
  60. ASSERT_EQ(console.posy, 0);
  61. console_receive(&console, reset);
  62. ASSERT_EQ(console.posx, 0);
  63. ASSERT_EQ(console.posy, 0);
  64. send = "Hello\r\n";
  65. console_receive(&console, send);
  66. ASSERT_EQ(console.posx, 0);
  67. ASSERT_EQ(console.posy, 1);
  68. }
  69. TEST(ConsolePosition, simplePositions) {
  70. console_init(&console);
  71. std::string send("\x1b[10;5H"); // Y;X
  72. console_receive(&console, send);
  73. ASSERT_EQ(console.posx, 4);
  74. ASSERT_EQ(console.posy, 9);
  75. std::string save_pos("\x1b[s");
  76. std::string restore_pos("\x1b[u");
  77. console_receive(&console, save_pos);
  78. ASSERT_EQ(console.posx, 4);
  79. ASSERT_EQ(console.posy, 9);
  80. send = "\x1b[5;10H";
  81. console_receive(&console, send);
  82. ASSERT_EQ(console.posx, 9);
  83. ASSERT_EQ(console.posy, 4);
  84. console_receive(&console, restore_pos);
  85. ASSERT_EQ(console.posx, 4);
  86. ASSERT_EQ(console.posy, 9);
  87. }
  88. TEST(ConsoleCRNL, NLCRTests) {
  89. console_init(&console);
  90. std::string send("Hello\r");
  91. console_receive(&console, send);
  92. ASSERT_EQ(console.posx, 0);
  93. ASSERT_EQ(console.posy, 0);
  94. send = "Hello\n";
  95. console_receive(&console, send);
  96. ASSERT_EQ(console.posy, 1);
  97. ASSERT_EQ(console.posx, 5);
  98. console_receive(&console, reset);
  99. send = "Hello\r\n";
  100. console_receive(&console, send);
  101. ASSERT_EQ(console.posx, 0);
  102. ASSERT_EQ(console.posy, 1);
  103. }
  104. } // namespace