Migrate optional.rs to googletest-rust

PiperOrigin-RevId: 583072875
pull/14775/head
Hong Shin 1 year ago committed by Copybara-Service
parent 1e0418df22
commit 1a5cdb9378
  1. 86
      rust/optional.rs

@ -619,109 +619,125 @@ mod tests {
#[test]
fn test_field_entry() {
use googletest::prelude::*;
let mut m1 = MyMessage::default();
let mut m2 = MyMessage::default();
let mut m1_a = m1.a_mut();
assert!(matches!(m1_a, Optional::Unset(_)));
assert_eq!(m1_a.as_view().val(), 0);
assert_that!(m1_a, matches_pattern!(Optional::Unset(_)));
assert_eq!(m2.b().val(), 5);
assert_that!(m1_a.as_view().val(), eq(0));
assert_that!(m2.b().val(), eq(5));
let mut m2_b = m2.b_mut();
assert!(m2_b.is_unset());
assert_eq!(m2_b.as_view().val(), 5);
assert_that!(m2_b.is_unset(), eq(true));
assert_that!(m2_b.as_view().val(), eq(5));
m2_b.set(10);
assert!(m2_b.is_set());
assert!(matches!(m2_b, Optional::Set(_)));
assert_eq!(m2_b.as_view().val(), 10);
assert_that!(m2_b.is_set(), eq(true));
assert_that!(m2_b, matches_pattern!(Optional::Set(_)));
assert_that!(m2_b.as_view().val(), eq(10));
assert_eq!(m1_a.or_default().as_view().val(), 0);
assert_eq!(m1.a_opt(), Optional::Set(VtableProxiedView { val: 0 }));
assert_that!(m1_a.or_default().as_view().val(), eq(0));
assert_that!(m1.a_opt(), eq(Optional::Set(VtableProxiedView { val: 0 })));
m1.a_mut().clear();
assert_eq!(m1.a().val(), 0);
assert_eq!(m1.b().val(), 5);
assert_eq!(m2.a().val(), 0);
assert_eq!(m2.b().val(), 10);
assert_that!(m1.a().val(), eq(0));
assert_that!(m1.b().val(), eq(5));
assert_that!(m2.a().val(), eq(0));
assert_that!(m2.b().val(), eq(10));
}
#[test]
fn test_or_set() {
use googletest::prelude::*;
let mut m1 = MyMessage::default();
let mut m2 = MyMessage::default();
assert_eq!(m1.a_mut().or_set(10).get().val(), 10);
assert_eq!(m1.a_opt(), Optional::Set(VtableProxiedView { val: 10 }));
assert_eq!(m1.a_mut().or_set(20).get().val(), 10);
assert_eq!(m1.a_opt(), Optional::Set(VtableProxiedView { val: 10 }));
assert_that!(m1.a_mut().or_set(10).get().val(), eq(10));
assert_that!(m1.a_opt(), eq(Optional::Set(VtableProxiedView { val: 10 })));
assert_that!(m1.a_mut().or_set(20).get().val(), eq(10));
assert_that!(m1.a_opt(), eq(Optional::Set(VtableProxiedView { val: 10 })));
assert_eq!(m2.a_mut().or_set_with(|| m1.a().val() + m1.b().val()).get().val(), 15);
assert_eq!(m2.a_opt(), Optional::Set(VtableProxiedView { val: 15 }));
assert_eq!(m2.a_mut().or_set_with(|| None::<i32>.unwrap()).get().val(), 15);
assert_eq!(m2.a_opt(), Optional::Set(VtableProxiedView { val: 15 }));
assert_that!(m2.a_mut().or_set_with(|| m1.a().val() + m1.b().val()).get().val(), eq(15));
assert_that!(m2.a_opt(), eq(Optional::Set(VtableProxiedView { val: 15 })));
assert_that!(m2.a_mut().or_set_with(|| None::<i32>.unwrap()).get().val(), eq(15));
assert_that!(m2.a_opt(), eq(Optional::Set(VtableProxiedView { val: 15 })));
}
#[test]
fn test_into_optional_view() {
use googletest::prelude::*;
let mut m1 = MyMessage::default();
assert_eq!(m1.a_mut().into_optional_view(), Optional::Unset(VtableProxiedView { val: 0 }));
assert_that!(
m1.a_mut().into_optional_view(),
eq(Optional::Unset(VtableProxiedView { val: 0 }))
);
m1.a_mut().set(10);
assert_eq!(m1.a_mut().into_optional_view(), Optional::Set(VtableProxiedView { val: 10 }));
assert_eq!(m1.b_mut().into_optional_view(), Optional::Unset(VtableProxiedView { val: 5 }));
assert_that!(
m1.a_mut().into_optional_view(),
eq(Optional::Set(VtableProxiedView { val: 10 }))
);
assert_that!(
m1.b_mut().into_optional_view(),
eq(Optional::Unset(VtableProxiedView { val: 5 }))
);
}
#[test]
fn test_try_into_mut() {
use googletest::prelude::*;
let mut m1 = MyMessage::default();
assert!(m1.a_mut().try_into_mut().is_none());
assert_that!(m1.a_mut().try_into_mut().is_none(), eq(true));
m1.a_mut().set(10);
let mut a_mut = m1.a_mut().try_into_mut().expect("field to be set");
a_mut.set(20);
assert_eq!(m1.a().val(), 20);
assert_that!(m1.a().val(), eq(20));
}
#[test]
fn test_present_field() {
use googletest::prelude::*;
let mut m = MyMessage::default();
m.a_mut().set(10);
match m.a_mut() {
Optional::Set(mut present) => {
assert_eq!(present.as_view().val(), 10);
assert_that!(present.as_view().val(), eq(10));
present.set(20);
assert_eq!(present.as_view().val(), 20);
assert_that!(present.as_view().val(), eq(20));
present.into_mut().set(30);
}
Optional::Unset(_) => unreachable!(),
}
assert_eq!(m.a_opt(), Optional::Set(VtableProxiedView { val: 30 }));
assert_that!(m.a_opt(), eq(Optional::Set(VtableProxiedView { val: 30 })));
m.b_mut().set(20);
match m.b_mut() {
Optional::Set(present) => present.clear(),
Optional::Unset(_) => unreachable!(),
};
assert_eq!(m.b_opt(), Optional::Unset(VtableProxiedView { val: 5 }));
assert_that!(m.b_opt(), eq(Optional::Unset(VtableProxiedView { val: 5 })));
}
#[test]
fn test_absent_field() {
use googletest::prelude::*;
let mut m = MyMessage::default();
match m.a_mut() {
Optional::Set(_) => unreachable!(),
Optional::Unset(absent) => {
assert_eq!(absent.as_view().val(), 0);
assert_that!(absent.as_view().val(), eq(0));
absent.set(20);
}
}
assert_eq!(m.a_opt(), Optional::Set(VtableProxiedView { val: 20 }));
assert_that!(m.a_opt(), eq(Optional::Set(VtableProxiedView { val: 20 })));
match m.b_mut() {
Optional::Set(_) => unreachable!(),
Optional::Unset(absent) => {
assert_eq!(absent.as_view().val(), 5);
assert_that!(absent.as_view().val(), eq(5));
absent.set_default();
}
}
assert_eq!(m.b_opt(), Optional::Set(VtableProxiedView { val: 5 }));
assert_that!(m.b_opt(), eq(Optional::Set(VtableProxiedView { val: 5 })));
}
}

Loading…
Cancel
Save