test-render.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 "render.h"
  7. #include "terminal.h"
  8. #include "utils.h"
  9. #include "gtest/gtest.h"
  10. #include <errno.h>
  11. #include <fcntl.h>
  12. #include <string>
  13. #include <sys/stat.h>
  14. #include <sys/types.h>
  15. #include <time.h>
  16. #include <unistd.h>
  17. #define GTEST_COUT std::cerr << "[ ] [ INFO ]"
  18. struct console_details console;
  19. namespace {
  20. class RenderFixture : public ::testing::Test {
  21. public:
  22. time_t start;
  23. int fd;
  24. const char *filename = "render.testing";
  25. protected:
  26. virtual void SetUp() {
  27. console_init(&console);
  28. start = time(NULL);
  29. // creat : File is WRITE ONLY.
  30. fd = creat(filename, S_IRUSR | S_IWUSR);
  31. }
  32. virtual void TearDown() {
  33. if (fd > 0)
  34. close(fd);
  35. unlink(filename);
  36. }
  37. };
  38. TEST_F(RenderFixture, CheckPause) {
  39. std::string pause( TRIGGER "P1");
  40. render(fd, pause);
  41. int elapsed = time(NULL) - start;
  42. ASSERT_GT(elapsed, 0);
  43. ASSERT_EQ(elapsed, 1);
  44. }
  45. TEST_F(RenderFixture, CheckPauseTwo) {
  46. std::string pause( TRIGGER "P2");
  47. render(fd, pause);
  48. int elapsed = time(NULL) - start;
  49. ASSERT_GT(elapsed, 1);
  50. ASSERT_EQ(elapsed, 2);
  51. }
  52. TEST_F(RenderFixture, ColorTest) {
  53. ASSERT_GT(fd, 0);
  54. std::string colors( TRIGGER "C01One" TRIGGER "C02Two" TRIGGER "C03Three" TRIGGER "C04Four");
  55. render(fd, colors);
  56. int lfd = open(filename, O_RDONLY);
  57. char buffer[100];
  58. int len = read(lfd, buffer, sizeof(buffer));
  59. if (len == -1) {
  60. ASSERT_EQ(errno, 0);
  61. }
  62. buffer[len] = 0;
  63. close(lfd);
  64. GTEST_COUT << "buffer:" << buffer << std::endl;
  65. ASSERT_EQ(len, 43);
  66. ASSERT_STREQ(buffer,
  67. "\x1B[0;34mOne\x1B[0;32mTwo\x1B[0;36mThree\x1B[0;31mFour");
  68. struct console_details cons;
  69. console_init(&cons);
  70. int color_list[4] = {4, 2, 6, 1};
  71. int color = 0;
  72. for (int x = 0; x < len; ++x) {
  73. termchar tc;
  74. tc = console_char(&cons, buffer[x]);
  75. if (tc.ansi == COLOR) {
  76. GTEST_COUT << "Console color:" << cons.fgcolor << std::endl;
  77. ASSERT_EQ(cons.fgcolor, color_list[color]);
  78. color++;
  79. }
  80. }
  81. ASSERT_EQ(color, 4);
  82. }
  83. TEST_F(RenderFixture, ColorEffectTest) {
  84. std::string colors( TRIGGER "S1" TRIGGER "R1\x1b[1;33;44mMEOW!\x1b[0m");
  85. render(fd, colors);
  86. int lfd = open(filename, O_RDONLY);
  87. char buffer[100];
  88. int len = read(lfd, buffer, sizeof(buffer));
  89. if (len == -1) {
  90. ASSERT_EQ(errno, 0);
  91. }
  92. buffer[len] = 0;
  93. close(lfd);
  94. ASSERT_EQ(strstr(buffer, "\x1b[1;33;44m"), buffer);
  95. GTEST_COUT << "buffer:" << buffer << std::endl;
  96. ASSERT_EQ(len, 29);
  97. ASSERT_STREQ(buffer, "\x1b[1;33;44mM \bE \bO \bW \b! \b\x1b[0m");
  98. }
  99. } // namespace