Refactor tests using fixtures

This commit is contained in:
Sebastian Messmer 2014-11-16 03:01:41 +01:00
parent 0e1a0f4867
commit ff9a222e6a
2 changed files with 114 additions and 127 deletions

View File

@ -42,56 +42,7 @@ public:
MOCK_METHOD1(utimens, void(const timespec[2])); MOCK_METHOD1(utimens, void(const timespec[2]));
}; };
TEST(FuseOpenFileListTest, EmptyList1) { struct FuseOpenFileListTest: public ::testing::Test {
FuseOpenFileList list;
ASSERT_THROW(list.get(0), std::out_of_range);
}
TEST(FuseOpenFileListTest, EmptyList2) {
FuseOpenFileList list;
ASSERT_THROW(list.get(3), std::out_of_range);
}
TEST(FuseOpenFileListTest, InvalidId) {
FuseOpenFileList list;
int valid_id = list.open(MockFile(3), 2);
int invalid_id = valid_id + 1;
ASSERT_THROW(list.get(invalid_id), std::out_of_range);
}
TEST(FuseOpenFileListTest, Open1AndGet) {
const int FILEID = 4;
const int FLAGS = 5;
FuseOpenFileList list;
int id = list.open(MockFile(FILEID), FLAGS);
MockOpenFile *openFile = dynamic_cast<MockOpenFile*>(list.get(id));
EXPECT_EQ(FILEID, openFile->fileid);
EXPECT_EQ(FLAGS, openFile->flags);
}
TEST(FuseOpenFileListTest, Open2AndGet) {
const int FILEID1 = 4;
const int FLAGS1 = 5;
const int FILEID2 = 6;
const int FLAGS2 = 7;
FuseOpenFileList list;
int id1 = list.open(MockFile(FILEID1), FLAGS1);
int id2 = list.open(MockFile(FILEID2), FLAGS2);
MockOpenFile *openFile1 = dynamic_cast<MockOpenFile*>(list.get(id1));
MockOpenFile *openFile2 = dynamic_cast<MockOpenFile*>(list.get(id2));
EXPECT_EQ(FILEID1, openFile1->fileid);
EXPECT_EQ(FLAGS1, openFile1->flags);
EXPECT_EQ(FILEID2, openFile2->fileid);
EXPECT_EQ(FLAGS2, openFile2->flags);
}
TEST(FuseOpenFileListTest, Open3AndGet) {
const int FILEID1 = 4; const int FILEID1 = 4;
const int FLAGS1 = 5; const int FLAGS1 = 5;
const int FILEID2 = 6; const int FILEID2 = 6;
@ -100,25 +51,58 @@ TEST(FuseOpenFileListTest, Open3AndGet) {
const int FLAGS3 = 9; const int FLAGS3 = 9;
FuseOpenFileList list; FuseOpenFileList list;
int id1 = list.open(MockFile(FILEID1), FLAGS1); int open(int fileid, int flags) {
int id2 = list.open(MockFile(FILEID2), FLAGS2); return list.open(MockFile(fileid), flags);
int id3 = list.open(MockFile(FILEID3), FLAGS3); }
int open() {
return open(FILEID1, FILEID2);
}
void check(int id, int fileid, int flags) {
MockOpenFile *openFile = dynamic_cast<MockOpenFile*>(list.get(id));
EXPECT_EQ(fileid, openFile->fileid);
EXPECT_EQ(flags, openFile->flags);
}
};
MockOpenFile *openFile1 = dynamic_cast<MockOpenFile*>(list.get(id1)); TEST_F(FuseOpenFileListTest, EmptyList1) {
MockOpenFile *openFile3 = dynamic_cast<MockOpenFile*>(list.get(id3)); ASSERT_THROW(list.get(0), std::out_of_range);
MockOpenFile *openFile2 = dynamic_cast<MockOpenFile*>(list.get(id2));
EXPECT_EQ(FILEID1, openFile1->fileid);
EXPECT_EQ(FLAGS1, openFile1->flags);
EXPECT_EQ(FILEID2, openFile2->fileid);
EXPECT_EQ(FLAGS2, openFile2->flags);
EXPECT_EQ(FILEID3, openFile3->fileid);
EXPECT_EQ(FLAGS3, openFile3->flags);
} }
TEST(FuseOpenFileListTest, DestructOnClose) { TEST_F(FuseOpenFileListTest, EmptyList2) {
FuseOpenFileList list; ASSERT_THROW(list.get(3), std::out_of_range);
int id = list.open(MockFile(3), 4); }
TEST_F(FuseOpenFileListTest, InvalidId) {
int valid_id = open();
int invalid_id = valid_id + 1;
ASSERT_THROW(list.get(invalid_id), std::out_of_range);
}
TEST_F(FuseOpenFileListTest, Open1AndGet) {
int id = open(FILEID1, FLAGS1);
check(id, FILEID1, FLAGS1);
}
TEST_F(FuseOpenFileListTest, Open2AndGet) {
int id1 = open(FILEID1, FLAGS1);
int id2 = open(FILEID2, FLAGS2);
check(id1, FILEID1, FLAGS1);
check(id2, FILEID2, FLAGS2);
}
TEST_F(FuseOpenFileListTest, Open3AndGet) {
int id1 = open(FILEID1, FLAGS1);
int id2 = open(FILEID2, FLAGS2);
int id3 = open(FILEID3, FLAGS3);
check(id1, FILEID1, FLAGS1);
check(id3, FILEID3, FLAGS3);
check(id2, FILEID2, FLAGS2);
}
TEST_F(FuseOpenFileListTest, DestructOnClose) {
int id = open();
MockOpenFile *openFile = dynamic_cast<MockOpenFile*>(list.get(id)); MockOpenFile *openFile = dynamic_cast<MockOpenFile*>(list.get(id));
@ -127,38 +111,33 @@ TEST(FuseOpenFileListTest, DestructOnClose) {
EXPECT_TRUE(openFile->destructed); EXPECT_TRUE(openFile->destructed);
} }
TEST(FuseOpenFileListTest, GetClosedItemOnEmptyList) { TEST_F(FuseOpenFileListTest, GetClosedItemOnEmptyList) {
FuseOpenFileList list; int id = open();
int id = list.open(MockFile(3), 4);
ASSERT_NO_THROW(list.get(id)); ASSERT_NO_THROW(list.get(id));
list.close(id); list.close(id);
ASSERT_THROW(list.get(id), std::out_of_range); ASSERT_THROW(list.get(id), std::out_of_range);
} }
TEST(FuseOpenFileListTest, GetClosedItemOnNonEmptyList) { TEST_F(FuseOpenFileListTest, GetClosedItemOnNonEmptyList) {
FuseOpenFileList list; int id = open();
int id = list.open(MockFile(3), 4); open();
list.open(MockFile(5), 4);
ASSERT_NO_THROW(list.get(id)); ASSERT_NO_THROW(list.get(id));
list.close(id); list.close(id);
ASSERT_THROW(list.get(id), std::out_of_range); ASSERT_THROW(list.get(id), std::out_of_range);
} }
TEST(FuseOpenFileListTest, CloseOnEmptyList1) { TEST_F(FuseOpenFileListTest, CloseOnEmptyList1) {
FuseOpenFileList list;
ASSERT_THROW(list.close(0), std::out_of_range); ASSERT_THROW(list.close(0), std::out_of_range);
} }
TEST(FuseOpenFileListTest, CloseOnEmptyList2) { TEST_F(FuseOpenFileListTest, CloseOnEmptyList2) {
FuseOpenFileList list;
ASSERT_THROW(list.close(4), std::out_of_range); ASSERT_THROW(list.close(4), std::out_of_range);
} }
TEST(FuseOpenFileListTest, RemoveInvalidId) { TEST_F(FuseOpenFileListTest, RemoveInvalidId) {
FuseOpenFileList list; int valid_id = open();
int valid_id = list.open(MockFile(3), 4);
int invalid_id = valid_id + 1; int invalid_id = valid_id + 1;
ASSERT_THROW(list.close(invalid_id), std::out_of_range); ASSERT_THROW(list.close(invalid_id), std::out_of_range);
} }

View File

@ -13,86 +13,94 @@ public:
int val; int val;
}; };
TEST(IdListTest, EmptyList1) { struct IdListTest: public ::testing::Test {
const int OBJ1 = 3;
const int OBJ2 = 10;
const int OBJ3 = 8;
IdList<MyObj> list; IdList<MyObj> list;
int add(int num) {
return list.add(make_unique<MyObj>(num));
}
int add() {
return add(OBJ1);
}
void check(int id, int num) {
EXPECT_EQ(num, list.get(id)->val);
}
void checkConst(int id, int num) {
const IdList<MyObj> &constList = list;
EXPECT_EQ(num, constList.get(id)->val);
}
};
TEST_F(IdListTest, EmptyList1) {
ASSERT_THROW(list.get(0), std::out_of_range); ASSERT_THROW(list.get(0), std::out_of_range);
} }
TEST(IdListTest, EmptyList2) { TEST_F(IdListTest, EmptyList2) {
IdList<MyObj> list;
ASSERT_THROW(list.get(3), std::out_of_range); ASSERT_THROW(list.get(3), std::out_of_range);
} }
TEST(IdListTest, InvalidId) { TEST_F(IdListTest, InvalidId) {
IdList<MyObj> list; int valid_id = add();
int valid_id = list.add(make_unique<MyObj>(6));
int invalid_id = valid_id + 1; int invalid_id = valid_id + 1;
ASSERT_THROW(list.get(invalid_id), std::out_of_range); ASSERT_THROW(list.get(invalid_id), std::out_of_range);
} }
TEST(IdListTest, GetRemovedItemOnEmptyList) { TEST_F(IdListTest, GetRemovedItemOnEmptyList) {
IdList<MyObj> list; int id = add();
int id = list.add(make_unique<MyObj>(6));
list.remove(id); list.remove(id);
ASSERT_THROW(list.get(id), std::out_of_range); ASSERT_THROW(list.get(id), std::out_of_range);
} }
TEST(IdListTest, GetRemovedItemOnNonEmptyList) { TEST_F(IdListTest, GetRemovedItemOnNonEmptyList) {
IdList<MyObj> list; int id = add();
int id = list.add(make_unique<MyObj>(6)); add();
list.add(make_unique<MyObj>(5));
list.remove(id); list.remove(id);
ASSERT_THROW(list.get(id), std::out_of_range); ASSERT_THROW(list.get(id), std::out_of_range);
} }
TEST(IdListTest, RemoveOnEmptyList1) { TEST_F(IdListTest, RemoveOnEmptyList1) {
IdList<MyObj> list;
ASSERT_THROW(list.remove(0), std::out_of_range); ASSERT_THROW(list.remove(0), std::out_of_range);
} }
TEST(IdListTest, RemoveOnEmptyList2) { TEST_F(IdListTest, RemoveOnEmptyList2) {
IdList<MyObj> list;
ASSERT_THROW(list.remove(4), std::out_of_range); ASSERT_THROW(list.remove(4), std::out_of_range);
} }
TEST(IdListTest, RemoveInvalidId) { TEST_F(IdListTest, RemoveInvalidId) {
IdList<MyObj> list; int valid_id = add();
int valid_id = list.add(make_unique<MyObj>(6));
int invalid_id = valid_id + 1; int invalid_id = valid_id + 1;
ASSERT_THROW(list.remove(invalid_id), std::out_of_range); ASSERT_THROW(list.remove(invalid_id), std::out_of_range);
} }
TEST(IdListTest, Add1AndGet) { TEST_F(IdListTest, Add1AndGet) {
IdList<MyObj> list; int id = add(OBJ1);
int id6 = list.add(make_unique<MyObj>(6)); check(id, OBJ1);
EXPECT_EQ(6, list.get(id6)->val);
} }
TEST(IdListTest, Add2AndGet) { TEST_F(IdListTest, Add2AndGet) {
IdList<MyObj> list; int id1 = add(OBJ1);
int id4 = list.add(make_unique<MyObj>(4)); int id2 = add(OBJ2);
int id5 = list.add(make_unique<MyObj>(5)); check(id1, OBJ1);
EXPECT_EQ(4, list.get(id4)->val); check(id2, OBJ2);
EXPECT_EQ(5, list.get(id5)->val);
} }
TEST(IdListTest, Add3AndGet) { TEST_F(IdListTest, Add3AndGet) {
IdList<MyObj> list; int id1 = add(OBJ1);
int id4 = list.add(make_unique<MyObj>(4)); int id2 = add(OBJ2);
int id10 = list.add(make_unique<MyObj>(10)); int id3 = add(OBJ3);
int id1 = list.add(make_unique<MyObj>(1)); check(id1, OBJ1);
EXPECT_EQ(10, list.get(id10)->val); check(id3, OBJ3);
EXPECT_EQ(4, list.get(id4)->val); check(id2, OBJ2);
EXPECT_EQ(1, list.get(id1)->val);
} }
TEST(IdListTest, Add3AndConstGet) { TEST_F(IdListTest, Add3AndConstGet) {
IdList<MyObj> list; int id1 = add(OBJ1);
int id4 = list.add(make_unique<MyObj>(4)); int id2 = add(OBJ2);
int id10 = list.add(make_unique<MyObj>(10)); int id3 = add(OBJ3);
int id1 = list.add(make_unique<MyObj>(1)); checkConst(id1, OBJ1);
const IdList<MyObj> &const_list = list; checkConst(id3, OBJ3);
EXPECT_EQ(10, const_list.get(id10)->val); checkConst(id2, OBJ2);
EXPECT_EQ(4, const_list.get(id4)->val);
EXPECT_EQ(1, const_list.get(id1)->val);
} }