We have a struct with data describing a book record. We focus on one operation - checking if the data provided is "complete".
#include <gtest/gtest.h>
struct Book {
std::string title;
std::string description;
bool isComplete() const {
if (title.empty() || description.empty()) {
return false;
}
return true;
}
};
TEST(BookTest, bookWithNoTitle_isNotComplete) {
Book book;
book.description = "My first book description";
// title left empty
EXPECT_FALSE(book.isComplete());
}
TEST(BookTest, bookWithNoDescription_isNotComplete) {
Book book;
book.title = "My first book";
// description left empty
EXPECT_FALSE(book.isComplete());
}
#include <gtest/gtest.h>
struct Book {
std::string title;
std::string description;
std::string author;
bool isComplete() const {
if (title.empty() || description.empty() || author.empty()) {
return false;
}
return true;
}
};
TEST(BookTest, bookWithNoTitle_isNotComplete) {
Book book;
book.description = "My first book description";
// title left empty
EXPECT_FALSE(book.isComplete());
}
TEST(BookTest, bookWithNoDescription_isNotComplete) {
Book book;
book.title = "My first book";
// description left empty
EXPECT_FALSE(book.isComplete());
}
#include <gtest/gtest.h>
struct Book {
std::string title;
std::string description;
bool isComplete() const {
if (title.empty() || description.empty()) {
return false;
}
return true;
}
};
Book createCompleteTestBook() {
Book book;
book.title = "My book title";
book.description = "My book description";
EXPECT_TRUE(book.isComplete());
return book;
}
TEST(BookTest, bookWithNoTitle_isNotComplete) {
Book book = createCompleteTestBook();
book.title = "";
EXPECT_FALSE(book.isComplete());
}
TEST(BookTest, bookWithNoDescription_isNotComplete) {
Book book = createCompleteTestBook();
book.description = "";
EXPECT_FALSE(book.isComplete());
}
#include <gtest/gtest.h>
struct Book {
std::string title;
std::string description;
bool isComplete() const {
if (title.empty() || description.empty()) {
return false;
}
return true;
}
};
TEST(BookTest, bookWithNoTitle_isNotComplete) {
Book book;
book.description = "My first book description";
// title left empty
EXPECT_FALSE(book.isComplete());
// test sanity check
book.title = "Title now present";
EXPECT_FALSE(book.isComplete());
}
TEST(BookTest, bookWithNoDescription_isNotComplete) {
Book book;
book.title = "My first book";
// description left empty
EXPECT_FALSE(book.isComplete());
// test sanity check
book.description = "Description now present";
EXPECT_FALSE(book.isComplete());
}
#include <gtest/gtest.h>
struct Book {
std::string title;
std::string description;
std::string author;
bool isComplete() const {
if (/*title.empty() || description.empty() ||*/ author.empty()) {
return false;
}
return true;
}
};
TEST(BookTest, bookWithNoTitle_isNotComplete) {
Book book;
book.description = "My first book description";
// title left empty <-- but also author empty
EXPECT_FALSE(book.isComplete()); // passing because author is empty
}
TEST(BookTest, bookWithNoDescription_isNotComplete) {
Book book;
book.title = "My first book";
// description left empty <-- but also author empty
EXPECT_FALSE(book.isComplete()); // passing because author is empty
}