textdata.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. * \brief 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. FAIL() << "Redundant line in actual output: " << actual_line;
  43. break;
  44. }
  45. if (actual.bad() || actual.fail() ||
  46. expected.bad() || expected.fail()) {
  47. throw std::runtime_error("Unexpected error in data streams");
  48. }
  49. EXPECT_EQ(expected_line, actual_line);
  50. }
  51. while (std::getline(expected, expected_line), !expected.eof()) {
  52. ADD_FAILURE() << "Missing line in actual output: " << expected_line;
  53. }
  54. }
  55. /// Similar to the fully templated version, but takes string for the second
  56. /// (actual) data.
  57. ///
  58. /// Due to the nature of textual data, it will often be the case that test
  59. /// data is given as a string object. This shortcut version helps such cases
  60. /// so that the test code doesn't have to create a string stream with the
  61. /// string data just for testing.
  62. template <typename EXPECTED_STREAM>
  63. void
  64. matchTextData(EXPECTED_STREAM& expected, const std::string& actual_text) {
  65. std::istringstream iss(actual_text);
  66. matchTextData(expected, iss);
  67. }
  68. /// Same for the previous version, but the first argument is string.
  69. template <typename ACTUAL_STREAM>
  70. void
  71. matchTextData(const std::string& expected_text, ACTUAL_STREAM& actual) {
  72. std::istringstream iss(expected_text);
  73. matchTextData(iss, actual);
  74. }
  75. /// Same for the previous two, but takes strings for both expected and
  76. /// actual data.
  77. void
  78. matchTextData(const std::string& expected_text,
  79. const std::string& actual_text)
  80. {
  81. std::istringstream expected_is(expected_text);
  82. std::istringstream actual_is(actual_text);
  83. matchTextData(expected_is, actual_is);
  84. }
  85. }
  86. }
  87. }
  88. #endif // __UTIL_UNITTESTS_TEXTDATA_H
  89. // Local Variables:
  90. // mode: c++
  91. // End: