forked from mozilla/uniffi-rs
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This comes from my experiences with mozilla#2333 and uniffi-bindgen-gecko-js. For those, I needed to run a lot of tests while (re)implementing a bindings generator. I love how the current tests cover basically all UniFFI features, but I think there a few things that can be improved. This commit adds a suite of tests specifically aimed at the bindings. The new tests target specific features rather than trying to mimic real-world examples. Before, I spent a lot of time trying to figure out what a test failure meant. I'm hoping that when one of these tests fail, it's obvious what's not working. It also means that the new test suites don't require writing so much foreign code. They also have a better external bindings story. I'm thinking we can direct external buindings authors to copy everything inside the `bindings-tests` directory and use that to test their bindings. They can use the test macro now, since it's doesn't hard code the mapping from file extension to test runner. Copying the code feels a bit weird, but I like it better than having to publish all these test fixture crates and I think it will work okay in practice. I still want to keep the current examples/fixtures. They're nice because they showcase real-world use-cases and test that those use-cases work like we expect them to. If this gets merged, we can rework those crates to focus on that. Maybe we rework them to only test one bindings language per example/fixture.
- Loading branch information
Showing
16 changed files
with
283 additions
and
19 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "bindings-tests" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
uniffi = { path = "../uniffi", features = ["bindgen-tests"] } | ||
|
||
[features] | ||
ffi-trace = ["uniffi/ffi-trace"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "uniffi-fixture-fn-calls" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
uniffi = { workspace = true } | ||
|
||
[features] | ||
ffi-trace = ["uniffi/ffi-trace"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
//! Extremely simple fixture to test making scaffolding calls without any arguments or return types | ||
//! | ||
//! The test is if the bindings can make a call to `test_func`. If in doubt, run the tests with | ||
//! `--features=ffi-trace` to check that the function is actually called. | ||
#[uniffi::export] | ||
pub fn test_func() {} | ||
|
||
uniffi::setup_scaffolding!("fn_calls"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "uniffi-fixture-primitive-types" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
uniffi = { workspace = true } | ||
|
||
[features] | ||
ffi-trace = ["uniffi/ffi-trace"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//! Test lifting/lowering primitive types | ||
// Simple tests | ||
|
||
#[uniffi::export] | ||
pub fn roundtrip(a: u32) -> u32 { | ||
a | ||
} | ||
|
||
#[uniffi::export] | ||
pub fn roundtrip_bool(a: bool) -> bool { | ||
a | ||
} | ||
|
||
#[uniffi::export] | ||
pub fn roundtrip_string(a: String) -> String { | ||
a | ||
} | ||
|
||
/// Complex test: input a bunch of different values and add them together | ||
#[uniffi::export] | ||
pub fn sum( | ||
a: u8, | ||
b: i8, | ||
c: u16, | ||
d: i16, | ||
e: u32, | ||
f: i32, | ||
g: u64, | ||
h: i64, | ||
i: f32, | ||
j: f64, | ||
negate: bool, | ||
) -> f64 { | ||
let all_values = [ | ||
a as f64, b as f64, c as f64, d as f64, e as f64, f as f64, g as f64, h as f64, i as f64, j, | ||
]; | ||
let sum: f64 = all_values.into_iter().sum(); | ||
if negate { | ||
-sum | ||
} else { | ||
sum | ||
} | ||
} | ||
|
||
uniffi::setup_scaffolding!("primitive_types"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
import fn_calls | ||
import unittest | ||
|
||
class FnCallsTest(unittest.TestCase): | ||
def test_fn_call(self): | ||
fn_calls.test_func() | ||
|
||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
import primitive_types | ||
import unittest | ||
|
||
class FnCallsTest(unittest.TestCase): | ||
def test_roundtrip(self): | ||
self.assertEqual(primitive_types.roundtrip(0), 0) | ||
self.assertEqual(primitive_types.roundtrip(42), 42) | ||
|
||
def test_roundtrip_bool(self): | ||
self.assertEqual(primitive_types.roundtrip_bool(True), True) | ||
self.assertEqual(primitive_types.roundtrip_bool(False), False) | ||
|
||
def test_roundtrip_string(self): | ||
self.assertEqual(primitive_types.roundtrip_string("Hello"), "Hello") | ||
|
||
def test_sum(self): | ||
self.assertEqual(primitive_types.sum(1, -1, 2, -2, 3, -3, 4, -4, 0.5, 1.5, True), -2.0) | ||
|
||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use uniffi::deps::anyhow::Result; | ||
|
||
uniffi::bindings_tests!( | ||
py: run_python_test, | ||
// TODO Kotlin/Swift/Ruby | ||
); | ||
|
||
pub fn run_python_test(tmp_dir: &str, fixture_name: &str) -> Result<()> { | ||
let script_name = format!("python/{fixture_name}.py"); | ||
uniffi::python_test::run_test(tmp_dir, fixture_name, &script_name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.