Skip to content

Commit e07d632

Browse files
committed
test: mv shared functions to utils; split compare to own test file
1 parent 585e479 commit e07d632

File tree

3 files changed

+76
-69
lines changed

3 files changed

+76
-69
lines changed

tests/compare.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use distributed_verification::SerFunction;
2+
use std::fs::{copy, remove_file};
3+
4+
mod utils;
5+
use utils::{assert_eq, cmd, expect_file};
6+
7+
fn get(text: &str, start: &str) -> SerFunction {
8+
let json = &text[text.find("[\n").unwrap()..];
9+
let v: Vec<SerFunction> = serde_json::from_str(json).unwrap();
10+
v.into_iter().find(|f| f.func.starts_with(start)).unwrap()
11+
}
12+
13+
#[test]
14+
fn compare_proof() {
15+
let file = "tests/compare/proof.rs";
16+
17+
copy("tests/compare/proof1.rs", file).unwrap();
18+
let text1 = &cmd(&["tests/compare/proof.rs"]);
19+
expect_file!["./snapshots/proof1.json"].assert_eq(text1);
20+
21+
// Add another proof won't affact the previous one.
22+
copy("tests/compare/proof2.rs", file).unwrap();
23+
let text2 = &cmd(&["tests/compare/proof.rs"]);
24+
expect_file!["./snapshots/proof2.json"].assert_eq(text2);
25+
26+
remove_file(file).unwrap();
27+
28+
let f = "pub fn f()";
29+
// For the same proof (w.r.t same path and body),
30+
// the hash value must be the same.
31+
assert_eq!(get(text1, f).hash, get(text2, f).hash);
32+
}
33+
34+
#[test]
35+
fn compare_contract() {
36+
let file = "tests/compare/contract.rs";
37+
38+
copy("tests/compare/contract1.rs", file).unwrap();
39+
let text1 = &cmd(&["tests/compare/contract.rs"]);
40+
expect_file!["./snapshots/contract1.json"].assert_eq(text1);
41+
42+
copy("tests/compare/contract2.rs", file).unwrap();
43+
let text2 = &cmd(&["tests/compare/contract.rs"]);
44+
expect_file!["./snapshots/contract2.json"].assert_eq(text2);
45+
46+
remove_file(file).unwrap();
47+
48+
let f = "pub fn f()";
49+
let f1 = get(text1, f);
50+
let f2 = get(text2, f);
51+
let callees1 = f1.callee_sorted_by_file_func();
52+
let callees2 = f2.callee_sorted_by_file_func();
53+
54+
assert_eq!(callees1, callees2);
55+
assert_eq!(f1.hash, f2.hash);
56+
}

tests/snapshots.rs

Lines changed: 2 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
1-
use assert_cmd::Command;
2-
use distributed_verification::SerFunction;
3-
use expect_test::expect_file;
4-
use pretty_assertions::assert_eq;
5-
use std::fs::{copy, remove_file};
6-
7-
fn cmd(args: &[&str]) -> String {
8-
let mut command = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
9-
command.env("RUST_LOG", "off").args(args);
10-
let output = command.output().unwrap();
11-
assert!(
12-
output.status.success(),
13-
"Failed to test standard_proof.rs:\n{}",
14-
std::str::from_utf8(&output.stderr).unwrap()
15-
);
16-
17-
String::from_utf8(output.stdout).unwrap()
18-
}
1+
mod utils;
2+
use utils::*;
193

204
#[test]
215
fn standard_proofs() {
@@ -28,54 +12,3 @@ fn standard_proofs_with_contracts() {
2812
let json = cmd(&["tests/standard_proofs_with_contracts.rs"]);
2913
expect_file!["./snapshots/standard_proofs_with_contracts.json"].assert_eq(&json);
3014
}
31-
32-
fn get(text: &str, start: &str) -> SerFunction {
33-
let json = &text[text.find("[\n").unwrap()..];
34-
let v: Vec<SerFunction> = serde_json::from_str(json).unwrap();
35-
v.into_iter().find(|f| f.func.starts_with(start)).unwrap()
36-
}
37-
38-
#[test]
39-
fn compare_proof() {
40-
let file = "tests/compare/proof.rs";
41-
42-
copy("tests/compare/proof1.rs", file).unwrap();
43-
let text1 = &cmd(&["tests/compare/proof.rs"]);
44-
expect_file!["./snapshots/proof1.json"].assert_eq(text1);
45-
46-
// Add another proof won't affact the previous one.
47-
copy("tests/compare/proof2.rs", file).unwrap();
48-
let text2 = &cmd(&["tests/compare/proof.rs"]);
49-
expect_file!["./snapshots/proof2.json"].assert_eq(text2);
50-
51-
remove_file(file).unwrap();
52-
53-
let f = "pub fn f()";
54-
// For the same proof (w.r.t same path and body),
55-
// the hash value must be the same.
56-
assert_eq!(get(text1, f).hash, get(text2, f).hash);
57-
}
58-
59-
#[test]
60-
fn compare_contract() {
61-
let file = "tests/compare/contract.rs";
62-
63-
copy("tests/compare/contract1.rs", file).unwrap();
64-
let text1 = &cmd(&["tests/compare/contract.rs"]);
65-
expect_file!["./snapshots/contract1.json"].assert_eq(text1);
66-
67-
copy("tests/compare/contract2.rs", file).unwrap();
68-
let text2 = &cmd(&["tests/compare/contract.rs"]);
69-
expect_file!["./snapshots/contract2.json"].assert_eq(text2);
70-
71-
remove_file(file).unwrap();
72-
73-
let f = "pub fn f()";
74-
let f1 = get(text1, f);
75-
let f2 = get(text2, f);
76-
let callees1 = f1.callee_sorted_by_file_func();
77-
let callees2 = f2.callee_sorted_by_file_func();
78-
79-
assert_eq!(callees1, callees2);
80-
assert_eq!(f1.hash, f2.hash);
81-
}

tests/utils.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use assert_cmd::Command;
2+
3+
pub use expect_test::expect_file;
4+
#[allow(unused_imports)] // seems a bug in clippy
5+
pub use pretty_assertions::assert_eq;
6+
7+
pub fn cmd(args: &[&str]) -> String {
8+
let mut command = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
9+
command.env("RUST_LOG", "off").args(args);
10+
let output = command.output().unwrap();
11+
assert!(
12+
output.status.success(),
13+
"Failed to test standard_proof.rs:\n{}",
14+
std::str::from_utf8(&output.stderr).unwrap()
15+
);
16+
17+
String::from_utf8(output.stdout).unwrap()
18+
}

0 commit comments

Comments
 (0)