Skip to content

Commit 280c98d

Browse files
authored
Merge pull request #13 from eyuuab/main
Implement guardSet and logger tests
2 parents fa3d1cd + 4e394be commit 280c98d

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

metta-run/tests/integration_test.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,65 @@ fn test_nested_constraint_tree_formatter() {
6565

6666
assert_eq!(String::from_utf8_lossy(&output.stdout), expected_output);
6767
}
68+
69+
#[test]
70+
fn test_guard_set_formatter() {
71+
use std::process::Command;
72+
73+
let output = Command::new("cargo")
74+
.arg("run")
75+
.arg("tests/resources/guard_set.metta")
76+
.arg("fgt")
77+
.output()
78+
.expect("failed to execute process");
79+
80+
let expected_output = "[(TreeNode (Value Nil False OR)\n└──GuardSets's\n ├─(TreeNode (Value B True LITERAL)\n ├─(TreeNode (Value D True LITERAL)\n ├─(TreeNode (Value E False LITERAL)\n";
81+
assert_eq!(String::from_utf8_lossy(&output.stdout), expected_output);
82+
}
83+
/// test logger functions
84+
#[test]
85+
fn test_logger() {
86+
use std::fs::File;
87+
use std::io::Read;
88+
use core::panic;
89+
use std::{io::BufRead, process::Command};
90+
use chrono::Local;
91+
92+
let now = Local::now();
93+
94+
let log_file = format!(
95+
"{}/metta-bin/{}.log",
96+
std::env::var("HOME").unwrap(),
97+
chrono::Local::now().format("%Y-%m-%d").to_string()
98+
);
99+
100+
let output = Command::new("cargo")
101+
.arg("run")
102+
.arg("--")
103+
.arg("tests/resources/metta_run.metta")
104+
.output()
105+
.expect("failed to execute process")
106+
.stdout
107+
.lines()
108+
.last()
109+
.unwrap();
110+
111+
let metta_output = match output {
112+
Ok(output) => output.to_string(),
113+
Err(_) => panic!("Error reading output"),
114+
};
115+
116+
let expected_output = vec![
117+
metta_output,
118+
format!("Start time: {}", now.format("%Y-%m-%d %H-%m").to_string()),
119+
];
120+
121+
let mut file = File::open(&log_file).unwrap();
122+
let mut contents = String::new();
123+
file.read_to_string(&mut contents).unwrap();
124+
125+
let last_three_lines = contents.lines().rev().take(3).collect::<Vec<&str>>();
126+
let output = vec![last_three_lines[0], &last_three_lines[2][..28]];
127+
128+
assert_eq!(output, expected_output);
129+
}

0 commit comments

Comments
 (0)