Working on a testing method

This commit is contained in:
2025-05-26 22:28:31 +02:00
parent b61518de00
commit c8e72dbba8
10 changed files with 130 additions and 10 deletions

77
src/test.rs Normal file
View File

@@ -0,0 +1,77 @@
use std::{
fs::File,
io::{Read, Stderr, Stdout},
process::{Command, Stdio},
};
#[test]
fn compare_tools() {
let targets = vec!["rust"];
for target in targets {
std::fs::remove_dir_all("./tests").unwrap();
std::fs::create_dir_all("./tests").unwrap();
Command::new("cargo")
.arg("run")
.arg("--")
.arg("compile")
.arg("--verbose")
.arg("examples/test.jrpc")
.arg("-o")
.arg(target.to_string() + ":tests/rust")
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
.expect("Failed to spawn process")
.wait()
.unwrap();
Command::new("node")
.arg("JsonRPC/lib/jrpc.js")
.arg("compile")
.arg("--verbose")
.arg("examples/test.jrpc")
.arg("-o")
.arg(target.to_string() + ":tests/js")
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
.expect("Failed to spawn process")
.wait()
.unwrap();
let rust_files = walkdir::WalkDir::new("tests/rust")
.into_iter()
.map(|e| e.unwrap())
.filter(|e| e.file_type().is_file())
.collect::<Vec<_>>();
let js_files = walkdir::WalkDir::new("tests/js")
.into_iter()
.map(|e| e.unwrap())
.filter(|e| e.file_type().is_file())
.collect::<Vec<_>>();
if rust_files.len() != js_files.len() {
panic!("Number of files mismatch");
}
for (rust_file, js_file) in rust_files.iter().zip(js_files.iter()) {
println!("Testing files {:?} {:?}", rust_file.path(), js_file.path());
let mut rust_str = String::new();
File::open(rust_file.path())
.unwrap()
.read_to_string(&mut rust_str)
.unwrap();
let mut js_str = String::new();
File::open(js_file.path())
.unwrap()
.read_to_string(&mut js_str)
.unwrap();
if rust_str != js_str {
panic!("Files are different!")
}
}
}
}