Simplify locale mock

This commit is contained in:
vitaut 2016-02-09 11:31:04 -08:00
parent b8c6192a61
commit 70e44a8e7f

View File

@ -456,26 +456,21 @@ TEST(BufferedFileTest, FilenoNoRetry) {
template <typename Mock>
struct ScopedMock : testing::StrictMock<Mock> {
private:
Mock *&global_mock_;
public:
explicit ScopedMock(Mock *&global_mock) : global_mock_(global_mock) {
global_mock = this;
}
~ScopedMock() { global_mock_ = 0; }
ScopedMock() { Mock::instance = this; }
~ScopedMock() { Mock::instance = 0; }
};
struct TestMock {};
struct TestMock {
static TestMock *instance;
} *TestMock::instance;
TEST(ScopedMock, Scope) {
TestMock *global_mock = 0;
{
ScopedMock<TestMock> mock(global_mock);
EXPECT_EQ(&mock, global_mock);
ScopedMock<TestMock> mock;
EXPECT_EQ(&mock, TestMock::instance);
TestMock &copy = mock;
}
EXPECT_EQ(0, global_mock);
EXPECT_EQ(0, TestMock::instance);
}
#ifdef FMT_LOCALE
@ -483,30 +478,31 @@ TEST(ScopedMock, Scope) {
typedef fmt::Locale::Type LocaleType;
struct LocaleMock {
static LocaleMock *instance;
MOCK_METHOD3(newlocale, LocaleType (int category_mask, const char *locale,
LocaleType base));
MOCK_METHOD1(freelocale, void (LocaleType locale));
MOCK_METHOD3(strtod_l, double (const char *nptr, char **endptr,
LocaleType locale));
} *locale_mock;
} *LocaleMock::instance;
#ifdef _MSC_VER
_locale_t _create_locale(int category, const char *locale) {
return locale_mock->newlocale(category, locale, 0);
return LocaleMock::instance->newlocale(category, locale, 0);
}
void _free_locale(_locale_t locale) {
locale_mock->freelocale(locale);
LocaleMock::instance->freelocale(locale);
}
double _strtod_l(const char *nptr, char **endptr, _locale_t locale) {
return locale_mock->strtod_l(nptr, endptr, locale);
return LocaleMock::instance->strtod_l(nptr, endptr, locale);
}
#endif
LocaleType newlocale(int category_mask, const char *locale, LocaleType base) {
return locale_mock->newlocale(category_mask, locale, base);
return LocaleMock::instance->newlocale(category_mask, locale, base);
}
#ifdef __APPLE__
@ -516,15 +512,15 @@ typedef void FreeLocaleResult;
#endif
FreeLocaleResult freelocale(LocaleType locale) {
locale_mock->freelocale(locale);
LocaleMock::instance->freelocale(locale);
}
double strtod_l(const char *nptr, char **endptr, LocaleType locale) {
return locale_mock->strtod_l(nptr, endptr, locale);
return LocaleMock::instance->strtod_l(nptr, endptr, locale);
}
TEST(LocaleTest, LocaleMock) {
ScopedMock<LocaleMock> mock(locale_mock);
ScopedMock<LocaleMock> mock;
LocaleType locale = reinterpret_cast<LocaleType>(11);
EXPECT_CALL(mock, newlocale(222, StrEq("foo"), locale));
newlocale(222, "foo", locale);
@ -534,7 +530,7 @@ TEST(LocaleTest, Locale) {
#ifndef LC_NUMERIC_MASK
enum { LC_NUMERIC_MASK = LC_NUMERIC };
#endif
ScopedMock<LocaleMock> mock(locale_mock);
ScopedMock<LocaleMock> mock;
LocaleType impl = reinterpret_cast<LocaleType>(42);
EXPECT_CALL(mock, newlocale(LC_NUMERIC_MASK, StrEq("C"), 0))
.WillOnce(Return(impl));
@ -544,7 +540,7 @@ TEST(LocaleTest, Locale) {
}
TEST(LocaleTest, Strtod) {
ScopedMock<LocaleMock> mock(locale_mock);
ScopedMock<LocaleMock> mock;
EXPECT_CALL(mock, newlocale(_, _, _))
.WillOnce(Return(reinterpret_cast<LocaleType>(42)));
EXPECT_CALL(mock, freelocale(_));