From: Huck Boles Date: Fri, 5 May 2023 15:30:16 +0000 (-0500) Subject: parsing tests X-Git-Url: https://git.huck.website/?a=commitdiff_plain;h=2420cd1380b43d8136945057ee601147e792ef82;p=metaforge.git parsing tests --- diff --git a/src/tests/test_files/test_expanded b/src/tests/test_files/test_expanded index e69de29..c43616c 100644 --- a/src/tests/test_files/test_expanded +++ b/src/tests/test_files/test_expanded @@ -0,0 +1,18 @@ +TESTS: + +var1 [val1]: val1 +var2 [BLANK]: +var3 [value with spaces]: value with spaces + +arr1 [val1]: val1 +arr3 [BLANK]: + +Pattern subs: + +pat1 [with array]:

value

value with spaces

+pat2: + .sub_pat:

SUBPATTERN

+ .default:

DEFAULT

+ .blank: + +This comment should not be rendered: diff --git a/src/tests/test_metafile.rs b/src/tests/test_metafile.rs index 4af6e59..1727a75 100644 --- a/src/tests/test_metafile.rs +++ b/src/tests/test_metafile.rs @@ -1,10 +1,12 @@ -use crate::{metafile_to_string, parse_file, RootDirs}; +#![allow(dead_code, unused)] +use crate::{metafile_to_string, parse_file, source, RootDirs, Source, Substitution}; 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"); +static SOURCE: &str = include_str!("./test_files/test_source.meta"); +static PATTERN: &str = include_str!("./test_files/test_pattern.meta"); +static PRE_EXPAND: &str = include_str!("./test_files/test_expand.meta"); +static POST_EXPAND: &str = include_str!("./test_files/test_expanded"); #[test] fn test_metafile_gets() -> Result<()> { @@ -31,6 +33,52 @@ fn test_metafile_gets() -> Result<()> { Ok(()) } +#[test] +fn parse_meta_file() -> Result<()> { + let source = parse_file(SOURCE)?; + + assert_eq!(source.variables.get("var").unwrap(), &"good"); + assert_eq!(source.variables.get("blank"), None); + assert_eq!(source.variables.get("not_here"), None); + + assert_eq!( + source.arrays.get("sub.array").unwrap(), + &vec!["sub", "value"] + ); + assert_eq!( + source.arrays.get("arr").unwrap(), + &vec!["split", "up", "values"] + ); + assert_eq!( + source.arrays.get("with_spaces").unwrap(), + &vec!["stuff", "with", "spaces"] + ); + assert_eq!(source.arrays.get("not_defined"), None); + + assert_eq!(source.patterns.get("pat").unwrap(), &"pattern"); + assert_eq!(source.patterns.get("pat.sub_pat"), None); + assert_eq!(source.patterns.get("blank_pat"), None); + assert_eq!(source.patterns.get("not_defined"), None); + + Ok(()) +} + +#[test] +fn parse_pattern_file() -> Result<()> { + let mut pattern_src = parse_file(PATTERN)?.source.into_iter(); + + pattern_src.next(); + assert_eq!(pattern_src.next().unwrap(), source!(var("var"))); + pattern_src.next(); + assert_eq!(pattern_src.next().unwrap(), source!(pat("pat"))); + assert_eq!(pattern_src.next().unwrap(), source!(arr("array"))); + pattern_src.next(); + assert_eq!(pattern_src.next().unwrap(), source!(var("blank"))); + + Ok(()) +} + +#[ignore = "todo: build tmp directory"] #[test] fn test_metafile_to_str() -> Result<()> { let metafile = parse_file(PRE_EXPAND)?; @@ -42,7 +90,7 @@ fn test_metafile_to_str() -> Result<()> { let file = metafile_to_string(&metafile, &dirs, None)?; - assert_eq!(file, ""); + assert_eq!(file, POST_EXPAND); Ok(()) } diff --git a/src/tests/test_parser.rs b/src/tests/test_parser.rs index ca32d62..43a4285 100644 --- a/src/tests/test_parser.rs +++ b/src/tests/test_parser.rs @@ -1,50 +1,54 @@ -use crate::{parse_file, source, Source, Substitution}; -use color_eyre::Result; +use crate::parse_file; -static SOURCE: &str = include_str!("test_source.meta"); -static PATTERN: &str = include_str!("test_pattern.meta"); +macro_rules! test_str ( + ($s: expr) => { + let str = $s; + parse_file(str).unwrap(); + }; +); #[test] -fn parse_meta_file() -> Result<()> { - let source = parse_file(SOURCE)?; +fn no_spaces_def() { + test_str!(r#"${v='v'}@{a=['a']}&{p='p'}"#); +} - assert_eq!(source.variables.get("var").unwrap(), &"good"); - assert_eq!(source.variables.get("blank"), None); - assert_eq!(source.variables.get("not_here"), None); +#[test] +fn just_source() { + test_str!(r#"This is just a &{source} snippet"#); +} - assert_eq!( - source.arrays.get("sub.array").unwrap(), - &vec!["sub", "value"] - ); - assert_eq!( - source.arrays.get("arr").unwrap(), - &vec!["split", "up", "values"] - ); - assert_eq!( - source.arrays.get("with_spaces").unwrap(), - &vec!["stuff", "with", "spaces"] - ); - assert_eq!(source.arrays.get("not_defined"), None); +#[test] +#[should_panic] +fn key_with_spaces() { + test_str!(r#"${ key with spaces = "value" }"#); +} - assert_eq!(source.patterns.get("pat").unwrap(), &"pattern"); - assert_eq!(source.patterns.get("pat.sub_pat"), None); - assert_eq!(source.patterns.get("blank_pat"), None); - assert_eq!(source.patterns.get("not_defined"), None); +#[test] +#[should_panic] +fn value_missing_quote() { + test_str!(r#"${ key = "value missing quote }"#); +} - Ok(()) +#[test] +#[should_panic] +fn mixed_quotes() { + test_str!(r#"${ key = "value mixing quotes' }"#); } #[test] -fn parse_pattern_file() -> Result<()> { - let mut pattern_src = parse_file(PATTERN)?.source.into_iter(); +#[should_panic] +fn spaces_in_substitution() { + test_str!(r#"This ${variable is not allowed}"#); +} - pattern_src.next(); - assert_eq!(pattern_src.next().unwrap(), source!(var("var"))); - pattern_src.next(); - assert_eq!(pattern_src.next().unwrap(), source!(pat("pat"))); - assert_eq!(pattern_src.next().unwrap(), source!(arr("array"))); - pattern_src.next(); - assert_eq!(pattern_src.next().unwrap(), source!(var("blank"))); +#[test] +#[should_panic] +fn missing_closing_brace() { + test_str!(r#"${ key = "value" "#); +} - Ok(()) +#[test] +#[should_panic] +fn map_in_source() { + test_str!(r#"This map: ${ is = "invalid" }"#); }