test(napi-derive): add trybuild negative macro tests
This commit is contained in:
parent
beb75111fc
commit
796ba363f5
15 changed files with 112 additions and 1 deletions
1
.github/workflows/test.yaml
vendored
1
.github/workflows/test.yaml
vendored
|
@ -70,6 +70,7 @@ jobs:
|
|||
yarn build:test
|
||||
yarn test --verbose
|
||||
yarn tsc -p examples/napi/tsconfig.json --noEmit
|
||||
yarn test:macro
|
||||
|
||||
- name: Electron tests
|
||||
if: matrix.os != 'ubuntu-latest'
|
||||
|
|
3
examples/napi/.gitignore
vendored
3
examples/napi/.gitignore
vendored
|
@ -1 +1,2 @@
|
|||
*.node
|
||||
*.node
|
||||
wip/
|
||||
|
|
|
@ -28,3 +28,6 @@ serde_json = "1"
|
|||
|
||||
[build-dependencies]
|
||||
napi-build = { path = "../../crates/build" }
|
||||
|
||||
[dev-dependencies]
|
||||
trybuild = "1.0"
|
||||
|
|
16
examples/napi/tests/README.md
Normal file
16
examples/napi/tests/README.md
Normal file
|
@ -0,0 +1,16 @@
|
|||
## [trybuild](https://docs.rs/trybuild/latest/trybuild/) tests
|
||||
|
||||
Set of tests to exercise various macro expansion errors.
|
||||
|
||||
### Notes on adding a new test
|
||||
|
||||
- Make sure to include the new test in [build_error_tests/mod.rs](./build_error_tests/mod.rs) so it gets included as
|
||||
part of the crate for formatting purposes.
|
||||
|
||||
### Running the tests
|
||||
|
||||
> yarn test:macro
|
||||
|
||||
or
|
||||
|
||||
> cargo test -p napi-examples
|
6
examples/napi/tests/build_error_tests/mod.rs
Normal file
6
examples/napi/tests/build_error_tests/mod.rs
Normal file
|
@ -0,0 +1,6 @@
|
|||
//! Include the test files here so they can be formatted properly with `cargo fmt`
|
||||
|
||||
pub mod ts_arg_type_1;
|
||||
pub mod ts_arg_type_2;
|
||||
pub mod ts_arg_type_3;
|
||||
pub mod ts_arg_type_4;
|
13
examples/napi/tests/build_error_tests/ts_arg_type_1.rs
Normal file
13
examples/napi/tests/build_error_tests/ts_arg_type_1.rs
Normal file
|
@ -0,0 +1,13 @@
|
|||
//! This is testing that `#[napi(ts_args_type="...")]` and `#[napi(ts_arg_type="...")]`
|
||||
//! are mutually exclusive
|
||||
|
||||
use napi_derive::napi;
|
||||
|
||||
#[napi(ts_args_type = "u: number, fn: object")]
|
||||
pub fn add(u: u32, #[napi(ts_arg_type = "object")] f: Option<String>) {
|
||||
println!("Hello, world! {f:?}-{u}");
|
||||
}
|
||||
|
||||
// Needed for the trybuild tests.
|
||||
#[allow(unused)]
|
||||
fn main() {}
|
|
@ -0,0 +1,5 @@
|
|||
error: Found a 'ts_args_type'="u: number, fn: object" override. Cannot use 'ts_arg_type' at the same time since they are mutually exclusive.
|
||||
--> tests/build_error_tests/ts_arg_type_1.rs:7:22
|
||||
|
|
||||
7 | pub fn add(u: u32, #[napi(ts_arg_type = "object")] f: Option<String>) {
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
13
examples/napi/tests/build_error_tests/ts_arg_type_2.rs
Normal file
13
examples/napi/tests/build_error_tests/ts_arg_type_2.rs
Normal file
|
@ -0,0 +1,13 @@
|
|||
//! This is testing that `#[napi(ts_arg_type="...")]` fails if the argument for `ts_arg_type`
|
||||
//! is not a string literal.
|
||||
|
||||
use napi_derive::napi;
|
||||
|
||||
#[napi]
|
||||
pub fn add(u: u32, #[napi(ts_arg_type = 32)] f: Option<String>) {
|
||||
println!("Hello, world! {f:?}-{u}");
|
||||
}
|
||||
|
||||
// Needed for the trybuild tests.
|
||||
#[allow(unused)]
|
||||
fn main() {}
|
|
@ -0,0 +1,5 @@
|
|||
error: Expected a string literal
|
||||
--> tests/build_error_tests/ts_arg_type_2.rs:7:41
|
||||
|
|
||||
7 | pub fn add(u: u32, #[napi(ts_arg_type = 32)] f: Option<String>) {
|
||||
| ^^
|
13
examples/napi/tests/build_error_tests/ts_arg_type_3.rs
Normal file
13
examples/napi/tests/build_error_tests/ts_arg_type_3.rs
Normal file
|
@ -0,0 +1,13 @@
|
|||
//! This is testing that `#[napi(ts_arg_type="...")]` fails if the attribute is not a `MetaNameValue`
|
||||
//! i.e. it's a name value pair.
|
||||
|
||||
use napi_derive::napi;
|
||||
|
||||
#[napi]
|
||||
pub fn add(u: u32, #[napi(ts_arg_type, not_expected)] f: Option<String>) {
|
||||
println!("Hello, world! {f:?}-{u}");
|
||||
}
|
||||
|
||||
// Needed for the trybuild tests.
|
||||
#[allow(unused)]
|
||||
fn main() {}
|
|
@ -0,0 +1,5 @@
|
|||
error: Expected Name Value
|
||||
--> tests/build_error_tests/ts_arg_type_3.rs:7:27
|
||||
|
|
||||
7 | pub fn add(u: u32, #[napi(ts_arg_type, not_expected)] f: Option<String>) {
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
13
examples/napi/tests/build_error_tests/ts_arg_type_4.rs
Normal file
13
examples/napi/tests/build_error_tests/ts_arg_type_4.rs
Normal file
|
@ -0,0 +1,13 @@
|
|||
//! This is testing that `#[napi(ts_arg_type="...")]` fails if the attribute is something other than
|
||||
//! `ts_arg_type`
|
||||
|
||||
use napi_derive::napi;
|
||||
|
||||
#[napi]
|
||||
pub fn add(u: u32, #[napi(not_expected = "obj")] f: Option<String>) {
|
||||
println!("Hello, world! {f:?}-{u}");
|
||||
}
|
||||
|
||||
// Needed for the trybuild tests.
|
||||
#[allow(unused)]
|
||||
fn main() {}
|
|
@ -0,0 +1,5 @@
|
|||
error: Did not find 'ts_arg_type'
|
||||
--> tests/build_error_tests/ts_arg_type_4.rs:7:27
|
||||
|
|
||||
7 | pub fn add(u: u32, #[napi(not_expected = "obj")] f: Option<String>) {
|
||||
| ^^^^^^^^^^^^
|
11
examples/napi/tests/macro_tests.rs
Normal file
11
examples/napi/tests/macro_tests.rs
Normal file
|
@ -0,0 +1,11 @@
|
|||
// `error_try_builds` is not really a feature.
|
||||
// This is used just to make the test files part of the crate so that they can get included by things
|
||||
// like `cargo fmt` for example, BUT not be part of compilation when running `cargo test`.
|
||||
#[cfg(feature = "error_try_builds")]
|
||||
mod build_error_tests;
|
||||
|
||||
#[test]
|
||||
fn run_build_error_tests() {
|
||||
let t = trybuild::TestCases::new();
|
||||
t.compile_fail("tests/build_error_tests/ts_arg_type_*.rs");
|
||||
}
|
|
@ -36,6 +36,7 @@
|
|||
"prepublishOnly": "npm run build && pinst --disable",
|
||||
"test": "ava \"./examples/napi/**/*.ts\" && ava --no-worker-threads \"./examples/napi-compat-mode/**/*.ts\" && ava \"./cli/**/*.ts\"",
|
||||
"test:electron": "electron examples/napi/electron.js",
|
||||
"test:macro": "cargo test -p napi-examples",
|
||||
"test:memory": "node memory-testing/index.mjs",
|
||||
"postinstall": "husky install",
|
||||
"postpublish": "pinst --enable"
|
||||
|
|
Loading…
Reference in a new issue