()
| 362 | |
| 363 | #[test] |
| 364 | fn flags() -> Result<()> { |
| 365 | let config = wasmtime_test_util::component::config(); |
| 366 | let engine = Engine::new(&config)?; |
| 367 | let mut store = Store::new(&engine, ()); |
| 368 | |
| 369 | // Simple 8-bit flags |
| 370 | wasmtime::component::flags! { |
| 371 | Foo { |
| 372 | #[component(name = "foo-bar-baz")] |
| 373 | const A; |
| 374 | const B; |
| 375 | const C; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | assert_eq!(Foo::default(), (Foo::A | Foo::B) & Foo::C); |
| 380 | assert_eq!(Foo::B, (Foo::A | Foo::B) & Foo::B); |
| 381 | assert_eq!(Foo::A, (Foo::A | Foo::B) & Foo::A); |
| 382 | assert_eq!(Foo::A | Foo::B, Foo::A ^ Foo::B); |
| 383 | assert_eq!(Foo::default(), Foo::A ^ Foo::A); |
| 384 | assert_eq!(Foo::B | Foo::C, !Foo::A); |
| 385 | |
| 386 | // Happy path: component type matches flag count and names |
| 387 | |
| 388 | let component = Component::new( |
| 389 | &engine, |
| 390 | make_echo_component(r#"(flags "foo-bar-baz" "B" "C")"#, 4), |
| 391 | )?; |
| 392 | let instance = Linker::new(&engine).instantiate(&mut store, &component)?; |
| 393 | let func = instance.get_typed_func::<(Foo,), (Foo,)>(&mut store, "echo")?; |
| 394 | |
| 395 | for n in 0..8 { |
| 396 | let mut input = Foo::default(); |
| 397 | if (n & 1) != 0 { |
| 398 | input |= Foo::A; |
| 399 | } |
| 400 | if (n & 2) != 0 { |
| 401 | input |= Foo::B; |
| 402 | } |
| 403 | if (n & 4) != 0 { |
| 404 | input |= Foo::C; |
| 405 | } |
| 406 | |
| 407 | let output = func.call(&mut store, (input,))?; |
| 408 | |
| 409 | assert_eq!((input,), output); |
| 410 | } |
| 411 | |
| 412 | // Sad path: flag count mismatch (too few) |
| 413 | |
| 414 | let component = Component::new( |
| 415 | &engine, |
| 416 | make_echo_component(r#"(flags "foo-bar-baz" "B")"#, 4), |
| 417 | )?; |
| 418 | let instance = Linker::new(&engine).instantiate(&mut store, &component)?; |
| 419 | |
| 420 | assert!( |
| 421 | instance |
nothing calls this directly
no test coverage detected