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

Stringizer and custom constraints #54

Open
MartinDelille opened this issue Feb 9, 2023 · 0 comments
Open

Stringizer and custom constraints #54

MartinDelille opened this issue Feb 9, 2023 · 0 comments

Comments

@MartinDelille
Copy link
Contributor

MartinDelille commented Feb 9, 2023

I tried to write a constraint for my custom type MyType:

class IsRange {
  Time _timeIn;
  Duration _duration;
public:
  IsRange(Time timeIn, Duration duration) :
    _timeIn(timeIn),
    _duration(duration) {
  }
  inline bool Matches(const MyType *actual) const {
    return  actual && actual->timeIn() == _timeIn && actual->duration() == _duration;
  }
  friend std::ostream& operator <<(std::ostream &stm, const IsRange&);
};

template <>struct snowhouse::Stringizer<MyType> {
  static std::string ToString(const MyType *obj) {
    return obj->description().toStdString();
  }
};

Since the stringizer doesn't work for pointer so when the test fails, it display the pointer adress for the actual value.

I converted my code to handle reference:

class IsRange {
  Time _timeIn;
  Duration _duration;
public:
  IsRange(Time timeIn, Duration duration) : _timeIn(timeIn), _duration(duration) {
  }
  inline bool Matches(const MyType &actual) const {
    return  actual.timeIn() == _timeIn && actual.duration() == _duration;
  }
  friend std::ostream& operator <<(std::ostream &stm, const IsRange&);
};

template <> struct snowhouse::Stringizer<MyType> {
  static std::string ToString(const MyType &obj) {
    return obj.description().toStdString();
  }
};

I now need to do two checks in my tests:

MyType *a = ...
AssertThat(a, Is().Not().Null());
AssertThat(*a, Fulfills(IsRange(2111, 889)));

Do you think using pointer would be possible ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant