From: Huck Boles Date: Fri, 5 May 2023 14:51:15 +0000 (-0500) Subject: builder test X-Git-Url: https://git.huck.website/?a=commitdiff_plain;h=07a7207edd2639a70d1238d0f459eb6c434be665;p=metaforge.git builder test --- diff --git a/src/filetype/builder.rs b/src/filetype/builder.rs index 0ea4c55..a3edb3e 100644 --- a/src/filetype/builder.rs +++ b/src/filetype/builder.rs @@ -12,7 +12,7 @@ pub fn build_metafile(file: &MetaFile, dirs: &RootDirs, path: &Path) -> Result<( )?) } -fn metafile_to_string(file: &MetaFile, dirs: &RootDirs, name: Option<&str>) -> Result { +pub fn metafile_to_string(file: &MetaFile, dirs: &RootDirs, name: Option<&str>) -> Result { let mut output = String::default(); for section in file.source.iter() { diff --git a/src/parser/parse.rs b/src/parser/parse.rs index 094c17a..c509760 100644 --- a/src/parser/parse.rs +++ b/src/parser/parse.rs @@ -16,7 +16,7 @@ pub fn parse_file(file: &str) -> Result { Ok(parse_pair(meta_source)) } -pub fn parse_pair(pair: Pair) -> MetaFile { +fn parse_pair(pair: Pair) -> MetaFile { let mut meta_file = MetaFile::new(); if Rule::file == pair.as_rule() { @@ -26,7 +26,8 @@ pub fn parse_pair(pair: Pair) -> MetaFile { Rule::var_def => meta_file.variables = parse_defs(pair.into_inner()), Rule::arr_def => meta_file.arrays = parse_array_defs(pair.into_inner()), Rule::pat_def => meta_file.patterns = parse_defs(pair.into_inner()), - Rule::EOI => (), + // do nothing on end of file + Rule::EOI => continue, // anything else is either hidden or children of previous nodes and will be dealt with // in respective parse functions _ => unreachable!(), @@ -96,6 +97,7 @@ fn parse_sub(pair: Pair) -> &'_ str { fn parse_assign(pair: Pair) -> (&'_ str, &'_ str) { let mut key = ""; let mut val = ""; + for pair in pair.into_inner() { if Rule::key == pair.as_rule() { key = pair.as_str(); @@ -103,7 +105,7 @@ fn parse_assign(pair: Pair) -> (&'_ str, &'_ str) { if Rule::value == pair.as_rule() { let tmp = pair.as_str(); // blank and default shoud be handled by whoever is getting the value - // set it to empty strings do remove it from the HashMap + // set it to empty strings to remove it from the HashMap if tmp == "BLANK" || tmp == "DEFAULT" { return ("", ""); } @@ -121,6 +123,7 @@ fn parse_assign(pair: Pair) -> (&'_ str, &'_ str) { fn parse_assign_array(pair: Pair) -> (&str, Vec<&str>) { let mut key = ""; let mut val = Vec::default(); + for pair in pair.into_inner() { if Rule::key == pair.as_rule() { key = pair.as_str(); @@ -140,9 +143,11 @@ fn parse_array(pairs: Pairs) -> Vec<&str> { if Rule::string == pair.as_rule() { let tmp = pair.as_str(); // remove surrounding quotes from values + // see parse_assign() for reasoning let val = &tmp[1..tmp.len() - 1]; vec.push(val); } } + vec } diff --git a/src/tests/test_files/test_expand.meta b/src/tests/test_files/test_expand.meta new file mode 100644 index 0000000..4e1db92 --- /dev/null +++ b/src/tests/test_files/test_expand.meta @@ -0,0 +1,37 @@ +${ + var1 = "val1" + var2 = BLANK + var3 = 'value with spaces' +} + +@{ + arr1 = ['val1'] + arr2 = [] + pat1.arr = ["value", "value with spaces"] +} + +&{ + pat1 = "pattern" + pat2.sub_pat = 'sub_pattern' + pat2.default = DEFAULT + pat2.blank = BLANK +} + +TESTS: + +var1 [val1]: ${var1} +var2 [BLANK]: ${var2} +var3 [value with spaces]: ${var3} + +arr1 [val1]: @{arr1} +arr3 [BLANK]: @{arr2} + +Pattern subs: + +pat1 [with array]: &{pat1} +pat2: + .sub_pat: &{pat2.sub_pat} + .default: &{pat2.default} + .blank: &{pat2.blank} + +This comment should not be rendered: -{arr1} diff --git a/src/tests/test_files/test_expanded b/src/tests/test_files/test_expanded new file mode 100644 index 0000000..e69de29 diff --git a/src/tests/test_pattern.meta b/src/tests/test_files/test_pattern.meta similarity index 100% rename from src/tests/test_pattern.meta rename to src/tests/test_files/test_pattern.meta diff --git a/src/tests/test_source.meta b/src/tests/test_files/test_source.meta similarity index 100% rename from src/tests/test_source.meta rename to src/tests/test_files/test_source.meta diff --git a/src/tests/test_metafile.rs b/src/tests/test_metafile.rs index f6a30be..4af6e59 100644 --- a/src/tests/test_metafile.rs +++ b/src/tests/test_metafile.rs @@ -1,7 +1,10 @@ -use crate::parse_file; +use crate::{metafile_to_string, parse_file, RootDirs}; use color_eyre::Result; +use std::path::PathBuf; static SOURCE: &str = include_str!("test_source.meta"); +static PRE_EXPAND: &str = include_str!("test_expand.meta"); +static POST_EXPAND: &str = include_str!("test_expanded"); #[test] fn test_metafile_gets() -> Result<()> { @@ -27,3 +30,19 @@ fn test_metafile_gets() -> Result<()> { Ok(()) } + +#[test] +fn test_metafile_to_str() -> Result<()> { + let metafile = parse_file(PRE_EXPAND)?; + let dirs = RootDirs { + source: PathBuf::new(), + build: PathBuf::new(), + pattern: PathBuf::new(), + }; + + let file = metafile_to_string(&metafile, &dirs, None)?; + + assert_eq!(file, ""); + + Ok(()) +} diff --git a/src/tests/test_parser.rs b/src/tests/test_parser.rs index 2e063f1..ca32d62 100644 --- a/src/tests/test_parser.rs +++ b/src/tests/test_parser.rs @@ -5,7 +5,7 @@ static SOURCE: &str = include_str!("test_source.meta"); static PATTERN: &str = include_str!("test_pattern.meta"); #[test] -fn build_meta_file() -> Result<()> { +fn parse_meta_file() -> Result<()> { let source = parse_file(SOURCE)?; assert_eq!(source.variables.get("var").unwrap(), &"good"); @@ -35,7 +35,7 @@ fn build_meta_file() -> Result<()> { } #[test] -fn build_pattern_file() -> Result<()> { +fn parse_pattern_file() -> Result<()> { let mut pattern_src = parse_file(PATTERN)?.source.into_iter(); pattern_src.next();