()
| 578 | |
| 579 | #[test] |
| 580 | fn variants() -> Result<()> { |
| 581 | let engine = super::engine(); |
| 582 | let mut store = Store::new(&engine, ()); |
| 583 | |
| 584 | let fragment = r#" |
| 585 | (type $c' (record (field "D" bool) (field "E" u32))) |
| 586 | (export $c "c" (type $c')) |
| 587 | (type $Foo' (variant |
| 588 | (case "A" u32) |
| 589 | (case "B" float64) |
| 590 | (case "C" $c) |
| 591 | )) |
| 592 | "#; |
| 593 | |
| 594 | let component = Component::new( |
| 595 | &engine, |
| 596 | make_echo_component_with_params( |
| 597 | fragment, |
| 598 | &[ |
| 599 | Param(Type::U8, Some(0)), |
| 600 | Param(Type::I64, Some(8)), |
| 601 | Param(Type::I32, None), |
| 602 | ], |
| 603 | ), |
| 604 | )?; |
| 605 | let instance = Linker::new(&engine).instantiate(&mut store, &component)?; |
| 606 | let func = instance.get_func(&mut store, "echo").unwrap(); |
| 607 | let input = Val::Variant("B".into(), Some(Box::new(Val::Float64(3.14159265)))); |
| 608 | let mut output = [Val::Bool(false)]; |
| 609 | func.call(&mut store, &[input.clone()], &mut output)?; |
| 610 | |
| 611 | assert_eq!(input, output[0]); |
| 612 | |
| 613 | // Do it again, this time using case "C" |
| 614 | |
| 615 | let component = Component::new( |
| 616 | &engine, |
| 617 | make_echo_component_with_params( |
| 618 | fragment, |
| 619 | &[ |
| 620 | Param(Type::U8, Some(0)), |
| 621 | Param(Type::I64, Some(8)), |
| 622 | Param(Type::I32, Some(12)), |
| 623 | ], |
| 624 | ), |
| 625 | )?; |
| 626 | let instance = Linker::new(&engine).instantiate(&mut store, &component)?; |
| 627 | let func = instance.get_func(&mut store, "echo").unwrap(); |
| 628 | let input = Val::Variant( |
| 629 | "C".into(), |
| 630 | Some(Box::new(Val::Record(vec![ |
| 631 | ("D".into(), Val::Bool(true)), |
| 632 | ("E".into(), Val::U32(314159265)), |
| 633 | ]))), |
| 634 | ); |
| 635 | func.call(&mut store, &[input.clone()], &mut output)?; |
| 636 | |
| 637 | assert_eq!(input, output[0]); |
nothing calls this directly
no test coverage detected