use facet_reflect::Partial;
use facet_testhelpers::test;
#[test]
fn test_option_building_manual() {
let mut wip = Partial::alloc::<Option<String>>().unwrap();
if let facet_core::Def::Option(option_def) = wip.shape().def {
println!("Option def found: inner type is {}", option_def.t());
}
wip = wip.set(Some("hello".to_string())).unwrap();
let option_value = wip
.build()
.unwrap()
.materialize::<Option<String>>()
.unwrap();
assert_eq!(option_value, Some("hello".to_string()));
}
#[test]
fn test_option_building_none() {
let mut wip = Partial::alloc::<Option<String>>().unwrap();
wip = wip.set(None::<String>).unwrap();
let option_value = wip
.build()
.unwrap()
.materialize::<Option<String>>()
.unwrap();
assert_eq!(option_value, None);
}
#[test]
fn test_option_building_with_begin_some() {
let mut wip = Partial::alloc::<Option<String>>().unwrap();
let result = wip.begin_some();
match result {
Ok(w) => {
wip = w;
wip = wip.set("hello".to_string()).unwrap();
wip = wip.end().unwrap();
let option_value = wip
.build()
.unwrap()
.materialize::<Option<String>>()
.unwrap();
assert_eq!(option_value, Some("hello".to_string()));
}
Err(e) => {
println!("begin_some failed as expected: {e:?}");
}
}
}
#[test]
fn test_option_building_set_default() {
let mut wip = Partial::alloc::<Option<String>>().unwrap();
wip = wip.set_default().unwrap();
let option_value = wip
.build()
.unwrap()
.materialize::<Option<String>>()
.unwrap();
assert_eq!(option_value, None);
}
#[test]
fn test_nested_option_building() {
let mut wip = Partial::alloc::<Option<Option<String>>>().unwrap();
wip = wip.set(Some(Some("hello".to_string()))).unwrap();
let option_value = wip
.build()
.unwrap()
.materialize::<Option<Option<String>>>()
.unwrap();
assert_eq!(option_value, Some(Some("hello".to_string())));
}
#[test]
fn test_option_in_struct() {
#[derive(facet::Facet, Debug, PartialEq)]
struct TestStruct {
name: Option<String>,
age: Option<u32>,
}
let mut wip = Partial::alloc::<TestStruct>().unwrap();
wip = wip.begin_nth_field(0).unwrap(); wip = wip.set(Some("Alice".to_string())).unwrap();
wip = wip.end().unwrap();
wip = wip.begin_nth_field(1).unwrap(); wip = wip.set(None::<u32>).unwrap();
wip = wip.end().unwrap();
let struct_value = wip.build().unwrap().materialize::<TestStruct>().unwrap();
assert_eq!(
struct_value,
TestStruct {
name: Some("Alice".to_string()),
age: None,
}
);
}
#[test]
fn test_option_field_manual_building() {
#[derive(facet::Facet, Debug, PartialEq)]
struct TestStruct {
value: Option<String>,
}
let mut wip = Partial::alloc::<TestStruct>().unwrap();
wip = wip.begin_nth_field(0).unwrap();
wip = wip.set(Some("test".to_string())).unwrap();
wip = wip.end().unwrap();
let struct_value = wip.build().unwrap().materialize::<TestStruct>().unwrap();
assert_eq!(struct_value.value, Some("test".to_string()));
}
#[test]
fn explore_option_shape() {
let _wip = Partial::alloc::<Option<String>>().unwrap();
println!("Option<String> shape: {:?}", _wip.shape());
if let facet_core::Def::Option(option_def) = _wip.shape().def {
println!("Inner type: {:?}", option_def.t());
println!("Option vtable: {:?}", option_def.vtable);
}
if let Some(inner_shape) = _wip.shape().inner {
println!("Inner shape: {inner_shape:?}");
}
}
use facet_testhelpers::IPanic;
#[test]
fn option_uninit_defaults_to_none() -> Result<(), IPanic> {
let hv = Partial::alloc::<Option<f64>>()?
.build()?
.materialize::<Option<f64>>()?;
assert_eq!(hv, None);
Ok(())
}
#[test]
fn option_init() -> Result<(), IPanic> {
let hv = Partial::alloc::<Option<f64>>()?
.set::<Option<f64>>(Some(6.241))?
.build()?
.materialize::<Option<f64>>()?;
assert_eq!(hv, Some(6.241));
Ok(())
}