diff options
Diffstat (limited to 'toolkit/components/uniffi-fixtures/arithmetic')
10 files changed, 215 insertions, 0 deletions
diff --git a/toolkit/components/uniffi-fixtures/arithmetic/Cargo.toml b/toolkit/components/uniffi-fixtures/arithmetic/Cargo.toml new file mode 100644 index 0000000000..b497b8cccc --- /dev/null +++ b/toolkit/components/uniffi-fixtures/arithmetic/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "uniffi-example-arithmetic" +edition = "2021" +version = "0.22.0" +authors = ["Firefox Sync Team <sync-team@mozilla.com>"] +license = "MPL-2.0" +publish = false + +[lib] +crate-type = ["lib", "cdylib"] +name = "arithmetical" + +[dependencies] +uniffi = { workspace = true } +thiserror = "1.0" + +[build-dependencies] +uniffi = { workspace = true, features = ["build"] } + +[dev-dependencies] +uniffi = { workspace = true, features = ["bindgen-tests"] } diff --git a/toolkit/components/uniffi-fixtures/arithmetic/build.rs b/toolkit/components/uniffi-fixtures/arithmetic/build.rs new file mode 100644 index 0000000000..303ac22d8d --- /dev/null +++ b/toolkit/components/uniffi-fixtures/arithmetic/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::generate_scaffolding("src/arithmetic.udl").unwrap(); +} diff --git a/toolkit/components/uniffi-fixtures/arithmetic/src/arithmetic.udl b/toolkit/components/uniffi-fixtures/arithmetic/src/arithmetic.udl new file mode 100644 index 0000000000..117df4834a --- /dev/null +++ b/toolkit/components/uniffi-fixtures/arithmetic/src/arithmetic.udl @@ -0,0 +1,16 @@ +[Error] +enum ArithmeticError { + "IntegerOverflow", +}; + +namespace arithmetic { + [Throws=ArithmeticError] + u64 add(u64 a, u64 b); + + [Throws=ArithmeticError] + u64 sub(u64 a, u64 b); + + u64 div(u64 dividend, u64 divisor); + + boolean equal(u64 a, u64 b); +}; diff --git a/toolkit/components/uniffi-fixtures/arithmetic/src/lib.rs b/toolkit/components/uniffi-fixtures/arithmetic/src/lib.rs new file mode 100644 index 0000000000..92ab8c072b --- /dev/null +++ b/toolkit/components/uniffi-fixtures/arithmetic/src/lib.rs @@ -0,0 +1,34 @@ +/* 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/. */ + +#[derive(Debug, thiserror::Error)] +pub enum ArithmeticError { + #[error("Integer overflow on an operation with {a} and {b}")] + IntegerOverflow { a: u64, b: u64 }, +} + +fn add(a: u64, b: u64) -> Result<u64> { + a.checked_add(b) + .ok_or(ArithmeticError::IntegerOverflow { a, b }) +} + +fn sub(a: u64, b: u64) -> Result<u64> { + a.checked_sub(b) + .ok_or(ArithmeticError::IntegerOverflow { a, b }) +} + +fn div(dividend: u64, divisor: u64) -> u64 { + if divisor == 0 { + panic!("Can't divide by zero"); + } + dividend / divisor +} + +fn equal(a: u64, b: u64) -> bool { + a == b +} + +type Result<T, E = ArithmeticError> = std::result::Result<T, E>; + +uniffi::include_scaffolding!("arithmetic"); diff --git a/toolkit/components/uniffi-fixtures/arithmetic/tests/bindings/test_arithmetic.kts b/toolkit/components/uniffi-fixtures/arithmetic/tests/bindings/test_arithmetic.kts new file mode 100644 index 0000000000..ef11850ae2 --- /dev/null +++ b/toolkit/components/uniffi-fixtures/arithmetic/tests/bindings/test_arithmetic.kts @@ -0,0 +1,29 @@ +import org.mozilla.uniffi.example.arithmetic.*; + +assert(add(2u, 4u) == 6uL) +assert(add(4u, 8u) == 12uL) + +try { + sub(0u, 2u) + throw RuntimeException("Should have thrown a IntegerOverflow exception!") +} catch (e: ArithmeticException) { + // It's okay! +} + +assert(sub(4u, 2u) == 2uL) +assert(sub(8u, 4u) == 4uL) + +assert(div(8u, 4u) == 2uL) + +try { + div(8u, 0u) + throw RuntimeException("Should have panicked when dividing by zero") +} catch (e: InternalException) { + // It's okay! +} + +assert(equal(2u, 2uL)) +assert(equal(4u, 4uL)) + +assert(!equal(2u, 4uL)) +assert(!equal(4u, 8uL)) diff --git a/toolkit/components/uniffi-fixtures/arithmetic/tests/bindings/test_arithmetic.py b/toolkit/components/uniffi-fixtures/arithmetic/tests/bindings/test_arithmetic.py new file mode 100644 index 0000000000..e1b4f71277 --- /dev/null +++ b/toolkit/components/uniffi-fixtures/arithmetic/tests/bindings/test_arithmetic.py @@ -0,0 +1,37 @@ +from arithmetic import InternalError, add, div, equal, sub + +try: + add(18446744073709551615, 1) + assert not ("Should have thrown a IntegerOverflow exception!") +except ArithmeticError.IntegerOverflow: + # It's okay! + pass + +assert add(2, 4) == 6 +assert add(4, 8) == 12 + +try: + sub(0, 1) + assert not ("Should have thrown a IntegerOverflow exception!") +except ArithmeticError.IntegerOverflow: + # It's okay! + pass + +assert sub(4, 2) == 2 +assert sub(8, 4) == 4 + +assert div(8, 4) == 2 + +try: + div(8, 0) +except InternalError: + # It's okay! + pass +else: + assert not ("Should have panicked when dividing by zero") + +assert equal(2, 2) +assert equal(4, 4) + +assert not equal(2, 4) +assert not equal(4, 8) diff --git a/toolkit/components/uniffi-fixtures/arithmetic/tests/bindings/test_arithmetic.rb b/toolkit/components/uniffi-fixtures/arithmetic/tests/bindings/test_arithmetic.rb new file mode 100644 index 0000000000..6669eb279f --- /dev/null +++ b/toolkit/components/uniffi-fixtures/arithmetic/tests/bindings/test_arithmetic.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'test/unit' +require 'arithmetic' + +include Test::Unit::Assertions + +assert_raise Arithmetic::ArithmeticError::IntegerOverflow do + Arithmetic.add 18_446_744_073_709_551_615, 1 +end + +assert_equal Arithmetic.add(2, 4), 6 +assert_equal Arithmetic.add(4, 8), 12 + +assert_raise Arithmetic::ArithmeticError::IntegerOverflow do + Arithmetic.sub 0, 1 +end + +assert_equal Arithmetic.sub(4, 2), 2 +assert_equal Arithmetic.sub(8, 4), 4 +assert_equal Arithmetic.div(8, 4), 2 + +assert_raise Arithmetic::InternalError do + Arithmetic.div 8, 0 +end + +assert Arithmetic.equal(2, 2) +assert Arithmetic.equal(4, 4) + +assert !Arithmetic.equal(2, 4) +assert !Arithmetic.equal(4, 8) diff --git a/toolkit/components/uniffi-fixtures/arithmetic/tests/bindings/test_arithmetic.swift b/toolkit/components/uniffi-fixtures/arithmetic/tests/bindings/test_arithmetic.swift new file mode 100644 index 0000000000..a8e34680e4 --- /dev/null +++ b/toolkit/components/uniffi-fixtures/arithmetic/tests/bindings/test_arithmetic.swift @@ -0,0 +1,32 @@ +import arithmetic + +do { + let _ = try add(a: 18446744073709551615, b: 1) + fatalError("Should have thrown a IntegerOverflow exception!") +} catch ArithmeticError.IntegerOverflow { + // It's okay! +} + +assert(try! add(a: 2, b: 4) == 6, "add work") +assert(try! add(a: 4, b: 8) == 12, "add work") + +do { + let _ = try sub(a: 0, b: 1) + fatalError("Should have thrown a IntegerOverflow exception!") +} catch ArithmeticError.IntegerOverflow { + // It's okay! +} + +assert(try! sub(a: 4, b: 2) == 2, "sub work") +assert(try! sub(a: 8, b: 4) == 4, "sub work") + +assert(div(dividend: 8, divisor: 4) == 2, "div works") + +// We can't test panicking in Swift because we force unwrap the error in +// `div`, which we can't catch. + +assert(equal(a: 2, b: 2), "equal works") +assert(equal(a: 4, b: 4), "equal works") + +assert(!equal(a: 2, b: 4), "non-equal works") +assert(!equal(a: 4, b: 8), "non-equal works") diff --git a/toolkit/components/uniffi-fixtures/arithmetic/tests/test_generated_bindings.rs b/toolkit/components/uniffi-fixtures/arithmetic/tests/test_generated_bindings.rs new file mode 100644 index 0000000000..168e6e1d4c --- /dev/null +++ b/toolkit/components/uniffi-fixtures/arithmetic/tests/test_generated_bindings.rs @@ -0,0 +1,6 @@ +uniffi::build_foreign_language_testcases!( + "tests/bindings/test_arithmetic.rb", + "tests/bindings/test_arithmetic.py", + "tests/bindings/test_arithmetic.kts", + "tests/bindings/test_arithmetic.swift", +); diff --git a/toolkit/components/uniffi-fixtures/arithmetic/uniffi.toml b/toolkit/components/uniffi-fixtures/arithmetic/uniffi.toml new file mode 100644 index 0000000000..883231dcaa --- /dev/null +++ b/toolkit/components/uniffi-fixtures/arithmetic/uniffi.toml @@ -0,0 +1,2 @@ +[bindings.kotlin] +package_name = "org.mozilla.uniffi.example.arithmetic" |