Skip to content

Commit

Permalink
Make channel matcher use matcher for name.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
grebe authored and copybara-github committed May 10, 2024
1 parent 0a69116 commit 66808c8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
11 changes: 7 additions & 4 deletions xls/ir/ir_matcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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(
Expand Down
18 changes: 14 additions & 4 deletions xls/ir/ir_matcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,8 @@ inline ::testing::Matcher<const ::xls::Node*> TupleIndex(
class ChannelMatcher
: public ::testing::MatcherInterface<const ::xls::Channel*> {
public:
ChannelMatcher(std::optional<int64_t> id, std::optional<std::string> name,
ChannelMatcher(std::optional<int64_t> id,
std::optional<::testing::Matcher<std::string>> name,
std::optional<ChannelKind> kind,
std::optional<std::string_view> type_string)
: id_(id),
Expand All @@ -673,7 +674,7 @@ class ChannelMatcher

protected:
std::optional<int64_t> id_;
std::optional<std::string> name_;
std::optional<::testing::Matcher<std::string>> name_;
std::optional<ChannelKind> kind_;
std::optional<std::string> type_string_;
};
Expand Down Expand Up @@ -702,10 +703,19 @@ inline ::testing::Matcher<const ::xls::Channel*> Channel(
: std::nullopt));
}

template <typename T>
inline ::testing::Matcher<const ::xls::Channel*> Channel(T name)
requires(std::is_convertible_v<T, std::string_view>)
{
return ::testing::MakeMatcher(new ::xls::op_matchers::ChannelMatcher(
std::nullopt, internal::NameMatcherInternal(std::string_view{name}),
std::nullopt, std::nullopt));
}

inline ::testing::Matcher<const ::xls::Channel*> Channel(
std::string_view name) {
const ::testing::Matcher<std::string>& 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<const ::xls::Channel*> Channel(ChannelKind kind) {
Expand Down
2 changes: 1 addition & 1 deletion xls/ir/ir_matcher_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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))),
Expand Down

0 comments on commit 66808c8

Please sign in to comment.