/thirdparty/breakpad/third_party/protobuf/protobuf/src/google/protobuf/io/printer_unittest.cc

http://github.com/tomahawk-player/tomahawk · C++ · 261 lines · 170 code · 51 blank · 40 comment · 4 complexity · 56bedf445f5fd1a8adaf671a2d5cd0bd MD5 · raw file

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. #include <vector>
  34. #include <google/protobuf/io/printer.h>
  35. #include <google/protobuf/io/zero_copy_stream_impl.h>
  36. #include <google/protobuf/stubs/common.h>
  37. #include <google/protobuf/testing/googletest.h>
  38. #include <gtest/gtest.h>
  39. namespace google {
  40. namespace protobuf {
  41. namespace io {
  42. namespace {
  43. // Each test repeats over several block sizes in order to test both cases
  44. // where particular writes cross a buffer boundary and cases where they do
  45. // not.
  46. TEST(Printer, EmptyPrinter) {
  47. char buffer[8192];
  48. const int block_size = 100;
  49. ArrayOutputStream output(buffer, GOOGLE_ARRAYSIZE(buffer), block_size);
  50. Printer printer(&output, '\0');
  51. EXPECT_TRUE(!printer.failed());
  52. }
  53. TEST(Printer, BasicPrinting) {
  54. char buffer[8192];
  55. for (int block_size = 1; block_size < 512; block_size *= 2) {
  56. ArrayOutputStream output(buffer, sizeof(buffer), block_size);
  57. {
  58. Printer printer(&output, '\0');
  59. printer.Print("Hello World!");
  60. printer.Print(" This is the same line.\n");
  61. printer.Print("But this is a new one.\nAnd this is another one.");
  62. EXPECT_FALSE(printer.failed());
  63. }
  64. buffer[output.ByteCount()] = '\0';
  65. EXPECT_STREQ("Hello World! This is the same line.\n"
  66. "But this is a new one.\n"
  67. "And this is another one.",
  68. buffer);
  69. }
  70. }
  71. TEST(Printer, WriteRaw) {
  72. char buffer[8192];
  73. for (int block_size = 1; block_size < 512; block_size *= 2) {
  74. ArrayOutputStream output(buffer, sizeof(buffer), block_size);
  75. {
  76. string string_obj = "From an object\n";
  77. Printer printer(&output, '$');
  78. printer.WriteRaw("Hello World!", 12);
  79. printer.PrintRaw(" This is the same line.\n");
  80. printer.PrintRaw("But this is a new one.\nAnd this is another one.");
  81. printer.WriteRaw("\n", 1);
  82. printer.PrintRaw(string_obj);
  83. EXPECT_FALSE(printer.failed());
  84. }
  85. buffer[output.ByteCount()] = '\0';
  86. EXPECT_STREQ("Hello World! This is the same line.\n"
  87. "But this is a new one.\n"
  88. "And this is another one."
  89. "\n"
  90. "From an object\n",
  91. buffer);
  92. }
  93. }
  94. TEST(Printer, VariableSubstitution) {
  95. char buffer[8192];
  96. for (int block_size = 1; block_size < 512; block_size *= 2) {
  97. ArrayOutputStream output(buffer, sizeof(buffer), block_size);
  98. {
  99. Printer printer(&output, '$');
  100. map<string, string> vars;
  101. vars["foo"] = "World";
  102. vars["bar"] = "$foo$";
  103. vars["abcdefg"] = "1234";
  104. printer.Print(vars, "Hello $foo$!\nbar = $bar$\n");
  105. printer.PrintRaw("RawBit\n");
  106. printer.Print(vars, "$abcdefg$\nA literal dollar sign: $$");
  107. vars["foo"] = "blah";
  108. printer.Print(vars, "\nNow foo = $foo$.");
  109. EXPECT_FALSE(printer.failed());
  110. }
  111. buffer[output.ByteCount()] = '\0';
  112. EXPECT_STREQ("Hello World!\n"
  113. "bar = $foo$\n"
  114. "RawBit\n"
  115. "1234\n"
  116. "A literal dollar sign: $\n"
  117. "Now foo = blah.",
  118. buffer);
  119. }
  120. }
  121. TEST(Printer, InlineVariableSubstitution) {
  122. char buffer[8192];
  123. ArrayOutputStream output(buffer, sizeof(buffer));
  124. {
  125. Printer printer(&output, '$');
  126. printer.Print("Hello $foo$!\n", "foo", "World");
  127. printer.PrintRaw("RawBit\n");
  128. printer.Print("$foo$ $bar$\n", "foo", "one", "bar", "two");
  129. EXPECT_FALSE(printer.failed());
  130. }
  131. buffer[output.ByteCount()] = '\0';
  132. EXPECT_STREQ("Hello World!\n"
  133. "RawBit\n"
  134. "one two\n",
  135. buffer);
  136. }
  137. TEST(Printer, Indenting) {
  138. char buffer[8192];
  139. for (int block_size = 1; block_size < 512; block_size *= 2) {
  140. ArrayOutputStream output(buffer, sizeof(buffer), block_size);
  141. {
  142. Printer printer(&output, '$');
  143. map<string, string> vars;
  144. vars["newline"] = "\n";
  145. printer.Print("This is not indented.\n");
  146. printer.Indent();
  147. printer.Print("This is indented\nAnd so is this\n");
  148. printer.Outdent();
  149. printer.Print("But this is not.");
  150. printer.Indent();
  151. printer.Print(" And this is still the same line.\n"
  152. "But this is indented.\n");
  153. printer.PrintRaw("RawBit has indent at start\n");
  154. printer.PrintRaw("but not after a raw newline\n");
  155. printer.Print(vars, "Note that a newline in a variable will break "
  156. "indenting, as we see$newline$here.\n");
  157. printer.Indent();
  158. printer.Print("And this");
  159. printer.Outdent();
  160. printer.Outdent();
  161. printer.Print(" is double-indented\nBack to normal.");
  162. EXPECT_FALSE(printer.failed());
  163. }
  164. buffer[output.ByteCount()] = '\0';
  165. EXPECT_STREQ(
  166. "This is not indented.\n"
  167. " This is indented\n"
  168. " And so is this\n"
  169. "But this is not. And this is still the same line.\n"
  170. " But this is indented.\n"
  171. " RawBit has indent at start\n"
  172. "but not after a raw newline\n"
  173. "Note that a newline in a variable will break indenting, as we see\n"
  174. "here.\n"
  175. " And this is double-indented\n"
  176. "Back to normal.",
  177. buffer);
  178. }
  179. }
  180. // Death tests do not work on Windows as of yet.
  181. #ifdef GTEST_HAS_DEATH_TEST
  182. TEST(Printer, Death) {
  183. char buffer[8192];
  184. ArrayOutputStream output(buffer, sizeof(buffer));
  185. Printer printer(&output, '$');
  186. EXPECT_DEBUG_DEATH(printer.Print("$nosuchvar$"), "Undefined variable");
  187. EXPECT_DEBUG_DEATH(printer.Print("$unclosed"), "Unclosed variable name");
  188. EXPECT_DEBUG_DEATH(printer.Outdent(), "without matching Indent");
  189. }
  190. #endif // GTEST_HAS_DEATH_TEST
  191. TEST(Printer, WriteFailure) {
  192. char buffer[16];
  193. ArrayOutputStream output(buffer, sizeof(buffer));
  194. Printer printer(&output, '$');
  195. // Print 16 bytes to fill the buffer exactly (should not fail).
  196. printer.Print("0123456789abcdef");
  197. EXPECT_FALSE(printer.failed());
  198. // Try to print one more byte (should fail).
  199. printer.Print(" ");
  200. EXPECT_TRUE(printer.failed());
  201. // Should not crash
  202. printer.Print("blah");
  203. EXPECT_TRUE(printer.failed());
  204. // Buffer should contain the first 16 bytes written.
  205. EXPECT_EQ("0123456789abcdef", string(buffer, sizeof(buffer)));
  206. }
  207. } // namespace
  208. } // namespace io
  209. } // namespace protobuf
  210. } // namespace google