Skip to content

Commit 8aa2cc2

Browse files
authored
Migrate resources test to wit-bindgen test (#1266)
I've started mimicking the pattern of some of the more complicated resource tests where I copy the exact test files as before and configure the test to not need any modifications there. Not exactly the easiest way to write the test and probably testing too much but it least makes these diffs smaller and/or easier to make.
1 parent 10a3c27 commit 8aa2cc2

File tree

13 files changed

+166
-290
lines changed

13 files changed

+166
-290
lines changed

crates/test-rust-wasm/Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,3 @@ test = false
1818
[[bin]]
1919
name = "options"
2020
test = false
21-
22-
[[bin]]
23-
name = "resources"
24-
test = false

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

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package example:composition;
2+
3+
let leaf = new test:leaf { ... };
4+
let resources = new test:resources {
5+
imports: leaf.imports,
6+
...
7+
};
8+
let runner = new test:runner {
9+
exports: resources.exports,
10+
...
11+
};
12+
13+
export runner...;
14+

tests/runtime-new/resources/leaf.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
include!(env!("BINDINGS"));
2+
3+
use crate::exports::imports::{Guest, GuestY, Y};
4+
use std::cell::Cell;
5+
6+
struct Component;
7+
8+
export!(Component);
9+
10+
struct MyY(Cell<i32>);
11+
12+
impl Guest for Component {
13+
type Y = MyY;
14+
}
15+
16+
impl GuestY for MyY {
17+
fn new(a: i32) -> MyY {
18+
MyY(Cell::new(a))
19+
}
20+
21+
fn get_a(&self) -> i32 {
22+
self.0.get()
23+
}
24+
25+
fn set_a(&self, a: i32) {
26+
self.0.set(a);
27+
}
28+
29+
fn add(y: Y, a: i32) -> Y {
30+
Y::new(MyY::new(y.get::<MyY>().0.get() + a))
31+
}
32+
}

tests/runtime/resources/wasm.rs renamed to tests/runtime-new/resources/resources.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
use std::cell::RefCell;
2-
3-
wit_bindgen::generate!({
4-
path: "../../tests/runtime/resources",
5-
});
1+
include!(env!("BINDINGS"));
62

73
use exports::exports::{KebabCase, ZBorrow, X, Z};
4+
use std::cell::RefCell;
85

96
pub struct Test {}
107

tests/runtime-new/resources/runner.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
include!(env!("BINDINGS"));
2+
3+
use crate::exports::*;
4+
5+
fn main() {
6+
test_imports().unwrap();
7+
8+
let x = X::new(5);
9+
assert_eq!(x.get_a(), 5);
10+
x.set_a(10);
11+
assert_eq!(x.get_a(), 10);
12+
let z1 = Z::new(10);
13+
assert_eq!(z1.get_a(), 10);
14+
let z2 = Z::new(20);
15+
assert_eq!(z2.get_a(), 20);
16+
17+
let xadd = X::add(x, 5);
18+
assert_eq!(xadd.get_a(), 15);
19+
20+
let zadd = add(&z1, &z2);
21+
assert_eq!(zadd.get_a(), 30);
22+
23+
let dropped_zs_start = Z::num_dropped();
24+
25+
drop(z1);
26+
drop(z2);
27+
28+
consume(xadd);
29+
30+
let dropped_zs_end = Z::num_dropped();
31+
if dropped_zs_start != 0 {
32+
assert_eq!(dropped_zs_end, dropped_zs_start + 2);
33+
}
34+
}

tests/runtime-new/resources/test.wit

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//@ dependencies = ['resources', 'leaf']
2+
//@ wac = 'compose.wac'
3+
4+
package test:resources;
5+
6+
world leaf {
7+
export imports: interface {
8+
resource y {
9+
constructor(a: s32);
10+
get-a: func() -> s32;
11+
set-a: func(a: s32);
12+
add: static func(y: y, a: s32) -> y;
13+
}
14+
}
15+
}
16+
17+
world resources {
18+
import imports: interface {
19+
resource y {
20+
constructor(a: s32);
21+
get-a: func() -> s32;
22+
set-a: func(a: s32);
23+
add: static func(y: y, a: s32) -> y;
24+
}
25+
}
26+
27+
export exports: interface {
28+
resource x {
29+
constructor(a: s32);
30+
get-a: func() -> s32;
31+
set-a: func(a: s32);
32+
add: static func(x: x, a: s32) -> x;
33+
}
34+
35+
resource z {
36+
constructor(a: s32);
37+
get-a: func() -> s32;
38+
39+
num-dropped: static func() -> u32;
40+
}
41+
42+
add: func(a: borrow<z>, b: borrow<z>) -> own<z>;
43+
44+
consume: func(x: x);
45+
46+
resource kebab-case {
47+
constructor(a: u32);
48+
get-a: func() -> u32;
49+
take-owned: static func(k: own<kebab-case>) -> u32;
50+
}
51+
52+
test-imports: func() -> result<_, string>;
53+
}
54+
}
55+
56+
world runner {
57+
import exports: interface {
58+
resource x {
59+
constructor(a: s32);
60+
get-a: func() -> s32;
61+
set-a: func(a: s32);
62+
add: static func(x: x, a: s32) -> x;
63+
}
64+
65+
resource z {
66+
constructor(a: s32);
67+
get-a: func() -> s32;
68+
69+
num-dropped: static func() -> u32;
70+
}
71+
72+
add: func(a: borrow<z>, b: borrow<z>) -> own<z>;
73+
74+
consume: func(x: x);
75+
76+
resource kebab-case {
77+
constructor(a: u32);
78+
get-a: func() -> u32;
79+
take-owned: static func(k: own<kebab-case>) -> u32;
80+
}
81+
82+
test-imports: func() -> result<_, string>;
83+
}
84+
}

tests/runtime/main.rs

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

1616
mod options;
17-
mod resources;
1817
mod results;
1918

2019
struct MyCtx {}

0 commit comments

Comments
 (0)