We have a Widget class, defined by its size and anchor position. For simplicity, all 1-dimensional. The constructor performs basic validations on the parameters and that's the scope of our tests.
#include <stdexcept>
#include <gtest/gtest.h>
class Widget {
public:
Widget(int _size, int _anchor) : size(_size), anchor(_anchor) {
if (_size < 0) {
throw std::invalid_argument("Invalid size");
}
if (_anchor < 0) {
throw std::invalid_argument("Invalid anchor");
}
}
private:
const int size, anchor;
};
TEST(WidgetTest, construct_negativeSize_throwsException) {
const int negativeSize = -1;
const int testAnchor = 0;
EXPECT_THROW(Widget(negativeSize, testAnchor), std::invalid_argument);
}
TEST(WidgetTest, construct_negativeAnchor_throwsException) {
const int testSize = 5;
const int negativeAnchor = -1;
EXPECT_THROW(Widget(testSize, negativeAnchor), std::invalid_argument);
}
#include <stdexcept>
#include <gtest/gtest.h>
class Widget {
public:
Widget(int _size, int _anchor) : size(_size), anchor(_anchor) {
if (_size < 10) {
throw std::invalid_argument("Invalid size");
}
if (_anchor < 0) {
throw std::invalid_argument("Invalid anchor");
}
}
private:
const int size, anchor;
};
TEST(WidgetTest, construct_negativeSize_throwsException) {
const int negativeSize = -1;
const int testAnchor = 0;
EXPECT_THROW(Widget(negativeSize, testAnchor), std::invalid_argument);
}
TEST(WidgetTest, construct_negativeAnchor_throwsException) {
const int testSize = 5;
const int negativeAnchor = -1;
// FAIL! exception still thrown but not because of negative anchor anymore
EXPECT_THROW(Widget(testSize, negativeAnchor), std::invalid_argument);
}
#include <stdexcept>
#include <gtest/gtest.h>
class Widget {
public:
class InvalidSize : public std::invalid_argument { using invalid_argument::invalid_argument; };
class InvalidAnchor : public std::invalid_argument { using invalid_argument::invalid_argument; };
Widget(int _size, int _anchor) : size(_size), anchor(_anchor) {
if (_size < 0) {
throw InvalidSize("Invalid size");
}
if (_anchor < 0) {
throw InvalidAnchor("Invalid anchor");
}
}
private:
const int size, anchor;
};
TEST(WidgetTest, construct_negativeSize_throwsException) {
const int negativeSize = -1;
const int testAnchor = 0;
EXPECT_THROW(Widget(negativeSize, testAnchor), Widget::InvalidSize);
}
TEST(WidgetTest, construct_negativeAnchor_throwsException) {
const int testSize = 5;
const int negativeAnchor = -1;
EXPECT_THROW(Widget(testSize, negativeAnchor), Widget::InvalidAnchor);
}
#define EXPECT_THROW_MSG(__statement, __msg) \
try { \
__statement; \
FAIL() << "Expected: Statement throws an exception.\n"\
" Actual: it throws nothing."; \
} catch (const std::exception& e) { \
ASSERT_TRUE(e.what()); \
EXPECT_STREQ(__msg, e.what()); \
} catch (...) { \
FAIL() << "Expected: Statement throws an exception.\n"\
" Actual: it throws something other than an std::exception."; \
}
#include <stdexcept>
#include <gtest/gtest.h>
class Widget {
public:
Widget(int _size, int _anchor) : size(_size), anchor(_anchor) {
if (_size < 0) {
throw std::invalid_argument("Invalid size");
}
if (_anchor < 0) {
throw std::invalid_argument("Invalid anchor");
}
}
private:
const int size, anchor;
};
TEST(WidgetTest, construct_negativeSize_throwsException) {
const int negativeSize = -1;
const int testAnchor = 0;
EXPECT_THROW_MSG(Widget(negativeSize, testAnchor), "Invalid size");
}
TEST(WidgetTest, construct_negativeAnchor_throwsException) {
const int testSize = 5;
const int negativeAnchor = -1;
EXPECT_THROW_MSG(Widget(testSize, negativeAnchor), "Invalid anchor");
}