Skip to content

Stringizer and custom constraints #54

Open
@MartinDelille

Description

@MartinDelille

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 ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions