summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Deymo <deymo@chromium.org>2015-07-28 13:09:29 -0700
committerChromeOS Commit Bot <chromeos-commit-bot@chromium.org>2015-07-29 08:45:26 +0000
commitf211ae662aee9b2d4cb8538e9845b5389ba0cc3e (patch)
treed78ac655859eee134d5d5cb9393b05873d7601a5
parentd0cb3fe72b7070d373bf960550b7cbfb0adbe843 (diff)
downloaddbus-binding-generator-f211ae662aee9b2d4cb8538e9845b5389ba0cc3e.tar.gz
chromeos-dbus-bindings: Print output diff when failing a test.
To help test-driven-development techniques and prevent lazy developers from copy-pasting the output of the test into the expected results without inspecting them, this patch shows the unified diff between the expected result and the generated file. It is still possible to update the unittest code by appliying the patch to the *_unittest.cc file. BUG=None TEST=Modified an expected result and saw the diff on the unittest. Change-Id: If24479fed1fa3be67f2a99c092206bf27cf2b110 Reviewed-on: https://chromium-review.googlesource.com/289300 Reviewed-by: Alex Deymo <deymo@chromium.org> Commit-Queue: Alex Deymo <deymo@chromium.org> Trybot-Ready: Alex Deymo <deymo@chromium.org> Tested-by: Alex Deymo <deymo@chromium.org>
-rw-r--r--chromeos-dbus-bindings/adaptor_generator_unittest.cc5
-rw-r--r--chromeos-dbus-bindings/chromeos-dbus-bindings.gyp1
-rw-r--r--chromeos-dbus-bindings/proxy_generator_mock_unittest.cc5
-rw-r--r--chromeos-dbus-bindings/proxy_generator_unittest.cc22
-rw-r--r--chromeos-dbus-bindings/test_utils.cc81
-rw-r--r--chromeos-dbus-bindings/test_utils.h33
6 files changed, 126 insertions, 21 deletions
diff --git a/chromeos-dbus-bindings/adaptor_generator_unittest.cc b/chromeos-dbus-bindings/adaptor_generator_unittest.cc
index 70b6421..9d9fad7 100644
--- a/chromeos-dbus-bindings/adaptor_generator_unittest.cc
+++ b/chromeos-dbus-bindings/adaptor_generator_unittest.cc
@@ -13,6 +13,7 @@
#include <gtest/gtest.h>
#include "chromeos-dbus-bindings/interface.h"
+#include "chromeos-dbus-bindings/test_utils.h"
using std::string;
using std::vector;
@@ -306,9 +307,7 @@ TEST_F(AdaptorGeneratorTest, GenerateAdaptors) {
EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
// The header guards contain the (temporary) filename, so we search for
// the content we need within the string.
- EXPECT_NE(string::npos, contents.find(kExpectedContent))
- << "Expected to find the following content...\n"
- << kExpectedContent << "...within content...\n" << contents;
+ test_utils::EXPECT_TEXT_CONTAINED(kExpectedContent, contents);
}
} // namespace chromeos_dbus_bindings
diff --git a/chromeos-dbus-bindings/chromeos-dbus-bindings.gyp b/chromeos-dbus-bindings/chromeos-dbus-bindings.gyp
index 705e0a1..218edb7 100644
--- a/chromeos-dbus-bindings/chromeos-dbus-bindings.gyp
+++ b/chromeos-dbus-bindings/chromeos-dbus-bindings.gyp
@@ -79,6 +79,7 @@
'name_parser_unittest.cc',
'proxy_generator_mock_unittest.cc',
'proxy_generator_unittest.cc',
+ 'test_utils.cc',
'xml_interface_parser_unittest.cc',
],
},
diff --git a/chromeos-dbus-bindings/proxy_generator_mock_unittest.cc b/chromeos-dbus-bindings/proxy_generator_mock_unittest.cc
index aebc973..0f1eb2c 100644
--- a/chromeos-dbus-bindings/proxy_generator_mock_unittest.cc
+++ b/chromeos-dbus-bindings/proxy_generator_mock_unittest.cc
@@ -13,6 +13,7 @@
#include <gtest/gtest.h>
#include "chromeos-dbus-bindings/interface.h"
+#include "chromeos-dbus-bindings/test_utils.h"
using std::string;
using std::vector;
@@ -183,9 +184,7 @@ TEST_F(ProxyGeneratorMockTest, GenerateMocks) {
EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
// The header guards contain the (temporary) filename, so we search for
// the content we need within the string.
- EXPECT_NE(string::npos, contents.find(kExpectedContent))
- << "Expected to find the following content...\n"
- << kExpectedContent << "...within content...\n" << contents;
+ test_utils::EXPECT_TEXT_CONTAINED(kExpectedContent, contents);
}
} // namespace chromeos_dbus_bindings
diff --git a/chromeos-dbus-bindings/proxy_generator_unittest.cc b/chromeos-dbus-bindings/proxy_generator_unittest.cc
index 165bd61..8840f41 100644
--- a/chromeos-dbus-bindings/proxy_generator_unittest.cc
+++ b/chromeos-dbus-bindings/proxy_generator_unittest.cc
@@ -13,6 +13,7 @@
#include <gtest/gtest.h>
#include "chromeos-dbus-bindings/interface.h"
+#include "chromeos-dbus-bindings/test_utils.h"
using std::string;
using std::vector;
@@ -1306,9 +1307,7 @@ TEST_F(ProxyGeneratorTest, GenerateAdaptors) {
EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
// The header guards contain the (temporary) filename, so we search for
// the content we need within the string.
- EXPECT_NE(string::npos, contents.find(kExpectedContent))
- << "Expected to find the following content...\n"
- << kExpectedContent << "...within content...\n" << contents;
+ test_utils::EXPECT_TEXT_CONTAINED(kExpectedContent, contents);
}
TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithServiceName) {
@@ -1327,9 +1326,7 @@ TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithServiceName) {
EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
// The header guards contain the (temporary) filename, so we search for
// the content we need within the string.
- EXPECT_NE(string::npos, contents.find(kExpectedContentWithService))
- << "Expected to find the following content...\n"
- << kExpectedContentWithService << "...within content...\n" << contents;
+ test_utils::EXPECT_TEXT_CONTAINED(kExpectedContentWithService, contents);
}
TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithObjectManager) {
@@ -1350,10 +1347,8 @@ TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithObjectManager) {
EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
// The header guards contain the (temporary) filename, so we search for
// the content we need within the string.
- EXPECT_NE(string::npos, contents.find(kExpectedContentWithObjectManager))
- << "Expected to find the following content...\n"
- << kExpectedContentWithObjectManager << "...within content...\n"
- << contents;
+ test_utils::EXPECT_TEXT_CONTAINED(
+ kExpectedContentWithObjectManager, contents);
}
TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithObjectManagerAndServiceName) {
@@ -1374,11 +1369,8 @@ TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithObjectManagerAndServiceName) {
EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
// The header guards contain the (temporary) filename, so we search for
// the content we need within the string.
- EXPECT_NE(string::npos,
- contents.find(kExpectedContentWithObjectManagerAndServiceName))
- << "Expected to find the following content...\n"
- << kExpectedContentWithObjectManagerAndServiceName
- << "...within content...\n" << contents;
+ test_utils::EXPECT_TEXT_CONTAINED(
+ kExpectedContentWithObjectManagerAndServiceName, contents);
}
} // namespace chromeos_dbus_bindings
diff --git a/chromeos-dbus-bindings/test_utils.cc b/chromeos-dbus-bindings/test_utils.cc
new file mode 100644
index 0000000..5d019df
--- /dev/null
+++ b/chromeos-dbus-bindings/test_utils.cc
@@ -0,0 +1,81 @@
+// Copyright 2015 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chromeos-dbus-bindings/test_utils.h"
+
+#include <string>
+#include <vector>
+
+#include <base/files/file_path.h>
+#include <base/files/file_util.h>
+#include <base/logging.h>
+#include <chromeos/process.h>
+#include <gtest/gtest.h>
+
+using std::string;
+using std::vector;
+
+namespace {
+
+// Return the diff between the texts |a| and |b|.
+string GetUnifiedDiff(const string& a, const string& b) {
+ base::FilePath path_a, path_b;
+ if (!base::CreateTemporaryFile(&path_a) ||
+ !base::CreateTemporaryFile(&path_b)) {
+ return "Error creating temporary file";
+ }
+ WriteFile(path_a, a.data(), a.size());
+ WriteFile(path_b, b.data(), b.size());
+
+ chromeos::ProcessImpl proc;
+ proc.AddArg("diff");
+ proc.AddArg("-u");
+ proc.AddArg(path_a.value());
+ proc.AddArg(path_b.value());
+ proc.SetSearchPath(true);
+ proc.RedirectUsingPipe(STDOUT_FILENO, false);
+ proc.Start();
+
+ int fd = proc.GetPipe(STDOUT_FILENO);
+ vector<char> buffer(32 * 1024);
+ string output;
+ while (true) {
+ int rc = read(fd, buffer.data(), buffer.size());
+ if (rc < 0) {
+ PLOG(ERROR) << "Reading from diff.";
+ break;
+ } else if (rc == 0) {
+ break;
+ } else {
+ output.append(buffer.data(), rc);
+ }
+ }
+ proc.Wait();
+
+ base::DeleteFile(path_a, false);
+ base::DeleteFile(path_b, false);
+ return output;
+}
+
+} // namespace
+
+namespace chromeos_dbus_bindings {
+namespace test_utils {
+
+void ExpectTextContained(const tracked_objects::Location& from_here,
+ const string& expected_str,
+ const string& expected_expr,
+ const string& actual_str,
+ const string& actual_expr) {
+ if (string::npos != actual_str.find(expected_str))
+ return;
+
+ ADD_FAILURE_AT(from_here.file_name(), from_here.line_number())
+ << "Expected to find " << expected_expr << " within " << actual_expr
+ << ".\nHere is the diff:\n"
+ << GetUnifiedDiff(expected_str, actual_str);
+}
+
+} // namespace test_utils
+} // namespace chromeos_dbus_bindings
diff --git a/chromeos-dbus-bindings/test_utils.h b/chromeos-dbus-bindings/test_utils.h
new file mode 100644
index 0000000..5619bc1
--- /dev/null
+++ b/chromeos-dbus-bindings/test_utils.h
@@ -0,0 +1,33 @@
+// Copyright 2015 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROMEOS_DBUS_BINDINGS_TEST_UTILS_H_
+#define CHROMEOS_DBUS_BINDINGS_TEST_UTILS_H_
+
+#include <string>
+
+#include <base/location.h>
+
+namespace chromeos_dbus_bindings {
+namespace test_utils {
+
+// Helper macro to call ExpectTextContained().
+#define EXPECT_TEXT_CONTAINED(expected, actual) \
+ ExpectTextContained(FROM_HERE, expected, #expected, actual, #actual)
+
+// Checks that the text |actual_str| is contained in the text |expected_str| and
+// fails the current test if not. If the |actual_str| text is not contained, a
+// meaningful line diff between |actual_str| and |expected_str| is displayed in
+// stderr. Use this function instead of EXPECT_EQ() when the compared values are
+// long texts.
+void ExpectTextContained(const tracked_objects::Location& from_here,
+ const std::string& expected_str,
+ const std::string& expected_expr,
+ const std::string& actual_str,
+ const std::string& actual_expr);
+
+} // namespace test_utils
+} // namespace chromeos_dbus_bindings
+
+#endif // CHROMEOS_DBUS_BINDINGS_TEST_UTILS_H_