textdata.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <istream>
  15. #include <string>
  16. #include <sstream>
  17. #include <gtest/gtest.h>
  18. #ifndef __UTIL_UNITTESTS_TEXTDATA_H
  19. #define __UTIL_UNITTESTS_TEXTDATA_H 1
  20. /**
  21. * @file textdata.h
  22. * @short Utilities for tests with text data.
  23. *
  24. * This utility provides convenient helper functions for unit tests using
  25. * textual data.
  26. */
  27. namespace isc {
  28. namespace util {
  29. namespace unittests {
  30. /// Line-by-line text comparison.
  31. ///
  32. /// This templated function takes two standard input streams, extracts
  33. /// strings from them, and compares the two sets of strings line by line.
  34. template <typename EXPECTED_STREAM, typename ACTUAL_STREAM>
  35. void
  36. matchTextData(EXPECTED_STREAM& expected, ACTUAL_STREAM& actual) {
  37. std::string actual_line;
  38. std::string expected_line;
  39. while (std::getline(actual, actual_line), !actual.eof()) {
  40. std::getline(expected, expected_line);
  41. if (expected.eof()) {
  42. ASSERT_FALSE(true) << "Redundant line in actual output: "
  43. << actual_line;
  44. break;
  45. }
  46. if (actual.bad() || actual.fail() ||
  47. expected.bad() || expected.fail()) {
  48. throw std::runtime_error("Unexpected error in data streams");
  49. }
  50. EXPECT_EQ(expected_line, actual_line);
  51. }
  52. while (std::getline(expected, expected_line), !expected.eof()) {
  53. EXPECT_FALSE(true) << "Missing line in actual output: "
  54. << expected_line;
  55. }
  56. }
  57. /// Similar to the fully templated version, but takes string for the second
  58. /// (actual) data.
  59. ///
  60. /// Due to the nature of textual data, it will often be the case that test
  61. /// data is given as a string object. This shortcut version helps such cases
  62. /// so that the test code doesn't have to create a string stream with the
  63. /// string data just for testing.
  64. template <typename EXPECTED_STREAM>
  65. void
  66. matchTextData(EXPECTED_STREAM& expected, const std::string& actual_text) {
  67. std::istringstream iss(actual_text);
  68. matchTextData(expected, iss);
  69. }
  70. /// Same for the previous version, but the first argument is string.
  71. template <typename ACTUAL_STREAM>
  72. void
  73. matchTextData(const std::string& expected_text, ACTUAL_STREAM& actual) {
  74. std::istringstream iss(expected_text);
  75. matchTextData(iss, actual);
  76. }
  77. /// Same for the previous two, but takes strings for both expected and
  78. /// actual data.
  79. void
  80. matchTextData(const std::string& expected_text,
  81. const std::string& actual_text)
  82. {
  83. std::istringstream expected_is(expected_text);
  84. std::istringstream actual_is(actual_text);
  85. matchTextData(expected_is, actual_is);
  86. }
  87. }
  88. }
  89. }
  90. #endif // __UTIL_UNITTESTS_TEXTDATA_H
  91. // Local Variables:
  92. // mode: c++
  93. // End: