contexts.qbk 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. [/
  2. / Copyright (c) 2015 Boost.Test contributors
  3. /
  4. / Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. /]
  7. [section:contexts Contexts]
  8. Contexts are a facility provided by the __UTF__ in order to be able to trace the location of assertions better. To grasp
  9. the idea, consider the following example:
  10. ``
  11. void test_operations(Processor& processor, int limit)
  12. {
  13. for (int i = 0; i < limit; ++i) {
  14. BOOST_TEST(processor.op1(i));
  15. for (int j = 0; j < i; ++j) {
  16. BOOST_TEST(processor.op2(i, j));
  17. }
  18. }
  19. }
  20. ``
  21. In case of failure, in order to see in the logs at which point of the loops the failure occurred, we need some extra
  22. information in the assertion, which can be achieved for instance [link boost_test.testing_tools.reports.custom_messages the following way]:
  23. ``
  24. BOOST_TEST(processor.op1(i));
  25. ``
  26. replaced by
  27. ``
  28. BOOST_TEST(processor.op1(i), "With parameter i = " << i);
  29. ``
  30. We see in this trivial example that a context, which is the variable `i` in this case, should be acknowledged by the
  31. assertion `BOOST_CHECK` in a particular way. In the approach above, this is done by adding a message to the assertion
  32. itself.
  33. What if the context is more complex than that? In case the complexity of the context increases, the fact that the
  34. assertion and the context is tightly coupled as in the approach above is difficult to maintain:
  35. ``
  36. void test_operations(Processor& processor, int limit, int level)
  37. {
  38. for (int i = 0; i < limit; ++i) {
  39. BOOST_TEST(processor.op1(i),
  40. "With optimization level " << level << ", With parameter i = " << i);
  41. for (int j = 0; j < i; ++j) {
  42. BOOST_TEST(processor.op2(i, j),
  43. "With optimization level " << level <<
  44. ", With parameter i = " << i << ", With parameter j = " << j);
  45. }
  46. }
  47. }
  48. BOOST_AUTO_TEST_CASE(test1)
  49. {
  50. Processor processor;
  51. for (int level = 0; level < 3; ++level) {
  52. processor.optimization_level(level);
  53. test_operations(processor, 2, level);
  54. }
  55. }
  56. ``
  57. Note the length of the messages, the repetition, and the fact, that we pass argument `level` to function
  58. `test_operations` only for the sake of generating an error message in case of a failure.
  59. Therefore, *loose* coupling between the context of an assertion and the assertion point is a property that is desirable.
  60. [#ref_BOOST_TEST_INFO][h3 Assertion-bound context]
  61. `BOOST_TEST_INFO` can be used to define an error message to be bound to the first following assertion. If (and only
  62. if) the assertion fails, the bound message will be displayed along:
  63. [bt_example example80_contexts..Assertion-bound context..run-fail]
  64. The information composed inside `BOOST_TEST_INFO` is bound only to the first assertion
  65. following the declaration. This information is only displayed if the assertion fails; otherwise the message is
  66. discarded. The `BOOST_TEST_INFO` declaration does not have to immediately precede the assertion, it is allowed to
  67. intertwine them with other instructions, they can even be declared in different scopes. It is also possible to
  68. bind more than one information to a given assertion.
  69. With `BOOST_TEST_INFO`, we can improve our initial example as follows:
  70. ``
  71. void test_operations(Processor& processor, int limit, int level)
  72. {
  73. for (int i = 0; i < limit; ++i) {
  74. BOOST_TEST_INFO("With optimization level " << level);
  75. BOOST_TEST_INFO("With parameter i = " << i);
  76. BOOST_TEST(processor.op1(i));
  77. for (int j = 0; j < i; ++j) {
  78. BOOST_TEST_INFO("With optimization level " << level);
  79. BOOST_TEST_INFO("With parameter i = " << i);
  80. BOOST_TEST_INFO("With parameter j = " << j);
  81. BOOST_TEST(processor.op2(i, j));
  82. }
  83. }
  84. }
  85. BOOST_AUTO_TEST_CASE(test1)
  86. {
  87. Processor processor;
  88. for (int level = 0; level < 3; ++level) {
  89. processor.optimization_level(level);
  90. test_operations(processor, 2, level);
  91. }
  92. }
  93. ``
  94. [#ref_BOOST_TEST_CONTEXT][h3 Scope-bound context]
  95. In the previous example, the information stored inside the calls to `BOOST_TEST_INFO` were all consumed by the next assertion. There are cases
  96. where we would like this information be persistent for the current scope. __UTF__ provides two tools to achieve this:
  97. * `BOOST_TEST_CONTEXT` defines a diagnostic message and a scope. The message is bound to every assertion in that scope,
  98. and is displayed along with every failed assertion.
  99. * `BOOST_TEST_INFO_SCOPE` acts the same as `BOOST_TEST_INFO`, but the stored context information is bound to all the assertions
  100. that follow the call to `BOOST_TEST_INFO_SCOPE` within the current scope.
  101. [tip Since Boost [link ref_CHANGE_LOG_3_10 Boost 1.70], `BOOST_TEST_CONTEXT` can accept multiple arguments.]
  102. [tip `BOOST_TEST_INFO_SCOPE` has been introduced in [link ref_CHANGE_LOG_3_10 Boost 1.70].]
  103. [bt_example example81_contexts..Scope-bound context..run-fail]
  104. In the previous example, there is an opening brace right after `BOOST_TEST_CONTEXT`: this pair of braces defines the scope in which
  105. the diagnostic message is in effect. If there is no braces, the scope applies only to the following statement.
  106. `BOOST_TEST_CONTEXT` declarations can nest.
  107. With `BOOST_TEST_CONTEXT`, we can further improve our initial example, by putting variable `level` into a scope-level context
  108. and not pass it as function parameter:
  109. ``
  110. void test_operations(Processor& processor, int limit)
  111. {
  112. for (int i = 0; i < limit; ++i) {
  113. BOOST_TEST_INFO("With parameter i = " << i);
  114. BOOST_TEST(processor.op1(i));
  115. for (int j = 0; j < i; ++j) {
  116. BOOST_TEST_INFO("With parameter i = " << i);
  117. BOOST_TEST_INFO("With parameter j = " << j);
  118. BOOST_TEST(processor.op2(i, j));
  119. }
  120. }
  121. }
  122. BOOST_AUTO_TEST_CASE(test1)
  123. {
  124. Processor processor;
  125. for (int level = 0; level < 3; ++level) {
  126. BOOST_TEST_CONTEXT("With optimization level " << level) {
  127. processor.optimization_level(level);
  128. test_operations(processor, 2);
  129. }
  130. }
  131. }
  132. ``
  133. If we observe that variable `i` also applies in a certain scope, we can improve our example further still.
  134. [bt_example example82_contexts..Using contexts..run-fail]
  135. Finally, it is possible to pass several arguments to `BOOST_TEST_CONTEXT`, which is more convenient than having
  136. several scopes:
  137. [bt_example example83_contexts..Multiple arguments to `BOOST_TEST_CONTEXT`..run-fail]
  138. `BOOST_TEST_INFO_SCOPE` is convenient when you augment the current scope information as new information arrives.
  139. The following example calls several time a quadratic polynomial estimation function with random polynomial. As the
  140. random values are drawn in a loop, they are placed in the current scope with `BOOST_TEST_INFO_SCOPE`, which allows us
  141. to easily debug the function.
  142. [bt_example example84_contexts..Sticky context with `BOOST_TEST_INFO_SCOPE`..run-fail]
  143. [endsect] [/ contexts ]