From 8c61fa2e8adaaf81ce40ece06c8a2784215fd710 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Mon, 30 May 2022 15:49:36 +0200 Subject: [PATCH] Add proc-macro test fixture with simple function calls --- Cargo.toml | 1 + fixtures/simple-fns/Cargo.toml | 22 ++++++++++++++++ fixtures/simple-fns/README.md | 3 +++ fixtures/simple-fns/build.rs | 7 ++++++ fixtures/simple-fns/src/lib.rs | 25 +++++++++++++++++++ fixtures/simple-fns/src/simple-fns.udl | 1 + .../tests/bindings/test_simple_fns.kts | 10 ++++++++ .../tests/bindings/test_simple_fns.py | 10 ++++++++ .../tests/bindings/test_simple_fns.swift | 10 ++++++++ .../tests/test_generated_bindings.rs | 8 ++++++ fixtures/simple-fns/uniffi.toml | 9 +++++++ 11 files changed, 106 insertions(+) create mode 100644 fixtures/simple-fns/Cargo.toml create mode 100644 fixtures/simple-fns/README.md create mode 100644 fixtures/simple-fns/build.rs create mode 100644 fixtures/simple-fns/src/lib.rs create mode 100644 fixtures/simple-fns/src/simple-fns.udl create mode 100644 fixtures/simple-fns/tests/bindings/test_simple_fns.kts create mode 100644 fixtures/simple-fns/tests/bindings/test_simple_fns.py create mode 100644 fixtures/simple-fns/tests/bindings/test_simple_fns.swift create mode 100644 fixtures/simple-fns/tests/test_generated_bindings.rs create mode 100644 fixtures/simple-fns/uniffi.toml diff --git a/Cargo.toml b/Cargo.toml index 2175311dbc..b577f0a384 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,7 @@ members = [ "fixtures/regressions/cdylib-crate-type-dependency/cdylib-dependency", "fixtures/uitests", "fixtures/uniffi-fixture-time", + "fixtures/simple-fns", "fixtures/swift-omit-labels", ] diff --git a/fixtures/simple-fns/Cargo.toml b/fixtures/simple-fns/Cargo.toml new file mode 100644 index 0000000000..f8a1f5ae82 --- /dev/null +++ b/fixtures/simple-fns/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "uniffi-fixture-simple-fns" +version = "0.17.0" +authors = ["Firefox Sync Team "] +edition = "2018" +license = "MPL-2.0" +publish = false + +[lib] +name = "uniffi_simple_fns" +crate-type = ["cdylib"] + +[dependencies] +uniffi = { path = "../../uniffi", features = ["builtin-bindgen"] } +thiserror = "1.0" +lazy_static = "1.4" + +[build-dependencies] +uniffi_build = { path = "../../uniffi_build", features = ["builtin-bindgen"] } + +[dev-dependencies] +uniffi_macros = { path = "../../uniffi_macros" } diff --git a/fixtures/simple-fns/README.md b/fixtures/simple-fns/README.md new file mode 100644 index 0000000000..41b7233d36 --- /dev/null +++ b/fixtures/simple-fns/README.md @@ -0,0 +1,3 @@ +# A basic test for uniffi components + +This test covers basic free-standing functions. diff --git a/fixtures/simple-fns/build.rs b/fixtures/simple-fns/build.rs new file mode 100644 index 0000000000..abc7531661 --- /dev/null +++ b/fixtures/simple-fns/build.rs @@ -0,0 +1,7 @@ +/* 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/. */ + +fn main() { + uniffi_build::generate_scaffolding("./src/simple-fns.udl").unwrap(); +} diff --git a/fixtures/simple-fns/src/lib.rs b/fixtures/simple-fns/src/lib.rs new file mode 100644 index 0000000000..36f57afad9 --- /dev/null +++ b/fixtures/simple-fns/src/lib.rs @@ -0,0 +1,25 @@ +/* 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/. */ + +#[uniffi::export] +fn get_string() -> String { + "String created by Rust".to_owned() +} + +#[uniffi::export] +fn get_int() -> i32 { + 1289 +} + +#[uniffi::export] +fn string_identity(s: String) -> String { + s +} + +#[uniffi::export] +fn byte_to_u32(byte: u8) -> u32 { + byte.into() +} + +include!(concat!(env!("OUT_DIR"), "/simple-fns.uniffi.rs")); diff --git a/fixtures/simple-fns/src/simple-fns.udl b/fixtures/simple-fns/src/simple-fns.udl new file mode 100644 index 0000000000..38af1eccaf --- /dev/null +++ b/fixtures/simple-fns/src/simple-fns.udl @@ -0,0 +1 @@ +namespace uniffi_simple_fns {}; diff --git a/fixtures/simple-fns/tests/bindings/test_simple_fns.kts b/fixtures/simple-fns/tests/bindings/test_simple_fns.kts new file mode 100644 index 0000000000..706cc86e24 --- /dev/null +++ b/fixtures/simple-fns/tests/bindings/test_simple_fns.kts @@ -0,0 +1,10 @@ +/* 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 uniffi.fixture.simple_fns.*; + +assert(getString() == "String created by Rust") +assert(getInt() == 1289) +assert(stringIdentity("String created by Kotlin") == "String created by Kotlin") +assert(byteToU32(255) == 255) diff --git a/fixtures/simple-fns/tests/bindings/test_simple_fns.py b/fixtures/simple-fns/tests/bindings/test_simple_fns.py new file mode 100644 index 0000000000..6060d0ea38 --- /dev/null +++ b/fixtures/simple-fns/tests/bindings/test_simple_fns.py @@ -0,0 +1,10 @@ +# 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/. + +from uniffi_simple_fns import * + +assert get_string() == "String created by Rust" +assert get_int() == 1289 +assert string_identity("String created by Python") == "String created by Python" +assert byte_to_u32(255) == 255 diff --git a/fixtures/simple-fns/tests/bindings/test_simple_fns.swift b/fixtures/simple-fns/tests/bindings/test_simple_fns.swift new file mode 100644 index 0000000000..2b25188ba3 --- /dev/null +++ b/fixtures/simple-fns/tests/bindings/test_simple_fns.swift @@ -0,0 +1,10 @@ +/* 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 uniffi_simple_fns + +assert(getString() == "String created by Rust") +assert(getInt() == 1289) +assert(stringIdentity(s: "String created by Kotlin") == "String created by Kotlin") +assert(byteToU32(byte: 255) == 255) diff --git a/fixtures/simple-fns/tests/test_generated_bindings.rs b/fixtures/simple-fns/tests/test_generated_bindings.rs new file mode 100644 index 0000000000..ba1bd52d61 --- /dev/null +++ b/fixtures/simple-fns/tests/test_generated_bindings.rs @@ -0,0 +1,8 @@ +uniffi_macros::build_foreign_language_testcases!( + ["src/simple-fns.udl"], + [ + "tests/bindings/test_simple_fns.kts", + "tests/bindings/test_simple_fns.swift", + "tests/bindings/test_simple_fns.py", + ] +); diff --git a/fixtures/simple-fns/uniffi.toml b/fixtures/simple-fns/uniffi.toml new file mode 100644 index 0000000000..bd4eef0834 --- /dev/null +++ b/fixtures/simple-fns/uniffi.toml @@ -0,0 +1,9 @@ +[bindings.kotlin] +package_name = "uniffi.fixture.simple_fns" +cdylib_name = "uniffi_simple_fns" + +[bindings.python] +cdylib_name = "uniffi_simple_fns" + +[bindings.swift] +cdylib_name = "uniffi_simple_fns"