Skip to content

Commit

Permalink
Merge pull request holochain#170 from c12i/fix-unit-tests
Browse files Browse the repository at this point in the history
Remove duplicate unit tests and run them in ci
  • Loading branch information
c12i committed Jan 11, 2024
1 parent fc8f70d commit 63d8fbf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 125 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
86 changes: 0 additions & 86 deletions src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
"#,
);
}
}
58 changes: 19 additions & 39 deletions src/templates/helpers/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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() {
Expand All @@ -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}}
"#;
Expand All @@ -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 {"}}
Expand Down Expand Up @@ -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
}
Expand All @@ -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}}
Expand Down

0 comments on commit 63d8fbf

Please sign in to comment.