Migrate accessors_test.rs to googletest-rust

The flexibility of googletest-rust looks promising:
```
assert_that!(msg.oneof_field(), matches_pattern!(not_set(_)));
assert_that!(msg.oneof_field(), matches_pattern!(OneofUint32(eq(7))));

+nonfatal assertions + custom matchers
```

PiperOrigin-RevId: 572272465
pull/14310/head
Hong Shin 1 year ago committed by Copybara-Service
parent 499dd1c3f4
commit 19aad63844
  1. 2
      rust/test/shared/BUILD
  2. 357
      rust/test/shared/accessors_test.rs

@ -127,6 +127,7 @@ rust_test(
"not_build:arm", "not_build:arm",
], ],
deps = [ deps = [
"//third_party/gtest_rust/googletest",
"//rust:protobuf_cpp", "//rust:protobuf_cpp",
"//rust/test:unittest_cc_rust_proto", "//rust/test:unittest_cc_rust_proto",
], ],
@ -143,6 +144,7 @@ rust_test(
"not_build:arm", "not_build:arm",
], ],
deps = [ deps = [
"//third_party/gtest_rust/googletest",
"//rust:protobuf_upb", "//rust:protobuf_upb",
"//rust/test:unittest_upb_rust_proto", "//rust/test:unittest_upb_rust_proto",
], ],

@ -7,359 +7,363 @@
//! Tests covering accessors for singular bool, int32, int64, and bytes fields. //! Tests covering accessors for singular bool, int32, int64, and bytes fields.
use googletest::prelude::*;
use protobuf::Optional; use protobuf::Optional;
use unittest_proto::proto2_unittest::{TestAllTypes, TestAllTypes_}; use unittest_proto::proto2_unittest::{TestAllTypes, TestAllTypes_};
#[test] #[test]
fn test_default_accessors() { fn test_default_accessors() {
let msg = TestAllTypes::new(); let msg = TestAllTypes::new();
assert_eq!(msg.default_int32(), 41); assert_that!(
assert_eq!(msg.default_int64(), 42); msg,
assert_eq!(msg.default_uint32(), 43); matches_pattern!(TestAllTypes{
assert_eq!(msg.default_uint64(), 44); default_int32(): eq(41),
assert_eq!(msg.default_sint32(), -45); default_int64(): eq(42),
assert_eq!(msg.default_sint64(), 46); default_uint32(): eq(43),
assert_eq!(msg.default_fixed32(), 47); default_uint64(): eq(44),
assert_eq!(msg.default_fixed64(), 48); default_sint32(): eq(-45),
assert_eq!(msg.default_sfixed32(), 49); default_sint64(): eq(46),
assert_eq!(msg.default_sfixed64(), -50); default_fixed32(): eq(47),
assert_eq!(msg.default_float(), 51.5); default_fixed64(): eq(48),
assert_eq!(msg.default_double(), 52000.0); default_sfixed32(): eq(49),
assert_eq!(msg.default_bool(), true); default_sfixed64(): eq(-50),
default_float(): eq(51.5),
default_double(): eq(52000.0),
default_bool(): eq(true),
})
);
} }
#[test] #[test]
fn test_optional_fixed32_accessors() { fn test_optional_fixed32_accessors() {
let mut msg = TestAllTypes::new(); let mut msg = TestAllTypes::new();
assert_eq!(msg.optional_fixed32_opt(), Optional::Unset(0)); assert_that!(msg.optional_fixed32_opt(), eq(Optional::Unset(0)));
assert_eq!(msg.optional_fixed32(), 0); assert_that!(msg.optional_fixed32(), eq(0));
msg.optional_fixed32_set(Some(99)); msg.optional_fixed32_set(Some(99));
assert_eq!(msg.optional_fixed32_opt(), Optional::Set(99)); assert_that!(msg.optional_fixed32_opt(), eq(Optional::Set(99)));
assert_eq!(msg.optional_fixed32(), 99); assert_that!(msg.optional_fixed32(), eq(99));
msg.optional_fixed32_set(None); msg.optional_fixed32_set(None);
assert_eq!(msg.optional_fixed32_opt(), Optional::Unset(0)); assert_that!(msg.optional_fixed32_opt(), eq(Optional::Unset(0)));
assert_that!(msg.optional_fixed32(), eq(0));
assert_eq!(msg.optional_fixed32(), 0);
} }
#[test] #[test]
fn test_optional_fixed64_accessors() { fn test_optional_fixed64_accessors() {
let mut msg = TestAllTypes::new(); let mut msg = TestAllTypes::new();
assert_eq!(msg.optional_fixed64_opt(), Optional::Unset(0)); assert_that!(msg.optional_fixed64_opt(), eq(Optional::Unset(0)));
assert_eq!(msg.optional_fixed64(), 0); assert_that!(msg.optional_fixed64(), eq(0));
msg.optional_fixed64_set(Some(2000)); msg.optional_fixed64_set(Some(2000));
assert_eq!(msg.optional_fixed64_opt(), Optional::Set(2000)); assert_that!(msg.optional_fixed64_opt(), eq(Optional::Set(2000)));
assert_eq!(msg.optional_fixed64(), 2000); assert_that!(msg.optional_fixed64(), eq(2000));
msg.optional_fixed64_set(None); msg.optional_fixed64_set(None);
assert_eq!(msg.optional_fixed64_opt(), Optional::Unset(0)); assert_that!(msg.optional_fixed64_opt(), eq(Optional::Unset(0)));
assert_eq!(msg.optional_fixed64(), 0); assert_that!(msg.optional_fixed64(), eq(0));
} }
#[test] #[test]
fn test_optional_int32_accessors() { fn test_optional_int32_accessors() {
let mut msg = TestAllTypes::new(); let mut msg = TestAllTypes::new();
assert_eq!(msg.optional_int32_opt(), Optional::Unset(0)); assert_that!(msg.optional_int32_opt(), eq(Optional::Unset(0)));
assert_eq!(msg.optional_int32(), 0); assert_that!(msg.optional_int32(), eq(0));
msg.optional_int32_set(Some(1)); msg.optional_int32_set(Some(1));
assert_eq!(msg.optional_int32_opt(), Optional::Set(1)); assert_that!(msg.optional_int32_opt(), eq(Optional::Set(1)));
assert_eq!(msg.optional_int32(), 1); assert_that!(msg.optional_int32(), eq(1));
msg.optional_int32_set(None); msg.optional_int32_set(None);
assert_eq!(msg.optional_int32_opt(), Optional::Unset(0)); assert_that!(msg.optional_int32_opt(), eq(Optional::Unset(0)));
assert_that!(msg.optional_int32(), eq(0));
assert_eq!(msg.optional_int32(), 0);
} }
#[test] #[test]
fn test_optional_int64_accessors() { fn test_optional_int64_accessors() {
let mut msg = TestAllTypes::new(); let mut msg = TestAllTypes::new();
assert_eq!(msg.optional_int64_opt(), Optional::Unset(0)); assert_that!(msg.optional_int64_opt(), eq(Optional::Unset(0)));
assert_eq!(msg.optional_int64(), 0); assert_that!(msg.optional_int64(), eq(0));
msg.optional_int64_set(Some(42)); msg.optional_int64_set(Some(42));
assert_eq!(msg.optional_int64_opt(), Optional::Set(42)); assert_that!(msg.optional_int64_opt(), eq(Optional::Set(42)));
assert_eq!(msg.optional_int64(), 42); assert_that!(msg.optional_int64(), eq(42));
msg.optional_int64_set(None); msg.optional_int64_set(None);
assert_eq!(msg.optional_int64_opt(), Optional::Unset(0)); assert_that!(msg.optional_int64_opt(), eq(Optional::Unset(0)));
assert_eq!(msg.optional_int64(), 0); assert_that!(msg.optional_int64(), eq(0));
} }
#[test] #[test]
fn test_optional_sint32_accessors() { fn test_optional_sint32_accessors() {
let mut msg = TestAllTypes::new(); let mut msg = TestAllTypes::new();
assert_eq!(msg.optional_sint32_opt(), Optional::Unset(0)); assert_that!(msg.optional_sint32_opt(), eq(Optional::Unset(0)));
assert_eq!(msg.optional_sint32(), 0); assert_that!(msg.optional_sint32(), eq(0));
msg.optional_sint32_set(Some(-22)); msg.optional_sint32_set(Some(-22));
assert_eq!(msg.optional_sint32_opt(), Optional::Set(-22)); assert_that!(msg.optional_sint32_opt(), eq(Optional::Set(-22)));
assert_eq!(msg.optional_sint32(), -22); assert_that!(msg.optional_sint32(), eq(-22));
msg.optional_sint32_set(None); msg.optional_sint32_set(None);
assert_eq!(msg.optional_sint32_opt(), Optional::Unset(0)); assert_that!(msg.optional_sint32_opt(), eq(Optional::Unset(0)));
assert_eq!(msg.optional_sint32(), 0); assert_that!(msg.optional_sint32(), eq(0));
} }
#[test] #[test]
fn test_optional_sint64_accessors() { fn test_optional_sint64_accessors() {
let mut msg = TestAllTypes::new(); let mut msg = TestAllTypes::new();
assert_eq!(msg.optional_sint64_opt(), Optional::Unset(0)); assert_that!(msg.optional_sint64_opt(), eq(Optional::Unset(0)));
assert_eq!(msg.optional_sint64(), 0); assert_that!(msg.optional_sint64(), eq(0));
msg.optional_sint64_set(Some(7000)); msg.optional_sint64_set(Some(7000));
assert_eq!(msg.optional_sint64_opt(), Optional::Set(7000)); assert_that!(msg.optional_sint64_opt(), eq(Optional::Set(7000)));
assert_eq!(msg.optional_sint64(), 7000); assert_that!(msg.optional_sint64(), eq(7000));
msg.optional_sint64_set(None); msg.optional_sint64_set(None);
assert_eq!(msg.optional_sint64_opt(), Optional::Unset(0)); assert_that!(msg.optional_sint64_opt(), eq(Optional::Unset(0)));
assert_eq!(msg.optional_sint64(), 0); assert_that!(msg.optional_sint64(), eq(0));
} }
#[test] #[test]
fn test_optional_uint32_accessors() { fn test_optional_uint32_accessors() {
let mut msg = TestAllTypes::new(); let mut msg = TestAllTypes::new();
assert_eq!(msg.optional_uint32_opt(), Optional::Unset(0)); assert_that!(msg.optional_uint32_opt(), eq(Optional::Unset(0)));
assert_eq!(msg.optional_uint32(), 0); assert_that!(msg.optional_uint32(), eq(0));
msg.optional_uint32_set(Some(9001)); msg.optional_uint32_set(Some(9001));
assert_eq!(msg.optional_uint32_opt(), Optional::Set(9001)); assert_that!(msg.optional_uint32_opt(), eq(Optional::Set(9001)));
assert_eq!(msg.optional_uint32(), 9001); assert_that!(msg.optional_uint32(), eq(9001));
msg.optional_uint32_set(None); msg.optional_uint32_set(None);
assert_eq!(msg.optional_uint32_opt(), Optional::Unset(0)); assert_that!(msg.optional_uint32_opt(), eq(Optional::Unset(0)));
assert_eq!(msg.optional_uint32(), 0); assert_that!(msg.optional_uint32(), eq(0));
} }
#[test] #[test]
fn test_optional_uint64_accessors() { fn test_optional_uint64_accessors() {
let mut msg = TestAllTypes::new(); let mut msg = TestAllTypes::new();
assert_eq!(msg.optional_uint64_opt(), Optional::Unset(0)); assert_that!(msg.optional_uint64_opt(), eq(Optional::Unset(0)));
assert_eq!(msg.optional_uint64(), 0); assert_that!(msg.optional_uint64(), eq(0));
msg.optional_uint64_set(Some(42)); msg.optional_uint64_set(Some(42));
assert_eq!(msg.optional_uint64_opt(), Optional::Set(42)); assert_that!(msg.optional_uint64_opt(), eq(Optional::Set(42)));
assert_eq!(msg.optional_uint64(), 42); assert_that!(msg.optional_uint64(), eq(42));
msg.optional_uint64_set(None); msg.optional_uint64_set(None);
assert_eq!(msg.optional_uint64_opt(), Optional::Unset(0)); assert_that!(msg.optional_uint64_opt(), eq(Optional::Unset(0)));
assert_eq!(msg.optional_uint64(), 0); assert_that!(msg.optional_uint64(), eq(0));
} }
#[test] #[test]
fn test_optional_float_accessors() { fn test_optional_float_accessors() {
let mut msg = TestAllTypes::new(); let mut msg = TestAllTypes::new();
assert_eq!(msg.optional_float_opt(), Optional::Unset(0.0)); assert_that!(msg.optional_float_opt(), eq(Optional::Unset(0.0)));
assert_eq!(msg.optional_float(), 0.0); assert_that!(msg.optional_float(), eq(0.0));
msg.optional_float_set(Some(3.14)); msg.optional_float_set(Some(std::f32::consts::PI));
assert_eq!(msg.optional_float_opt(), Optional::Set(3.14)); assert_that!(msg.optional_float_opt(), eq(Optional::Set(std::f32::consts::PI)));
assert_eq!(msg.optional_float(), 3.14); assert_that!(msg.optional_float(), eq(std::f32::consts::PI));
msg.optional_float_set(None); msg.optional_float_set(None);
assert_eq!(msg.optional_float_opt(), Optional::Unset(0.0)); assert_that!(msg.optional_float_opt(), eq(Optional::Unset(0.0)));
assert_eq!(msg.optional_float(), 0.0); assert_that!(msg.optional_float(), eq(0.0));
} }
#[test] #[test]
fn test_optional_double_accessors() { fn test_optional_double_accessors() {
let mut msg = TestAllTypes::new(); let mut msg = TestAllTypes::new();
assert_eq!(msg.optional_double_opt(), Optional::Unset(0.0)); assert_that!(msg.optional_double_opt(), eq(Optional::Unset(0.0)));
assert_eq!(msg.optional_double(), 0.0); assert_that!(msg.optional_double(), eq(0.0));
msg.optional_double_set(Some(-10.99)); msg.optional_double_set(Some(-10.99));
assert_eq!(msg.optional_double_opt(), Optional::Set(-10.99)); assert_that!(msg.optional_double_opt(), eq(Optional::Set(-10.99)));
assert_eq!(msg.optional_double(), -10.99); assert_that!(msg.optional_double(), eq(-10.99));
msg.optional_double_set(None); msg.optional_double_set(None);
assert_eq!(msg.optional_double_opt(), Optional::Unset(0.0)); assert_that!(msg.optional_double_opt(), eq(Optional::Unset(0.0)));
assert_eq!(msg.optional_double(), 0.0); assert_that!(msg.optional_double(), eq(0.0));
} }
#[test] #[test]
fn test_optional_bool_accessors() { fn test_optional_bool_accessors() {
let mut msg = TestAllTypes::new(); let mut msg = TestAllTypes::new();
assert_eq!(msg.optional_bool_opt(), Optional::Unset(false)); assert_that!(msg.optional_bool_opt(), eq(Optional::Unset(false)));
msg.optional_bool_set(Some(true)); msg.optional_bool_set(Some(true));
assert_eq!(msg.optional_bool_opt(), Optional::Set(true)); assert_that!(msg.optional_bool_opt(), eq(Optional::Set(true)));
msg.optional_bool_set(None); msg.optional_bool_set(None);
assert_eq!(msg.optional_bool_opt(), Optional::Unset(false)); assert_that!(msg.optional_bool_opt(), eq(Optional::Unset(false)));
} }
#[test] #[test]
fn test_optional_bytes_accessors() { fn test_optional_bytes_accessors() {
let mut msg = TestAllTypes::new(); let mut msg = TestAllTypes::new();
assert_eq!(msg.optional_bytes(), b""); assert_that!(msg.optional_bytes(), eq(b""));
assert_eq!(msg.optional_bytes_opt(), Optional::Unset(&b""[..])); assert_that!(msg.optional_bytes_opt(), eq(Optional::Unset(&b""[..])));
assert_eq!(msg.optional_bytes_mut().get(), b""); assert_that!(msg.optional_bytes_mut().get(), eq(b""));
assert!(msg.optional_bytes_mut().is_unset()); assert_that!(msg.optional_bytes_mut().is_unset(), eq(true));
{ {
let s = Vec::from(&b"hello world"[..]); let s = Vec::from(&b"hello world"[..]);
msg.optional_bytes_mut().set(&s[..]); msg.optional_bytes_mut().set(&s[..]);
} }
assert_eq!(msg.optional_bytes(), b"hello world"); assert_that!(msg.optional_bytes(), eq(b"hello world"));
assert_eq!(msg.optional_bytes_opt(), Optional::Set(&b"hello world"[..])); assert_that!(msg.optional_bytes_opt(), eq(Optional::Set(&b"hello world"[..])));
assert!(msg.optional_bytes_mut().is_set()); assert_that!(msg.optional_bytes_mut().is_set(), eq(true));
assert_eq!(msg.optional_bytes_mut().get(), b"hello world"); assert_that!(msg.optional_bytes_mut().get(), eq(b"hello world"));
msg.optional_bytes_mut().or_default().set(b"accessors_test"); msg.optional_bytes_mut().or_default().set(b"accessors_test");
assert_eq!(msg.optional_bytes(), b"accessors_test"); assert_that!(msg.optional_bytes(), eq(b"accessors_test"));
assert_eq!(msg.optional_bytes_opt(), Optional::Set(&b"accessors_test"[..])); assert_that!(msg.optional_bytes_opt(), eq(Optional::Set(&b"accessors_test"[..])));
assert!(msg.optional_bytes_mut().is_set()); assert_that!(msg.optional_bytes_mut().is_set(), eq(true));
assert_eq!(msg.optional_bytes_mut().get(), b"accessors_test"); assert_that!(msg.optional_bytes_mut().get(), eq(b"accessors_test"));
assert_eq!(msg.optional_bytes_mut().or_default().get(), b"accessors_test"); assert_that!(msg.optional_bytes_mut().or_default().get(), eq(b"accessors_test"));
msg.optional_bytes_mut().clear(); msg.optional_bytes_mut().clear();
assert_eq!(msg.optional_bytes(), b""); assert_that!(msg.optional_bytes(), eq(b""));
assert_eq!(msg.optional_bytes_opt(), Optional::Unset(&b""[..])); assert_that!(msg.optional_bytes_opt(), eq(Optional::Unset(&b""[..])));
assert!(msg.optional_bytes_mut().is_unset()); assert_that!(msg.optional_bytes_mut().is_unset(), eq(true));
msg.optional_bytes_mut().set(b""); msg.optional_bytes_mut().set(b"");
assert_eq!(msg.optional_bytes(), b""); assert_that!(msg.optional_bytes(), eq(b""));
assert_eq!(msg.optional_bytes_opt(), Optional::Set(&b""[..])); assert_that!(msg.optional_bytes_opt(), eq(Optional::Set(&b""[..])));
msg.optional_bytes_mut().clear(); msg.optional_bytes_mut().clear();
msg.optional_bytes_mut().or_default(); msg.optional_bytes_mut().or_default();
assert_eq!(msg.optional_bytes(), b""); assert_that!(msg.optional_bytes(), eq(b""));
assert_eq!(msg.optional_bytes_opt(), Optional::Set(&b""[..])); assert_that!(msg.optional_bytes_opt(), eq(Optional::Set(&b""[..])));
msg.optional_bytes_mut().or_default().set(b"\xffbinary\x85non-utf8"); msg.optional_bytes_mut().or_default().set(b"\xffbinary\x85non-utf8");
assert_eq!(msg.optional_bytes(), b"\xffbinary\x85non-utf8"); assert_that!(msg.optional_bytes(), eq(b"\xffbinary\x85non-utf8"));
assert_eq!(msg.optional_bytes_opt(), Optional::Set(&b"\xffbinary\x85non-utf8"[..])); assert_that!(msg.optional_bytes_opt(), eq(Optional::Set(&b"\xffbinary\x85non-utf8"[..])));
assert!(msg.optional_bytes_mut().is_set()); assert_that!(msg.optional_bytes_mut().is_set(), eq(true));
assert_eq!(msg.optional_bytes_mut().get(), b"\xffbinary\x85non-utf8"); assert_that!(msg.optional_bytes_mut().get(), eq(b"\xffbinary\x85non-utf8"));
assert_eq!(msg.optional_bytes_mut().or_default().get(), b"\xffbinary\x85non-utf8"); assert_that!(msg.optional_bytes_mut().or_default().get(), eq(b"\xffbinary\x85non-utf8"));
} }
#[test] #[test]
fn test_nonempty_default_bytes_accessors() { fn test_nonempty_default_bytes_accessors() {
let mut msg = TestAllTypes::new(); let mut msg = TestAllTypes::new();
assert_eq!(msg.default_bytes(), b"world"); assert_that!(msg.default_bytes(), eq(b"world"));
assert_eq!(msg.default_bytes_opt(), Optional::Unset(&b"world"[..])); assert_that!(msg.default_bytes_opt(), eq(Optional::Unset(&b"world"[..])));
assert_eq!(msg.default_bytes_mut().get(), b"world"); assert_that!(msg.default_bytes_mut().get(), eq(b"world"));
assert!(msg.default_bytes_mut().is_unset()); assert_that!(msg.default_bytes_mut().is_unset(), eq(true));
{ {
let s = String::from("hello world"); let s = String::from("hello world");
msg.default_bytes_mut().set(s.as_bytes()); msg.default_bytes_mut().set(s.as_bytes());
} }
assert_eq!(msg.default_bytes(), b"hello world"); assert_that!(msg.default_bytes(), eq(b"hello world"));
assert_eq!(msg.default_bytes_opt(), Optional::Set(&b"hello world"[..])); assert_that!(msg.default_bytes_opt(), eq(Optional::Set(&b"hello world"[..])));
assert!(msg.default_bytes_mut().is_set()); assert_that!(msg.default_bytes_mut().is_set(), eq(true));
assert_eq!(msg.default_bytes_mut().get(), b"hello world"); assert_that!(msg.default_bytes_mut().get(), eq(b"hello world"));
msg.default_bytes_mut().or_default().set(b"accessors_test"); msg.default_bytes_mut().or_default().set(b"accessors_test");
assert_eq!(msg.default_bytes(), b"accessors_test"); assert_that!(msg.default_bytes(), eq(b"accessors_test"));
assert_eq!(msg.default_bytes_opt(), Optional::Set(&b"accessors_test"[..])); assert_that!(msg.default_bytes_opt(), eq(Optional::Set(&b"accessors_test"[..])));
assert!(msg.default_bytes_mut().is_set()); assert_that!(msg.default_bytes_mut().is_set(), eq(true));
assert_eq!(msg.default_bytes_mut().get(), b"accessors_test"); assert_that!(msg.default_bytes_mut().get(), eq(b"accessors_test"));
assert_eq!(msg.default_bytes_mut().or_default().get(), b"accessors_test"); assert_that!(msg.default_bytes_mut().or_default().get(), eq(b"accessors_test"));
msg.default_bytes_mut().clear(); msg.default_bytes_mut().clear();
assert_eq!(msg.default_bytes(), b"world"); assert_that!(msg.default_bytes(), eq(b"world"));
assert_eq!(msg.default_bytes_opt(), Optional::Unset(&b"world"[..])); assert_that!(msg.default_bytes_opt(), eq(Optional::Unset(&b"world"[..])));
assert!(msg.default_bytes_mut().is_unset()); assert_that!(msg.default_bytes_mut().is_unset(), eq(true));
msg.default_bytes_mut().set(b""); msg.default_bytes_mut().set(b"");
assert_eq!(msg.default_bytes(), b""); assert_that!(msg.default_bytes(), eq(b""));
assert_eq!(msg.default_bytes_opt(), Optional::Set(&b""[..])); assert_that!(msg.default_bytes_opt(), eq(Optional::Set(&b""[..])));
msg.default_bytes_mut().clear(); msg.default_bytes_mut().clear();
msg.default_bytes_mut().or_default(); msg.default_bytes_mut().or_default();
assert_eq!(msg.default_bytes(), b"world"); assert_that!(msg.default_bytes(), eq(b"world"));
assert_eq!(msg.default_bytes_opt(), Optional::Set(&b"world"[..])); assert_that!(msg.default_bytes_opt(), eq(Optional::Set(&b"world"[..])));
msg.default_bytes_mut().or_default().set(b"\xffbinary\x85non-utf8"); msg.default_bytes_mut().or_default().set(b"\xffbinary\x85non-utf8");
assert_eq!(msg.default_bytes(), b"\xffbinary\x85non-utf8"); assert_that!(msg.default_bytes(), eq(b"\xffbinary\x85non-utf8"));
assert_eq!(msg.default_bytes_opt(), Optional::Set(&b"\xffbinary\x85non-utf8"[..])); assert_that!(msg.default_bytes_opt(), eq(Optional::Set(&b"\xffbinary\x85non-utf8"[..])));
assert!(msg.default_bytes_mut().is_set()); assert_that!(msg.default_bytes_mut().is_set(), eq(true));
assert_eq!(msg.default_bytes_mut().get(), b"\xffbinary\x85non-utf8"); assert_that!(msg.default_bytes_mut().get(), eq(b"\xffbinary\x85non-utf8"));
assert_eq!(msg.default_bytes_mut().or_default().get(), b"\xffbinary\x85non-utf8"); assert_that!(msg.default_bytes_mut().or_default().get(), eq(b"\xffbinary\x85non-utf8"));
} }
#[test] #[test]
fn test_optional_string_accessors() { fn test_optional_string_accessors() {
let mut msg = TestAllTypes::new(); let mut msg = TestAllTypes::new();
assert_eq!(msg.optional_string(), ""); assert_that!(msg.optional_string(), eq(""));
assert_eq!(msg.optional_string_opt(), Optional::Unset("".into())); assert_that!(msg.optional_string_opt(), eq(Optional::Unset("".into())));
assert_eq!(msg.optional_string_mut().get(), ""); assert_that!(msg.optional_string_mut().get(), eq(""));
assert!(msg.optional_string_mut().is_unset()); assert_that!(msg.optional_string_mut().is_unset(), eq(true));
{ {
let s = String::from("hello world"); let s = String::from("hello world");
msg.optional_string_mut().set(&s[..]); msg.optional_string_mut().set(&s[..]);
} }
assert_eq!(msg.optional_string(), "hello world"); assert_that!(msg.optional_string(), eq("hello world"));
assert_eq!(msg.optional_string_opt(), Optional::Set("hello world".into())); assert_that!(msg.optional_string_opt(), eq(Optional::Set("hello world".into())));
assert!(msg.optional_string_mut().is_set()); assert_that!(msg.optional_string_mut().is_set(), eq(true));
assert_eq!(msg.optional_string_mut().get(), "hello world"); assert_that!(msg.optional_string_mut().get(), eq("hello world"));
msg.optional_string_mut().or_default().set("accessors_test"); msg.optional_string_mut().or_default().set("accessors_test");
assert_eq!(msg.optional_string(), "accessors_test"); assert_that!(msg.optional_string(), eq("accessors_test"));
assert_eq!(msg.optional_string_opt(), Optional::Set("accessors_test".into())); assert_that!(msg.optional_string_opt(), eq(Optional::Set("accessors_test".into())));
assert!(msg.optional_string_mut().is_set()); assert_that!(msg.optional_string_mut().is_set(), eq(true));
assert_eq!(msg.optional_string_mut().get(), "accessors_test"); assert_that!(msg.optional_string_mut().get(), eq("accessors_test"));
assert_eq!(msg.optional_string_mut().or_default().get(), "accessors_test"); assert_that!(msg.optional_string_mut().or_default().get(), eq("accessors_test"));
msg.optional_string_mut().clear(); msg.optional_string_mut().clear();
assert_eq!(msg.optional_string(), ""); assert_that!(msg.optional_string(), eq(""));
assert_eq!(msg.optional_string_opt(), Optional::Unset("".into())); assert_that!(msg.optional_string_opt(), eq(Optional::Unset("".into())));
assert!(msg.optional_string_mut().is_unset()); assert_that!(msg.optional_string_mut().is_unset(), eq(true));
msg.optional_string_mut().set(""); msg.optional_string_mut().set("");
assert_eq!(msg.optional_string(), ""); assert_that!(msg.optional_string(), eq(""));
assert_eq!(msg.optional_string_opt(), Optional::Set("".into())); assert_that!(msg.optional_string_opt(), eq(Optional::Set("".into())));
msg.optional_string_mut().clear(); msg.optional_string_mut().clear();
msg.optional_string_mut().or_default(); msg.optional_string_mut().or_default();
assert_eq!(msg.optional_string(), ""); assert_that!(msg.optional_string(), eq(""));
assert_eq!(msg.optional_string_opt(), Optional::Set("".into())); assert_that!(msg.optional_string_opt(), eq(Optional::Set("".into())));
} }
#[test] #[test]
fn test_nonempty_default_string_accessors() { fn test_nonempty_default_string_accessors() {
let mut msg = TestAllTypes::new(); let mut msg = TestAllTypes::new();
assert_eq!(msg.default_string(), "hello"); assert_that!(msg.default_string(), eq("hello"));
assert_eq!(msg.default_string_opt(), Optional::Unset("hello".into())); assert_that!(msg.default_string_opt(), eq(Optional::Unset("hello".into())));
assert_eq!(msg.default_string_mut().get(), "hello"); assert_that!(msg.default_string_mut().get(), eq("hello"));
assert!(msg.default_string_mut().is_unset()); assert_that!(msg.default_string_mut().is_unset(), eq(true));
{ {
let s = String::from("hello world"); let s = String::from("hello world");
msg.default_string_mut().set(&s[..]); msg.default_string_mut().set(&s[..]);
} }
assert_eq!(msg.default_string(), "hello world"); assert_that!(msg.default_string(), eq("hello world"));
assert_eq!(msg.default_string_opt(), Optional::Set("hello world".into())); assert_that!(msg.default_string_opt(), eq(Optional::Set("hello world".into())));
assert!(msg.default_string_mut().is_set()); assert_that!(msg.default_string_mut().is_set(), eq(true));
assert_eq!(msg.default_string_mut().get(), "hello world"); assert_that!(msg.default_string_mut().get(), eq("hello world"));
msg.default_string_mut().or_default().set("accessors_test"); msg.default_string_mut().or_default().set("accessors_test");
assert_eq!(msg.default_string(), "accessors_test"); assert_that!(msg.default_string(), eq("accessors_test"));
assert_eq!(msg.default_string_opt(), Optional::Set("accessors_test".into())); assert_that!(msg.default_string_opt(), eq(Optional::Set("accessors_test".into())));
assert!(msg.default_string_mut().is_set()); assert_that!(msg.default_string_mut().is_set(), eq(true));
assert_eq!(msg.default_string_mut().get(), "accessors_test"); assert_that!(msg.default_string_mut().get(), eq("accessors_test"));
assert_eq!(msg.default_string_mut().or_default().get(), "accessors_test"); assert_that!(msg.default_string_mut().or_default().get(), eq("accessors_test"));
msg.default_string_mut().clear(); msg.default_string_mut().clear();
assert_eq!(msg.default_string(), "hello"); assert_that!(msg.default_string(), eq("hello"));
assert_eq!(msg.default_string_opt(), Optional::Unset("hello".into())); assert_that!(msg.default_string_opt(), eq(Optional::Unset("hello".into())));
assert!(msg.default_string_mut().is_unset()); assert_that!(msg.default_string_mut().is_unset(), eq(true));
msg.default_string_mut().set(""); msg.default_string_mut().set("");
assert_eq!(msg.default_string(), ""); assert_that!(msg.default_string(), eq(""));
assert_eq!(msg.default_string_opt(), Optional::Set("".into())); assert_that!(msg.default_string_opt(), eq(Optional::Set("".into())));
msg.default_string_mut().clear(); msg.default_string_mut().clear();
msg.default_string_mut().or_default(); msg.default_string_mut().or_default();
assert_eq!(msg.default_string(), "hello"); assert_that!(msg.default_string(), eq("hello"));
assert_eq!(msg.default_string_opt(), Optional::Set("hello".into())); assert_that!(msg.default_string_opt(), eq(Optional::Set("hello".into())));
} }
#[test] #[test]
@ -376,19 +380,20 @@ fn test_oneof_accessors() {
use TestAllTypes_::OneofField::*; use TestAllTypes_::OneofField::*;
let mut msg = TestAllTypes::new(); let mut msg = TestAllTypes::new();
assert!(matches!(msg.oneof_field(), not_set(_))); assert_that!(msg.oneof_field(), matches_pattern!(not_set(_)));
msg.oneof_uint32_set(Some(7)); msg.oneof_uint32_set(Some(7));
assert_eq!(msg.oneof_uint32_opt(), Optional::Set(7)); assert_that!(msg.oneof_uint32_opt(), eq(Optional::Set(7)));
assert!(matches!(msg.oneof_field(), OneofUint32(7))); assert_that!(msg.oneof_field(), matches_pattern!(OneofUint32(eq(7))));
msg.oneof_uint32_set(None); msg.oneof_uint32_set(None);
assert_eq!(msg.oneof_uint32_opt(), Optional::Unset(0)); assert_that!(msg.oneof_uint32_opt(), eq(Optional::Unset(0)));
assert!(matches!(msg.oneof_field(), not_set(_))); assert_that!(msg.oneof_field(), matches_pattern!(not_set(_)));
msg.oneof_uint32_set(Some(7)); msg.oneof_uint32_set(Some(7));
msg.oneof_bytes_mut().set(b""); msg.oneof_bytes_mut().set(b"");
assert_eq!(msg.oneof_uint32_opt(), Optional::Unset(0)); assert_that!(msg.oneof_uint32_opt(), eq(Optional::Unset(0)));
// This should show it set to the OneofBytes but its not supported yet. // This should show it set to the OneofBytes but its not supported yet.
assert!(matches!(msg.oneof_field(), not_set(_))); assert_that!(msg.oneof_field(), matches_pattern!(not_set(_)));
} }

Loading…
Cancel
Save