Move repeated tests into their own file

accessors_test.rs is getting a bit unwieldy, approaching 1K LOC. This CL breaks out the repeated tests into their own file (accessors_repeated_test.rs), as we expect the tests here to grow. This follows the precedent of `accessors_map_test.rs`.

PiperOrigin-RevId: 588554476
pull/14974/head
Hong Shin 1 year ago committed by Copybara-Service
parent 7e247e7906
commit acad27f027
  1. 40
      rust/test/shared/BUILD
  2. 127
      rust/test/shared/accessors_repeated_test.rs
  3. 118
      rust/test/shared/accessors_test.rs

@ -280,6 +280,46 @@ rust_test(
],
)
rust_test(
name = "accessors_repeated_cpp_test",
srcs = ["accessors_repeated_test.rs"],
aliases = {
"//rust:protobuf_cpp": "protobuf",
},
proc_macro_deps = [
"@crate_index//:paste",
],
tags = [
# TODO: Enable testing on arm once we support sanitizers for Rust on Arm.
"not_build:arm",
],
deps = [
"@crate_index//:googletest",
"//rust:protobuf_cpp",
"//rust/test:unittest_cc_rust_proto",
],
)
rust_test(
name = "accessors_repeated_upb_test",
srcs = ["accessors_repeated_test.rs"],
aliases = {
"//rust:protobuf_upb": "protobuf",
},
proc_macro_deps = [
"@crate_index//:paste",
],
tags = [
# TODO: Enable testing on arm once we support sanitizers for Rust on Arm.
"not_build:arm",
],
deps = [
"@crate_index//:googletest",
"//rust:protobuf_upb",
"//rust/test:unittest_upb_rust_proto",
],
)
rust_test(
name = "accessors_map_cpp_test",
srcs = ["accessors_map_test.rs"],

@ -0,0 +1,127 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 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::prelude::*;
use paste::paste;
use unittest_proto::proto2_unittest::TestAllTypes;
macro_rules! generate_repeated_numeric_test {
($(($t: ty, $field: ident)),*) => {
paste! { $(
#[test]
fn [< test_repeated_ $field _accessors >]() {
let mut msg = TestAllTypes::new();
assert_that!(msg.[< repeated_ $field >]().len(), eq(0));
assert_that!(msg.[<repeated_ $field >]().get(0), none());
let mut mutator = msg.[<repeated_ $field _mut >]();
mutator.push(1 as $t);
assert_that!(mutator.len(), eq(1));
assert_that!(mutator.get(0), some(eq(1 as $t)));
mutator.set(0, 2 as $t);
assert_that!(mutator.get(0), some(eq(2 as $t)));
mutator.push(1 as $t);
mutator.push(3 as $t);
assert_that!(mutator.get_mut(2).is_some(), eq(true));
let mut mut_elem = mutator.get_mut(2).unwrap();
mut_elem.set(4 as $t);
assert_that!(mut_elem.get(), eq(4 as $t));
mut_elem.clear();
assert_that!(mut_elem.get(), eq(0 as $t));
assert_that!(
mutator.iter().collect::<Vec<_>>(),
eq(vec![2 as $t, 1 as $t, 0 as $t])
);
assert_that!(
(*mutator).into_iter().collect::<Vec<_>>(),
eq(vec![2 as $t, 1 as $t, 0 as $t])
);
for mut mutable_elem in msg.[<repeated_ $field _mut >]() {
mutable_elem.set(0 as $t);
}
assert_that!(
msg.[<repeated_ $field _mut >]().iter().all(|v| v == (0 as $t)),
eq(true)
);
}
#[test]
fn [< test_repeated_ $field _set >]() {
let mut msg = TestAllTypes::new();
let mut mutator = msg.[<repeated_ $field _mut>]();
let mut msg2 = TestAllTypes::new();
let mut mutator2 = msg2.[<repeated_ $field _mut>]();
for i in 0..5 {
mutator2.push(i as $t);
}
protobuf::MutProxy::set(&mut mutator, *mutator2);
assert_that!(
mutator.iter().collect::<Vec<_>>(),
eq(mutator2.iter().collect::<Vec<_>>())
);
}
)* }
};
}
generate_repeated_numeric_test!(
(i32, int32),
(u32, uint32),
(i64, int64),
(u64, uint64),
(f32, float),
(f64, double)
);
#[test]
fn test_repeated_bool_accessors() {
let mut msg = TestAllTypes::new();
assert_that!(msg.repeated_bool().len(), eq(0));
assert_that!(msg.repeated_bool().get(0), none());
let mut mutator = msg.repeated_bool_mut();
mutator.push(true);
assert_that!(mutator.len(), eq(1));
assert_that!(mutator.get(0), some(eq(true)));
mutator.set(0, false);
assert_that!(mutator.get(0), some(eq(false)));
mutator.push(true);
mutator.push(false);
assert_that!(mutator.get_mut(2), pat!(Some(_)));
let mut mut_elem = mutator.get_mut(2).unwrap();
mut_elem.set(true);
assert_that!(mut_elem.get(), eq(true));
mut_elem.clear();
assert_that!(mut_elem.get(), eq(false));
assert_that!(mutator.iter().collect::<Vec<_>>(), eq(vec![false, true, false]));
assert_that!((*mutator).into_iter().collect::<Vec<_>>(), eq(vec![false, true, false]));
for mut mutable_elem in msg.repeated_bool_mut() {
mutable_elem.set(false);
}
assert_that!(msg.repeated_bool().iter().all(|v| v), eq(false));
}
#[test]
fn test_repeated_bool_set() {
let mut msg = TestAllTypes::new();
let mut mutator = msg.repeated_bool_mut();
let mut msg2 = TestAllTypes::new();
let mut mutator2 = msg2.repeated_bool_mut();
for _ in 0..5 {
mutator2.push(true);
}
protobuf::MutProxy::set(&mut mutator, *mutator2);
assert_that!(mutator.iter().collect::<Vec<_>>(), eq(mutator2.iter().collect::<Vec<_>>()));
}

@ -9,7 +9,6 @@
use googletest::prelude::*;
use matchers::{is_set, is_unset};
use paste::paste;
use protobuf::Optional;
use unittest_proto::proto2_unittest::{TestAllTypes, TestAllTypes_};
@ -734,120 +733,3 @@ fn test_oneof_mut_accessors() {
msg.oneof_bytes_mut().set(b"123");
assert_that!(msg.oneof_field_mut(), matches_pattern!(OneofBytes(_)));
}
macro_rules! generate_repeated_numeric_test {
($(($t: ty, $field: ident)),*) => {
paste! { $(
#[test]
fn [< test_repeated_ $field _accessors >]() {
let mut msg = TestAllTypes::new();
assert_that!(msg.[< repeated_ $field >]().len(), eq(0));
assert_that!(msg.[<repeated_ $field >]().get(0), none());
let mut mutator = msg.[<repeated_ $field _mut >]();
mutator.push(1 as $t);
assert_that!(mutator.len(), eq(1));
assert_that!(mutator.get(0), some(eq(1 as $t)));
mutator.set(0, 2 as $t);
assert_that!(mutator.get(0), some(eq(2 as $t)));
mutator.push(1 as $t);
mutator.push(3 as $t);
assert_that!(mutator.get_mut(2).is_some(), eq(true));
let mut mut_elem = mutator.get_mut(2).unwrap();
mut_elem.set(4 as $t);
assert_that!(mut_elem.get(), eq(4 as $t));
mut_elem.clear();
assert_that!(mut_elem.get(), eq(0 as $t));
assert_that!(
mutator.iter().collect::<Vec<_>>(),
eq(vec![2 as $t, 1 as $t, 0 as $t])
);
assert_that!(
(*mutator).into_iter().collect::<Vec<_>>(),
eq(vec![2 as $t, 1 as $t, 0 as $t])
);
for mut mutable_elem in msg.[<repeated_ $field _mut >]() {
mutable_elem.set(0 as $t);
}
assert_that!(
msg.[<repeated_ $field _mut >]().iter().all(|v| v == (0 as $t)),
eq(true)
);
}
#[test]
fn [< test_repeated_ $field _set >]() {
let mut msg = TestAllTypes::new();
let mut mutator = msg.[<repeated_ $field _mut>]();
let mut msg2 = TestAllTypes::new();
let mut mutator2 = msg2.[<repeated_ $field _mut>]();
for i in 0..5 {
mutator2.push(i as $t);
}
protobuf::MutProxy::set(&mut mutator, *mutator2);
assert_that!(
mutator.iter().collect::<Vec<_>>(),
eq(mutator2.iter().collect::<Vec<_>>())
);
}
)* }
};
}
generate_repeated_numeric_test!(
(i32, int32),
(u32, uint32),
(i64, int64),
(u64, uint64),
(f32, float),
(f64, double)
);
#[test]
fn test_repeated_bool_accessors() {
let mut msg = TestAllTypes::new();
assert_that!(msg.repeated_bool().len(), eq(0));
assert_that!(msg.repeated_bool().get(0), none());
let mut mutator = msg.repeated_bool_mut();
mutator.push(true);
assert_that!(mutator.len(), eq(1));
assert_that!(mutator.get(0), some(eq(true)));
mutator.set(0, false);
assert_that!(mutator.get(0), some(eq(false)));
mutator.push(true);
mutator.push(false);
assert_that!(mutator.get_mut(2), pat!(Some(_)));
let mut mut_elem = mutator.get_mut(2).unwrap();
mut_elem.set(true);
assert_that!(mut_elem.get(), eq(true));
mut_elem.clear();
assert_that!(mut_elem.get(), eq(false));
assert_that!(mutator.iter().collect::<Vec<_>>(), eq(vec![false, true, false]));
assert_that!((*mutator).into_iter().collect::<Vec<_>>(), eq(vec![false, true, false]));
for mut mutable_elem in msg.repeated_bool_mut() {
mutable_elem.set(false);
}
assert_that!(msg.repeated_bool().iter().all(|v| v), eq(false));
}
#[test]
fn test_repeated_bool_set() {
let mut msg = TestAllTypes::new();
let mut mutator = msg.repeated_bool_mut();
let mut msg2 = TestAllTypes::new();
let mut mutator2 = msg2.repeated_bool_mut();
for _ in 0..5 {
mutator2.push(true);
}
protobuf::MutProxy::set(&mut mutator, *mutator2);
assert_that!(mutator.iter().collect::<Vec<_>>(), eq(mutator2.iter().collect::<Vec<_>>()));
}

Loading…
Cancel
Save