test-door.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "door.h"
  2. #include "gtest/gtest.h"
  3. namespace {
  4. class DoorTest : public ::testing::Test {
  5. protected:
  6. void SetUp() override {
  7. int argc = 5;
  8. char argv0[] = "./test";
  9. char argv1[] = "-l";
  10. char argv2[] = "-u";
  11. char argv3[] = "test";
  12. char argv4[] = "--debuggering";
  13. char *argv[] = {argv0, argv1, argv2, argv3, argv4};
  14. door::debug_capture = true;
  15. d = new door::Door("test", argc, argv);
  16. d->debug_buffer.clear();
  17. }
  18. void TearDown() override {
  19. delete d;
  20. d = nullptr;
  21. }
  22. public:
  23. door::Door *d;
  24. };
  25. TEST_F(DoorTest, BasicColorOut1) {
  26. door::ANSIColor BonY(door::COLOR::BLUE, door::COLOR::YELLOW);
  27. char BLUE_ON_YELLOW[] = "\x1b[34;43m";
  28. *d << BonY;
  29. EXPECT_STREQ(d->debug_buffer.c_str(), BLUE_ON_YELLOW);
  30. d->debug_buffer.clear();
  31. door::ANSIColor YonB(door::COLOR::YELLOW, door::COLOR::BLUE,
  32. door::ATTR::BOLD);
  33. char Y_ON_B[] = "\x1b[1;33;44m";
  34. *d << YonB;
  35. EXPECT_STREQ(d->debug_buffer.c_str(), Y_ON_B);
  36. *d << door::reset;
  37. d->debug_buffer.clear();
  38. // Without the reset, an extra 0; gets added to the output.
  39. door::ANSIColor GonR(door::COLOR::GREEN, door::COLOR::RED, door::ATTR::BLINK);
  40. char G_ON_R[] = "\x1b[5;32;41m";
  41. *d << GonR;
  42. EXPECT_STREQ(d->debug_buffer.c_str(), G_ON_R);
  43. d->debug_buffer.clear();
  44. }
  45. TEST_F(DoorTest, ResetOutput) {
  46. *d << door::reset;
  47. EXPECT_STREQ(d->debug_buffer.c_str(), "\x1b[0m");
  48. d->debug_buffer.clear();
  49. *d << door::nl;
  50. EXPECT_STREQ(d->debug_buffer.c_str(), "\r\n");
  51. d->debug_buffer.clear();
  52. *d << door::cls;
  53. // CLS + GOTO
  54. EXPECT_STREQ(d->debug_buffer.c_str(), "\x1b[2J\x1b[H");
  55. d->debug_buffer.clear();
  56. }
  57. TEST_F(DoorTest, ColorOptimizeOut1) {
  58. door::ANSIColor RonB(door::COLOR::RED, door::COLOR::BLUE);
  59. char R_ON_B[] = "\x1b[31;44m";
  60. *d << RonB;
  61. EXPECT_STREQ(d->debug_buffer.c_str(), R_ON_B);
  62. d->debug_buffer.clear();
  63. door::ANSIColor GonB(door::COLOR::GREEN, door::COLOR::BLUE);
  64. char G_ON_B[] = "\x1b[32m";
  65. *d << GonB;
  66. EXPECT_STREQ(d->debug_buffer.c_str(), G_ON_B);
  67. d->debug_buffer.clear();
  68. }
  69. TEST_F(DoorTest, GotoOutput) {
  70. door::Goto pos(1, 1);
  71. *d << pos;
  72. EXPECT_STREQ(d->debug_buffer.c_str(), "\x1b[H");
  73. d->debug_buffer.clear();
  74. pos.set(5, 10);
  75. *d << pos;
  76. EXPECT_STREQ(d->debug_buffer.c_str(), "\x1b[10;5H");
  77. d->debug_buffer.clear();
  78. pos.set(5, 1);
  79. *d << pos;
  80. EXPECT_STREQ(d->debug_buffer.c_str(), "\x1b[;5H");
  81. d->debug_buffer.clear();
  82. pos.set(1, 10);
  83. *d << pos;
  84. EXPECT_STREQ(d->debug_buffer.c_str(), "\x1b[10H");
  85. d->debug_buffer.clear();
  86. }
  87. TEST_F(DoorTest, LineOutput) {
  88. door::Line line("Meow");
  89. *d << line;
  90. EXPECT_STREQ(d->debug_buffer.c_str(), "Meow");
  91. d->debug_buffer.clear();
  92. door::Line color("Cat", 4,
  93. door::ANSIColor(door::COLOR::BLACK, door::COLOR::WHITE));
  94. *d << color;
  95. EXPECT_STREQ(d->debug_buffer.c_str(), "\x1b[30;47mCat ");
  96. *d << door::reset;
  97. d->debug_buffer.clear();
  98. door::Line pad("Test", 4, door::ANSIColor(door::COLOR::RED));
  99. pad.setPadding("**", door::ANSIColor(door::COLOR::GREEN));
  100. *d << pad;
  101. EXPECT_STREQ(d->debug_buffer.c_str(), "\x1b[32m**\x1b[31mTest\x1b[32m**");
  102. d->debug_buffer.clear();
  103. }
  104. } // namespace