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