test-mangle.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 "wordplay.h"
  10. #include "gtest/gtest.h"
  11. #include <errno.h>
  12. #include <fcntl.h>
  13. #include <string>
  14. #include <sys/stat.h>
  15. #include <sys/types.h>
  16. #include <time.h>
  17. #include <unistd.h>
  18. #define GTEST_COUT std::cerr << "[ ] [ INFO ]"
  19. struct console_details console;
  20. std::string username = "Pseudonym";
  21. std::string fullname = "Joe User";
  22. int harry_level(void) { return 4; }
  23. int randint(int x) { return 1; }
  24. int randrange(int x, int y) { return x; }
  25. int random_activate(int x) { return 1; }
  26. char *repr(const char *) {
  27. static char text[] = "tada!";
  28. return text;
  29. }
  30. bool replace(std::string &str, const std::string &from, const std::string &to) {
  31. size_t start_pos = str.find(from);
  32. if (start_pos == std::string::npos)
  33. return false;
  34. str.replace(start_pos, from.length(), to);
  35. return true;
  36. }
  37. const char *logrepr(const char *) {
  38. static char text[] = "repr!";
  39. return text;
  40. }
  41. namespace {
  42. class MangleFixture : public ::testing::Test {
  43. public:
  44. time_t start;
  45. int fd;
  46. const char *filename = "mangle.testing";
  47. protected:
  48. virtual void SetUp() {
  49. console_init(&console);
  50. start = time(NULL);
  51. // creat : File is WRITE ONLY.
  52. fd = creat(filename, S_IRUSR | S_IWUSR);
  53. }
  54. virtual void TearDown() {
  55. if (fd > 0)
  56. close(fd);
  57. unlink(filename);
  58. }
  59. };
  60. TEST_F(MangleFixture, TestMangle) {
  61. std::string data("The bear is sticky");
  62. // This tests the tangler code
  63. mangle(fd, data);
  64. int lfd = open(filename, O_RDONLY);
  65. char buffer[100];
  66. int len = read(lfd, buffer, sizeof(buffer));
  67. if (len == -1) {
  68. ASSERT_EQ(errno, 0);
  69. }
  70. buffer[len] = 0;
  71. close(lfd);
  72. GTEST_COUT << "buffer:" << buffer << std::endl;
  73. ASSERT_STREQ(
  74. buffer,
  75. "T \bh \be \b \bb \be \ba \br \b \bi \bs \b \bs \bt \bi \bc \bk \by \b");
  76. }
  77. /*
  78. TEST_F(RenderFixture, ColorTest) {
  79. ASSERT_GT(fd, 0);
  80. std::string colors("^C01One^C02Two^C03Three^C04Four");
  81. render(fd, colors);
  82. int lfd = open(filename, O_RDONLY);
  83. char buffer[100];
  84. int len = read(lfd, buffer, sizeof(buffer));
  85. if (len == -1) {
  86. ASSERT_EQ(errno, 0);
  87. }
  88. buffer[len] = 0;
  89. close(lfd);
  90. GTEST_COUT << "buffer:" << buffer << std::endl;
  91. ASSERT_EQ(len, 43);
  92. ASSERT_STREQ(buffer,
  93. "\x1B[0;34mOne\x1B[0;32mTwo\x1B[0;36mThree\x1B[0;31mFour");
  94. struct console_details cons;
  95. console_init(&cons);
  96. int color_list[4] = {4, 2, 6, 1};
  97. int color = 0;
  98. for (int x = 0; x < len; ++x) {
  99. termchar tc;
  100. tc = console_char(&cons, buffer[x]);
  101. if (tc.ansi == COLOR) {
  102. GTEST_COUT << "Console color:" << cons.fgcolor << std::endl;
  103. ASSERT_EQ(cons.fgcolor, color_list[color]);
  104. color++;
  105. }
  106. }
  107. ASSERT_EQ(color, 4);
  108. }
  109. TEST_F(RenderFixture, ColorEffectTest) {
  110. std::string colors("^S1^R1\x1b[1;33;44mMEOW!\x1b[0m");
  111. render(fd, colors);
  112. int lfd = open(filename, O_RDONLY);
  113. char buffer[100];
  114. int len = read(lfd, buffer, sizeof(buffer));
  115. if (len == -1) {
  116. ASSERT_EQ(errno, 0);
  117. }
  118. buffer[len] = 0;
  119. close(lfd);
  120. ASSERT_EQ(strstr(buffer, "\x1b[1;33;44m"), buffer);
  121. GTEST_COUT << "buffer:" << buffer << std::endl;
  122. ASSERT_EQ(len, 29);
  123. ASSERT_STREQ(buffer, "\x1b[1;33;44mM \bE \bO \bW \b! \b\x1b[0m");
  124. }
  125. */
  126. } // namespace