Skip to content

Commit dba6e3b

Browse files
authored
Migrate resource_floats test to wit-bindgen test (#1261)
* Migrate `resource_floats` test to `wit-bindgen test` * More migration
1 parent 4536acf commit dba6e3b

File tree

11 files changed

+140
-151
lines changed

11 files changed

+140
-151
lines changed

crates/test-rust-wasm/Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ test = false
2727
name = "resources"
2828
test = false
2929

30-
[[bin]]
31-
name = "resource_floats"
32-
test = false
33-
3430
[[bin]]
3531
name = "resource_with_lists"
3632
test = false

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

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

tests/runtime/resource_floats/wasm.cs renamed to tests/runtime-new/resource_floats/intermediate.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using Import1 = ResourceFloatsWorld.wit.imports.IImports;
2-
using Import2 = ResourceFloatsWorld.wit.imports.test.resourceFloats.ITest;
1+
using Import1 = IntermediateWorld.wit.imports.IImports;
2+
using Import2 = IntermediateWorld.wit.imports.test.resourceFloats.ITest;
33

4-
namespace ResourceFloatsWorld.wit.exports
4+
namespace IntermediateWorld.wit.exports
55
{
66
public class ExportsImpl : IExports {
77
public class Float : IExports.Float, IExports.IFloat {
@@ -22,8 +22,8 @@ public static IExports.Float Add(IExports.Float a, double b) {
2222
}
2323
}
2424

25-
namespace ResourceFloatsWorld {
26-
public class ResourceFloatsWorldImpl : IResourceFloatsWorld {
25+
namespace IntermediateWorld {
26+
public class IntermediateWorldImpl : IIntermediateWorld {
2727
public static Import2.Float Add(Import2.Float a, Import2.Float b) {
2828
return new Import2.Float(a.Get() + b.Get() + 5.0);
2929
}

tests/runtime/resource_floats/wasm.rs renamed to tests/runtime-new/resource_floats/intermediate.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
wit_bindgen::generate!({
2-
path: "../../tests/runtime/resource_floats",
3-
});
1+
include!(env!("BINDINGS"));
42

53
use exports::exports::{Float as FloatExport, GuestFloat};
64
use imports::Float as ImportFloat1;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
include!(env!("BINDINGS"));
2+
3+
use exports::imports::{Float as ImportFloat1, Guest, GuestFloat};
4+
use exports::test::resource_floats::test::{Guest as Guest2, GuestFloat as GuestFloat2};
5+
6+
struct Component;
7+
8+
export!(Component);
9+
10+
#[derive(Default)]
11+
pub struct MyFloat(f64);
12+
13+
impl Guest for Component {
14+
type Float = MyFloat;
15+
}
16+
17+
impl GuestFloat for MyFloat {
18+
fn new(v: f64) -> MyFloat {
19+
MyFloat(v + 2.0)
20+
}
21+
22+
fn get(&self) -> f64 {
23+
self.0 + 4.0
24+
}
25+
26+
fn add(a: ImportFloat1, b: f64) -> ImportFloat1 {
27+
ImportFloat1::new(<MyFloat as GuestFloat>::new(a.get::<MyFloat>().0 + b + 6.0))
28+
}
29+
}
30+
31+
impl Guest2 for Component {
32+
type Float = MyFloat;
33+
}
34+
35+
impl GuestFloat2 for MyFloat {
36+
fn new(v: f64) -> MyFloat {
37+
MyFloat(v + 1.0)
38+
}
39+
40+
fn get(&self) -> f64 {
41+
self.0 + 3.0
42+
}
43+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
include!(env!("BINDINGS"));
2+
3+
use exports::Float as Float2;
4+
5+
fn main() {
6+
let float3 = add(&Float::new(42.0), &Float::new(55.0));
7+
assert_eq!(float3.get(), 114.0);
8+
9+
let float3 = Float2::new(22.0);
10+
assert_eq!(float3.get(), 22. + 1. + 2. + 4. + 3.);
11+
12+
let res = Float2::add(float3, 7.0);
13+
assert_eq!(res.get(), 59.0);
14+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//@ dependencies = ['intermediate', 'leaf']
2+
//@ wac = 'compose.wac'
3+
4+
package test:resource-floats;
5+
6+
interface test {
7+
resource float {
8+
constructor(v: f64);
9+
get: func() -> f64;
10+
}
11+
}
12+
13+
world leaf {
14+
export test;
15+
16+
export imports: interface {
17+
resource float {
18+
constructor(v: f64);
19+
get: func() -> f64;
20+
add: static func(a: float, b: f64) -> float;
21+
}
22+
}
23+
}
24+
25+
world intermediate {
26+
use test.{float};
27+
28+
export exports: interface {
29+
resource float {
30+
constructor(v: f64);
31+
get: func() -> f64;
32+
add: static func(a: float, b: f64) -> float;
33+
}
34+
}
35+
36+
import imports: interface {
37+
resource float {
38+
constructor(v: f64);
39+
get: func() -> f64;
40+
add: static func(a: float, b: f64) -> float;
41+
}
42+
}
43+
44+
export add: func(a: borrow<float>, b: borrow<float>) -> own<float>;
45+
}
46+
47+
world runner {
48+
use test.{float};
49+
50+
import exports: interface {
51+
resource float {
52+
constructor(v: f64);
53+
get: func() -> f64;
54+
add: static func(a: float, b: f64) -> float;
55+
}
56+
}
57+
58+
import add: func(a: borrow<float>, b: borrow<float>) -> own<float>;
59+
}

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 resource_floats;
1918
mod resource_with_lists;
2019
mod resources;
2120
mod results;

tests/runtime/resource_floats.rs

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

0 commit comments

Comments
 (0)