googletest打印COleDateTime值

发布于 2024-11-19 11:12:59 字数 1137 浏览 2 评论 0原文

我已将 googletest 集成到我们的 MFC 应用程序中。然而,在编写涉及 COleDateTime 对象的测试时,我遇到了以下警告:

1>gtest/gtest-printers.h(169) : warning C4244: 'initializing' : conversion from 'DATE' to 'const testing::internal::BiggestInt', possible loss of data
1>gtest/gtest-printers.h(168) : while compiling class template member function 'void testing::internal2::TypeWithoutFormatter<T,kTypeKind>::PrintValue(const T &,std::ostream *)'

测试如下:

TEST(FunctionTest, SumDays) 
{
    COleDateTime res = SumDays(COleDateTime(2010,10,31,0,0,0), 1);
    EXPECT_EQ(COleDateTime(2010,11,01,0,0,0), res);
}

问题是我无法添加 <<运算符或 PrintTo 方法(如文档所示)。 分配更多测试将涉及日期值,因此我想避免文档引用的内联解决方案。

是否有一个好的解决方案来控制 COleDateTime 值的打印字符串?

当前的输出如下:

<failure message="Value of: res&#x0A;  Actual: 40512&#x0A;Expected: COleDateTime(2010,10,30,0,0,0)&#x0A;Which is: 40481" type=""><![CDATA[.\Code.cpp:6837
Value of: res
  Actual: 40512
Expected: COleDateTime(2010,10,30,0,0,0)
Which is: 40481]]></failure>

注意 实际 值!

I have integrated googletest into our MFC application. However while writing tests involving COleDateTime objects I came across the following warning:

1>gtest/gtest-printers.h(169) : warning C4244: 'initializing' : conversion from 'DATE' to 'const testing::internal::BiggestInt', possible loss of data
1>gtest/gtest-printers.h(168) : while compiling class template member function 'void testing::internal2::TypeWithoutFormatter<T,kTypeKind>::PrintValue(const T &,std::ostream *)'

The test was the following:

TEST(FunctionTest, SumDays) 
{
    COleDateTime res = SumDays(COleDateTime(2010,10,31,0,0,0), 1);
    EXPECT_EQ(COleDateTime(2010,11,01,0,0,0), res);
}

The problem is I cannot add a << operator or a PrintTo method as the documentation announces.
Allot more tests are going to involve date values so I want to avoid the inline solution the documentation refers to.

Is there a good solution to control the print string for COleDateTime values?

The current output comes out like:

<failure message="Value of: res
  Actual: 40512
Expected: COleDateTime(2010,10,30,0,0,0)
Which is: 40481" type=""><![CDATA[.\Code.cpp:6837
Value of: res
  Actual: 40512
Expected: COleDateTime(2010,10,30,0,0,0)
Which is: 40481]]></failure>

Notice the Actual value!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

童话里做英雄 2024-11-26 11:12:59

我有同样的问题并发现,与 Caerbanogs 声明相反 - 实施PrintTo 函数确实有帮助。一件重要的事情是确保该类“扩展”googletest 的行为与 PrintTo-Function 具有完全相同的命名空间

在本例中,这就是命名空间 ATL

这导致了以下针对 COleDateTimeCOleDateTimeSpan 的解决方案:

namespace ATL {

    void PrintTo(const COleDateTime& dtDatum, ::std::ostream* os)
    {
        // I want an additional Format, so I append a human readable notion
        *os << dtDatum.m_dt << " (" << (LPCSTR)dtDatum.Format(_T("%d.%m.%Y %H:%M:%S")) << ")";
    }

    void PrintTo(const COleDateTimeSpan& dsSpanne, ::std::ostream* os)
    {
        *os << dsSpanne.m_span;
    }

}

只需将其放入一个位置,您可以将其包含在所有 googletest-Projects 中(如果您有多个项目) 。

最后它对我有用:-)

I had the same porblem and figured out, that - in contrast to Caerbanogs statement - implementing the PrintTo-Function(s) helps indeed. One important thing is to make sure that the Class for that one "extends" a behaviour for googletest has exactly the same namespace as the PrintTo-Function.

In this case that is the namespace ATL!

This leads to the following solution for COleDateTime and COleDateTimeSpan:

namespace ATL {

    void PrintTo(const COleDateTime& dtDatum, ::std::ostream* os)
    {
        // I want an additional Format, so I append a human readable notion
        *os << dtDatum.m_dt << " (" << (LPCSTR)dtDatum.Format(_T("%d.%m.%Y %H:%M:%S")) << ")";
    }

    void PrintTo(const COleDateTimeSpan& dsSpanne, ::std::ostream* os)
    {
        *os << dsSpanne.m_span;
    }

}

Just put that into a place, which you can include from all your googletest-Projects (if you have more than one).

At last it works for me :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文