From 07e11e359afd366f67b12acc083ac17c8f51b8fe Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Fri, 29 Sep 2023 16:37:22 -0400 Subject: [PATCH] Add tests for odd headline levels. --- notes/test_names.org | 3 ++- src/compare/compare.rs | 4 ++-- src/compare/parse.rs | 13 +++++++++++-- src/context/global_settings.rs | 14 +++++++++++--- src/context/mod.rs | 1 + src/lib.rs | 10 +++++++--- src/main.rs | 4 ++-- src/parser/in_buffer_settings.rs | 2 +- tests/test_template | 24 +++++++++++++++++++----- 9 files changed, 56 insertions(+), 19 deletions(-) diff --git a/notes/test_names.org b/notes/test_names.org index d9e4f54..fa0dde3 100644 --- a/notes/test_names.org +++ b/notes/test_names.org @@ -1,6 +1,7 @@ * Autogen tests The autogen tests are the tests automatically generated to compare the output of Organic vs the upstream Emacs Org-mode parser using the sample documents in the =org_mode_samples= folder. They will have a prefix based on the settings for each test. -- No prefix :: The test is run with the default settings (The upstream Emacs Org-mode determines the default settings) +- default :: The test is run with the default settings (The upstream Emacs Org-mode determines the default settings) - la :: Short for "list alphabetic". Enables alphabetic plain lists. - t# :: Sets the tab-width to # (as in t4 sets the tab-width to 4). +- odd :: Sets the org-odd-levels-only setting to true (meaning "odd" as opposed to "oddeven"). diff --git a/src/compare/compare.rs b/src/compare/compare.rs index 4a1c033..0224267 100644 --- a/src/compare/compare.rs +++ b/src/compare/compare.rs @@ -6,9 +6,9 @@ use crate::compare::parse::emacs_parse_file_org_document; use crate::compare::parse::get_emacs_version; use crate::compare::parse::get_org_mode_version; use crate::compare::sexp::sexp; +use crate::context::GlobalSettings; +use crate::context::LocalFileAccessInterface; use crate::parser::parse_with_settings; -use crate::GlobalSettings; -use crate::LocalFileAccessInterface; pub fn run_anonymous_compare>( org_contents: P, diff --git a/src/compare/parse.rs b/src/compare/parse.rs index 0a35077..cfa3e17 100644 --- a/src/compare/parse.rs +++ b/src/compare/parse.rs @@ -1,7 +1,8 @@ use std::path::Path; use std::process::Command; -use crate::GlobalSettings; +use crate::context::HeadlineLevelFilter; +use crate::settings::GlobalSettings; /// Generate elisp to configure org-mode parsing settings /// @@ -12,7 +13,15 @@ fn global_settings_elisp(global_settings: &GlobalSettings) -> String { if global_settings.list_allow_alphabetical { ret += "(setq org-list-allow-alphabetical t)\n" } - ret += format!("(setq-default tab-width {})", global_settings.tab_width).as_str(); + if global_settings.tab_width != crate::settings::DEFAULT_TAB_WIDTH { + ret += format!("(setq-default tab-width {})", global_settings.tab_width).as_str(); + } + if global_settings.odd_levels_only != HeadlineLevelFilter::default() { + ret += match global_settings.odd_levels_only { + HeadlineLevelFilter::Odd => "(setq org-odd-levels-only t)\n", + HeadlineLevelFilter::OddEven => "(setq org-odd-levels-only nil)\n", + }; + } ret } diff --git a/src/context/global_settings.rs b/src/context/global_settings.rs index 5932cc1..c12bbcf 100644 --- a/src/context/global_settings.rs +++ b/src/context/global_settings.rs @@ -29,6 +29,8 @@ pub struct GlobalSettings<'g, 's> { pub odd_levels_only: HeadlineLevelFilter, } +pub const DEFAULT_TAB_WIDTH: IndentationLevel = 8; + impl<'g, 's> GlobalSettings<'g, 's> { fn new() -> GlobalSettings<'g, 's> { GlobalSettings { @@ -39,8 +41,8 @@ impl<'g, 's> GlobalSettings<'g, 's> { in_progress_todo_keywords: BTreeSet::new(), complete_todo_keywords: BTreeSet::new(), list_allow_alphabetical: false, - tab_width: 8, - odd_levels_only: HeadlineLevelFilter::OddEven, + tab_width: DEFAULT_TAB_WIDTH, + odd_levels_only: HeadlineLevelFilter::default(), } } } @@ -51,8 +53,14 @@ impl<'g, 's> Default for GlobalSettings<'g, 's> { } } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub enum HeadlineLevelFilter { Odd, OddEven, } + +impl Default for HeadlineLevelFilter { + fn default() -> Self { + HeadlineLevelFilter::OddEven + } +} diff --git a/src/context/mod.rs b/src/context/mod.rs index c2d4a0e..7e45fdd 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -26,5 +26,6 @@ pub use file_access_interface::FileAccessInterface; pub use file_access_interface::LocalFileAccessInterface; pub use global_settings::GlobalSettings; pub use global_settings::HeadlineLevelFilter; +pub use global_settings::DEFAULT_TAB_WIDTH; pub(crate) use list::List; pub(crate) use parser_with_context::parser_with_context; diff --git a/src/lib.rs b/src/lib.rs index de24125..b8a8d10 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,10 @@ mod iter; pub mod parser; pub mod types; -pub use context::FileAccessInterface; -pub use context::GlobalSettings; -pub use context::LocalFileAccessInterface; +pub mod settings { + pub use crate::context::FileAccessInterface; + pub use crate::context::GlobalSettings; + pub use crate::context::HeadlineLevelFilter; + pub use crate::context::LocalFileAccessInterface; + pub use crate::context::DEFAULT_TAB_WIDTH; +} diff --git a/src/main.rs b/src/main.rs index e6e7880..6cdd851 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,8 +5,8 @@ use std::path::Path; use ::organic::parser::parse; use organic::parser::parse_with_settings; -use organic::GlobalSettings; -use organic::LocalFileAccessInterface; +use organic::settings::GlobalSettings; +use organic::settings::LocalFileAccessInterface; #[cfg(feature = "tracing")] use crate::init_tracing::init_telemetry; diff --git a/src/parser/in_buffer_settings.rs b/src/parser/in_buffer_settings.rs index 58976ab..167d428 100644 --- a/src/parser/in_buffer_settings.rs +++ b/src/parser/in_buffer_settings.rs @@ -11,8 +11,8 @@ use super::OrgSource; use crate::context::HeadlineLevelFilter; use crate::error::CustomError; use crate::error::Res; +use crate::settings::GlobalSettings; use crate::types::Keyword; -use crate::GlobalSettings; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub(crate) fn scan_for_in_buffer_settings<'s>( diff --git a/tests/test_template b/tests/test_template index 1376ed3..108ac0c 100644 --- a/tests/test_template +++ b/tests/test_template @@ -1,8 +1,8 @@ -// TODO: Investigate writing a proc macro to make specifying these permutations easier. +// TODO: Investigate writing a proc macro to make specifying these combinations easier. For example, currently I am only setting 1 setting per test to keep the repetition reasonable when I should be mixing all the different combinations. {expect_fail} #[test] -fn autogen_{name}() -> Result<(), Box> {{ +fn autogen_default_{name}() -> Result<(), Box> {{ let org_path = "{path}"; let org_contents = std::fs::read_to_string(org_path).expect("Read org file."); organic::compare::run_anonymous_compare(org_contents.as_str())?; @@ -15,7 +15,7 @@ fn autogen_la_{name}() -> Result<(), Box> {{ let org_path = "{path}"; let org_contents = std::fs::read_to_string(org_path).expect("Read org file."); let global_settings = {{ - let mut global_settings = organic::GlobalSettings::default(); + let mut global_settings = organic::settings::GlobalSettings::default(); global_settings.list_allow_alphabetical = true; global_settings }}; @@ -29,7 +29,7 @@ fn autogen_t1_{name}() -> Result<(), Box> {{ let org_path = "{path}"; let org_contents = std::fs::read_to_string(org_path).expect("Read org file."); let global_settings = {{ - let mut global_settings = organic::GlobalSettings::default(); + let mut global_settings = organic::settings::GlobalSettings::default(); global_settings.tab_width = 1; global_settings }}; @@ -43,10 +43,24 @@ fn autogen_t16_{name}() -> Result<(), Box> {{ let org_path = "{path}"; let org_contents = std::fs::read_to_string(org_path).expect("Read org file."); let global_settings = {{ - let mut global_settings = organic::GlobalSettings::default(); + let mut global_settings = organic::settings::GlobalSettings::default(); global_settings.tab_width = 16; global_settings }}; organic::compare::run_anonymous_compare_with_settings(org_contents.as_str(), &global_settings)?; Ok(()) }} + +{expect_fail} +#[test] +fn autogen_odd_{name}() -> Result<(), Box> {{ + let org_path = "{path}"; + let org_contents = std::fs::read_to_string(org_path).expect("Read org file."); + let global_settings = {{ + let mut global_settings = organic::settings::GlobalSettings::default(); + global_settings.odd_levels_only = organic::settings::HeadlineLevelFilter::Odd; + global_settings + }}; + organic::compare::run_anonymous_compare_with_settings(org_contents.as_str(), &global_settings)?; + Ok(()) +}}