diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 5b44c4ac1..cb79ba471 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -8,6 +8,15 @@ on: branches: [ develop, develop-0.1, develop-0.2 ] jobs: + cargotest: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - uses: dtolnay/rust-toolchain@stable + + - run: cargo test --no-fail-fast + testbuild: runs-on: ubuntu-latest steps: diff --git a/src/templates.rs b/src/templates.rs index 26d9560b3..febeafe7a 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -295,89 +295,3 @@ pub fn choose_or_get_template( Ok(chosen_template_name) } - -#[cfg(test)] -mod tests { - // Note this useful idiom: importing names from outer (for mod tests) scope. - use super::*; - use handlebars::Handlebars; - use serde_json::{Map, Value}; - - #[test] - fn test_get_scope_open_and_close_char_indexes() { - let text = String::from("const s = {};"); - let scope_opener = String::from("const s = {"); - - let (scope_opener_index, scope_close_index) = - get_scope_open_and_close_char_indexes(&text, &scope_opener).unwrap(); - - assert_eq!(scope_opener_index, 10); - assert_eq!(scope_close_index, 11); - } - - #[test] - fn test_merge_match_scope() { - let h = Handlebars::new(); - - let mut h = register_helpers(h); - - let code = String::from( - r#" -export class A { - nestedFn1() { - - } -} -export class B { - nestedFn() { - // First line - } -} - "#, - ); - let mut map = Map::new(); - map.insert( - String::from("previous_file_content"), - Value::String(String::from(code)), - ); - let context = Context::from(Value::Object(map)); - let template = r#" -{{#merge previous_file_content}} - {{#match_scope "export class A {"}} - nestedFn2() { - - } - {{previous_scope_content}} - {{/match_scope}} - {{#match_scope "export class B {"}} - {{#merge previous_scope_content}} - {{#match_scope "nestedFn() {"}} - {{previous_scope_content}} - // New line - {{/match_scope}} - {{/merge}} - {{/match_scope}} -{{/merge}} - "#; - - assert_eq!( - h.render_template_with_context(template, &context).unwrap(), - r#" -export class A { - nestedFn2() { - - } - nestedFn1() { - - } -} -export class B { - nestedFn() { - // First line - // New line - } -} - "#, - ); - } -} diff --git a/src/templates/helpers/merge.rs b/src/templates/helpers/merge.rs index 6d8dfbef0..5453058c0 100644 --- a/src/templates/helpers/merge.rs +++ b/src/templates/helpers/merge.rs @@ -5,7 +5,7 @@ use handlebars::{ use serde::{Deserialize, Serialize}; use serde_json::{Map, Number, Value}; -fn get_scope_open_and_close_char_indexes( +pub fn get_scope_open_and_close_char_indexes( text: &String, scope_opener: &String, ) -> Result<(usize, usize), RenderError> { @@ -237,7 +237,7 @@ mod tests { // Note this useful idiom: importing names from outer (for mod tests) scope. use super::*; use handlebars::Handlebars; - use serde_json::{Map, Value}; + use serde_json::json; #[test] fn test_get_scope_open_and_close_char_indexes() { @@ -257,19 +257,14 @@ mod tests { let h = register_merge(h); - let code = String::from( - r#"class A { + let code = r#" + class A { // Multiline // Comment } -"#, - ); - let mut map = Map::new(); - map.insert( - String::from("previous_file_content"), - Value::String(String::from(code.clone())), - ); - let context = Context::from(Value::Object(map)); +"#; + let value = json!({"previous_file_content": code}); + let context = Context::from(value); let template = r#"{{#merge previous_file_content}} {{/merge}} "#; @@ -286,19 +281,13 @@ mod tests { let h = register_merge(h); - let code = String::from( - r#"class A { + let code = r#"class A { // Multiline // Comment } -"#, - ); - let mut map = Map::new(); - map.insert( - String::from("previous_file_content"), - Value::String(String::from(code)), - ); - let context = Context::from(Value::Object(map)); +"#; + let value = json!({"previous_file_content": code}); + let context = Context::from(value); let template = r#" {{#merge previous_file_content}} {{#match_scope "class A {"}} @@ -330,8 +319,7 @@ class A { let h = register_merge(h); - let code = String::from( - r#"export class A { + let code = r#"export class A { nestedFn1() { // First line } @@ -341,21 +329,13 @@ export class B { // First line } } -"#, - ); - let mut map = Map::new(); - map.insert( - String::from("previous_file_content"), - Value::String(String::from(code)), - ); - map.insert( - String::from("class_functions"), - Value::Array(vec![ - Value::String(String::from("nestedFn2")), - Value::String(String::from("nestedFn3")), - ]), - ); - let context = Context::from(Value::Object(map)); +"#; + let value = json!({ + "previous_file_content": code, + "class_functions": ["nestedFn2", "nestedFn3"] + }); + + let context = Context::from(value); let template = r#"{{#merge previous_file_content}} {{#match_scope "export class B {"}} {{#merge untrimmed_previous_scope_content}}