Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy committed Nov 12, 2023
1 parent 457cad1 commit 58caab2
Show file tree
Hide file tree
Showing 19 changed files with 376 additions and 2 deletions.
101 changes: 101 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ build:
cargo build

test: build
cargo test
cd example && ../target/debug/deno_bindgen -o bindings/mod.ts && deno test -A --unstable

bench: build
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn add(a: u32, b: u32) -> u32 {
Use the exported functions directly in ESM with TypeScript typings

```typescript
import { add } from "@ffi/example";
import { add } from "./bindings/mod.ts";

add(1, 2);
```
Expand Down
5 changes: 4 additions & 1 deletion deno_bindgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ deno_bindgen_macro = { path = "../deno_bindgen_macro", version = "0.8.1" }
deno_bindgen_ir = { path = "../deno_bindgen_ir", version = "0.1.0" }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
linkme = "0.3"
linkme = "0.3"

[dev-dependencies]
trybuild = "1.0.85"
15 changes: 15 additions & 0 deletions deno_bindgen/tests/compile_fail/impl_registration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use deno_bindgen::deno_bindgen;

// struct Foo is not "registered" in the inventory, so it `impl Foo`
// is not allowed.
struct Foo;

#[deno_bindgen]
impl Foo {
#[constructor]
fn new() -> Foo {
Foo
}
}

fn main() {}
12 changes: 12 additions & 0 deletions deno_bindgen/tests/compile_fail/impl_registration.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0277]: the trait bound `Foo: BindgenType` is not satisfied
--> tests/compile_fail/impl_registration.rs:8:6
|
8 | impl Foo {
| ^^^ the trait `BindgenType` is not implemented for `Foo`
|
note: required by a bound in `_assert_impl`
--> tests/compile_fail/impl_registration.rs:7:1
|
7 | #[deno_bindgen]
| ^^^^^^^^^^^^^^^ required by this bound in `_assert_impl`
= note: this error originates in the attribute macro `deno_bindgen` (in Nightly builds, run with -Z macro-backtrace for more info)
20 changes: 20 additions & 0 deletions deno_bindgen/tests/compile_fail/struct_by_val.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use deno_bindgen::deno_bindgen;

#[deno_bindgen]
struct Foo;

#[deno_bindgen]
impl Foo {
#[constructor]
fn new() -> Foo {
Foo
}
}

#[deno_bindgen]
fn foo(_foo: Foo) {} // Fail

#[deno_bindgen]
fn foo2(_foo: &mut Foo) {} // Pass

fn main() {}
15 changes: 15 additions & 0 deletions deno_bindgen/tests/compile_fail/struct_by_val.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0308]: mismatched types
--> tests/compile_fail/struct_by_val.rs:15:8
|
15 | fn foo(_foo: Foo) {} // Fail
| --- ^^^^ expected `Foo`, found `&mut _`
| |
| arguments to this function are incorrect
|
= note: expected struct `Foo`
found mutable reference `&mut _`
note: function defined here
--> tests/compile_fail/struct_by_val.rs:15:4
|
15 | fn foo(_foo: Foo) {} // Fail
| ^^^ ---------
22 changes: 22 additions & 0 deletions deno_bindgen/tests/pass/constructor_override.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use deno_bindgen::deno_bindgen;

#[deno_bindgen]
struct Input {
a: i32,
b: i32,
}

#[deno_bindgen]
impl Input {
#[constructor]
fn new(a: i32, b: i32) -> Input {
Input { a, b }
}

#[constructor]
fn new2() -> Input {
Input { a: 0, b: 0 }
}
}

fn main() {}
63 changes: 63 additions & 0 deletions deno_bindgen/tests/pass/simple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use deno_bindgen_macro::deno_bindgen;

#[deno_bindgen]
fn add(a: i32, b: i32) -> i32 {
a + b
}

#[deno_bindgen]
fn buf_mut(b: &mut [u8]) {
b[0] = 99;
}

#[deno_bindgen]
fn cstr() -> *const u8 {
b"Hello, World!\0".as_ptr()
}

#[deno_bindgen]
fn strlen(s: *const u8) -> u32 {
let mut len = 0;
unsafe {
while *s.add(len as usize) != 0 {
len += 1;
}
}
len
}

#[deno_bindgen(non_blocking)]
fn non_blocking() -> i32 {
42
}

#[deno_bindgen]
struct Foo {
internal: i32,
}

#[deno_bindgen]
impl Foo {
#[constructor]
fn new(internal: i32) -> Foo {
Foo { internal }
}

fn bar(&self) -> i32 {
42
}

fn baz(&self, a: i32) -> i32 {
a
}

fn qux(&self, a: i32, b: i32) -> i32 {
a + b
}

fn quux(&mut self) {
self.internal += 1;
}
}

fn main() {}
11 changes: 11 additions & 0 deletions deno_bindgen/tests/ui.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#[test]
fn ui_pass() {
let t = trybuild::TestCases::new();
t.pass("tests/pass/*.rs");
}

#[test]
fn ui_compile_fail() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/compile_fail/*.rs");
}
4 changes: 4 additions & 0 deletions deno_bindgen_macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ serde = { version = "1.0.59", features = ["derive"] }
serde_json = "1.0.59"
Inflector = "0.11.4"
deno_bindgen_ir = { path = "../deno_bindgen_ir", version = "0.1.0" }

[dev-dependencies]
prettyplease = "0.2.15"
testing_macros = "0.2.11"
Loading

0 comments on commit 58caab2

Please sign in to comment.