]> git.huck.website - metaforge.git/commitdiff
test site passes
authorHuck Boles <huck@huck.website>
Wed, 10 May 2023 01:46:50 +0000 (20:46 -0500)
committerHuck Boles <huck@huck.website>
Wed, 10 May 2023 01:46:50 +0000 (20:46 -0500)
24 files changed:
bacon.toml
src/filetype/builder.rs
src/parser/parse.rs
src/tests/test_metafile.rs
test_site/pattern/base/default.meta [moved from tests/files/test_site/pattern/base/default.meta with 100% similarity]
test_site/pattern/pat/default.meta [moved from tests/files/test_site/pattern/pat/default.meta with 100% similarity]
test_site/pattern/pat/pattern.meta [moved from tests/files/test_site/pattern/pat/pattern.meta with 100% similarity]
test_site/pattern/test/array.meta [moved from tests/files/test_site/pattern/test/array.meta with 100% similarity]
test_site/pattern/test/blank/default.meta [moved from tests/files/test_site/pattern/test/blank/default.meta with 100% similarity]
test_site/pattern/test/default/default.meta [moved from tests/files/test_site/pattern/test/default/default.meta with 100% similarity]
test_site/pattern/test/new/default.meta [moved from tests/files/test_site/pattern/test/new/default.meta with 100% similarity]
test_site/pattern/test/new/new.meta [moved from tests/files/test_site/pattern/test/new/new.meta with 100% similarity]
test_site/pattern/test/overwrite/default.meta [moved from tests/files/test_site/pattern/test/overwrite/default.meta with 100% similarity]
test_site/pattern/test/overwrite/overwrite.meta [moved from tests/files/test_site/pattern/test/overwrite/overwrite.meta with 100% similarity]
test_site/pattern/test/parent/default.meta [moved from tests/files/test_site/pattern/test/parent/default.meta with 100% similarity]
test_site/pattern/test/parent/parent.meta [moved from tests/files/test_site/pattern/test/parent/parent.meta with 100% similarity]
test_site/pattern/test/pattern.meta [moved from tests/files/test_site/pattern/test/pattern.meta with 100% similarity]
test_site/pattern/test/sub_pat/default.meta [moved from tests/files/test_site/pattern/test/sub_pat/default.meta with 100% similarity]
test_site/pattern/test/sub_pat/sub_pattern.meta [moved from tests/files/test_site/pattern/test/sub_pat/sub_pattern.meta with 100% similarity]
test_site/source/expand.meta [moved from tests/files/test_site/source/expand.meta with 100% similarity]
test_site/source/sub_dir/sub_source.meta [moved from tests/files/test_site/source/sub_dir/sub_source.meta with 100% similarity]
test_site/source/test_source.meta [moved from tests/files/test_site/source/test_source.meta with 100% similarity]
tests/files/expanded
tests/metafile_builder.rs

index a4c3c37f49bea017e3519804c71e797c81ba8458..ae7b891377eee935fec06302b6cd4d3ecc847b2f 100644 (file)
@@ -24,8 +24,8 @@ command = [
 need_stdout = false
 
 [jobs.test]
-command = [ "cargo", "test", "--color", "always" ]
-need_stdout = true
+command = [ "cargo", "test", "--color", "always", "--", "--color", "always" ]
+need_stdout = false
 
 [jobs.doc]
 command = ["cargo", "doc", "--color", "always", "--no-deps"]
index f1f3c646ca2b50652125c282e2c64afe362d7a0e..f3320d34e13f68c4aa24ce215b00177ef9f94af8 100644 (file)
@@ -14,7 +14,8 @@ pub fn build_metafile(file: &MetaFile, dirs: &RootDirs, path: &Path) -> Result<(
 }
 
 pub fn metafile_to_string(file: &MetaFile, dirs: &RootDirs, name: Option<&str>) -> Result<String> {
-    let mut output = String::default();
+    let mut output = String::new();
+    let mut arrays = false;
 
     for section in file.source.iter() {
         match section {
@@ -27,30 +28,53 @@ pub fn metafile_to_string(file: &MetaFile, dirs: &RootDirs, name: Option<&str>)
                 let expanded = match sub {
                     Substitution::Variable(key) => file
                         .get_var(key)
+                        .filter(|val| *val != "BLANK")
                         .map(|val| val.to_string())
                         .unwrap_or_default(),
                     Substitution::Pattern(key) => get_pattern(key, file, dirs)?,
                     // comments have already been removed at this point,
                     // so we use them to mark keys for array substitution
-                    Substitution::Array(key) => format!("-{{{key}}}"),
+                    Substitution::Array(key) => {
+                        arrays = true;
+                        format!("-{{{key}}}")
+                    }
                 };
                 output.push_str(&format!("\n{}\n", expanded));
             }
         }
     }
 
+    println!("{}", output);
+
     // deal with arrays
-    expand_arrays(output, file, name)
+    if arrays {
+        expand_arrays(output, file, name)
+    } else {
+        Ok(output)
+    }
 }
 
 fn get_pattern(key: &str, file: &MetaFile, dirs: &RootDirs) -> Result<String> {
-    let filename = file.get_pat(key).unwrap_or("default");
+    let mut filename = file.get_pat(key).unwrap_or("default");
+    if filename == "BLANK" {
+        return Ok(String::new());
+    };
+
+    if filename == "DEFAULT" {
+        filename = "default";
+    }
 
     let pattern_path = key.replace('.', "/") + "/" + filename;
     let mut path = dirs.pattern.join(pattern_path);
     path.set_extension("meta");
+    eprintln!("{:?}", path);
     let pattern = &fs::read_to_string(path.to_str().unwrap_or_default())?;
-    let pattern = parse_file(pattern)?;
+    let mut pattern = parse_file(pattern)?;
+
+    pattern.variables = file.variables.clone();
+    pattern.arrays = file.arrays.clone();
+    pattern.patterns = file.patterns.clone();
+
     metafile_to_string(&pattern, dirs, Some(key))
 }
 
@@ -83,7 +107,6 @@ fn expand_arrays(output: String, file: &MetaFile, name: Option<&str>) -> Result<
             } else {
                 key = array.to_string();
             }
-            // let key = dbg!(name.unwrap_or_default().to_owned() + "." + array);
             let value = file.get_arr(&key).unwrap_or_default();
             (*array, value)
         })
index c50976049cda8c0477e718e23090ebca0aa5e9d5..37df17e98ed20d90eb9e618683a7c1a65dd3e9ec 100644 (file)
@@ -104,11 +104,15 @@ fn parse_assign(pair: Pair<Rule>) -> (&'_ 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 to remove it from the HashMap
-            if tmp == "BLANK" || tmp == "DEFAULT" {
-                return ("", "");
+
+            if tmp == "BLANK" {
+                return (key, "BLANK");
+            }
+
+            if tmp == "DEFAULT" {
+                return (key, "DEFAULT");
             }
+
             // remove surrounding quotes from values by returning
             // everything except first and last characters
             // a string is defined as " ... " or ' ... '
index 144849c9ba5a742cd71a2bc98c32fda6787de0b0..04cc4ea0902ace2f992b0a8566fa2c00f968a0f7 100644 (file)
@@ -1,10 +1,9 @@
-use crate::{metafile_to_string, parse_file, source, RootDirs, Source, Substitution};
+use crate::{parse_file, source, Source, Substitution};
 use color_eyre::Result;
 use pretty_assertions::assert_eq;
-use std::{fs, path::PathBuf};
 
-static SOURCE: &str = include_str!("../../tests/files/test_site/source/test_source.meta");
-static PATTERN: &str = include_str!("../../tests/files//test_site/pattern/test/pattern.meta");
+static SOURCE: &str = include_str!("../../test_site/source/test_source.meta");
+static PATTERN: &str = include_str!("../../test_site/pattern/test/pattern.meta");
 
 #[test]
 fn test_metafile_gets() -> Result<()> {
@@ -12,7 +11,7 @@ fn test_metafile_gets() -> Result<()> {
 
     assert_eq!(source.get_var("var").unwrap(), "GOOD");
     assert_eq!(source.get_var("single_quotes").unwrap(), "GOOD");
-    assert_eq!(source.get_var("blank"), None);
+    assert_eq!(source.get_var("blank").unwrap(), "BLANK");
     assert_eq!(source.get_var("not_defined"), None);
 
     assert_eq!(source.get_arr("sub.array").unwrap(), ["GOOD", "GOOD"]);
@@ -24,8 +23,8 @@ fn test_metafile_gets() -> Result<()> {
     assert_eq!(source.get_arr("not_defined"), None);
 
     assert_eq!(source.get_pat("test").unwrap(), "pattern");
-    assert_eq!(source.get_pat("test.sub_pat"), None);
-    assert_eq!(source.get_pat("blank_pat"), None);
+    assert_eq!(source.get_pat("test.sub_pat").unwrap(), "DEFAULT");
+    assert_eq!(source.get_pat("blank_pat").unwrap(), "BLANK");
     assert_eq!(source.get_pat("not_defined"), None);
 
     Ok(())
@@ -36,7 +35,7 @@ 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("blank").unwrap(), &"BLANK");
     assert_eq!(source.variables.get("not_here"), None);
 
     assert_eq!(
@@ -54,8 +53,8 @@ fn parse_meta_file() -> Result<()> {
     assert_eq!(source.arrays.get("not_defined"), None);
 
     assert_eq!(source.patterns.get("test").unwrap(), &"pattern");
-    assert_eq!(source.patterns.get("test.sub_pat"), None);
-    assert_eq!(source.patterns.get("blank_pat"), None);
+    assert_eq!(source.patterns.get("test.sub_pat").unwrap(), &"DEFAULT");
+    assert_eq!(source.patterns.get("blank_pat").unwrap(), &"BLANK");
     assert_eq!(source.patterns.get("not_defined"), None);
 
     Ok(())
index 8e3f4dd8dc3ef5cd612712e9a39e761559e17095..bc2fff8dd1989384dc6367d9ec784315883fa826 100644 (file)
@@ -6,20 +6,22 @@ var2 [BLANK]:
 
 var3 [VAL WITH SPACES]:
 GOOD GOOD
-
 arr1 [VALUE]:
 GOOD
 arr2 [BLANK]:
 
 Pattern subs:
 
-test [WITH ARRAY]: 
-<p>GOOD</p>
-<p>GOOD GOOD</p>
+test [WITH ARRAY]:
+<p>
+GOOD
+</p><p>
+GOOD GOOD
+</p>
 test.sub_pat:
 <h1>SUBPATTERN</h1>
 test.default:
 <h1>DEFAULT</h1>
-test.blank: 
+test.blank:
 
-This comment should not be rendered: 
+This comment should not be rendered:
index 262ee9e56dc8b75d93f0d6bbc34e4d58fdbf5880..80f64e52b4057f6955cd4b4b2e14043c8d754c53 100644 (file)
@@ -3,7 +3,7 @@ use metaforge::*;
 use pretty_assertions::assert_eq;
 use std::{fs, path::PathBuf};
 
-static PRE_EXPAND: &str = include_str!("./files/test_site/source/expand.meta");
+static PRE_EXPAND: &str = include_str!("../test_site/source/expand.meta");
 static POST_EXPAND: &str = include_str!("./files/expanded");
 
 #[test]
@@ -13,13 +13,14 @@ fn test_metafile_to_str() -> Result<()> {
 
     let file = metafile_to_string(&metafile, &dirs, None)?;
 
-    assert_eq!(file, POST_EXPAND);
+    eprintln!("{}", file);
+    assert_eq!(file + "\n", POST_EXPAND);
 
     Ok(())
 }
 
 fn build_rootdir() -> Result<RootDirs> {
-    let dir = PathBuf::from("./tests/files/test_site");
+    let dir = PathBuf::from("./test_site");
 
     let dirs = RootDirs {
         source: dir.join("source"),