]> git.proxmox.com Git - rustc.git/blob - vendor/insta/tests/test_redaction.rs
New upstream version 1.46.0+dfsg1
[rustc.git] / vendor / insta / tests / test_redaction.rs
1 #![cfg(feature = "redactions")]
2
3 use insta::_macro_support::Selector;
4 use insta::{
5 assert_debug_snapshot, assert_json_snapshot, assert_yaml_snapshot, with_settings, Settings,
6 };
7 use serde::Serialize;
8
9 #[test]
10 fn test_selector_parser() {
11 macro_rules! assert_selector {
12 ($short:expr, $sel:expr) => {
13 assert_debug_snapshot!($short, Selector::parse($sel).unwrap());
14 };
15 }
16
17 assert_selector!("foo_bar", ".foo.bar");
18 assert_selector!("foo_bar_alt", ".foo[\"bar\"]");
19 assert_selector!("foo_bar_full_range", ".foo.bar[]");
20 assert_selector!("foo_bar_range_to", ".foo.bar[:10]");
21 assert_selector!("foo_bar_range_from", ".foo.bar[10:]");
22 assert_selector!("foo_bar_range", ".foo.bar[10:20]");
23 assert_selector!("foo_bar_deep", ".foo.bar.**");
24 }
25
26 #[derive(Serialize)]
27 pub struct Email(String);
28
29 #[derive(Serialize)]
30 pub struct User {
31 id: u32,
32 username: String,
33 email: Email,
34 extra: String,
35 }
36
37 #[test]
38 fn test_with_random_value() {
39 assert_yaml_snapshot!("user", &User {
40 id: 42,
41 username: "john_doe".to_string(),
42 email: Email("john@example.com".to_string()),
43 extra: "".to_string(),
44 }, {
45 ".id" => "[id]"
46 });
47 }
48
49 #[test]
50 fn test_with_random_value_inline_callback() {
51 assert_yaml_snapshot!("user", &User {
52 id: 23,
53 username: "john_doe".to_string(),
54 email: Email("john@example.com".to_string()),
55 extra: "".to_string(),
56 }, {
57 ".id" => insta::dynamic_redaction(|value, path| {
58 assert_eq!(path.to_string(), ".id");
59 assert_eq!(value.as_u64().unwrap(), 23);
60 "[id]"
61 }),
62 });
63 }
64
65 #[test]
66 fn test_with_random_value_and_trailing_comma() {
67 assert_yaml_snapshot!("user", &User {
68 id: 11,
69 username: "john_doe".to_string(),
70 email: Email("john@example.com".to_string()),
71 extra: "".to_string(),
72 }, {
73 ".id" => "[id]",
74 });
75 }
76
77 #[cfg(feature = "ron")]
78 #[test]
79 fn test_with_random_value_ron() {
80 use insta::assert_ron_snapshot;
81 assert_ron_snapshot!("user_ron", &User {
82 id: 53,
83 username: "john_ron".to_string(),
84 email: Email("john@example.com".to_string()),
85 extra: "".to_string(),
86 }, {
87 ".id" => "[id]"
88 });
89 }
90
91 #[test]
92 fn test_with_random_value_json() {
93 assert_json_snapshot!("user_json", &User {
94 id: 9999,
95 username: "jason_doe".to_string(),
96 email: Email("jason@example.com".to_string()),
97 extra: "ssn goes here".to_string(),
98 }, {
99 ".id" => "[id]",
100 ".extra" => "[extra]"
101 });
102 }
103
104 #[test]
105 fn test_with_random_value_json_settings() {
106 let mut settings = Settings::new();
107 settings.add_redaction(".id", "[id]");
108 settings.add_redaction(".extra", "[extra]");
109 settings.bind(|| {
110 assert_json_snapshot!(
111 "user_json_settings",
112 &User {
113 id: 122,
114 username: "jason_doe".to_string(),
115 email: Email("jason@example.com".to_string()),
116 extra: "ssn goes here".to_string(),
117 }
118 );
119 });
120 }
121
122 #[test]
123 fn test_with_callbacks() {
124 let mut settings = Settings::new();
125 settings.add_dynamic_redaction(".id", |value, path| {
126 assert_eq!(path.to_string(), ".id");
127 assert_eq!(value.as_u64().unwrap(), 1234);
128 "[id]"
129 });
130 settings.bind(|| {
131 assert_json_snapshot!(
132 "user_json_settings_callback",
133 &User {
134 id: 1234,
135 username: "jason_doe".to_string(),
136 email: Email("jason@example.com".to_string()),
137 extra: "extra here".to_string(),
138 }
139 );
140 });
141 }
142
143 #[test]
144 fn test_with_random_value_json_settings2() {
145 with_settings!({redactions => vec![
146 (".id", "[id]".into()),
147 (".extra", "[extra]".into()),
148 ]}, {
149 assert_json_snapshot!(
150 &User {
151 id: 975,
152 username: "jason_doe".to_string(),
153 email: Email("jason@example.com".to_string()),
154 extra: "ssn goes here".to_string(),
155 }
156 );
157 });
158 }
159
160 #[test]
161 fn test_redact_newtype() {
162 #[derive(Serialize, Clone)]
163 pub struct User {
164 id: String,
165 name: String,
166 }
167
168 #[derive(Serialize)]
169 pub struct UserWrapper(User);
170
171 let user = User {
172 id: "my-id".into(),
173 name: "my-name".into(),
174 };
175 let wrapper = UserWrapper(user.clone());
176
177 // This works as expected
178 assert_json_snapshot!(user, {
179 r#".id"# => "[id]"
180 }, @r###"
181 {
182 "id": "[id]",
183 "name": "my-name"
184 }
185 "###);
186
187 // This fails - 'id' is not redacted
188 assert_json_snapshot!(wrapper, {
189 r#".id"# => "[id]"
190 }, @r###"
191 {
192 "id": "[id]",
193 "name": "my-name"
194 }
195 "###);
196 }
197
198 #[test]
199 fn test_redact_recursive() {
200 #[derive(Serialize)]
201 pub struct Node {
202 id: u64,
203 next: Option<Box<Node>>,
204 }
205
206 let root = Node {
207 id: 0,
208 next: Some(Box::new(Node { id: 1, next: None })),
209 };
210
211 assert_json_snapshot!(root, {
212 ".**.id" => "[id]",
213 }, @r###"
214 {
215 "id": "[id]",
216 "next": {
217 "id": "[id]",
218 "next": null
219 }
220 }
221 "###);
222 }