From 66808c8037cdaef3054389f4d1075ff704f82d70 Mon Sep 17 00:00:00 2001 From: Paul Rigge Date: Fri, 10 May 2024 15:43:26 -0700 Subject: [PATCH] Make channel matcher use matcher for name. Previously, it took a string instead of a string matcher. Also, clean up the interface to be consistent with other matchers that take strings. PiperOrigin-RevId: 632623075 --- xls/ir/ir_matcher.cc | 11 +++++++---- xls/ir/ir_matcher.h | 18 ++++++++++++++---- xls/ir/ir_matcher_test.cc | 2 +- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/xls/ir/ir_matcher.cc b/xls/ir/ir_matcher.cc index d8be1138e6..81cb135839 100644 --- a/xls/ir/ir_matcher.cc +++ b/xls/ir/ir_matcher.cc @@ -77,9 +77,8 @@ bool ChannelMatcher::MatchAndExplain( return false; } - if (name_.has_value() && channel->name() != name_.value()) { - *listener << absl::StreamFormat(" has incorrect name (%s), expected: %s", - channel->name(), name_.value()); + if (name_.has_value() && + !name_->MatchAndExplain(std::string{channel->name()}, listener)) { return false; } @@ -105,7 +104,11 @@ void ChannelMatcher::DescribeTo(::std::ostream* os) const { pieces.push_back(absl::StrFormat("id=%d", id_.value())); } if (name_.has_value()) { - pieces.push_back(absl::StrFormat("name=%s", name_.value())); + std::stringstream ss; + ss << "name=\""; + name_->DescribeTo(&ss); + ss << '"'; + pieces.push_back(ss.str()); } if (kind_.has_value()) { pieces.push_back( diff --git a/xls/ir/ir_matcher.h b/xls/ir/ir_matcher.h index ff9ca8243e..18475924a6 100644 --- a/xls/ir/ir_matcher.h +++ b/xls/ir/ir_matcher.h @@ -658,7 +658,8 @@ inline ::testing::Matcher TupleIndex( class ChannelMatcher : public ::testing::MatcherInterface { public: - ChannelMatcher(std::optional id, std::optional name, + ChannelMatcher(std::optional id, + std::optional<::testing::Matcher> name, std::optional kind, std::optional type_string) : id_(id), @@ -673,7 +674,7 @@ class ChannelMatcher protected: std::optional id_; - std::optional name_; + std::optional<::testing::Matcher> name_; std::optional kind_; std::optional type_string_; }; @@ -702,10 +703,19 @@ inline ::testing::Matcher Channel( : std::nullopt)); } +template +inline ::testing::Matcher Channel(T name) + requires(std::is_convertible_v) +{ + return ::testing::MakeMatcher(new ::xls::op_matchers::ChannelMatcher( + std::nullopt, internal::NameMatcherInternal(std::string_view{name}), + std::nullopt, std::nullopt)); +} + inline ::testing::Matcher Channel( - std::string_view name) { + const ::testing::Matcher& matcher) { return ::testing::MakeMatcher(new ::xls::op_matchers::ChannelMatcher( - std::nullopt, std::string{name}, std::nullopt, std::nullopt)); + std::nullopt, matcher, std::nullopt, std::nullopt)); } inline ::testing::Matcher Channel(ChannelKind kind) { diff --git a/xls/ir/ir_matcher_test.cc b/xls/ir/ir_matcher_test.cc index a255a6e23c..78b6c64064 100644 --- a/xls/ir/ir_matcher_test.cc +++ b/xls/ir/ir_matcher_test.cc @@ -419,7 +419,7 @@ TEST(IrMatchersTest, ReceiveOps) { EXPECT_THAT(Explain(receive.node(), m::Receive(m::Channel(444))), HasSubstr("has incorrect id (42), expected: 444")); EXPECT_THAT(Explain(receive.node(), m::Receive(m::Channel("foobar"))), - HasSubstr("has incorrect name (ch42), expected: foobar")); + HasSubstr("ch42 has incorrect name, expected: foobar.")); EXPECT_THAT( Explain(receive.node(), m::Receive(m::Channel(ChannelKind::kSingleValue))),