| 1 |
use pretty_assertions::assert_eq; |
| 2 |
use wit_encoder::{ |
| 3 |
Field, Flag, Params, ResourceFunc, Result_, Type, TypeDef, TypeDefKind, VariantCase, |
| 4 |
}; |
| 5 |
|
| 6 |
const PACKAGE: &str = indoc::indoc! {" |
| 7 |
package wit-encoder:tests; |
| 8 |
|
| 9 |
/// interface documentation |
| 10 |
interface type-defs { |
| 11 |
type t1 = u8; |
| 12 |
type t2 = u16; |
| 13 |
type t3 = u32; |
| 14 |
type t4 = u64; |
| 15 |
type t5 = s8; |
| 16 |
type t6 = s16; |
| 17 |
type t7 = s32; |
| 18 |
type t8 = s64; |
| 19 |
type t9a = f32; |
| 20 |
type t9b = f32; |
| 21 |
type t10a = f64; |
| 22 |
type t10b = f64; |
| 23 |
type t11 = char; |
| 24 |
type t12 = list<char>; |
| 25 |
type t13 = string; |
| 26 |
type t14 = option<u32>; |
| 27 |
type t15 = result<u32, u32>; |
| 28 |
type t16 = result<_, u32>; |
| 29 |
type t17 = result<u32>; |
| 30 |
/// this is a documentation comment |
| 31 |
type t18 = result; |
| 32 |
record t20 { } |
| 33 |
record t21 { |
| 34 |
a: u32, |
| 35 |
} |
| 36 |
record t22 { |
| 37 |
a: u32, |
| 38 |
} |
| 39 |
record t23 { |
| 40 |
a: u32, |
| 41 |
b: u64, |
| 42 |
} |
| 43 |
record t24 { |
| 44 |
a: u32, |
| 45 |
b: u64, |
| 46 |
} |
| 47 |
record t25 { |
| 48 |
x: u32, |
| 49 |
} |
| 50 |
record %record { |
| 51 |
a: u32, |
| 52 |
} |
| 53 |
type t26 = tuple<>; |
| 54 |
type t27 = tuple<u32>; |
| 55 |
type t29 = tuple<u32, u64>; |
| 56 |
flags t30 { |
| 57 |
} |
| 58 |
flags t31 { |
| 59 |
/// option a |
| 60 |
a, |
| 61 |
/// option b |
| 62 |
b, |
| 63 |
/// option c |
| 64 |
c, |
| 65 |
} |
| 66 |
flags t32 { |
| 67 |
a, |
| 68 |
b, |
| 69 |
c, |
| 70 |
} |
| 71 |
variant t33 { |
| 72 |
a, |
| 73 |
} |
| 74 |
variant t34 { |
| 75 |
a, |
| 76 |
b, |
| 77 |
} |
| 78 |
variant t35 { |
| 79 |
a, |
| 80 |
b, |
| 81 |
} |
| 82 |
variant t36 { |
| 83 |
a, |
| 84 |
b(u32), |
| 85 |
} |
| 86 |
variant t37 { |
| 87 |
a, |
| 88 |
b(option<u32>), |
| 89 |
} |
| 90 |
enum t41 { |
| 91 |
a, |
| 92 |
b, |
| 93 |
c, |
| 94 |
} |
| 95 |
enum t42 { |
| 96 |
a, |
| 97 |
b, |
| 98 |
c, |
| 99 |
} |
| 100 |
type t43 = bool; |
| 101 |
type t44 = string; |
| 102 |
type t45 = list<list<list<t32>>>; |
| 103 |
type t46 = t44; |
| 104 |
type foo = bar; |
| 105 |
type bar = u32; |
| 106 |
type %f64 = f64; |
| 107 |
resource t50 { |
| 108 |
} |
| 109 |
resource t51 { |
| 110 |
/// create a new t51 |
| 111 |
constructor(a: u32); |
| 112 |
/// set a |
| 113 |
set-a: func(a: u32); |
| 114 |
/// get a |
| 115 |
get-a: func() -> u32; |
| 116 |
/// do b |
| 117 |
b: static func(); |
| 118 |
} |
| 119 |
} |
| 120 |
"}; |
| 121 |
|
| 122 |
#[test] |
| 123 |
fn types() { |
| 124 |
let name = wit_encoder::PackageName::new("wit-encoder", "tests", None); |
| 125 |
let mut package = wit_encoder::Package::new(name); |
| 126 |
package.interface({ |
| 127 |
let mut interface = wit_encoder::Interface::new("type-defs"); |
| 128 |
interface.set_docs(Some("interface documentation")); |
| 129 |
interface.type_def(TypeDef::type_("t1", Type::U8)); |
| 130 |
interface.type_def(TypeDef::type_("t2", Type::U16)); |
| 131 |
interface.type_def(TypeDef::type_("t3", Type::U32)); |
| 132 |
interface.type_def(TypeDef::type_("t4", Type::U64)); |
| 133 |
interface.type_def(TypeDef::type_("t5", Type::S8)); |
| 134 |
interface.type_def(TypeDef::type_("t6", Type::S16)); |
| 135 |
interface.type_def(TypeDef::type_("t7", Type::S32)); |
| 136 |
interface.type_def(TypeDef::type_("t8", Type::S64)); |
| 137 |
interface.type_def(TypeDef::type_("t9a", Type::F32)); |
| 138 |
interface.type_def(TypeDef::type_("t9b", Type::F32)); |
| 139 |
interface.type_def(TypeDef::type_("t10a", Type::F64)); |
| 140 |
interface.type_def(TypeDef::type_("t10b", Type::F64)); |
| 141 |
interface.type_def(TypeDef::type_("t11", Type::Char)); |
| 142 |
interface.type_def(TypeDef::type_("t12", Type::list(Type::Char))); |
| 143 |
interface.type_def(TypeDef::type_("t13", Type::String)); |
| 144 |
interface.type_def(TypeDef::type_("t14", Type::option(Type::U32))); |
| 145 |
interface.type_def(TypeDef::type_( |
| 146 |
"t15", |
| 147 |
Type::result_both(Type::U32, Type::U32), |
| 148 |
)); |
| 149 |
interface.type_def(TypeDef::type_("t16", Type::result(Result_::err(Type::U32)))); |
| 150 |
interface.type_def(TypeDef::type_("t17", Type::result(Result_::ok(Type::U32)))); |
| 151 |
interface.type_def({ |
| 152 |
let mut type_ = TypeDef::type_("t18", Type::result(Result_::empty())); |
| 153 |
type_.set_docs(Some("this is a documentation comment")); |
| 154 |
type_ |
| 155 |
}); |
| 156 |
|
| 157 |
interface.type_def(TypeDef::record("t20", Vec::<Field>::new())); |
| 158 |
interface.type_def(TypeDef::record("t21", [Field::new("a", Type::U32)])); |
| 159 |
interface.type_def(TypeDef::record("t22", [Field::new("a", Type::U32)])); |
| 160 |
interface.type_def(TypeDef::record( |
| 161 |
"t23", |
| 162 |
[Field::new("a", Type::U32), Field::new("b", Type::U64)], |
| 163 |
)); |
| 164 |
interface.type_def(TypeDef::record( |
| 165 |
"t24", |
| 166 |
[Field::new("a", Type::U32), Field::new("b", Type::U64)], |
| 167 |
)); |
| 168 |
interface.type_def(TypeDef::record("t25", [Field::new("x", Type::U32)])); |
| 169 |
interface.type_def(TypeDef::record("record", [Field::new("a", Type::U32)])); |
| 170 |
|
| 171 |
interface.type_def(TypeDef::type_("t26", Type::tuple(Vec::<Type>::new()))); |
| 172 |
interface.type_def(TypeDef::type_("t27", Type::tuple([Type::U32]))); |
| 173 |
interface.type_def(TypeDef::type_("t29", Type::tuple([Type::U32, Type::U64]))); |
| 174 |
|
| 175 |
interface.type_def(TypeDef::flags("t30", Vec::<Flag>::new())); |
| 176 |
interface.type_def(TypeDef::flags( |
| 177 |
"t31", |
| 178 |
[("a", "option a"), ("b", "option b"), ("c", "option c")], |
| 179 |
)); |
| 180 |
interface.type_def(TypeDef::flags("t32", [("a",), ("b",), ("c",)])); |
| 181 |
|
| 182 |
interface.type_def(TypeDef::variant("t33", [("a",)])); |
| 183 |
interface.type_def(TypeDef::variant("t34", [("a",), ("b",)])); |
| 184 |
interface.type_def(TypeDef::variant("t35", [("a",), ("b",)])); |
| 185 |
interface.type_def(TypeDef::new( |
| 186 |
"t36", |
| 187 |
TypeDefKind::Variant( |
| 188 |
[VariantCase::empty("a"), VariantCase::value("b", Type::U32)].into(), |
| 189 |
), |
| 190 |
)); |
| 191 |
interface.type_def(TypeDef::new( |
| 192 |
"t37", |
| 193 |
TypeDefKind::Variant( |
| 194 |
[ |
| 195 |
VariantCase::empty("a"), |
| 196 |
VariantCase::value("b", Type::option(Type::U32)), |
| 197 |
] |
| 198 |
.into(), |
| 199 |
), |
| 200 |
)); |
| 201 |
|
| 202 |
interface.type_def(TypeDef::enum_("t41", ["a", "b", "c"])); |
| 203 |
interface.type_def(TypeDef::enum_("t42", ["a", "b", "c"])); |
| 204 |
|
| 205 |
interface.type_def(TypeDef::type_("t43", Type::Bool)); |
| 206 |
interface.type_def(TypeDef::type_("t44", Type::String)); |
| 207 |
interface.type_def(TypeDef::type_( |
| 208 |
"t45", |
| 209 |
Type::list(Type::list(Type::list(Type::named("t32")))), |
| 210 |
)); |
| 211 |
interface.type_def(TypeDef::type_("t46", Type::named("t44"))); |
| 212 |
interface.type_def(TypeDef::type_("foo", Type::named("bar"))); |
| 213 |
interface.type_def(TypeDef::type_("bar", Type::U32)); |
| 214 |
|
| 215 |
interface.type_def(TypeDef::type_("f64", Type::F64)); |
| 216 |
|
| 217 |
interface.type_def(TypeDef::resource("t50", Vec::<ResourceFunc>::new())); |
| 218 |
interface.type_def(TypeDef::resource( |
| 219 |
"t51", |
| 220 |
[ |
| 221 |
{ |
| 222 |
let mut func = ResourceFunc::constructor(); |
| 223 |
func.set_params(Params::from_iter([("a", Type::U32)])); |
| 224 |
func.set_docs(Some("create a new t51")); |
| 225 |
func |
| 226 |
}, |
| 227 |
{ |
| 228 |
let mut func = ResourceFunc::method("set-a", false); |
| 229 |
func.set_params(Params::from_iter([("a", Type::U32)])); |
| 230 |
func.set_docs(Some("set a")); |
| 231 |
func |
| 232 |
}, |
| 233 |
{ |
| 234 |
let mut func = ResourceFunc::method("get-a", false); |
| 235 |
func.set_result(Some(Type::U32)); |
| 236 |
func.set_docs(Some("get a")); |
| 237 |
func |
| 238 |
}, |
| 239 |
{ |
| 240 |
let mut func = ResourceFunc::static_("b", false); |
| 241 |
func.set_docs(Some("do b")); |
| 242 |
func |
| 243 |
}, |
| 244 |
], |
| 245 |
)); |
| 246 |
interface |
| 247 |
}); |
| 248 |
assert_eq!(PACKAGE, package.to_string()); |
| 249 |
} |