1
1
mod format;
2
2
mod normalize;
3
+ mod runtime;
3
4
mod source;
4
5
#[ cfg( test) ]
5
6
mod tests;
@@ -10,6 +11,18 @@ pub use normalize::NormalizeMatches;
10
11
pub use normalize:: NormalizeNewlines ;
11
12
pub use normalize:: NormalizePaths ;
12
13
pub use source:: DataSource ;
14
+ pub use source:: Inline ;
15
+ pub use source:: Position ;
16
+
17
+ pub trait ToDebug {
18
+ fn to_debug ( & self ) -> Data ;
19
+ }
20
+
21
+ impl < D : std:: fmt:: Debug > ToDebug for D {
22
+ fn to_debug ( & self ) -> Data {
23
+ Data :: text ( format ! ( "{:#?}\n " , self ) )
24
+ }
25
+ }
13
26
14
27
/// Declare an expected value for an assert from a file
15
28
///
@@ -53,6 +66,37 @@ macro_rules! file {
53
66
} } ;
54
67
}
55
68
69
+ /// Declare an expected value from within Rust source
70
+ ///
71
+ /// ```
72
+ /// # use snapbox::str;
73
+ /// str![["
74
+ /// Foo { value: 92 }
75
+ /// "]];
76
+ /// str![r#"{"Foo": 92}"#];
77
+ /// ```
78
+ ///
79
+ /// Leading indentation is stripped.
80
+ #[ macro_export]
81
+ macro_rules! str {
82
+ [ $data: literal] => { $crate:: str ![ [ $data] ] } ;
83
+ [ [ $data: literal] ] => { {
84
+ let position = $crate:: data:: Position {
85
+ file: $crate:: path:: current_rs!( ) ,
86
+ line: line!( ) ,
87
+ column: column!( ) ,
88
+ } ;
89
+ let inline = $crate:: data:: Inline {
90
+ position,
91
+ data: $data,
92
+ indent: true ,
93
+ } ;
94
+ inline
95
+ } } ;
96
+ [ ] => { $crate:: str ![ [ "" ] ] } ;
97
+ [ [ ] ] => { $crate:: str ![ [ "" ] ] } ;
98
+ }
99
+
56
100
/// Test fixture, actual output, or expected result
57
101
///
58
102
/// This provides conveniences for tracking the intended format (binary vs text).
@@ -165,6 +209,9 @@ impl Data {
165
209
pub fn write_to ( & self , source : & DataSource ) -> Result < ( ) , crate :: Error > {
166
210
match & source. inner {
167
211
source:: DataSourceInner :: Path ( p) => self . write_to_path ( p) ,
212
+ source:: DataSourceInner :: Inline ( p) => runtime:: get ( )
213
+ . write ( self , p)
214
+ . map_err ( |err| err. to_string ( ) . into ( ) ) ,
168
215
}
169
216
}
170
217
0 commit comments