Skip to content

Commit

Permalink
Make import! of protobuf_gtest_matchers work properly.
Browse files Browse the repository at this point in the history
This required making a shim file for a crate to be able to re-export either the _cpp or _upb impl so that it can be one crate that conditionally delegates to the intended one.

PiperOrigin-RevId: 688639897
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Oct 22, 2024
1 parent c0f3d4c commit c6753b9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 36 deletions.
22 changes: 13 additions & 9 deletions rust/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -161,21 +161,25 @@ rust_library(
deps = [":protobuf_cpp"],
)

alias(
rust_library(
name = "protobuf_gtest_matchers",
actual = select({
":use_upb_kernel": ":protobuf_gtest_matchers_upb",
"//conditions:default": ":protobuf_gtest_matchers_cpp",
testonly = True,
srcs = ["gtest_matchers.rs"],
aliases = select({
":use_upb_kernel": {"//rust:protobuf_gtest_matchers_upb": "protobuf_gtest_matchers_impl"},
"//conditions:default": {"//rust:protobuf_gtest_matchers_cpp": "protobuf_gtest_matchers_impl"},
}),
visibility = ["//visibility:public"],
deps = select({
":use_upb_kernel": [":protobuf_gtest_matchers_upb"],
"//conditions:default": [":protobuf_gtest_matchers_cpp"],
}),
visibility = [
"//visibility:public",
],
)

rust_library(
name = "protobuf_gtest_matchers_cpp",
testonly = True,
srcs = ["gtest_matchers.rs"],
srcs = ["gtest_matchers_impl.rs"],
aliases = {
"//rust:protobuf_cpp": "protobuf",
},
Expand All @@ -189,7 +193,7 @@ rust_library(
rust_library(
name = "protobuf_gtest_matchers_upb",
testonly = True,
srcs = ["gtest_matchers.rs"],
srcs = ["gtest_matchers_impl.rs"],
aliases = {
"//rust:protobuf_upb": "protobuf",
},
Expand Down
30 changes: 3 additions & 27 deletions rust/gtest_matchers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,7 @@
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd

use googletest::description::Description;
use googletest::matcher::{Matcher, MatcherBase, MatcherResult};
use protobuf::__internal::MatcherEq;
extern crate protobuf_gtest_matchers_impl;

#[derive(MatcherBase)]
pub struct MessageMatcher<T: MatcherEq> {
expected: T,
}

impl<T> Matcher<&T> for MessageMatcher<T>
where
T: MatcherEq,
{
fn matches(&self, actual: &T) -> MatcherResult {
actual.matches(&self.expected).into()
}

fn describe(&self, matcher_result: MatcherResult) -> Description {
match matcher_result {
MatcherResult::Match => format!("is equal to {:?}", self.expected).into(),
MatcherResult::NoMatch => format!("is not equal to {:?}", self.expected).into(),
}
}
}

pub fn proto_eq<T: MatcherEq>(expected: T) -> MessageMatcher<T> {
MessageMatcher { expected }
}
// Re-export all symbols from the underlying implementation of gtest_matchers.
pub use protobuf_gtest_matchers_impl::*;
35 changes: 35 additions & 0 deletions rust/gtest_matchers_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2024 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd

use googletest::description::Description;
use googletest::matcher::{Matcher, MatcherBase, MatcherResult};
use protobuf::__internal::MatcherEq;

#[derive(MatcherBase)]
pub struct MessageMatcher<T: MatcherEq> {
expected: T,
}

impl<T> Matcher<&T> for MessageMatcher<T>
where
T: MatcherEq,
{
fn matches(&self, actual: &T) -> MatcherResult {
actual.matches(&self.expected).into()
}

fn describe(&self, matcher_result: MatcherResult) -> Description {
match matcher_result {
MatcherResult::Match => format!("is equal to {:?}", self.expected).into(),
MatcherResult::NoMatch => format!("is not equal to {:?}", self.expected).into(),
}
}
}

pub fn proto_eq<T: MatcherEq>(expected: T) -> MessageMatcher<T> {
MessageMatcher { expected }
}
4 changes: 4 additions & 0 deletions rust/test/shared/gtest_matchers_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd

google3::import! {
"//third_party/protobuf/rust:protobuf_gtest_matchers";
}

use edition_unittest_rust_proto::TestAllTypes as TestAllTypesEditions;
use googletest::prelude::*;
use paste::paste;
Expand Down

0 comments on commit c6753b9

Please sign in to comment.