Giter VIP home page Giter VIP logo

Comments (7)

raffienficiaud avatar raffienficiaud commented on August 26, 2024

I may have better for you: making the context variadic, like this:

for (int i = 8; i < 24; ++i) {
    BOOST_TEST_CONTEXT("With level " << i, "Random value=" << some other value){

    }
}

Would that work for you? You can try it on the branch topic/GH-197-plural-context-in-single-scope

from test.

dudamoos avatar dudamoos commented on August 26, 2024

Unfortunately that doesn't actually do what I want it to. I need to build the context up as I generate data I want included in it so I can debug test failures. What I'm doing is something like this:

std::vector<uint8_t> data = ...; // generate data
MY_BOOST_TEST_CONTEXT("data      = " << data);
session->SendData(data, ... /* packet header fields */);

// RequirePacket() reads the data from the stream including the length header.
// It also does a few other checks to make sure the data isn't garbage.
const std::vector<uint32_t> packet = RequirePacket(numWords);
MY_BOOST_TEST_CONTEXT("packet    = " << packet);
// ... Do some other checks on the packet. ...
WriteSingleWord(TESTING_ACK); // Acknowledge the packet.

std::vector<uint8_t> parseData;
// ParsePacket() returns false if the packet was badly formatted or failed integrity checks.
BOOST_TEST(ParsePacket(packet, parseData, ... /* output parameters to receive header fields */));
MY_BOOST_TEST_CONTEXT("parseData = " << parseData);

// With all of the above context I can manually inspect the original data, sent packet, and parsed
// data in case this comparison fails.
BOOST_TEST(data == parseData, boost::test_tools::per_element());

I also use it to remove an extra level of indentation from the entire body of Require*() functions (like RequirePacket() above) like so:

#define RequirePacket(numWords) \
    Internal_RequirePacket((numWords), BOOST_TEST_L(__FILE__), static_cast<std::size_t>(__LINE__))
std::vector<uint32_t>
Internal_RequirePacket(unsigned numWords, boost::unit_test::const_string file, std::size_t line) {
    MY_BOOST_TEST_CONTEXT("RequirePacket() call-site: " << file << "(" << line << ")");

    // Code to read packet from stream and validate length and some other things.
    ...
}

from test.

raffienficiaud avatar raffienficiaud commented on August 26, 2024

I guess you have considered the assertion bound context ?

If a context needs to span several assertions, we need a scope, otherwise it will confuse users. If a context is associated to only one assertion, then BOOST_TEST_INFO is doing just fine.

What I did here is to have several information going into a single BOOST_TEST_CONTEXT, to avoid precisely having several scopes for a single context.

from test.

dudamoos avatar dudamoos commented on August 26, 2024

Yes. I need the context to span several assertions. I understand why you made BOOST_TEST_CONTEXT create a scope that the context will apply to. However, in my case I want the context to last for the rest of the enclosing scope. Is your last statement supposed to mean that adding context to the enclosing scope is an explicit non-goal?

from test.

raffienficiaud avatar raffienficiaud commented on August 26, 2024

It is not an explicit non-goal, but I need to think about it.

from test.

raffienficiaud avatar raffienficiaud commented on August 26, 2024

Ok, I implemented this via a new macro BOOST_TEST_INFO_SCOPE. Example of use

  for(int i = 0; i < 50; i++) {
    BOOST_TEST_INFO_SCOPE("trial " << i+1);
    int root1 = dis(gen);
    int root2 = dis(gen);
    if(root1 > root2) {
      std::swap(root1, root2);
    }
    BOOST_TEST_INFO_SCOPE("root1 = " << root1);
    BOOST_TEST_INFO_SCOPE("root2 = " << root2);

    std::pair<double, double> estimated = estimate_polynomial_roots(
      gen,
      [root1, root2](double x) -> double { return (x - root1) * (x - root2); });

    BOOST_TEST(estimated.first == double(root1), 10. % boost::test_tools::tolerance());
    BOOST_TEST(estimated.second == double(root2), 10. % boost::test_tools::tolerance());
  }

Let me know if that corresponds to what you asked (same branch as before if you want to give a try).

from test.

dudamoos avatar dudamoos commented on August 26, 2024

That fix does the trick. I'll work on getting everyone here using an upgraded or patched version. Thanks!

from test.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.