snowhouse icon indicating copy to clipboard operation
snowhouse copied to clipboard

Stringizer and custom constraints

Open MartinDelille opened this issue 2 years ago • 0 comments

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 ?

MartinDelille avatar Feb 09 '23 17:02 MartinDelille