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

Function variant_derive

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

Source from the content-addressed store, hash-verified

125
126#[test]
127fn variant_derive() -> Result<()> {
128 #[derive(ComponentType, Lift, Lower, PartialEq, Eq, Debug, Copy, Clone)]
129 #[component(variant)]
130 enum Foo {
131 #[component(name = "foo-bar-baz")]
132 A(i32),
133 B(u32),
134 C,
135 }
136
137 let engine = super::engine();
138 let mut store = Store::new(&engine, ());
139
140 // Happy path: component type matches case count, names, and types
141
142 let component = Component::new(
143 &engine,
144 make_echo_component(
145 r#"(variant (case "foo-bar-baz" s32) (case "B" u32) (case "C"))"#,
146 8,
147 ),
148 )?;
149 let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
150 let func = instance.get_typed_func::<(Foo,), (Foo,)>(&mut store, "echo")?;
151
152 for &input in &[Foo::A(-42), Foo::B(73), Foo::C] {
153 let output = func.call(&mut store, (input,))?;
154
155 assert_eq!((input,), output);
156 }
157
158 // Sad path: case count mismatch (too few)
159
160 let component = Component::new(
161 &engine,
162 make_echo_component(r#"(variant (case "foo-bar-baz" s32) (case "B" u32))"#, 8),
163 )?;
164 let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
165
166 assert!(
167 instance
168 .get_typed_func::<(Foo,), (Foo,)>(&mut store, "echo")
169 .is_err()
170 );
171
172 // Sad path: case count mismatch (too many)
173
174 let component = Component::new(
175 &engine,
176 make_echo_component(
177 r#"(variant (case "foo-bar-baz" s32) (case "B" u32) (case "C") (case "D" u32))"#,
178 8,
179 ),
180 )?;
181 let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
182
183 assert!(
184 instance

Callers

nothing calls this directly

Calls 8

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

Tested by

no test coverage detected