GoogleTest 中用于不等于双重比较的便捷方法?
我正在寻找类似于 ASSERT_DOUBLE_EQ 的 ASSERT_EQ / ASSERT_NE 的东西。
也许我缺少一种在没有 ASSERT_DOUBLE_NE 的情况下执行此操作的简单方法?
I'm looking for something similar to the ASSERT_EQ / ASSERT_NE for ASSERT_DOUBLE_EQ.
Maybe I'm missing an easy way of doing this without having a ASSERT_DOUBLE_NE?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用配套的模拟框架 Google Mock。它有一个强大的匹配器库(类似于 Hamcrest),您可以将其与 EXPECT_THAT/ASSERT_THAT 宏一起使用:
You can use the companion mocking framework Google Mock. It has a powerful library of matchers (a la Hamcrest), which you can use with the EXPECT_THAT/ASSERT_THAT macros:
看来你运气不好。不过,您可以自己添加一个。我使用 ASSERT_DOUBLE_EQ 和 ASSERT_NE 作为模式构建了以下代码。
It looks like you're out of luck. However, you could add one yourself. I built the following code using ASSERT_DOUBLE_EQ and ASSERT_NE as a pattern.
您可以将宏定义为现有助手的逆,而不是创建新的 CmpHelperFloatingPointNE 助手:
这不像 deft_code 的解决方案那么优雅,因为当断言失败时,没有诸如“期望值”和“实际值”之类的具体细节”,只是断言的行号和文件。不过,就我的目的而言,行号就足够了。
instead of creating a new CmpHelperFloatingPointNE helper, you can just define the macro as the inverse of the existing helper:
This is not as graceful as deft_code's solution because when the assertion fails, there are no specific details like "expected value" and "actual value", just the line number and file of the assertion. For my purposes, though, the line number was enough.