1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Adapted from https://github.com/welf/type-state-builder/blob/main/tests/test_raw_identifiers.rs
//! Test raw identifiers with keywords
macro_rules! tests {
($kind: literal in mod $module: ident $($unwrap: ident)?) => {
mod $module {
#[test]
fn fields() {
use bauer::Builder;
#[derive(Builder)]
#[builder(kind = $kind)]
struct TestWithKeywords {
r#type: String,
r#async: Option<bool>,
}
let instance = TestWithKeywords::builder()
.r#type("test".to_string())
.r#async(true)
.build()
$(.$unwrap())?;
assert_eq!(instance.r#type, "test");
assert_eq!(instance.r#async, Some(true));
}
#[test]
fn generics() {
#![allow(non_camel_case_types)]
use bauer::Builder;
#[derive(Builder)]
#[builder(kind = $kind)]
struct TestWithKeywords<r#type> {
field: r#type,
}
let instance = TestWithKeywords::builder()
.field("test".to_string())
.build()
$(.$unwrap())?;
assert_eq!(instance.field, "test");
}
#[test]
fn struct_name() {
#![allow(non_camel_case_types)]
use bauer::Builder;
#[derive(Builder)]
#[builder(kind = $kind)]
struct r#type {
field: String,
}
let instance = r#type::builder()
.field("test".to_string())
.build()
$(.$unwrap())?;
assert_eq!(instance.field, "test");
}
#[test]
fn rename() {
use bauer::Builder;
#[derive(Builder)]
#[builder(kind = $kind)]
struct Foo {
#[builder(rename = "r#type")]
field: String,
}
let instance = Foo::builder()
.r#type("test".to_string())
.build()
$(.$unwrap())?;
assert_eq!(instance.field, "test");
}
#[test]
fn prefix() {
use bauer::Builder;
#[derive(Builder)]
#[builder(kind = $kind, prefix = "ty")]
struct Foo {
pe: String,
}
let instance = Foo::builder()
.r#type("test".to_string())
.build()
$(.$unwrap())?;
assert_eq!(instance.pe, "test");
}
#[test]
fn suffix() {
use bauer::Builder;
#[derive(Builder)]
#[builder(kind = $kind, suffix = "pe")]
struct Foo {
ty: String,
}
let instance = Foo::builder()
.r#type("test".to_string())
.build()
$(.$unwrap())?;
assert_eq!(instance.ty, "test");
}
}
};
}
tests!("borrowed" in mod borrowed unwrap);
tests!("owned" in mod owned unwrap);
tests!("type-state" in mod type_state);