googletest:sample2分析
目录
googletest:sample1
googletest:sample2
googletest:sample3
googletest:sample4
googletest:sample5
googletest:sample6
googletest:sample7
googletest:sample8
googletest:sample9
待测文件
sample2 包含一个MyString类,实现了函数:
- 构造函数(ctor),拷贝构造函数(copy-ctor),析构函数(dtor);
- 对象成员函数,private赋值运算重载符(operator=);
- 类成员函数(static函数).
MyString类功能就是包装C风格字符串,实现拷贝、求长度的功能.
sample2.h
#include <string.h>
// A simple string class.
class MyString {
private:
const char* c_string_;
const MyString& operator=(const MyString& rhs);
public:
// Clones a 0-terminated C string, allocating memory using new.
static const char* CloneCString(const char* a_c_string);
////////////////////////////////////////////////////////////
//
// C'tors
// The default c'tor constructs a NULL string.
MyString() : c_string_(nullptr) {}
// Constructs a MyString by cloning a 0-terminated C string.
explicit MyString(const char* a_c_string) : c_string_(nullptr) {
Set(a_c_string);
}
// Copy c'tor
MyString(const MyString& string) : c_string_(nullptr) {
Set(string.c_string_);
}
////////////////////////////////////////////////////////////
//
// D'tor. MyString is intended to be a final class, so the d'tor
// doesn't need to be virtual.
~MyString() { delete[] c_string_; }
// Gets the 0-terminated C string this MyString object represents.
const char* c_string() const { return c_string_; }
size_t Length() const { return c_string_ == nullptr ? 0 : strlen(c_string_); }
// Sets the 0-terminated C string this MyString object represents.
void Set(const char* c_string);
};
sample2.cc
#include "sample2.h"
#include <string.h>
// Clones a 0-terminated C string, allocating memory using new.
const char* MyString::CloneCString(const char* a_c_string) {
if (a_c_string == nullptr) return nullptr;
const size_t len = strlen(a_c_string);
char* const clone = new char[len + 1];
memcpy(clone, a_c_string, len + 1);
return clone;
}
// Sets the 0-terminated C string this MyString object
// represents.
void MyString::Set(const char* a_c_string) {
// Makes sure this works when c_string == c_string_
const char* const temp = MyString::CloneCString(a_c_string);
delete[] c_string_;
c_string_ = temp;
}
测试文件
sample2_unittest.cc
#include "sample2.h"
#include "gtest/gtest.h"
namespace {
// In this example, we test the MyString class (a simple string).
// Tests the default c'tor.
TEST(MyString, DefaultConstructor) {
const MyString s;
EXPECT_STREQ(nullptr, s.c_string());
EXPECT_EQ(0u, s.Length());
}
const char kHelloString[] = "Hello, world!";
// Tests the c'tor that accepts a C string.
TEST(MyString, ConstructorFromCString) {
const MyString s(kHelloString);
EXPECT_EQ(0, strcmp(s.c_string(), kHelloString));
EXPECT_EQ(sizeof(kHelloString) / sizeof(kHelloString[0]) - 1, s.Length());
}
// Tests the copy c'tor.
TEST(MyString, CopyConstructor) {
const MyString s1(kHelloString);
const MyString s2 = s1;
EXPECT_EQ(0, strcmp(s2.c_string(), kHelloString));
}
// Tests the Set method.
TEST(MyString, Set) {
MyString s;
s.Set(kHelloString);
EXPECT_EQ(0, strcmp(s.c_string(), kHelloString));
// Set should work when the input pointer is the same as the one
// already in the MyString object.
s.Set(s.c_string());
EXPECT_EQ(0, strcmp(s.c_string(), kHelloString));
// Can we set the MyString to NULL?
s.Set(nullptr);
EXPECT_STREQ(nullptr, s.c_string());
}
} // namespace
类名MyString作为一个测试组件,实现了测试用例:
- DefaultConstructor:测试默认构造函数;
- ConstructorFromCString:测试从C字符串构造MyString对象;
- CopyConstructor:测试拷贝构造函数,从另一个MyString对象来构造;
- Set:测试设置方法,是否能实现字符串设置.
EXPECT_EQ(v1, v2) 断言值相等 v1 == v2;
EXPECT_STREQ(s1, s2) 断言字符串相等 s1[begin..end] == s2[begin..end];
如果用NULL替换nullptr,那么就不能用EXPECT_EQ,需要用EXPECT_STREQ判断字符串是否相等.
因为EXPECT_EQ(v1, v2)需要知道v1,v2的值类型,而编译器默认会将null作为指针类型. 或者,需要进行转型:
EXPECT_EQ(static_cast<const char*>(NULL), s.c_string());
小结
-
同一个类的测试用例都归属到同一个类的测试组件;
-
可为构造函数设置测试用例,但不对private方法设置测试用例;
-
字符串断言最好用
EXPECT_STREQ,或者strcmp + EXPECT_EQ.

浙公网安备 33010602011771号