mirror of
https://pdfium.googlesource.com/pdfium
synced 2026-08-02 20:37:52 +08:00
Add some unit tests for FX_ParseDateUsingFormat().
Move the existing FakeTimeTest class to its own file, and use it in fx_date_helpers_unittest.cpp to write tests that work consistently, regardless of the actual time. Change-Id: I17981c08723887c2f539ec6f8ffe5960b708fa8c Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/101412 Reviewed-by: K. Moon <kmoon@chromium.org> Commit-Queue: Lei Zhang <thestig@chromium.org>
This commit is contained in:
parent
f467b3693e
commit
f4bdb41b40
@ -162,6 +162,19 @@ source_set("fxcrt") {
|
||||
}
|
||||
}
|
||||
|
||||
source_set("unit_test_support") {
|
||||
testonly = true
|
||||
sources = [
|
||||
"fake_time_test.cpp",
|
||||
"fake_time_test.h",
|
||||
]
|
||||
configs += [ "../../:pdfium_strict_config" ]
|
||||
deps = [
|
||||
":fxcrt",
|
||||
"//testing/gtest",
|
||||
]
|
||||
}
|
||||
|
||||
pdfium_unittest_source_set("unittests") {
|
||||
sources = [
|
||||
"autonuller_unittest.cpp",
|
||||
@ -210,7 +223,7 @@ pdfium_unittest_source_set("unittests") {
|
||||
"xml/cfx_xmlparser_unittest.cpp",
|
||||
"xml/cfx_xmltext_unittest.cpp",
|
||||
]
|
||||
deps = []
|
||||
deps = [ ":unit_test_support" ]
|
||||
pdfium_root_dir = "../../"
|
||||
|
||||
if (pdf_enable_xfa) {
|
||||
|
||||
@ -4,22 +4,7 @@
|
||||
|
||||
#include "core/fxcrt/cfx_datetime.h"
|
||||
|
||||
#include "core/fxcrt/fx_extension.h"
|
||||
#include "testing/gtest/include/gtest/gtest.h"
|
||||
|
||||
class FakeTimeTest : public ::testing::Test {
|
||||
public:
|
||||
void SetUp() override {
|
||||
// Arbitrary, picked descending digits, 2020-04-23 15:05:21.
|
||||
FXSYS_SetTimeFunction([]() -> time_t { return 1587654321; });
|
||||
FXSYS_SetLocaltimeFunction([](const time_t* t) { return gmtime(t); });
|
||||
}
|
||||
|
||||
void TearDown() override {
|
||||
FXSYS_SetTimeFunction(nullptr);
|
||||
FXSYS_SetLocaltimeFunction(nullptr);
|
||||
}
|
||||
};
|
||||
#include "core/fxcrt/fake_time_test.h"
|
||||
|
||||
TEST_F(FakeTimeTest, Now) {
|
||||
CFX_DateTime dt = CFX_DateTime::Now();
|
||||
|
||||
18
core/fxcrt/fake_time_test.cpp
Normal file
18
core/fxcrt/fake_time_test.cpp
Normal file
@ -0,0 +1,18 @@
|
||||
// Copyright 2022 The PDFium Authors
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "core/fxcrt/fake_time_test.h"
|
||||
|
||||
#include "core/fxcrt/fx_extension.h"
|
||||
|
||||
void FakeTimeTest::SetUp() {
|
||||
// Arbitrary, picked descending digits, 2020-04-23 15:05:21.
|
||||
FXSYS_SetTimeFunction([]() -> time_t { return 1587654321; });
|
||||
FXSYS_SetLocaltimeFunction([](const time_t* t) { return gmtime(t); });
|
||||
}
|
||||
|
||||
void FakeTimeTest::TearDown() {
|
||||
FXSYS_SetTimeFunction(nullptr);
|
||||
FXSYS_SetLocaltimeFunction(nullptr);
|
||||
}
|
||||
16
core/fxcrt/fake_time_test.h
Normal file
16
core/fxcrt/fake_time_test.h
Normal file
@ -0,0 +1,16 @@
|
||||
// Copyright 2022 The PDFium Authors
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef CORE_FXCRT_FAKE_TIME_TEST_H_
|
||||
#define CORE_FXCRT_FAKE_TIME_TEST_H_
|
||||
|
||||
#include "testing/gtest/include/gtest/gtest.h"
|
||||
|
||||
class FakeTimeTest : public ::testing::Test {
|
||||
public:
|
||||
void SetUp() override;
|
||||
void TearDown() override;
|
||||
};
|
||||
|
||||
#endif // CORE_FXCRT_FAKE_TIME_TEST_H_
|
||||
@ -250,7 +250,10 @@ if (pdf_enable_v8) {
|
||||
"fx_date_helpers_unittest.cpp",
|
||||
]
|
||||
configs = [ "//v8:external_startup_data" ]
|
||||
deps = [ ":fxjs" ]
|
||||
deps = [
|
||||
":fxjs",
|
||||
"../core/fxcrt:unit_test_support",
|
||||
]
|
||||
pdfium_root_dir = "../"
|
||||
if (pdf_enable_xfa) {
|
||||
sources += [
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
|
||||
#include "fxjs/fx_date_helpers.h"
|
||||
|
||||
#include "core/fxcrt/fake_time_test.h"
|
||||
#include "testing/gtest/include/gtest/gtest.h"
|
||||
|
||||
namespace {
|
||||
@ -12,6 +13,8 @@ constexpr double kMilliSecondsInADay = 1000 * 60 * 60 * 24;
|
||||
|
||||
} // namespace
|
||||
|
||||
using fxjs::ConversionStatus;
|
||||
|
||||
TEST(FX_DateHelper, GetYearFromTime) {
|
||||
static constexpr struct {
|
||||
double time_ms;
|
||||
@ -106,3 +109,47 @@ TEST(FX_DateHelper, GetMonthFromTime) {
|
||||
<< test.time_ms;
|
||||
}
|
||||
}
|
||||
|
||||
using FXDateHelperFakeTimeTest = FakeTimeTest;
|
||||
|
||||
TEST_F(FXDateHelperFakeTimeTest, ParseDateUsingFormatWithEmptyParams) {
|
||||
double result = 0.0;
|
||||
EXPECT_EQ(ConversionStatus::kSuccess,
|
||||
FX_ParseDateUsingFormat(L"", L"", &result));
|
||||
EXPECT_DOUBLE_EQ(1'587'654'321'000, result);
|
||||
EXPECT_EQ(ConversionStatus::kSuccess,
|
||||
FX_ParseDateUsingFormat(L"value", L"", &result));
|
||||
EXPECT_DOUBLE_EQ(1'587'654'321'000, result);
|
||||
EXPECT_EQ(ConversionStatus::kSuccess,
|
||||
FX_ParseDateUsingFormat(L"", L"format", &result));
|
||||
EXPECT_DOUBLE_EQ(1'587'654'321'000, result);
|
||||
}
|
||||
|
||||
TEST_F(FXDateHelperFakeTimeTest, ParseDateUsingFormatForValidMonthDay) {
|
||||
double result = 0.0;
|
||||
EXPECT_EQ(ConversionStatus::kSuccess,
|
||||
FX_ParseDateUsingFormat(L"01/02/2000", L"mm/dd/yyyy", &result));
|
||||
EXPECT_DOUBLE_EQ(946'825'521'000, result);
|
||||
EXPECT_EQ(ConversionStatus::kSuccess,
|
||||
FX_ParseDateUsingFormat(L"1/2/2000", L"m/d/yyyy", &result));
|
||||
EXPECT_DOUBLE_EQ(946'825'521'000, result);
|
||||
EXPECT_EQ(ConversionStatus::kSuccess,
|
||||
FX_ParseDateUsingFormat(L"1-2-2000", L"m-d-yyyy", &result));
|
||||
EXPECT_DOUBLE_EQ(946'825'521'000, result);
|
||||
EXPECT_EQ(ConversionStatus::kSuccess,
|
||||
FX_ParseDateUsingFormat(L"2-1-2000", L"d-m-yyyy", &result));
|
||||
EXPECT_DOUBLE_EQ(946'825'521'000, result);
|
||||
|
||||
EXPECT_EQ(ConversionStatus::kSuccess,
|
||||
FX_ParseDateUsingFormat(L"11/12/2000", L"mm/dd/yyyy", &result));
|
||||
EXPECT_DOUBLE_EQ(973'955'121'000, result);
|
||||
EXPECT_EQ(ConversionStatus::kSuccess,
|
||||
FX_ParseDateUsingFormat(L"11/12/2000", L"m/d/yyyy", &result));
|
||||
EXPECT_DOUBLE_EQ(973'955'121'000, result);
|
||||
EXPECT_EQ(ConversionStatus::kSuccess,
|
||||
FX_ParseDateUsingFormat(L"11-12-2000", L"m-d-yyyy", &result));
|
||||
EXPECT_DOUBLE_EQ(973'955'121'000, result);
|
||||
EXPECT_EQ(ConversionStatus::kSuccess,
|
||||
FX_ParseDateUsingFormat(L"12-11-2000", L"d-m-yyyy", &result));
|
||||
EXPECT_DOUBLE_EQ(973'955'121'000, result);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user