1
- use bson:: { self , Bson , Document } ;
2
-
3
- fn print_doc_with_indent_level ( doc : & Document , n : i32 ) {
4
- macro_rules! print_spaces {
5
- ( $n: expr ) => {
6
- for _ in 0 ..$n {
7
- print!( " " ) ;
8
- }
9
- } ;
10
- }
11
-
12
- println ! ( "{{" ) ;
13
-
14
- for ( key, value) in doc. iter ( ) {
15
- print_spaces ! ( n + 4 ) ;
16
- print ! ( "{}: " , key) ;
17
-
18
- print_bson_with_indent_level ( value, n) ;
19
- println ! ( "" ) ;
20
- }
21
-
22
- print_spaces ! ( n) ;
23
- println ! ( "}}" ) ;
24
- }
25
-
26
- #[ macro_export]
27
- macro_rules! print_doc {
28
- ( $doc: expr ) => {
29
- print_doc_with_indent_level( $doc, 0 )
30
- } ;
31
- }
32
-
33
- fn print_bson_with_indent_level ( bson : & Bson , n : i32 ) {
34
- match bson {
35
- & Bson :: Document ( ref d) => print_doc_with_indent_level ( & d, n + 4 ) ,
36
- & Bson :: String ( ref s) => print ! ( "\" {}\" ," , s) ,
37
- & Bson :: Boolean ( b) => print ! ( "{}," , b) ,
38
- & Bson :: Array ( ref v) => {
39
- print ! ( "[ " ) ;
40
-
41
- for b in v {
42
- print_bson_with_indent_level ( b, n) ;
43
- print ! ( " " ) ;
44
- }
45
-
46
- print ! ( "]" ) ;
47
- } ,
48
- ref bson => print ! ( "{:?}," , bson)
49
- } ;
50
- }
51
-
1
+ use bson:: Bson ;
52
2
53
3
#[ test]
54
4
fn recursive_macro ( ) {
@@ -64,5 +14,60 @@ fn recursive_macro() {
64
14
"c" => [ -7 ]
65
15
} ;
66
16
67
- print_doc ! ( & doc) ;
17
+ match doc. get ( "a" ) {
18
+ Some ( & Bson :: String ( ref s) ) => assert_eq ! ( "foo" , s) ,
19
+ _ => panic ! ( "String 'foo' was not inserted correctly." ) ,
20
+ }
21
+
22
+ // Inner Doc 1
23
+ match doc. get ( "b" ) {
24
+ Some ( & Bson :: Document ( ref doc) ) => {
25
+
26
+ // Inner doc 2
27
+ match doc. get ( "bar" ) {
28
+ Some ( & Bson :: Document ( ref inner_doc) ) => {
29
+
30
+ // Inner array
31
+ match inner_doc. get ( "harbor" ) {
32
+ Some ( & Bson :: Array ( ref arr) ) => {
33
+ assert_eq ! ( 2 , arr. len( ) ) ;
34
+
35
+ // Match array items
36
+ match arr[ 0 ] {
37
+ Bson :: String ( ref s) => assert_eq ! ( "seal" , s) ,
38
+ _ => panic ! ( "String 'seal' was not inserted into inner array correctly." ) ,
39
+ }
40
+ match arr[ 1 ] {
41
+ Bson :: Boolean ( ref b) => assert ! ( !b) ,
42
+ _ => panic ! ( "Boolean 'false' was not inserted into inner array correctly." ) ,
43
+ }
44
+ } ,
45
+ _ => panic ! ( "Inner array was not inserted correctly." ) ,
46
+ }
47
+
48
+ // Inner floating point
49
+ match inner_doc. get ( "jelly" ) {
50
+ Some ( & Bson :: FloatingPoint ( ref fp) ) => assert_eq ! ( 42.0 , * fp) ,
51
+ _ => panic ! ( "Floating point 42.0 was not inserted correctly." ) ,
52
+ }
53
+ } ,
54
+ _ => panic ! ( "Second inner document was not inserted correctly." ) ,
55
+ }
56
+ } ,
57
+ _ => panic ! ( "Inner document was not inserted correctly." ) ,
58
+ }
59
+
60
+ // Outer array
61
+ match doc. get ( "c" ) {
62
+ Some ( & Bson :: Array ( ref arr) ) => {
63
+ assert_eq ! ( 1 , arr. len( ) ) ;
64
+
65
+ // Integer type
66
+ match arr[ 0 ] {
67
+ Bson :: I32 ( ref i) => assert_eq ! ( -7 , * i) ,
68
+ _ => panic ! ( "I32 '-7' was not inserted correctly." ) ,
69
+ }
70
+ } ,
71
+ _ => panic ! ( "Array was not inserted correctly." ) ,
72
+ }
68
73
}
0 commit comments