diff --git a/docs-test-gen/.gitignore b/docs-test-gen/.gitignore new file mode 100644 index 00000000..16737b13 --- /dev/null +++ b/docs-test-gen/.gitignore @@ -0,0 +1,2 @@ +tests/ +go_tests/ diff --git a/docs-test-gen/src/main.rs b/docs-test-gen/src/main.rs index df62bc82..149c6180 100644 --- a/docs-test-gen/src/main.rs +++ b/docs-test-gen/src/main.rs @@ -1,20 +1,19 @@ -use anyhow::Result; +use anyhow::{anyhow, Result}; use glob::glob; use phf::phf_map; use pulldown_cmark::{CodeBlockKind, Event, Tag, TagEnd}; -use std::fs; -use strum::{EnumIter, IntoEnumIterator}; +use std::{fs, path::Path}; +use strum::{AsRefStr, EnumIter, IntoEnumIterator}; static TEMPLATES: phf::Map<&'static str, &'static str> = phf_map! { "core" => include_str!("../templates/core.tpl"), }; -fn is_goey(fence: &str) -> bool { - fence.split_whitespace().any(|item| item.trim() == "go") -} - -fn is_rusty(fence: &str) -> bool { - fence.split_whitespace().any(|item| item.trim() == "rust") +#[inline] +fn find_language(fence: &str) -> Option { + fence.split_whitespace().find_map(|item| { + Language::iter().find(|language| language.as_ref().eq_ignore_ascii_case(item)) + }) } fn get_template_name(fence: &str) -> Option<&str> { @@ -32,7 +31,8 @@ fn get_template_name(fence: &str) -> Option<&str> { None } -#[derive(Clone, Copy, Debug, EnumIter)] +#[derive(AsRefStr, Clone, Copy, Debug, EnumIter)] +#[strum(serialize_all = "lowercase")] enum Language { Go, Rust, @@ -61,86 +61,91 @@ struct CodeBlock { code: String, } -fn main() -> Result<()> { - let mdx_files = glob("../src/**/*.mdx").unwrap(); - - for language in Language::iter() { - fs::create_dir_all(language.test_dir())?; - } +fn process_file(path: &Path) -> Result<()> { + let src = fs::read_to_string(path)?; + let parser = pulldown_cmark::Parser::new(&src); - for path in mdx_files { - let path = path?; + let mut is_aggregating = false; - let src = fs::read_to_string(&path)?; - let parser = pulldown_cmark::Parser::new(&src); + let mut language = Language::Rust; + let mut template = String::new(); + let mut code = String::new(); + let mut blocks = Vec::new(); - let mut is_aggregating = false; + for event in parser { + if is_aggregating { + if let Event::Text(ref text) = event { + code.push_str(text); + } + } - let mut language = Language::Rust; - let mut template = String::new(); - let mut agg = String::new(); - let mut blocks = Vec::new(); + match event { + Event::Start(Tag::CodeBlock(CodeBlockKind::Fenced(fence))) => { + language = match find_language(&fence) { + Some(found_lang) => found_lang, + None => continue, + }; - for event in parser { - if is_aggregating { - if let Event::Text(ref text) = event { - agg.push_str(&text); + if let Some(template_name) = get_template_name(&fence) { + is_aggregating = true; + template = template_name.to_string(); } } + Event::End(TagEnd::CodeBlock) if is_aggregating => { + is_aggregating = false; - match event { - Event::Start(Tag::CodeBlock(CodeBlockKind::Fenced(fence))) => { - if is_rusty(&fence) { - language = Language::Rust; - } else if is_goey(&fence) { - language = Language::Go; - } else { - continue; - } - - if let Some(template_name) = get_template_name(&fence) { - is_aggregating = true; - template = template_name.to_string(); - } - } - Event::End(TagEnd::CodeBlock) if is_aggregating => { - is_aggregating = false; - - blocks.push(CodeBlock { - template: template.clone(), - code: agg.clone(), - language, - }); + blocks.push(CodeBlock { + template: template.clone(), + code: code.clone(), + language, + }); - template.clear(); - agg.clear(); - } - _ => (), + template.clear(); + code.clear(); } + _ => (), } + } - if blocks.is_empty() { - continue; - } + for (idx, block) in blocks.iter().enumerate() { + let mdx_path = path.to_str().ok_or_else(|| { + anyhow!( + "filename of path \"{}\" isn't valid utf-8. (really?! in the 21st century?)", + path.display() + ) + })? + .replace("../", "") + .replace('/', "_"); + + let filename = format!( + "{}_{}.{}", + mdx_path.replace('.', "_"), + idx, + block.language.file_ext(), + ); + let path = format!("{}/{filename}", block.language.test_dir()); + + let template = TEMPLATES + .get(&block.template) + .ok_or_else(|| anyhow!("template \"{}\" not found", block.template))?; + let code = template.replace("{{code}}", &block.code); + + fs::write(path, code)?; + } - for (idx, block) in blocks.iter().enumerate() { - // Actually write the tests into files or smth. Please kill me. + Ok(()) +} - let mdx_filename = path.file_name().unwrap().to_str().unwrap(); - let filename = format!( - "{}_{}_{}.{}", - mdx_filename.replace(".", "_"), - block.template, - idx, - block.language.file_ext(), - ); - let path = format!("{}/{filename}", block.language.test_dir()); +fn main() -> Result<()> { + let mdx_files = glob("../src/**/*.mdx")?; - let template = TEMPLATES.get(&block.template).unwrap(); - let code = template.replace("{{code}}", &block.code); + for language in Language::iter() { + fs::create_dir_all(language.test_dir())?; + } - fs::write(path, code)?; - } + for path in mdx_files { + let path = path?; + process_file(&path)?; } Ok(()) diff --git a/docs-test-gen/tests/entrypoints_mdx_core_0.rs b/docs-test-gen/tests/entrypoints_mdx_core_0.rs deleted file mode 100644 index cc81149a..00000000 --- a/docs-test-gen/tests/entrypoints_mdx_core_0.rs +++ /dev/null @@ -1,26 +0,0 @@ -use cosmwasm_std::*; -use cosmwasm_schema::cw_serde; - -#[cw_serde] -struct InstantiateMsg {} - -#[cw_serde] -struct QueryMsg {} - -#[cw_serde] -struct ExecuteMsg {} - -#[test] -fn doctest() { - #[entry_point] -pub fn instantiate( - deps: DepsMut, - env: Env, - info: MessageInfo, - msg: InstantiateMsg, -) -> Result { - // Do some logic here - Ok(Response::default()) -} - -}