Skip to content

Commit

Permalink
test: Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jpedroh committed Dec 5, 2023
1 parent 9a0afac commit ef59a91
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 87 deletions.
16 changes: 16 additions & 0 deletions bin/src/control.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
use std::{error::Error, fmt};

use parsing::ParserConfiguration;

#[derive(Debug)]
pub enum ExecutionError {
ParsingError,
}

impl fmt::Display for ExecutionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ExecutionError::ParsingError => write!(f, "Parsing error occurred"),
}
}
}

impl Error for ExecutionError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
None
}
}

#[derive(Debug)]
pub enum ExecutionResult {
WithConflicts(String),
Expand Down
3 changes: 3 additions & 0 deletions bin/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod control;

pub use control::run_tool_on_merge_scenario;
8 changes: 6 additions & 2 deletions bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ fn main() {
.unwrap_or_else(|_| std::process::exit(cli_exit_codes::WRITING_FILE_ERROR));

match result {
control::ExecutionResult::WithConflicts(_) => std::process::exit(cli_exit_codes::SUCCESS_WITH_CONFLICTS),
control::ExecutionResult::WithoutConflicts(_) => std::process::exit(cli_exit_codes::SUCCESS_WITHOUT_CONFLICTS),
control::ExecutionResult::WithConflicts(_) => {
std::process::exit(cli_exit_codes::SUCCESS_WITH_CONFLICTS)
}
control::ExecutionResult::WithoutConflicts(_) => {
std::process::exit(cli_exit_codes::SUCCESS_WITHOUT_CONFLICTS)
}
}
}
156 changes: 78 additions & 78 deletions bin/tests/cli.rs
Original file line number Diff line number Diff line change
@@ -1,91 +1,91 @@
use std::process::Command;
// use std::process::Command;

use assert_cmd::prelude::*;
// use assert_cmd::prelude::*;

#[test]
fn it_does_not_crash_and_produces_the_expected_output() {
let mut cmd = Command::cargo_bin("generic-merge").unwrap();
cmd.arg("--base-path=tests/samples/smoke_java/base.java")
.arg("--left-path=tests/samples/smoke_java/left.java")
.arg("--right-path=tests/samples/smoke_java/right.java")
.arg("--merge-path=tests/samples/smoke_java/merge.output.java")
.assert()
.code(0);
// #[test]
// fn it_does_not_crash_and_produces_the_expected_output() {
// let mut cmd = Command::cargo_bin("generic-merge").unwrap();
// cmd.arg("--base-path=tests/samples/smoke_java/base.java")
// .arg("--left-path=tests/samples/smoke_java/left.java")
// .arg("--right-path=tests/samples/smoke_java/right.java")
// .arg("--merge-path=tests/samples/smoke_java/merge.output.java")
// .assert()
// .code(0);

assert_eq!(
std::fs::read_to_string("tests/samples/smoke_java/merge.expected.java").unwrap(),
std::fs::read_to_string("tests/samples/smoke_java/merge.output.java").unwrap()
)
}
// assert_eq!(
// std::fs::read_to_string("tests/samples/smoke_java/merge.expected.java").unwrap(),
// std::fs::read_to_string("tests/samples/smoke_java/merge.output.java").unwrap()
// )
// }

#[test]
fn if_left_equals_base_then_output_right_as_result() {
let mut cmd = Command::cargo_bin("generic-merge").unwrap();
cmd.arg("--base-path=tests/samples/one_parent_equals_base/base.java")
.arg("--left-path=tests/samples/one_parent_equals_base/base.java")
.arg("--right-path=tests/samples/one_parent_equals_base/changed_parent.java")
.arg("--merge-path=tests/samples/one_parent_equals_base/merge.output.right.java")
.assert()
.code(0);
// #[test]
// fn if_left_equals_base_then_output_right_as_result() {
// let mut cmd = Command::cargo_bin("generic-merge").unwrap();
// cmd.arg("--base-path=tests/samples/one_parent_equals_base/base.java")
// .arg("--left-path=tests/samples/one_parent_equals_base/base.java")
// .arg("--right-path=tests/samples/one_parent_equals_base/changed_parent.java")
// .arg("--merge-path=tests/samples/one_parent_equals_base/merge.output.right.java")
// .assert()
// .code(0);

assert_eq!(
std::fs::read_to_string("tests/samples/one_parent_equals_base/changed_parent.java")
.unwrap(),
std::fs::read_to_string("tests/samples/one_parent_equals_base/merge.output.right.java")
.unwrap()
)
}
// assert_eq!(
// std::fs::read_to_string("tests/samples/one_parent_equals_base/changed_parent.java")
// .unwrap(),
// std::fs::read_to_string("tests/samples/one_parent_equals_base/merge.output.right.java")
// .unwrap()
// )
// }

#[test]
fn if_right_equals_base_then_output_left_as_result() {
let mut cmd = Command::cargo_bin("generic-merge").unwrap();
cmd.arg("--base-path=tests/samples/one_parent_equals_base/base.java")
.arg("--left-path=tests/samples/one_parent_equals_base/base.java")
.arg("--right-path=tests/samples/one_parent_equals_base/changed_parent.java")
.arg("--merge-path=tests/samples/one_parent_equals_base/merge.output.left.java")
.assert()
.code(0);
// #[test]
// fn if_right_equals_base_then_output_left_as_result() {
// let mut cmd = Command::cargo_bin("generic-merge").unwrap();
// cmd.arg("--base-path=tests/samples/one_parent_equals_base/base.java")
// .arg("--left-path=tests/samples/one_parent_equals_base/base.java")
// .arg("--right-path=tests/samples/one_parent_equals_base/changed_parent.java")
// .arg("--merge-path=tests/samples/one_parent_equals_base/merge.output.left.java")
// .assert()
// .code(0);

assert_eq!(
std::fs::read_to_string("tests/samples/one_parent_equals_base/changed_parent.java")
.unwrap(),
std::fs::read_to_string("tests/samples/one_parent_equals_base/merge.output.left.java")
.unwrap()
)
}
// assert_eq!(
// std::fs::read_to_string("tests/samples/one_parent_equals_base/changed_parent.java")
// .unwrap(),
// std::fs::read_to_string("tests/samples/one_parent_equals_base/merge.output.left.java")
// .unwrap()
// )
// }

#[test]
fn it_works_on_semi_structured_merge() {
run_tool_on_scenario("semi_structured")
}
// #[test]
// fn it_works_on_semi_structured_merge() {
// run_tool_on_scenario("semi_structured")
// }

#[test]
fn it_works_on_node_reordering() {
run_tool_on_scenario("node_reordering")
}
// #[test]
// fn it_works_on_node_reordering() {
// run_tool_on_scenario("node_reordering")
// }

#[test]
fn it_works_on_no_conflicts() {
run_tool_on_scenario("no_conflicts")
}
// #[test]
// fn it_works_on_no_conflicts() {
// run_tool_on_scenario("no_conflicts")
// }

fn run_tool_on_scenario(scenario_name: &str) {
let mut cmd = Command::cargo_bin("generic-merge").unwrap();
cmd.arg(format!("-b=tests/samples/{}/base.java", scenario_name))
.arg(format!("-l=tests/samples/{}/left.java", scenario_name))
.arg(format!("-r=tests/samples/{}/right.java", scenario_name))
.arg(format!(
"-m=tests/samples/{}/merge.output.java",
scenario_name
))
.assert()
.code(0);
// fn run_tool_on_scenario(scenario_name: &str) {
// let mut cmd = Command::cargo_bin("generic-merge").unwrap();
// cmd.arg(format!("-b=tests/samples/{}/base.java", scenario_name))
// .arg(format!("-l=tests/samples/{}/left.java", scenario_name))
// .arg(format!("-r=tests/samples/{}/right.java", scenario_name))
// .arg(format!(
// "-m=tests/samples/{}/merge.output.java",
// scenario_name
// ))
// .assert()
// .code(0);

let expected_result_path = format!("tests/samples/{}/merge.expected.java", scenario_name);
let actual_result_path = format!("tests/samples/{}/merge.output.java", scenario_name);
// let expected_result_path = format!("tests/samples/{}/merge.expected.java", scenario_name);
// let actual_result_path = format!("tests/samples/{}/merge.output.java", scenario_name);

assert_eq!(
std::fs::read_to_string(expected_result_path).unwrap(),
std::fs::read_to_string(actual_result_path).unwrap()
)
}
// assert_eq!(
// std::fs::read_to_string(expected_result_path).unwrap(),
// std::fs::read_to_string(actual_result_path).unwrap()
// )
// }
30 changes: 30 additions & 0 deletions bin/tests/control.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#[test]
fn all_java_samples_work_correctly() -> Result<(), Box<dyn std::error::Error>> {
let sample_names = get_samples_names()?;

for sample_path in sample_names {
let base = std::fs::read_to_string(format!("{}/base.java", sample_path.display()))?;
let left = std::fs::read_to_string(format!("{}/left.java", sample_path.display()))?;
let right = std::fs::read_to_string(format!("{}/right.java", sample_path.display()))?;

let expected = std::fs::read_to_string(format!("{}/merge.java", sample_path.display()))?;
let result = bin::run_tool_on_merge_scenario(model::Language::Java, &base, &left, &right)?;

assert_eq!(expected, result.to_string())
}

Ok(())
}

fn get_samples_names() -> Result<Vec<std::path::PathBuf>, std::io::Error> {
std::fs::read_dir("tests/samples")?
.into_iter()
.filter(|sample| {
sample
.as_ref()
.map(|sample| sample.path().is_dir())
.unwrap_or(false)
})
.map(|sample| sample.map(|sample| sample.path()))
.collect()
}
File renamed without changes.
3 changes: 0 additions & 3 deletions bin/tests/samples/one_parent_equals_base/base.java

This file was deleted.

4 changes: 0 additions & 4 deletions bin/tests/samples/one_parent_equals_base/changed_parent.java

This file was deleted.

File renamed without changes.

0 comments on commit ef59a91

Please sign in to comment.