1+ use indexmap:: IndexMap ;
2+ use std:: path:: { Path , PathBuf } ;
3+
14mod utils;
2- use utils:: * ;
5+ use utils:: { assert_eq , * } ;
36
4- #[ test]
5- fn test_proofs ( ) -> eyre:: Result < ( ) > {
7+ fn get_proofs ( dir : & str ) -> Result < Vec < PathBuf > > {
68 let mut proofs = vec ! [ ] ;
7- for entry in std:: fs:: read_dir ( "tests/proofs" ) ? {
9+ for entry in std:: fs:: read_dir ( dir ) ? {
810 let entry = entry?;
911 let path = entry. path ( ) ;
1012 if path. is_file ( )
@@ -13,8 +15,35 @@ fn test_proofs() -> eyre::Result<()> {
1315 proofs. push ( path) ;
1416 }
1517 }
16-
1718 proofs. sort ( ) ;
19+ Ok ( proofs)
20+ }
21+
22+ fn assert_unique_hash ( proofs : & [ PathBuf ] , v_json : & [ Vec < SerFunction > ] ) {
23+ let mut map = IndexMap :: < & str , Vec < ( & Path , & str ) > > :: new ( ) ;
24+ for ( v, proof) in v_json. iter ( ) . zip ( proofs) {
25+ for json in v {
26+ let def_id = & * json. def_id ;
27+ let item = ( & * * proof, def_id) ;
28+ map. entry ( & json. hash )
29+ . and_modify ( |v_item| v_item. push ( item) )
30+ . or_insert_with ( || vec ! [ item] ) ;
31+ }
32+ }
33+ for ( hash, v_item) in & map {
34+ let v_item_len = v_item. len ( ) ;
35+ assert_eq ! (
36+ v_item_len, 1 ,
37+ "{hash} should only correspond to single item, but \
38+ {v_item_len} items have the hash value:\n {v_item:?}",
39+ ) ;
40+ }
41+ }
42+
43+ #[ test]
44+ fn test_proofs ( ) -> Result < ( ) > {
45+ let proofs = get_proofs ( "tests/proofs" ) ?;
46+
1847 expect ! [ [ r#"
1948 [
2049 "tests/proofs/ad_hoc.rs",
@@ -26,12 +55,41 @@ fn test_proofs() -> eyre::Result<()> {
2655 "# ] ]
2756 . assert_debug_eq ( & proofs) ;
2857
58+ let mut v_json = Vec :: < Vec < SerFunction > > :: with_capacity ( proofs. len ( ) ) ;
59+ for path in & proofs {
60+ let file_stem = path. file_stem ( ) . and_then ( |f| f. to_str ( ) ) . unwrap ( ) ;
61+ let text = cmd ( & [ & format ! ( "tests/proofs/{file_stem}.rs" ) ] ) ;
62+ expect_file ! [ format!( "./snapshots/{file_stem}.json" ) ] . assert_eq ( & text) ;
63+ v_json. push ( serde_json:: from_str ( & text) . unwrap ( ) ) ;
64+ }
65+
66+ assert_unique_hash ( & proofs, & v_json) ;
67+ Ok ( ( ) )
68+ }
69+
70+ #[ test]
71+ fn test_compare ( ) -> Result < ( ) > {
72+ let proofs = get_proofs ( "tests/compare" ) ?;
73+
74+ expect ! [ [ r#"
75+ [
76+ "tests/compare/contract1.rs",
77+ "tests/compare/contract2.rs",
78+ "tests/compare/proof1.rs",
79+ "tests/compare/proof2.rs",
80+ ]
81+ "# ] ]
82+ . assert_debug_eq ( & proofs) ;
83+
84+ let mut v_json = Vec :: < Vec < SerFunction > > :: with_capacity ( proofs. len ( ) ) ;
2985 for path in & proofs {
3086 let file_stem = path. file_stem ( ) . and_then ( |f| f. to_str ( ) ) . unwrap ( ) ;
31- let json = cmd ( & [ & format ! ( "tests/proofs/{file_stem}.rs" ) ] ) ;
32- expect_file ! [ format!( "./snapshots/{file_stem}.json" ) ] . assert_eq ( & json) ;
87+ let text = cmd ( & [ & format ! ( "tests/compare/{file_stem}.rs" ) ] ) ;
88+ // NOTE: don't write text to json file, since compare.rs write it in a different way
89+ v_json. push ( serde_json:: from_str ( & text) . unwrap ( ) ) ;
3390 }
3491
92+ assert_unique_hash ( & proofs, & v_json) ;
3593 Ok ( ( ) )
3694}
3795
0 commit comments