Skip to content

Commit 303f581

Browse files
committed
Use trybuild for testing macros
1 parent d8a3775 commit 303f581

20 files changed

+363
-45
lines changed

.editorconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,7 @@ indent_style = unset
4646
[HELP.md]
4747
max_line_length = 400
4848
trim_trailing_whitespace = false
49+
50+
[*.stderr]
51+
indent_size = unset
52+
max_line_length = 150

.github/workflows/ci.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,6 @@ jobs:
121121
run: cd butane_test_helper && cargo +stable test --all-features
122122
- name: Test Core
123123
run: cd butane_core && cargo +stable test --all-features
124-
- name: Test Codegen
125-
run: cd butane_codegen && cargo +stable test --all-features
126124
- name: Test CLI
127125
run: cd butane_cli && cargo +stable test --all-features
128126
- name: Test

Cargo.lock

Lines changed: 123 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

butane/tests/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ name = "query"
104104
path = "query.rs"
105105
required-features = ["async"]
106106

107+
[[test]]
108+
name = "trybuild"
109+
path = "trybuild.rs"
110+
107111
[[test]]
108112
name = "unmigrate"
109113
path = "unmigrate.rs"

butane/tests/trybuild.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Compile-time tests for FieldType derive macro using trybuild
2+
3+
#[test]
4+
fn compile_tests() {
5+
let t = trybuild::TestCases::new();
6+
t.pass("tests/trybuild/pass/*.rs");
7+
t.compile_fail("tests/trybuild/fail/*.rs");
8+
}

butane/tests/trybuild/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "butane_codegen-tests"
3+
version = "0.0.0"
4+
edition = "2021"
5+
publish = false
6+
7+
[dependencies]
8+
butane = { path = "../../", features = ["json"] }
9+
butane_codegen = { path = "../../butane_codegen" }
10+
serde = "1.0"
11+
serde_json = "1.0"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use butane_codegen::FieldType;
2+
3+
// This should fail because enums with data in variants require json feature
4+
#[derive(FieldType)]
5+
pub enum ComplexEnum {
6+
Variant1(String),
7+
Variant2 { field: i32 },
8+
}
9+
10+
fn main() {}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
error[E0277]: the trait bound `ComplexEnum: serde::Serialize` is not satisfied
2+
--> tests/trybuild/fail/complex_enum_without_json.rs:4:10
3+
|
4+
4 | #[derive(FieldType)]
5+
| ^^^^^^^^^ unsatisfied trait bound
6+
|
7+
help: the trait `Serialize` is not implemented for `ComplexEnum`
8+
--> tests/trybuild/fail/complex_enum_without_json.rs:5:1
9+
|
10+
5 | pub enum ComplexEnum {
11+
| ^^^^^^^^^^^^^^^^^^^^
12+
= note: for local types consider adding `#[derive(serde::Serialize)]` to your `ComplexEnum` type
13+
= note: for types from other crates check whether the crate offers a `serde` feature flag
14+
= help: the following other types implement trait `Serialize`:
15+
&'a T
16+
&'a mut T
17+
()
18+
(T,)
19+
(T0, T1)
20+
(T0, T1, T2)
21+
(T0, T1, T2, T3)
22+
(T0, T1, T2, T3, T4)
23+
and $N others
24+
= note: required for `&ComplexEnum` to implement `Serialize`
25+
note: required by a bound in `to_value`
26+
--> $CARGO/serde_json-$VERSION/src/value/mod.rs
27+
|
28+
| pub fn to_value<T>(value: T) -> Result<Value, Error>
29+
| -------- required by a bound in this function
30+
| where
31+
| T: Serialize,
32+
| ^^^^^^^^^ required by this bound in `to_value`
33+
= note: this error originates in the derive macro `FieldType` (in Nightly builds, run with -Z macro-backtrace for more info)
34+
35+
error[E0599]: no variant or associated item named `deserialize` found for enum `ComplexEnum` in the current scope
36+
--> tests/trybuild/fail/complex_enum_without_json.rs:4:10
37+
|
38+
4 | #[derive(FieldType)]
39+
| ^^^^^^^^^ variant or associated item not found in `ComplexEnum`
40+
5 | pub enum ComplexEnum {
41+
| -------------------- variant or associated item `deserialize` not found for this enum
42+
|
43+
= help: items from traits can only be used if the trait is implemented and in scope
44+
= note: the following traits define an item `deserialize`, perhaps you need to implement one of them:
45+
candidate #1: `Deserialize`
46+
candidate #2: `DeserializeSeed`
47+
= note: this error originates in the derive macro `FieldType` (in Nightly builds, run with -Z macro-backtrace for more info)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use butane_codegen::FieldType;
2+
3+
// This should fail because the json feature is required for non-newtype structs
4+
#[derive(FieldType)]
5+
pub struct Metadata {
6+
pub title: String,
7+
pub version: i32,
8+
}
9+
10+
fn main() {}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
error[E0277]: the trait bound `Metadata: serde::Serialize` is not satisfied
2+
--> tests/trybuild/fail/struct_without_json_feature.rs:4:10
3+
|
4+
4 | #[derive(FieldType)]
5+
| ^^^^^^^^^ unsatisfied trait bound
6+
|
7+
help: the trait `Serialize` is not implemented for `Metadata`
8+
--> tests/trybuild/fail/struct_without_json_feature.rs:5:1
9+
|
10+
5 | pub struct Metadata {
11+
| ^^^^^^^^^^^^^^^^^^^
12+
= note: for local types consider adding `#[derive(serde::Serialize)]` to your `Metadata` type
13+
= note: for types from other crates check whether the crate offers a `serde` feature flag
14+
= help: the following other types implement trait `Serialize`:
15+
&'a T
16+
&'a mut T
17+
()
18+
(T,)
19+
(T0, T1)
20+
(T0, T1, T2)
21+
(T0, T1, T2, T3)
22+
(T0, T1, T2, T3, T4)
23+
and $N others
24+
= note: required for `&Metadata` to implement `Serialize`
25+
note: required by a bound in `to_value`
26+
--> $CARGO/serde_json-$VERSION/src/value/mod.rs
27+
|
28+
| pub fn to_value<T>(value: T) -> Result<Value, Error>
29+
| -------- required by a bound in this function
30+
| where
31+
| T: Serialize,
32+
| ^^^^^^^^^ required by this bound in `to_value`
33+
= note: this error originates in the derive macro `FieldType` (in Nightly builds, run with -Z macro-backtrace for more info)
34+
35+
error[E0599]: no function or associated item named `deserialize` found for struct `Metadata` in the current scope
36+
--> tests/trybuild/fail/struct_without_json_feature.rs:4:10
37+
|
38+
4 | #[derive(FieldType)]
39+
| ^^^^^^^^^ function or associated item not found in `Metadata`
40+
5 | pub struct Metadata {
41+
| ------------------- function or associated item `deserialize` not found for this struct
42+
|
43+
= help: items from traits can only be used if the trait is implemented and in scope
44+
= note: the following traits define an item `deserialize`, perhaps you need to implement one of them:
45+
candidate #1: `Deserialize`
46+
candidate #2: `DeserializeSeed`
47+
= note: this error originates in the derive macro `FieldType` (in Nightly builds, run with -Z macro-backtrace for more info)

0 commit comments

Comments
 (0)