1 #![cfg(feature = "redactions")]
3 use insta
::_macro_support
::Selector
;
5 assert_debug_snapshot
, assert_json_snapshot
, assert_yaml_snapshot
, with_settings
, Settings
,
10 fn test_selector_parser() {
11 macro_rules
! assert_selector
{
12 ($short
:expr
, $sel
:expr
) => {
13 assert_debug_snapshot
!($short
, Selector
::parse($sel
).unwrap());
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.**");
27 pub struct Email(String
);
38 fn test_with_random_value() {
39 assert_yaml_snapshot
!("user", &User
{
41 username
: "john_doe".to_string(),
42 email
: Email("john@example.com".to_string()),
43 extra
: "".to_string(),
50 fn test_with_random_value_inline_callback() {
51 assert_yaml_snapshot
!("user", &User
{
53 username
: "john_doe".to_string(),
54 email
: Email("john@example.com".to_string()),
55 extra
: "".to_string(),
57 ".id" => insta
::dynamic_redaction(|value
, path
| {
58 assert_eq
!(path
.to_string(), ".id");
59 assert_eq
!(value
.as_u64().unwrap(), 23);
66 fn test_with_random_value_and_trailing_comma() {
67 assert_yaml_snapshot
!("user", &User
{
69 username
: "john_doe".to_string(),
70 email
: Email("john@example.com".to_string()),
71 extra
: "".to_string(),
77 #[cfg(feature = "ron")]
79 fn test_with_random_value_ron() {
80 use insta
::assert_ron_snapshot
;
81 assert_ron_snapshot
!("user_ron", &User
{
83 username
: "john_ron".to_string(),
84 email
: Email("john@example.com".to_string()),
85 extra
: "".to_string(),
92 fn test_with_random_value_json() {
93 assert_json_snapshot
!("user_json", &User
{
95 username
: "jason_doe".to_string(),
96 email
: Email("jason@example.com".to_string()),
97 extra
: "ssn goes here".to_string(),
100 ".extra" => "[extra]"
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]");
110 assert_json_snapshot
!(
111 "user_json_settings",
114 username
: "jason_doe".to_string(),
115 email
: Email("jason@example.com".to_string()),
116 extra
: "ssn goes here".to_string(),
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);
131 assert_json_snapshot
!(
132 "user_json_settings_callback",
135 username
: "jason_doe".to_string(),
136 email
: Email("jason@example.com".to_string()),
137 extra
: "extra here".to_string(),
144 fn test_with_random_value_json_settings2() {
145 with_settings
!({redactions
=> vec
![
146 (".id", "[id]".into()),
147 (".extra", "[extra]".into()),
149 assert_json_snapshot
!(
152 username
: "jason_doe".to_string(),
153 email
: Email("jason@example.com".to_string()),
154 extra
: "ssn goes here".to_string(),
161 fn test_redact_newtype() {
162 #[derive(Serialize, Clone)]
169 pub struct UserWrapper(User
);
173 name
: "my-name".into(),
175 let wrapper
= UserWrapper(user
.clone());
177 // This works as expected
178 assert_json_snapshot
!(user
, {
187 // This fails - 'id' is not redacted
188 assert_json_snapshot
!(wrapper
, {
199 fn test_redact_recursive() {
203 next
: Option
<Box
<Node
>>,
208 next
: Some(Box
::new(Node { id: 1, next: None }
)),
211 assert_json_snapshot
!(root
, {