summaryrefslogtreecommitdiffstats
path: root/toolkit/components/uniffi-fixtures/arithmetic/tests
diff options
context:
space:
mode:
Diffstat (limited to 'toolkit/components/uniffi-fixtures/arithmetic/tests')
-rw-r--r--toolkit/components/uniffi-fixtures/arithmetic/tests/bindings/test_arithmetic.kts29
-rw-r--r--toolkit/components/uniffi-fixtures/arithmetic/tests/bindings/test_arithmetic.py37
-rw-r--r--toolkit/components/uniffi-fixtures/arithmetic/tests/bindings/test_arithmetic.rb31
-rw-r--r--toolkit/components/uniffi-fixtures/arithmetic/tests/bindings/test_arithmetic.swift32
-rw-r--r--toolkit/components/uniffi-fixtures/arithmetic/tests/test_generated_bindings.rs6
5 files changed, 135 insertions, 0 deletions
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",
+);