We have a simple Widget with just two members for its position. The constructor simply initializes the fields. For the sake of this exercise, let's assume that in reality the Widget's constructor is doing much more to justify its existence over relying on the aggregate initialization.
#include <gtest/gtest.h>
struct Widget {
Widget(int _posx, int _posy)
: posx(_posx),
posy(_posy)
{ }
const int posx;
const int posy;
};
TEST(WidgetTest, constructor) {
const Widget widget(10, 10);
EXPECT_EQ(10, widget.posx);
EXPECT_EQ(10, widget.posy);
}
#include <gtest/gtest.h>
struct Widget {
Widget(int _posx, int _posy)
: posx(_posy),
posy(_posx)
{ }
const int posx;
const int posy;
};
TEST(WidgetTest, constructor) {
const Widget widget(10, 10);
EXPECT_EQ(10, widget.posx); // FAIL! undetected simple bug in constructor
EXPECT_EQ(10, widget.posy); // FAIL! undetected simple bug in constructor
}
#include <gtest/gtest.h>
struct Widget {
Widget(int _posx, int _posy)
: posx(_posx),
posy(_posy)
{ }
const int posx;
const int posy;
};
TEST(WidgetTest, constructor) {
const Widget widget(11, 12);
EXPECT_EQ(11, widget.posx);
EXPECT_EQ(12, widget.posy);
}