Skip to content

Commit b996144

Browse files
committed
Refactor EXPECT_SAME_FILE
Factor the guts of EXPECT_SAME_FILE into a function to reduce code gen bloat and increase flexibility.
1 parent fa88cfa commit b996144

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

test/quick-lint-js/file-matcher.h

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,27 @@
77
#include <gtest/gtest.h>
88
#include <quick-lint-js/file.h>
99

10-
#define EXPECT_SAME_FILE(path_a, path_b) \
11-
do { \
12-
::quick_lint_js::canonical_path_result path_a_canonical_ = \
13-
::quick_lint_js::canonicalize_path((path_a)); \
14-
ASSERT_TRUE(path_a_canonical_.ok()) \
15-
<< std::move(path_a_canonical_).error(); \
16-
::quick_lint_js::canonical_path_result path_b_canonical_ = \
17-
::quick_lint_js::canonicalize_path((path_b)); \
18-
ASSERT_TRUE(path_b_canonical_.ok()) \
19-
<< std::move(path_b_canonical_).error(); \
20-
EXPECT_EQ(path_a_canonical_.path(), path_b_canonical_.path()) \
21-
<< (path_a) << " should be the same file as " << (path_b); \
22-
} while (false)
10+
#define EXPECT_SAME_FILE(path_a, path_b) \
11+
EXPECT_PRED_FORMAT2(::quick_lint_js::assert_same_file, path_a, path_b)
12+
13+
namespace quick_lint_js {
14+
template <class String>
15+
::testing::AssertionResult assert_same_file(const char *lhs_expr,
16+
const char *rhs_expr,
17+
String lhs_path, String rhs_path) {
18+
canonical_path_result lhs_canonical = canonicalize_path(lhs_path);
19+
EXPECT_TRUE(lhs_canonical.ok()) << std::move(lhs_canonical).error();
20+
canonical_path_result rhs_canonical = canonicalize_path(rhs_path);
21+
EXPECT_TRUE(rhs_canonical.ok()) << std::move(rhs_canonical).error();
22+
if (lhs_canonical.path() == rhs_canonical.path()) {
23+
return ::testing::AssertionSuccess();
24+
} else {
25+
return ::testing::AssertionFailure()
26+
<< lhs_expr << " (" << lhs_path << ") is not the same file as "
27+
<< rhs_expr << " (" << rhs_path << ')';
28+
}
29+
}
30+
}
2331

2432
#endif
2533

0 commit comments

Comments
 (0)