Working on a testing method
This commit is contained in:
17
src/main.rs
17
src/main.rs
@ -2,6 +2,9 @@ use anyhow::Result;
|
||||
use clap::{Parser, Subcommand};
|
||||
use libjrpc::{targets::rust::RustCompiler, FileProcessor};
|
||||
|
||||
#[cfg(test)]
|
||||
mod test;
|
||||
|
||||
#[derive(Parser)]
|
||||
struct Cli {
|
||||
#[command(subcommand)]
|
||||
@ -14,6 +17,7 @@ struct Cli {
|
||||
enum Commands {
|
||||
Compile {
|
||||
input: String,
|
||||
#[arg(short, long)]
|
||||
output: String,
|
||||
#[arg(short, long)]
|
||||
definition: Option<String>,
|
||||
@ -39,7 +43,18 @@ pub fn main() -> Result<()> {
|
||||
let mut fp = FileProcessor::new();
|
||||
let ir = fp.start_compile(&input)?;
|
||||
|
||||
libjrpc::targets::compile::<RustCompiler>(ir, &output)?;
|
||||
let output_split = output.split(':').collect::<Vec<&str>>();
|
||||
let output_target = output_split[0];
|
||||
let output_dir = output_split[1];
|
||||
|
||||
match output_target {
|
||||
"rust" => {
|
||||
libjrpc::targets::compile::<RustCompiler>(ir, output_dir)?;
|
||||
}
|
||||
_ => {
|
||||
println!("Unsupported target: {}", output_target);
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: Implement definition output!
|
||||
}
|
||||
|
77
src/test.rs
Normal file
77
src/test.rs
Normal 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!")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user