MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / enum_derive

Function enum_derive

tests/all/component_model/macros.rs:252–361  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

250
251#[test]
252fn enum_derive() -> Result<()> {
253 #[derive(ComponentType, Lift, Lower, PartialEq, Eq, Debug, Copy, Clone)]
254 #[component(enum)]
255 #[repr(u8)]
256 enum Foo {
257 #[component(name = "foo-bar-baz")]
258 A,
259 B,
260 C,
261 }
262
263 let engine = super::engine();
264 let mut store = Store::new(&engine, ());
265
266 // Happy path: component type matches case count and names
267
268 let component = Component::new(
269 &engine,
270 make_echo_component(r#"(enum "foo-bar-baz" "B" "C")"#, 4),
271 )?;
272 let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
273 let func = instance.get_typed_func::<(Foo,), (Foo,)>(&mut store, "echo")?;
274
275 for &input in &[Foo::A, Foo::B, Foo::C] {
276 let output = func.call(&mut store, (input,))?;
277
278 assert_eq!((input,), output);
279 }
280
281 // Sad path: case count mismatch (too few)
282
283 let component = Component::new(
284 &engine,
285 make_echo_component(r#"(enum "foo-bar-baz" "B")"#, 4),
286 )?;
287 let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
288
289 assert!(
290 instance
291 .get_typed_func::<(Foo,), (Foo,)>(&mut store, "echo")
292 .is_err()
293 );
294
295 // Sad path: case count mismatch (too many)
296
297 let component = Component::new(
298 &engine,
299 make_echo_component(r#"(enum "foo-bar-baz" "B" "C" "D")"#, 4),
300 )?;
301 let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
302
303 assert!(
304 instance
305 .get_typed_func::<(Foo,), (Foo,)>(&mut store, "echo")
306 .is_err()
307 );
308
309 // Sad path: case name mismatch

Callers

nothing calls this directly

Calls 6

make_echo_componentFunction · 0.85
OkFunction · 0.85
engineFunction · 0.50
newFunction · 0.50
instantiateMethod · 0.45
callMethod · 0.45

Tested by

no test coverage detected