Skip to content

Commit c581498

Browse files
authored
Migrate ownership test to wit-bindgen test (#1255)
Nice test to make use of many-runners-but-one-test.
1 parent f1bdaa7 commit c581498

File tree

14 files changed

+179
-250
lines changed

14 files changed

+179
-250
lines changed

crates/test-rust-wasm/Cargo.toml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,6 @@ test = false
2323
name = "options"
2424
test = false
2525

26-
[[bin]]
27-
name = "owning"
28-
test = false
29-
30-
[[bin]]
31-
name = "borrowing"
32-
test = false
33-
34-
[[bin]]
35-
name = "borrowing-duplicate-if-necessary"
36-
test = false
37-
3826
[[bin]]
3927
name = "resources"
4028
test = false

crates/test-rust-wasm/src/bin/borrowing-duplicate-if-necessary.rs

Lines changed: 0 additions & 3 deletions
This file was deleted.

crates/test-rust-wasm/src/bin/borrowing.rs

Lines changed: 0 additions & 3 deletions
This file was deleted.

crates/test-rust-wasm/src/bin/owning.rs

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//@ args = '--ownership borrowing-duplicate-if-necessary'
2+
3+
include!(env!("BINDINGS"));
4+
5+
impl PartialEq for thing_in_and_out::ThingResult {
6+
fn eq(&self, other: &Self) -> bool {
7+
self.name == other.name && self.value == other.value
8+
}
9+
}
10+
11+
fn main() {
12+
let value = &[&["foo", "bar"] as &[_]] as &[_];
13+
assert_eq!(
14+
vec![vec!["foo".to_owned(), "bar".to_owned()]],
15+
lists::foo(value)
16+
);
17+
18+
thing_in::bar(thing_in::Thing {
19+
name: "thing 1",
20+
value: &["some value", "another value"],
21+
});
22+
23+
let value = thing_in_and_out::ThingParam {
24+
name: "thing 1",
25+
value: &["some value", "another value"],
26+
};
27+
assert_eq!(
28+
thing_in_and_out::ThingResult {
29+
name: "thing 1".to_owned(),
30+
value: vec!["some value".to_owned(), "another value".to_owned()],
31+
},
32+
thing_in_and_out::baz(value)
33+
);
34+
35+
let strings = vec!["foo", "bar", "baz"];
36+
let resource = test::ownership::both_list_and_resource::TheResource::new(&strings);
37+
test::ownership::both_list_and_resource::list_and_resource(
38+
test::ownership::both_list_and_resource::Thing {
39+
a: strings.iter().map(|s| s.to_string()).collect(),
40+
b: resource,
41+
},
42+
);
43+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//@ args = '--ownership borrowing'
2+
3+
include!(env!("BINDINGS"));
4+
5+
impl PartialEq for thing_in_and_out::Thing {
6+
fn eq(&self, other: &Self) -> bool {
7+
self.name == other.name && self.value == other.value
8+
}
9+
}
10+
11+
fn main() {
12+
let value = &[&["foo", "bar"] as &[_]] as &[_];
13+
assert_eq!(
14+
vec![vec!["foo".to_owned(), "bar".to_owned()]],
15+
lists::foo(value)
16+
);
17+
18+
thing_in::bar(thing_in::Thing {
19+
name: "thing 1",
20+
value: &["some value", "another value"],
21+
});
22+
23+
let value = thing_in_and_out::Thing {
24+
name: "thing 1".to_owned(),
25+
value: vec!["some value".to_owned(), "another value".to_owned()],
26+
};
27+
assert_eq!(value, thing_in_and_out::baz(&value));
28+
29+
let strings = vec!["foo", "bar", "baz"];
30+
let resource = test::ownership::both_list_and_resource::TheResource::new(&strings);
31+
test::ownership::both_list_and_resource::list_and_resource(
32+
test::ownership::both_list_and_resource::Thing {
33+
a: strings.iter().map(|s| s.to_string()).collect(),
34+
b: resource,
35+
},
36+
);
37+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//@ args = '--ownership owning'
2+
3+
include!(env!("BINDINGS"));
4+
impl PartialEq for thing_in_and_out::Thing {
5+
fn eq(&self, other: &Self) -> bool {
6+
self.name == other.name && self.value == other.value
7+
}
8+
}
9+
10+
fn main() {
11+
let value = vec![vec!["foo".to_owned(), "bar".to_owned()]];
12+
assert_eq!(value, lists::foo(&value));
13+
14+
thing_in::bar(&thing_in::Thing {
15+
name: "thing 1".to_owned(),
16+
value: vec!["some value".to_owned(), "another value".to_owned()],
17+
});
18+
19+
let value = thing_in_and_out::Thing {
20+
name: "thing 1".to_owned(),
21+
value: vec!["some value".to_owned(), "another value".to_owned()],
22+
};
23+
assert_eq!(value, thing_in_and_out::baz(&value));
24+
25+
let strings = vec!["foo".to_string(), "bar".to_string(), "baz".to_string()];
26+
let resource = test::ownership::both_list_and_resource::TheResource::new(&strings);
27+
test::ownership::both_list_and_resource::list_and_resource(
28+
test::ownership::both_list_and_resource::Thing {
29+
a: strings,
30+
b: resource,
31+
},
32+
);
33+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
include!(env!("BINDINGS"));
2+
3+
use crate::exports::test::ownership::both_list_and_resource;
4+
use crate::exports::{lists, thing_in, thing_in_and_out};
5+
6+
struct Component;
7+
8+
export!(Component);
9+
10+
struct MyResource(Vec<String>);
11+
12+
impl lists::Guest for Component {
13+
fn foo(list: Vec<Vec<String>>) -> Vec<Vec<String>> {
14+
list
15+
}
16+
}
17+
18+
impl thing_in::Guest for Component {
19+
fn bar(_value: thing_in::Thing) {}
20+
}
21+
22+
impl thing_in_and_out::Guest for Component {
23+
fn baz(value: thing_in_and_out::Thing) -> thing_in_and_out::Thing {
24+
value
25+
}
26+
}
27+
28+
impl both_list_and_resource::Guest for Component {
29+
type TheResource = MyResource;
30+
31+
fn list_and_resource(value: both_list_and_resource::Thing) {
32+
assert_eq!(value.a, value.b.get::<MyResource>().0);
33+
}
34+
}
35+
36+
impl both_list_and_resource::GuestTheResource for MyResource {
37+
fn new(list: Vec<String>) -> MyResource {
38+
MyResource(list)
39+
}
40+
}

tests/runtime/ownership/world.wit renamed to tests/runtime-new/rust/ownership/test.wit

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ interface both-list-and-resource {
1212
list-and-resource: func(a: thing);
1313
}
1414

15-
world ownership {
15+
world runner {
1616
import lists: interface {
1717
foo: func(a: list<list<string>>) -> list<list<string>>;
1818
}
@@ -36,6 +36,30 @@ world ownership {
3636
}
3737

3838
import both-list-and-resource;
39+
}
40+
41+
world test {
42+
export lists: interface {
43+
foo: func(a: list<list<string>>) -> list<list<string>>;
44+
}
45+
46+
export thing-in: interface {
47+
record thing {
48+
name: string,
49+
value: list<string>
50+
}
51+
52+
bar: func(a: thing);
53+
}
54+
55+
export thing-in-and-out: interface {
56+
record thing {
57+
name: string,
58+
value: list<string>
59+
}
60+
61+
baz: func(a: thing) -> thing;
62+
}
3963

40-
export foo: func();
64+
export both-list-and-resource;
4165
}

tests/runtime/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use wit_parser::{Resolve, WorldId, WorldItem};
1515

1616
mod flavorful;
1717
mod options;
18-
mod ownership;
1918
mod resource_alias;
2019
mod resource_floats;
2120
mod resource_import_and_export;

0 commit comments

Comments
 (0)