Browse Source

Updated tests to work with Terminal class.

I had to make getters for most values for testing.
Steve Thielemann 4 years ago
parent
commit
f4d19e081f
3 changed files with 124 additions and 2 deletions
  1. 8 1
      terminal.cpp
  2. 8 0
      terminal.h
  3. 108 1
      test-terminal.cpp

+ 8 - 1
terminal.cpp

@@ -27,6 +27,14 @@ void Terminal::init(void) {
   this->saved_cursor_position.clear();
 }
 
+int Terminal::getx(void) { return posx; }
+int Terminal::gety(void) { return posy; }
+int Terminal::getstatus(void) { return status; }
+int Terminal::inANSI(void) { return in_ansi; }
+int Terminal::fg(void) { return fgcolor; }
+int Terminal::bg(void) { return bgcolor; }
+bool Terminal::ansiempty(void) { return ansi.empty(); }
+
 void Terminal::ansi_color(int color) {
   // ZF_LOGV("ansi_color(%d)", color);
   if (color == 0) {
@@ -386,7 +394,6 @@ void Terminal::putstring(std::string text) {
     putchar(*strit);
 }
 
-
 // Old non-C++ ways
 
 void console_init(struct console_details *cdp) {

+ 8 - 0
terminal.h

@@ -24,6 +24,14 @@ public:
   termchar putchar(char ch);
   void putstring(std::string text);
 
+  int getx(void);
+  int gety(void);
+  int getstatus(void);
+  int inANSI(void);
+  int fg(void);
+  int bg(void);
+  bool ansiempty(void);
+
 private:
   void ansi_color(int color);
   ANSI_TYPE ansi_code(std::string ansi);

+ 108 - 1
test-terminal.cpp

@@ -35,6 +35,33 @@ ANSI_TYPE console_ansi(struct console_details *cdp, const char *ansi);
 termchar console_char(struct console_details *cdp, char ch);
 void console_receive(struct console_details *cdp, std::string chars);
 
+// Ok, it's a problem testing this.  :(
+
+class Terminal {
+private:
+  int posx, posy;
+  std::vector<std::pair<int, int>> saved_cursor_position;
+  std::string ansi;
+  int in_ansi;
+  int fgcolor, bgcolor, status;
+  enum ANSI_TYPE { START, CURSOR, COLOR, CLEAR, OTHER };
+  struct termchar {
+    int in_ansi;
+    ANSI_TYPE ansi;
+  };
+
+public:
+  Terminal(void);
+  void init(void);
+  std::string color_restore(void);
+  termchar putchar(char ch);
+  void putstring(std::string text);
+
+private:
+  void ansi_color(int color);
+  ANSI_TYPE ansi_code(std::string ansi);
+};
+
 */
 
 console_details console;
@@ -54,7 +81,7 @@ TEST(ConsoleInit, init) {
   ASSERT_EQ(console.bgcolor, 0);
   ASSERT_EQ(console.fgcolor, 7);
 
-  std::string reset("\x1b[0m\x1b[2J\x1b[1;1H");
+  // std::string reset("\x1b[0m\x1b[2J\x1b[1;1H");
   console_receive(&console, reset);
   ASSERT_EQ(console.posx, 0);
   ASSERT_EQ(console.posy, 0);
@@ -121,4 +148,84 @@ TEST(ConsoleCRNL, NLCRTests) {
   ASSERT_EQ(console.posy, 1);
 }
 
+TEST(Terminal, init) {
+  Terminal term;
+
+  ASSERT_EQ(term.getx(), 0);
+  ASSERT_EQ(term.gety(), 0);
+  ASSERT_EQ(term.inANSI(), 0);
+  ASSERT_TRUE(term.ansiempty());
+  ASSERT_EQ(term.getstatus(), 0);
+  ASSERT_EQ(term.bg(), 0);
+  ASSERT_EQ(term.fg(), 7);
+
+  term.putstring(reset);
+
+  ASSERT_EQ(term.getx(), 0);
+  ASSERT_EQ(term.gety(), 0);
+  ASSERT_EQ(term.inANSI(), 0);
+  ASSERT_TRUE(term.ansiempty());
+  ASSERT_EQ(term.getstatus(), 0);
+  ASSERT_EQ(term.bg(), 0);
+  ASSERT_EQ(term.fg(), 7);
+}
+
+TEST(TerminalBasic, simpleString) {
+  Terminal term;
+  std::string send("Hello");
+
+  term.putstring(send);
+  ASSERT_NE(term.getx(), 0);
+  ASSERT_EQ(term.getx(), (int)send.size());
+  ASSERT_EQ(term.gety(), 0);
+
+  term.putstring(reset);
+  ASSERT_EQ(term.getx(), 0);
+  ASSERT_EQ(term.gety(), 0);
+  send = "Hello\r\n";
+  term.putstring(send);
+  ASSERT_EQ(term.getx(), 0);
+  ASSERT_EQ(term.gety(), 1);
+}
+
+TEST(TerminalPosition, simplePositions) {
+  Terminal term;
+
+  std::string send("\x1b[10;5H"); // Y;X
+
+  term.putstring(send);
+  ASSERT_EQ(term.getx(), 4);
+  ASSERT_EQ(term.gety(), 9);
+  std::string save_pos("\x1b[s");
+  std::string restore_pos("\x1b[u");
+  term.putstring(save_pos);
+  ASSERT_EQ(term.getx(), 4);
+  ASSERT_EQ(term.gety(), 9);
+  send = "\x1b[5;10H";
+  term.putstring(send);
+  ASSERT_EQ(term.getx(), 9);
+  ASSERT_EQ(term.gety(), 4);
+  term.putstring(restore_pos);
+  ASSERT_EQ(term.getx(), 4);
+  ASSERT_EQ(term.gety(), 9);
+}
+
+TEST(TerminalCRNL, NLCRTests) {
+  Terminal term;
+
+  std::string send("Hello\r");
+  term.putstring(send);
+  ASSERT_EQ(term.getx(), 0);
+  ASSERT_EQ(term.gety(), 0);
+  send = "Hello\n";
+  term.putstring(send);
+  ASSERT_EQ(term.getx(), 5);
+  ASSERT_EQ(term.gety(), 1);
+  term.putstring(reset);
+  send = "Hello\r\n";
+  term.putstring(send);
+  ASSERT_EQ(term.getx(), 0);
+  ASSERT_EQ(term.gety(), 1);
+}
+
 } // namespace