Use a mock to test locale support

This commit is contained in:
Victor Zverovich 2016-05-19 15:04:25 -07:00
parent ebff26f8f1
commit e2a332e5df
3 changed files with 29 additions and 14 deletions

View File

@ -43,6 +43,22 @@
// Test that the library compiles if None is defined to 0 as done by xlib.h.
#define None 0
struct LocaleMock {
static LocaleMock *instance;
MOCK_METHOD0(localeconv, lconv *());
} *LocaleMock::instance;
namespace fmt {
namespace std {
using namespace ::std;
lconv *localeconv() {
return LocaleMock::instance ?
LocaleMock::instance->localeconv() : ::std::localeconv();
}
}
}
#include "fmt/format.h"
#include "fmt/time.h"
@ -1209,13 +1225,12 @@ TEST(FormatterTest, FormatOct) {
}
TEST(FormatterTest, FormatIntLocale) {
#ifndef _WIN32
const char *locale = "en_US.utf-8";
#else
const char *locale = "English_United States";
#endif
if (std::setlocale(LC_ALL, locale))
EXPECT_EQ("1,234,567", format("{:n}", 1234567));
ScopedMock<LocaleMock> mock;
lconv lc = {};
char sep[] = "--";
lc.thousands_sep = sep;
EXPECT_CALL(mock, localeconv()).WillOnce(testing::Return(&lc));
EXPECT_EQ("1--234--567", format("{:n}", 1234567));
}
TEST(FormatterTest, FormatFloat) {

View File

@ -29,7 +29,7 @@
#define FMT_GTEST_EXTRA_H_
#include <string>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "fmt/format.h"
@ -172,4 +172,10 @@ std::string read(fmt::File &f, std::size_t count);
#endif // FMT_USE_FILE_DESCRIPTORS
template <typename Mock>
struct ScopedMock : testing::StrictMock<Mock> {
ScopedMock() { Mock::instance = this; }
~ScopedMock() { Mock::instance = 0; }
};
#endif // FMT_GTEST_EXTRA_H_

View File

@ -453,12 +453,6 @@ TEST(BufferedFileTest, FilenoNoRetry) {
fileno_count = 0;
}
template <typename Mock>
struct ScopedMock : testing::StrictMock<Mock> {
ScopedMock() { Mock::instance = this; }
~ScopedMock() { Mock::instance = 0; }
};
struct TestMock {
static TestMock *instance;
} *TestMock::instance;