Skip to content

Commit

Permalink
Add docs and a tests demonstrating how not to use the fakes
Browse files Browse the repository at this point in the history
  • Loading branch information
jmartinesp committed Oct 28, 2023
1 parent 73b1448 commit 81e622a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
23 changes: 21 additions & 2 deletions fixtures/coverall/tests/bindings/test_coverall.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import java.util.concurrent.*

import uniffi.coverall.*

import com.sun.jna.Pointer

// TODO: use an actual test runner.

// Test some_dict().
Expand Down Expand Up @@ -446,3 +444,24 @@ FakeCoveralls("using_fakes").use { coveralls ->
coveralls.addPatch(patch)
assert(!coveralls.getRepairs().isEmpty())
}

FakeCoveralls("using_fakes_and_calling_methods_without_override_crashes").use { coveralls ->
var exception: Throwable? = null
try {
coveralls.cloneMe()
} catch (e: Throwable) {
exception = e
}
assert(exception != null)
}

Coveralls("using_fakes_with_real_objects_crashes").use { coveralls ->
val patch = FakePatch(Color.RED)
var exception: Throwable? = null
try {
coveralls.addPatch(patch)
} catch (e: Throwable) {
exception = e
}
assert(exception != null)
}
11 changes: 10 additions & 1 deletion uniffi_bindgen/src/bindings/kotlin/templates/ObjectRuntime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,16 @@ abstract class FFIObject: Disposable, AutoCloseable {
this.pointer = pointer
}

/**
* This constructor can be used to instantiate a fake object.
*
* **WARNING: Any object instantiated with this constructor cannot be passed to an actual Rust-backed object.**
* Since there isn't a backing [Pointer] the FFI lower functions will crash.
* @param noPointer Placeholder value so we can have a constructor separate from the default empty one that may be
* implemented for classes extending [FFIObject].
*/
@Suppress("UNUSED_PARAMETER")
protected constructor(noPointer: NoPointer) {
constructor(noPointer: NoPointer) {
this.pointer = null
}

Expand Down Expand Up @@ -171,4 +179,5 @@ abstract class FFIObject: Disposable, AutoCloseable {
}
}

/** Used to instantiate a [FFIObject] without an actual pointer, for fakes in tests, mostly. */
object NoPointer
10 changes: 9 additions & 1 deletion uniffi_bindgen/src/bindings/kotlin/templates/ObjectTemplate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ open class {{ impl_class_name }} : FFIObject{% if obj.is_trait_interface() %}, {

constructor(pointer: Pointer): super(pointer)

protected constructor(noPointer: NoPointer): super(noPointer)
/**
* This constructor can be used to instantiate a fake object.
*
* **WARNING: Any object instantiated with this constructor cannot be passed to an actual Rust-backed object.**
* Since there isn't a backing [Pointer] the FFI lower functions will crash.
* @param noPointer Placeholder value so we can have a constructor separate from the default empty one that may be
* implemented for classes extending [FFIObject].
*/
constructor(noPointer: NoPointer): super(noPointer)

{%- match obj.primary_constructor() %}
{%- when Some with (cons) %}
Expand Down

0 comments on commit 81e622a

Please sign in to comment.