@@ -80,3 +80,50 @@ fn test_guard_set_formatter() {
80
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
81
assert_eq ! ( String :: from_utf8_lossy( & output. stdout) , expected_output) ;
82
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