Skip to content

Commit

Permalink
Add set name to resources test
Browse files Browse the repository at this point in the history
  • Loading branch information
kajacx committed May 3, 2024
1 parent 9c294c4 commit 6e904cf
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 24 deletions.
24 changes: 21 additions & 3 deletions tests/wit_components/resources/guest.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use std::cell::Cell;

wit_bindgen::generate!({
path: "../protocol.wit",
world: "resources",
});

use component_test::wit_protocol::companies::Company;
use exports::component_test::wit_protocol::employees::Employee;
use exports::component_test::wit_protocol::employees::GuestEmployee;

struct MyEmployees;

Expand All @@ -14,10 +17,16 @@ impl exports::component_test::wit_protocol::employees::Guest for MyEmployees {

impl exports::component_test::wit_protocol::guest_fns::Guest for MyEmployees {
fn company_roundtrip(company: Company) -> Company {
let name = company.get_name();
let name = name + " round";
company.set_name(&name);
component_test::wit_protocol::host_fns::company_roundtrip(company)
}

fn employee_roundtrip(employee: Employee) -> Employee {
let name = employee.get::<MyEmployee>().get_name();
let name = name + " round trip";
employee.get::<MyEmployee>().set_name(name);
employee
}

Expand All @@ -29,17 +38,26 @@ impl exports::component_test::wit_protocol::guest_fns::Guest for MyEmployees {
}

pub struct MyEmployee {
name: String,
name: Cell<String>,
min_salary: u32,
}

impl exports::component_test::wit_protocol::employees::GuestEmployee for MyEmployee {
fn new(name: String, min_salary: u32) -> Self {
Self { name, min_salary }
Self {
name: Cell::new(name),
min_salary,
}
}

fn get_name(&self) -> String {
self.name.clone()
let name = self.name.take();
self.name.set(name.clone());
name
}

fn set_name(&self, name: String) {
self.name.set(name)
}

fn get_min_salary(&self) -> u32 {
Expand Down
65 changes: 44 additions & 21 deletions tests/wit_components/resources/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ impl State {
self.resources.get(company).unwrap()
}

fn get_company_mut(&mut self, company: &Resource<MyCompany>) -> &mut MyCompany {
self.resources.get_mut(company).unwrap()
}

fn drop_company(&mut self, company: Resource<MyCompany>) {
self.resources.delete(company).unwrap();
}
Expand All @@ -45,6 +49,11 @@ impl component_test::wit_protocol::companies::HostCompany for State {
Ok(self.get_company(&self_).name.clone())
}

fn set_name(&mut self, self_: Resource<MyCompany>, name: String) -> Result<()> {
self.get_company_mut(&self_).name = name;
Ok(())
}

fn get_max_salary(&mut self, self_: Resource<MyCompany>) -> Result<u32> {
Ok(self.get_company(&self_).max_salary)
}
Expand All @@ -58,6 +67,7 @@ impl component_test::wit_protocol::companies::Host for State {}

impl component_test::wit_protocol::host_fns::Host for State {
fn company_roundtrip(&mut self, company: Resource<MyCompany>) -> Result<Resource<MyCompany>> {
self.get_company_mut(&company).name += " trip";
Ok(company)
}
}
Expand All @@ -70,15 +80,44 @@ pub fn run_test(component_bytes: &[u8]) -> Result<()> {
let mut store = Store::new(&engine, State::default());

#[allow(deprecated)]
let component = Component::new(&store.engine(), &component_bytes).expect("create component");
let component = Component::new(&store.engine(), &component_bytes).unwrap();

let mut linker = Linker::new(store.engine());
Resources::add_to_linker(&mut linker, |state| state).unwrap();

#[allow(deprecated)]
let (instance, _) = Resources::instantiate(&mut store, &component, &linker).unwrap();

let employees = instance.component_test_wit_protocol_employees().employee();
let guest_fns = instance.component_test_wit_protocol_guest_fns();

// Company roundtrip
let company = store.data_mut().new_company(MyCompany {
name: "CompanyName".into(),
max_salary: 30_000,
});

let result = guest_fns
.call_company_roundtrip(&mut store, company)
.unwrap();
assert_eq!(
store.data().get_company(&result).name,
"CompanyName round trip"
);
store.data_mut().drop_company(result);

// Employee roundtrip
let employee = employees
.call_constructor(&mut store, "EmployeeName".into(), 50_000)
.unwrap();
let result = guest_fns
.call_employee_roundtrip(&mut store, employee)
.unwrap();
assert_eq!(
employees.call_get_name(&mut store, result).unwrap(),
"EmployeeName round trip"
);

// Find job - no job found
let employee = employees
.call_constructor(&mut store, "Mike".into(), 50_000)
.unwrap();
Expand All @@ -92,12 +131,12 @@ pub fn run_test(component_bytes: &[u8]) -> Result<()> {
max_salary: 30_000,
});

let result = instance
.component_test_wit_protocol_guest_fns()
let result = guest_fns
.call_find_job(&mut store, employee, &[company1])
.unwrap();
assert!(result.is_none());

// Find job - job found
let employee = employees
.call_constructor(&mut store, "Mike".into(), 50_000)
.unwrap();
Expand All @@ -111,29 +150,13 @@ pub fn run_test(component_bytes: &[u8]) -> Result<()> {
max_salary: 60_000,
});

let result = instance
.component_test_wit_protocol_guest_fns()
let result = guest_fns
.call_find_job(&mut store, employee, &[company1, company2])
.unwrap() // WASM call
.unwrap(); // Option return type
assert_eq!(store.data().get_company(&result).name, "Company2");
store.data_mut().drop_company(result);

let company = store.data_mut().new_company(MyCompany {
name: "MyCompany Roundtrip".into(),
max_salary: 30_000,
});

let result = instance
.component_test_wit_protocol_guest_fns()
.call_company_roundtrip(&mut store, company)
.unwrap();
assert_eq!(
store.data().get_company(&result).name,
"MyCompany Roundtrip"
);
store.data_mut().drop_company(result);

// TODO: assert that all resources have been deleted

Ok(())
Expand Down
2 changes: 2 additions & 0 deletions tests/wit_components/resources/protocol.wit
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ interface companies {
resource company {
constructor(name: string, max-salary: u32);
get-name: func() -> string;
set-name: func(name: string);
get-max-salary: func() -> u32;
}
}
Expand All @@ -22,6 +23,7 @@ interface employees {
resource employee {
constructor(name: string, min-salary: u32);
get-name: func() -> string;
set-name: func(name: string);
get-min-salary: func() -> u32;
}
}
Expand Down

0 comments on commit 6e904cf

Please sign in to comment.