Skip to content

Commit 3896f1f

Browse files
committed
test: assert_unique_hash for proofs under compare folder
1 parent 59bcc96 commit 3896f1f

File tree

1 file changed

+47
-22
lines changed

1 file changed

+47
-22
lines changed

tests/proofs.rs

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use std::path::{Path, PathBuf};
44
mod utils;
55
use utils::{assert_eq, *};
66

7-
pub fn get_proofs() -> Result<Vec<PathBuf>> {
7+
fn get_proofs(dir: &str) -> Result<Vec<PathBuf>> {
88
let mut proofs = vec![];
9-
for entry in std::fs::read_dir("tests/proofs")? {
9+
for entry in std::fs::read_dir(dir)? {
1010
let entry = entry?;
1111
let path = entry.path();
1212
if path.is_file()
@@ -19,9 +19,30 @@ pub fn get_proofs() -> Result<Vec<PathBuf>> {
1919
Ok(proofs)
2020
}
2121

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+
2243
#[test]
2344
fn test_proofs() -> Result<()> {
24-
let proofs = get_proofs()?;
45+
let proofs = get_proofs("tests/proofs")?;
2546

2647
expect![[r#"
2748
[
@@ -43,29 +64,33 @@ fn test_proofs() -> Result<()> {
4364
}
4465

4566
assert_unique_hash(&proofs, &v_json);
46-
4767
Ok(())
4868
}
4969

50-
fn assert_unique_hash(proofs: &[PathBuf], v_json: &[Vec<SerFunction>]) {
51-
let mut map = IndexMap::<&str, Vec<(&Path, &str)>>::new();
52-
for (v, proof) in v_json.iter().zip(proofs) {
53-
for json in v {
54-
let def_id = &*json.def_id;
55-
let item = (&**proof, def_id);
56-
map.entry(&json.hash)
57-
.and_modify(|v_item| v_item.push(item))
58-
.or_insert_with(|| vec![item]);
59-
}
60-
}
61-
for (hash, v_item) in &map {
62-
let v_item_len = v_item.len();
63-
assert_eq!(
64-
v_item_len, 1,
65-
"{hash} should only correspond to single item, but \
66-
{v_item_len} items have the hash value:\n{v_item:?}",
67-
);
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());
85+
for path in &proofs {
86+
let file_stem = path.file_stem().and_then(|f| f.to_str()).unwrap();
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());
6890
}
91+
92+
assert_unique_hash(&proofs, &v_json);
93+
Ok(())
6994
}
7095

7196
#[test]

0 commit comments

Comments
 (0)