From c8e72dbba896bce44bf1139440a4b056e15e6b64 Mon Sep 17 00:00:00 2001 From: Fabian Stamm Date: Mon, 26 May 2025 22:28:31 +0200 Subject: [PATCH] Working on a testing method --- Cargo.lock | 29 ++++++++++++ Cargo.toml | 4 +- {libjrpc => examples}/import.jrpc | 2 +- {libjrpc => examples}/test.jrpc | 2 +- libjrpc/src/compile.rs | 2 +- libjrpc/src/lib.rs | 5 +- libjrpc/src/parser.rs | 2 +- src/main.rs | 17 ++++++- src/test.rs | 77 +++++++++++++++++++++++++++++++ test.ts | 0 10 files changed, 130 insertions(+), 10 deletions(-) rename {libjrpc => examples}/import.jrpc (79%) rename {libjrpc => examples}/test.jrpc (95%) create mode 100644 src/test.rs delete mode 100644 test.ts diff --git a/Cargo.lock b/Cargo.lock index ae8a130..759afe3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -587,6 +587,7 @@ dependencies = [ "clap", "libjrpc", "log", + "walkdir", ] [[package]] @@ -962,6 +963,15 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + [[package]] name = "schannel" version = "0.1.24" @@ -1313,6 +1323,16 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + [[package]] name = "want" version = "0.3.1" @@ -1441,6 +1461,15 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "winapi-util" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" +dependencies = [ + "windows-sys 0.59.0", +] + [[package]] name = "windows-registry" version = "0.2.0" diff --git a/Cargo.toml b/Cargo.toml index d92b644..531b562 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,4 +11,6 @@ members = [".", "zed", "libjrpc"] anyhow = "1" clap = { version = "4", features = ["derive"] } libjrpc = { path = "libjrpc" } -log = "0.4.27" +log = "0.4" +[dev-dependencies] +walkdir = "2" diff --git a/libjrpc/import.jrpc b/examples/import.jrpc similarity index 79% rename from libjrpc/import.jrpc rename to examples/import.jrpc index f823010..95466fe 100644 --- a/libjrpc/import.jrpc +++ b/examples/import.jrpc @@ -1,4 +1,4 @@ -import "./import.jrpc"; +import "./import"; type TestAtom { val_number?: float; diff --git a/libjrpc/test.jrpc b/examples/test.jrpc similarity index 95% rename from libjrpc/test.jrpc rename to examples/test.jrpc index 5cd5024..7d00954 100644 --- a/libjrpc/test.jrpc +++ b/examples/test.jrpc @@ -54,7 +54,7 @@ service TestService { FunctionWithArrayAsParamAndReturn(values1: float[], values2: float[]): float[]; - FunctionWithKeywords(type: float, static: float, event: float): float; + FunctionWithKeywords(_type: float, static: float, event: float): float; } type Test2 { diff --git a/libjrpc/src/compile.rs b/libjrpc/src/compile.rs index 0ac7ebc..2f29104 100644 --- a/libjrpc/src/compile.rs +++ b/libjrpc/src/compile.rs @@ -66,7 +66,7 @@ impl FileGenerator { } pub fn a(&mut self, indent: usize, content: T) { - let line = " ".repeat(indent) + &content.to_string(); + let line = " ".repeat(indent) + &content.to_string(); self.content.push(line); } diff --git a/libjrpc/src/lib.rs b/libjrpc/src/lib.rs index b3175e6..c8c40da 100644 --- a/libjrpc/src/lib.rs +++ b/libjrpc/src/lib.rs @@ -13,10 +13,7 @@ pub use tokenizer::{tokenize, Token, TokenError, TokenPosition, TokenType}; #[cfg(test)] mod test { - use crate::{ - compile::{Compile, CompileContext}, - targets::{self, rust::RustCompiler}, - }; + use crate::targets::{self, rust::RustCompiler}; #[cfg(test)] #[ctor::ctor] diff --git a/libjrpc/src/parser.rs b/libjrpc/src/parser.rs index 6475ff3..aaca716 100644 --- a/libjrpc/src/parser.rs +++ b/libjrpc/src/parser.rs @@ -1,6 +1,6 @@ use anyhow::Result; use log::{debug, trace}; -use std::{collections::HashMap, error::Error, fmt::Display, sync::Arc}; +use std::{error::Error, fmt::Display, sync::Arc}; use crate::{Token, TokenPosition, TokenType}; diff --git a/src/main.rs b/src/main.rs index 630bafd..14b0348 100644 --- a/src/main.rs +++ b/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, @@ -39,7 +43,18 @@ pub fn main() -> Result<()> { let mut fp = FileProcessor::new(); let ir = fp.start_compile(&input)?; - libjrpc::targets::compile::(ir, &output)?; + let output_split = output.split(':').collect::>(); + let output_target = output_split[0]; + let output_dir = output_split[1]; + + match output_target { + "rust" => { + libjrpc::targets::compile::(ir, output_dir)?; + } + _ => { + println!("Unsupported target: {}", output_target); + } + } //TODO: Implement definition output! } diff --git a/src/test.rs b/src/test.rs new file mode 100644 index 0000000..847f193 --- /dev/null +++ b/src/test.rs @@ -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::>(); + + let js_files = walkdir::WalkDir::new("tests/js") + .into_iter() + .map(|e| e.unwrap()) + .filter(|e| e.file_type().is_file()) + .collect::>(); + + 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!") + } + } + } +} diff --git a/test.ts b/test.ts deleted file mode 100644 index e69de29..0000000