test(napi-derive): add trybuild negative macro tests

This commit is contained in:
Jose Acevedo 2022-05-21 01:53:40 -04:00 committed by LongYinan
parent beb75111fc
commit 796ba363f5
15 changed files with 112 additions and 1 deletions

View file

@ -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'

View file

@ -1 +1,2 @@
*.node
*.node
wip/

View file

@ -28,3 +28,6 @@ serde_json = "1"
[build-dependencies]
napi-build = { path = "../../crates/build" }
[dev-dependencies]
trybuild = "1.0"

View 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

View 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;

View 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() {}

View file

@ -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>) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

View 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() {}

View file

@ -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>) {
| ^^

View 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() {}

View file

@ -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>) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^

View 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() {}

View file

@ -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>) {
| ^^^^^^^^^^^^

View 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");
}

View file

@ -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"