Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,9 @@ impl<'a> UnimplementedFunction<'a> {
}
TypeDefKind::Tuple(t) => {
source.push('(');
for (i, ty) in t.types.iter().enumerate() {
if i > 0 {
source.push_str(", ");
}
for ty in t.types.iter() {
self.print_type(ty, trie, source)?;
source.push_str(", ");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead, can we rewrite the loop body above this statement to be:

self.print_type(ty, trie, source)?;
source.push_str(", ");

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely! Should we remove the training comma for multi-element tuples or leave it for a formatter to take care of?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The formatter will take care of it, and its valid syntax even if not, so I think its fine to leave it in

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect sounds good

source.push(')');
}
Expand Down
56 changes: 56 additions & 0 deletions tests/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,59 @@ fn it_supports_the_proxy_option() -> Result<()> {

Ok(())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_single_element_tuple_generation() -> Result<()> {
let (server, config, _task) = spawn_server(["test"]).await?;

publish_wit(
config,
"test:tuples",
"1.0.0",
r#"package test:tuples@1.0.0;

world test-world {
export test-interface: interface {
test-function: func(params: tuple<string>) -> result<tuple<u32>, string>;
another-function: func(single: tuple<bool>) -> tuple<list<string>>;
}
}
"#,
)
.await?;

let project = server.project("test-tuple", true, ["--target", "test:tuples@1.0.0"])?;

project
.cargo_component(["build"])
.assert()
.stderr(contains(
"Finished `dev` profile [unoptimized + debuginfo] target(s)",
))
.success();

let lib_rs = fs::read_to_string(project.root().join("src/lib.rs"))?;

assert!(
lib_rs.contains("params: (String,)"),
"Single-element tuple parameter should have trailing comma. Generated code:\n{}",
lib_rs
);
assert!(
lib_rs.contains("Result<(u32,), String>") || lib_rs.contains("-> (u32,)"),
"Single-element tuple return should have trailing comma. Generated code:\n{}",
lib_rs
);
assert!(
lib_rs.contains("single: (bool,)"),
"Single-element tuple parameter should have trailing comma. Generated code:\n{}",
lib_rs
);
assert!(
lib_rs.contains("-> (Vec<String>,)"),
"Single-element tuple return should have trailing comma. Generated code:\n{}",
lib_rs
);

Ok(())
}
Loading