From 66c71e7e40036e853d05327e3dbb522d16c9b2f1 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Thu, 10 Aug 2023 18:46:19 -0400 Subject: [PATCH] Switch the compiled bin to running a diff just like the automated tests. This is mostly so I can test a variety of org-mode documents without needing to integrate them into the org samples folder. --- src/main.rs | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 59a2cd1a..3ad6b6fe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,51 @@ #![feature(round_char_boundary)] +use std::path::Path; + use ::organic::parser::document; +use organic::compare_document; +use organic::emacs_parse_org_document; +use organic::parser::sexp::sexp_with_padding; use crate::init_tracing::init_telemetry; use crate::init_tracing::shutdown_telemetry; mod init_tracing; -const TEST_DOC: &'static str = include_str!("../toy_language.txt"); - fn main() -> Result<(), Box> { init_telemetry()?; - let parsed = document(TEST_DOC); - println!("{}\n\n\n", TEST_DOC); - println!("{:#?}", parsed); + run_compare( + std::env::args() + .nth(1) + .expect("Pass a single file into this script."), + )?; shutdown_telemetry()?; Ok(()) } + +fn run_compare>(todo_org_path: P) -> Result<(), Box> { + let org_contents = std::fs::read_to_string(todo_org_path.as_ref()).expect("Read org file."); + let (remaining, rust_parsed) = document(org_contents.as_str()).expect("Org Parse failure"); + let org_sexp = + emacs_parse_org_document(todo_org_path.as_ref()).expect("Use emacs to parse org file."); + let (_remaining, parsed_sexp) = + sexp_with_padding(org_sexp.as_str()).expect("Sexp Parse failure"); + + println!("{}\n\n\n", org_contents.as_str()); + println!("{}", org_sexp); + println!("{:#?}", rust_parsed); + + // We do the diffing after printing out both parsed forms in case the diffing panics + let diff_result = + compare_document(&parsed_sexp, &rust_parsed).expect("Compare parsed documents."); + diff_result + .print() + .expect("Print document parse tree diff."); + + if diff_result.is_bad() { + Err("Diff results do not match.")?; + } + if remaining != "" { + Err(format!("There was unparsed text remaining: {}", remaining))?; + } + + Ok(()) +}