Make import! of protobuf_gtest_matchers work properly.

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
pull/18939/head
Protobuf Team Bot 3 months ago committed by Copybara-Service
parent c0f3d4c887
commit c6753b9d06
  1. 22
      rust/BUILD
  2. 30
      rust/gtest_matchers.rs
  3. 35
      rust/gtest_matchers_impl.rs
  4. 4
      rust/test/shared/gtest_matchers_test.rs

@ -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",
},
@ -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",
},

@ -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::*;

@ -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 }
}

@ -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;

Loading…
Cancel
Save