MCPcopy Create free account
hub / github.com/cel-rust/cel-rust / test_opaque_fn

Function test_opaque_fn

cel/src/objects.rs:2114–2153  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

2112
2113 #[test]
2114 fn test_opaque_fn() {
2115 pub fn my_fn(ftx: &FunctionContext) -> Result<Value, ExecutionError> {
2116 if let Some(Some(opaque)) = ftx.this.as_ref().map(|v| v.downcast_ref::<OpaqueVal>())
2117 {
2118 if opaque.val.runtime_type_name() == "my_struct" {
2119 Ok(opaque
2120 .val
2121 .deref()
2122 .downcast_ref::<MyStruct>()
2123 .unwrap()
2124 .field
2125 .clone()
2126 .into())
2127 } else {
2128 Err(ExecutionError::UnexpectedType {
2129 got: opaque.val.runtime_type_name().to_string(),
2130 want: "my_struct".to_string(),
2131 })
2132 }
2133 } else {
2134 Err(ExecutionError::UnexpectedType {
2135 got: format!("{:?}", ftx.this),
2136 want: "Value::Opaque".to_string(),
2137 })
2138 }
2139 }
2140
2141 let value = Arc::new(MyStruct {
2142 field: String::from("value"),
2143 });
2144
2145 let mut ctx = Context::default();
2146 ctx.add_variable_from_value("mine", Value::Opaque(value.clone()));
2147 ctx.add_function("myFn", my_fn);
2148 let prog = Program::compile("mine.myFn()").unwrap();
2149 assert_eq!(
2150 Ok(Value::String(Arc::new("value".into()))),
2151 prog.execute(&ctx)
2152 );
2153 }
2154
2155 #[test]
2156 fn opaque_eq() {

Callers

nothing calls this directly

Calls 4

OpaqueInterface · 0.85
cloneMethod · 0.80
add_functionMethod · 0.80

Tested by

no test coverage detected