Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile Error: AssertThat(LastException<std::runtime_error>().what(), Is().Containing("fail")); #68

Closed
ifknot opened this issue Jan 6, 2016 · 11 comments
Labels
third-party Due to a third-party issue or user error
Milestone

Comments

@ifknot
Copy link

ifknot commented Jan 6, 2016

Trying Bandit/Snowhouse for the first time so this could be my fault? I'm using Qt Creator + Clang but...

Test code

struct Test {

    void test() {
        throw std::runtime_error("fail");
    }

};

go_bandit([](){

    describe("exception test", [](){

        Test test;

        it("should fail", [&](){
            AssertThrows(std::runtime_error, test.test());
            AssertThat(LastException<std::runtime_error>().what(), Is().Containing("fail"));
        });

  });

});

Compile Error

.../bandit/assertion_frameworks/snowhouse/snowhouse/constraints/containsconstraint.h:22: error: member reference base type 'const char *const' is not a structure or union
return std::find(container.begin(), container.end(), expected) != container.end();
~~~~~~~~~^~~~~~
.../bandit/assertion_frameworks/snowhouse/snowhouse/assertmacro.h:18: expanded from macro 'AssertThat'
SNOWHOUSE_ASSERT_THAT((p1), (p2), ::snowhouse::DefaultFailureHandler);
^
.../bandit/assertion_frameworks/snowhouse/snowhouse/assertmacro.h:13: expanded from macro 'SNOWHOUSE_ASSERT_THAT'
::snowhouse::ConfigurableAssert<FAILURE_HANDLER>::That((p1), (p2), FILE, LINE);
^

namespace snowhouse {

  template <typename ContainerType>
  struct find_in_container_traits
  {
    template <typename ExpectedType>
    static bool find(const ContainerType& container, const ExpectedType& expected)
    {
***      return std::find(container.begin(), container.end(), expected) != container.end();
    }
  };
@nabijaczleweli
Copy link
Contributor

A const char * can_not_ contain "fail" by its very definition, and it's therefore not a bug, that this code fails, it's your problem.

@ifknot
Copy link
Author

ifknot commented Jan 6, 2016

Okay thanks for your, somewhat brusque, reply but it's an example from the Snowhouse Usage section https://github.com/joakimkarlsson/snowhouse#assertions

Making Assertions on the Thrown Exceptions

AssertThrows(std::logic_error, myObject.a_method(42));
AssertThat(LastException<std::logic_error>().what(), Is().Containing("logic failure"));

So, given your expertise, perhaps you would be willing to advise how my test code can be corrected?

@joakimkarlsson
Copy link
Collaborator

Yes, that looks suspiciously like the snowhouse example.

Could you run the make file for bandit itself and see what happens? One of the tests in snowhouse performs what you're attempting.

https://github.com/joakimkarlsson/snowhouse/blob/master/example/exceptions_tests.cpp#L47

This is run as part of bandit's test suite as well.

@nabijaczleweli
Copy link
Contributor

Well, chars can not, in any way, be equal to "fail".

@ifknot
Copy link
Author

ifknot commented Jan 6, 2016

Why is it in the example then?
What would the correct usage be?

@nabijaczleweli
Copy link
Contributor

std::string(LastException<std::runtime_error>().what()).find("fail") != std::string::npos

@joakimkarlsson
Copy link
Collaborator

I tried your code and got the same result.

I got it to work by using Assert::That instead of the AssertThat macro. I'm not sure why yet.

@ifknot
Copy link
Author

ifknot commented Jan 6, 2016

.
Success!
Test run complete. 1 tests run. 1 succeeded.

Many thanks using Assert::That instead of the AssertThat fixed it for me too!

@joakimkarlsson
Copy link
Collaborator

That's a bug. I'll have to take a look at it later.

Thanks for reporting!

@ifknot
Copy link
Author

ifknot commented Jan 6, 2016

Pleasure thanks for your prompt responses and fix 👍

@sbeyer
Copy link
Member

sbeyer commented Feb 5, 2017

The problem is fixed in banditcpp/snowhouse@65668a3 and will be available here as soon as there is an update of the snowhouse submodule.

The problem was caused by a missing conversion from const char * to std::string in case the filename and the line number was given. (When you use Assert::That, filename and line number is not set. That's why it was a workaround.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
third-party Due to a third-party issue or user error
Projects
None yet
Development

No branches or pull requests

4 participants