test_utils.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2011 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // Author: jdtang@google.com (Jonathan Tang)
  16. #ifndef GUMBO_TEST_UTILS_H_
  17. #define GUMBO_TEST_UTILS_H_
  18. #include <assert.h>
  19. #include <stdint.h>
  20. #include <string>
  21. #include "gtest/gtest.h"
  22. #include "gumbo.h"
  23. #include "parser.h"
  24. inline std::string ToString(const GumboStringPiece& str) {
  25. return std::string(str.data, str.length);
  26. }
  27. int GetChildCount(GumboNode* node);
  28. GumboTag GetTag(GumboNode* node);
  29. GumboNode* GetChild(GumboNode* parent, int index);
  30. int GetAttributeCount(GumboNode* node);
  31. GumboAttribute* GetAttribute(GumboNode* node, int index);
  32. // Convenience function to do some basic assertions on the structure of the
  33. // document (nodes are elements, nodes have the right tags) and then return
  34. // the body node.
  35. void GetAndAssertBody(GumboNode* root, GumboNode** body);
  36. void SanityCheckPointers(
  37. const char* input, size_t input_length, const GumboNode* node, int depth);
  38. // Custom allocator machinery to sanity check for memory leaks. Normally we can
  39. // use heapcheck/valgrind/ASAN for this, but they only give the
  40. // results when the program terminates. This means that if the parser is run in
  41. // a loop (say, a MapReduce) and there's a leak, it may end up exhausting memory
  42. // before it can catch the particular document responsible for the leak. These
  43. // allocators let us check each document individually for leaks.
  44. typedef struct {
  45. uint64_t bytes_allocated;
  46. uint64_t objects_allocated;
  47. uint64_t objects_freed;
  48. } MallocStats;
  49. void InitLeakDetection(GumboOptions* options, MallocStats* stats);
  50. // Base class for Gumbo tests. This provides an GumboParser object that's
  51. // been initialized to sane values, as normally happens in the beginning of
  52. // gumbo_parse, and then a destructor that cleans up after it.
  53. class GumboTest : public ::testing::Test {
  54. protected:
  55. GumboTest();
  56. virtual ~GumboTest();
  57. MallocStats malloc_stats_;
  58. GumboOptions options_;
  59. GumboParser parser_;
  60. bool errors_are_expected_;
  61. const char* text_;
  62. };
  63. #endif // GUMBO_TEST_UTILS_H_