kunit-stream.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * C++ stream style string formatter and printer used in KUnit for outputting
  4. * KUnit messages.
  5. *
  6. * Copyright (C) 2020, Google LLC.
  7. * Author: Brendan Higgins <[email protected]>
  8. */
  9. #ifndef _KUNIT_KUNIT_STREAM_H
  10. #define _KUNIT_KUNIT_STREAM_H
  11. #include <linux/types.h>
  12. #include <kunit/string-stream.h>
  13. struct kunit;
  14. /**
  15. * struct kunit_stream - a std::stream style string builder.
  16. *
  17. * A std::stream style string builder. Allows messages to be built up and
  18. * printed all at once. Note that the intention is to only use
  19. * &struct kunit_stream to communicate with a user of KUnit, most often to
  20. * communicate something about an expectation or an assertion to the user. If
  21. * you want a similar interface, but aren't sure if this is the right class for
  22. * you to use, you probably want to use the related string_stream class, which
  23. * is allowed for generic string construction in a similar manner. This class is
  24. * really only for the KUnit library to communicate certain kinds of information
  25. * to KUnit users and should not be used by anyone else.
  26. *
  27. * A note on &struct kunit_stream's usage: a kunit_stream will generally
  28. * accompany *one* expectation or assertion. Multiple expectations/assertions
  29. * may be validated concurrently at any given time, even within a single test
  30. * case, so sharing a kunit_stream between expectations/assertions may result in
  31. * unintended consequences.
  32. */
  33. struct kunit_stream {
  34. /* private: internal use only. */
  35. struct kunit *test;
  36. struct string_stream *internal_stream;
  37. };
  38. /**
  39. * alloc_kunit_stream() - constructs a new &struct kunit_stream.
  40. * @test: The test context object.
  41. * @gfp: The GFP flags to use for internal allocations.
  42. *
  43. * Constructs a new test managed &struct kunit_stream.
  44. */
  45. struct kunit_stream *alloc_kunit_stream(struct kunit *test,
  46. gfp_t gfp);
  47. /**
  48. * kunit_stream_add(): adds the formatted input to the internal buffer.
  49. * @kstream: the stream being operated on.
  50. * @fmt: printf style format string to append to stream.
  51. *
  52. * Appends the formatted string, @fmt, to the internal buffer.
  53. */
  54. void __printf(2, 3) kunit_stream_add(struct kunit_stream *kstream,
  55. const char *fmt, ...);
  56. /**
  57. * kunit_stream_append(): appends the contents of @other to @kstream.
  58. * @kstream: the stream to which @other is appended.
  59. * @other: the stream whose contents are appended to @kstream.
  60. *
  61. * Appends the contents of @other to @kstream.
  62. */
  63. void kunit_stream_append(struct kunit_stream *kstream,
  64. struct kunit_stream *other);
  65. /**
  66. * kunit_stream_commit(): prints out the internal buffer to the user.
  67. * @kstream: the stream being operated on.
  68. *
  69. * Outputs the contents of the internal buffer as a kunit_printk formatted
  70. * output. KUNIT_STREAM ONLY OUTPUTS ITS BUFFER TO THE USER IF COMMIT IS
  71. * CALLED!!! The reason for this is that it allows us to construct a message
  72. * before we know whether we want to print it out; this can be extremely handy
  73. * if there is information you might need for a failure message that is easiest
  74. * to collect in the steps leading up to the actual check.
  75. */
  76. void kunit_stream_commit(struct kunit_stream *kstream);
  77. /**
  78. * kunit_stream_clear(): clears the internal buffer.
  79. * @kstream: the stream being operated on.
  80. *
  81. * Clears the contents of the internal buffer.
  82. */
  83. void kunit_stream_clear(struct kunit_stream *kstream);
  84. #endif /* _KUNIT_KUNIT_STREAM_H */