Skip to content

Commit 3e38098

Browse files
authored
Initialize a test suite for wit-encoder (#1626)
* change `wit-encoder` indent to 2 * test empty `Result`s in `wit-encoder` * import wit test files for `wit-encoder` * use external wit test file for `functions.rs` test * add `empty` test * add `wit-encoder` overlap tests * fix more existing tests * Add `include` capabilities to `wit-encoder` * tidy up * rebase on main * check in `.gitattributes` to normalize line endings Rebasing brok * remove additional wit files * inline tests
1 parent 8afdf05 commit 3e38098

18 files changed

Lines changed: 314 additions & 134 deletions

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
* text=auto
12
*.waves text eol=lf
23
*.out text eol=lf

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ bitflags = "2.5.0"
8484
hashbrown = { version = "0.14.3", default-features = false, features = ['ahash'] }
8585
ahash = { version = "0.8.11", default-features = false }
8686
termcolor = "1.2.0"
87+
indoc = "2.0.5"
8788

8889
wasm-compose = { version = "0.211.1", path = "crates/wasm-compose" }
8990
wasm-encoder = { version = "0.211.1", path = "crates/wasm-encoder" }

crates/wit-encoder/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ workspace = true
1313
[dependencies]
1414
semver = { workspace = true }
1515
pretty_assertions = { workspace = true }
16+
17+
[dev-dependencies]
18+
indoc = { workspace = true }

crates/wit-encoder/src/function.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ where
108108
impl Results {
109109
// For the common case of an empty results list.
110110
pub fn empty() -> Results {
111-
Results::Named(Default::default())
111+
Results::Named(Params::empty())
112112
}
113113

114114
pub fn anon(type_: Type) -> Results {
@@ -166,3 +166,14 @@ impl StandaloneFunc {
166166
self.docs = docs.map(|d| d.into());
167167
}
168168
}
169+
170+
#[cfg(test)]
171+
mod test {
172+
use crate::Results;
173+
174+
#[test]
175+
fn is_empty() {
176+
let res = Results::empty();
177+
assert!(res.is_empty());
178+
}
179+
}

crates/wit-encoder/src/include.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use std::fmt;
2+
3+
use crate::{Ident, Render};
4+
5+
/// Enable the union of a world with another world
6+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
7+
pub struct Include {
8+
use_path: Ident,
9+
include_names_list: Vec<String>,
10+
}
11+
12+
impl Include {
13+
pub fn new(use_path: impl Into<Ident>) -> Self {
14+
Self {
15+
use_path: use_path.into(),
16+
include_names_list: vec![],
17+
}
18+
}
19+
}
20+
21+
impl Render for Include {
22+
fn render(&self, f: &mut fmt::Formatter<'_>, opts: &crate::RenderOpts) -> fmt::Result {
23+
write!(f, "{}include {};\n", opts.spaces(), self.use_path)?;
24+
Ok(())
25+
}
26+
}

crates/wit-encoder/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod enum_;
88
mod flags;
99
mod function;
1010
mod ident;
11+
mod include;
1112
mod interface;
1213
mod package;
1314
mod record;
@@ -24,6 +25,7 @@ pub use enum_::*;
2425
pub use flags::*;
2526
pub use function::*;
2627
pub use ident::*;
28+
pub use include::*;
2729
pub use interface::*;
2830
pub use package::*;
2931
pub use record::*;

crates/wit-encoder/src/package.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,21 @@ impl Package {
4141
impl Render for Package {
4242
fn render(&self, f: &mut fmt::Formatter<'_>, opts: &RenderOpts) -> fmt::Result {
4343
write!(f, "{}package {};\n", opts.spaces(), self.name)?;
44-
write!(f, "\n")?;
4544
for item in &self.items {
45+
write!(f, "\n")?;
4646
match item {
4747
PackageItem::Interface(interface) => {
4848
if let Some(docs) = &interface.docs {
4949
docs.render(f, opts)?;
5050
}
51-
write!(f, "{}interface {} {{\n", opts.spaces(), interface.name)?;
52-
interface.items.render(f, &opts.indent())?;
53-
write!(f, "{}}}\n", opts.spaces())?;
51+
write!(f, "{}interface {} {{", opts.spaces(), interface.name)?;
52+
if !interface.items.is_empty() {
53+
write!(f, "\n")?;
54+
interface.items.render(f, &opts.indent())?;
55+
write!(f, "{}}}\n", opts.spaces())?;
56+
} else {
57+
write!(f, "}}\n")?;
58+
}
5459
}
5560
PackageItem::World(world) => {
5661
world.render(f, opts)?;

crates/wit-encoder/src/render.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub struct RenderOpts {
1111
impl Default for RenderOpts {
1212
fn default() -> Self {
1313
Self {
14-
indent_width: 4,
14+
indent_width: 2,
1515
ident_count: 0,
1616
}
1717
}

crates/wit-encoder/src/ty.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,11 @@ impl Render for TypeDef {
317317
if let Some(docs) = &self.docs {
318318
docs.render(f, opts)?;
319319
}
320-
write!(f, "{}record {} {{\n", opts.spaces(), self.name)?;
321-
for field in &record.fields {
320+
write!(f, "{}record {} {{", opts.spaces(), self.name)?;
321+
for (index, field) in record.fields.iter().enumerate() {
322+
if index == 0 {
323+
write!(f, "\n")?;
324+
}
322325
let opts = opts.indent();
323326
if let Some(docs) = &field.docs {
324327
docs.render(f, &opts)?;

0 commit comments

Comments
 (0)