Move File module and tests to the shell project

Signed-off-by: Ava Affine <ava@sunnypup.io>
This commit is contained in:
Ava Apples Affine 2024-07-12 21:07:04 -07:00
parent 6d2925984f
commit 74f73fb493
4 changed files with 87 additions and 82 deletions

View file

@ -23,7 +23,6 @@ pub mod control;
pub mod decl;
pub mod math;
pub mod strings;
pub mod file;
pub const CFG_FILE_VNAME: &str = "FLESH_CFG_FILE";
@ -37,5 +36,4 @@ pub fn static_stdlib(syms: &mut SymTable) {
control::add_control_lib(syms);
boolean::add_bool_lib(syms);
math::add_math_lib(syms);
file::add_file_lib(syms);
}

View file

@ -1,76 +0,0 @@
mod file_lib_tests {
use flesh::ast::{eval, lex, SymTable};
use flesh::stdlib::static_stdlib;
#[test]
fn test_fexists() {
let document = "(exists? \"/tmp\")";
let result = "true";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
#[test]
fn test_fexists_doesnt() {
let document = "(exists? \"cargo.timtam\")";
let result = "false";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
#[test]
fn test_write_file() {
let document = "
(let ((s \"test\")
(t \"/tmp/flesh-lib-test-file-1\"))
(write-file t s)
(echo (read-file t))
(eq? (read-file t) s))";
let result = "true";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
#[test]
fn test_append_file() {
let document = "
(let ((s \"test\")
(t \"/tmp/flesh-lib-test-file-2\"))
(write-file t s)
(append-file t s)
(eq? (read-file t)
(concat s s)))";
let result = "true";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
}

View file

@ -15,9 +15,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use crate::segment::{Ctr, Seg, Type};
use crate::sym::{SymTable, Symbol, ValueType, Args};
use crate::error::{Traceback, start_trace};
use flesh::ast::{
start_trace, Ctr, Seg, Type, SymTable,
Symbol, ValueType, Args, Traceback,
};
use std::io::Write;
use std::fs::{File, read_to_string, OpenOptions};
use std::rc::Rc;
@ -170,3 +172,81 @@ pub fn add_file_lib(syms: &mut SymTable) {
);
}
#[cfg(test)]
mod tests {
use flesh::ast::{eval, lex, SymTable};
use flesh::stdlib::static_stdlib;
use crate::stl::static_stdlib_overwrites;
#[test]
fn test_fexists() {
let document = "(exists? \"/tmp\")";
let result = "true";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib_overwrites(&mut syms);
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
#[test]
fn test_fexists_doesnt() {
let document = "(exists? \"cargo.timtam\")";
let result = "false";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib_overwrites(&mut syms);
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
#[test]
fn test_write_file() {
let document = "
(let ((s \"test\")
(t \"/tmp/flesh-lib-test-file-1\"))
(write-file t s)
(echo (read-file t))
(eq? (read-file t) s))";
let result = "true";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib_overwrites(&mut syms);
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
#[test]
fn test_append_file() {
let document = "
(let ((s \"test\")
(t \"/tmp/flesh-lib-test-file-2\"))
(write-file t s)
(append-file t s)
(eq? (read-file t)
(concat s s)))";
let result = "true";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
static_stdlib_overwrites(&mut syms);
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
}

View file

@ -33,6 +33,8 @@ use crate::posix;
mod window;
#[path = "store.rs"]
mod store;
#[path = "file.rs"]
mod file;
pub const CONSOLE_XDIM_VNAME: &str = "_FLESH_WIDTH";
pub const CONSOLE_YDIM_VNAME: &str = "_FLESH_HEIGHT";
@ -63,6 +65,7 @@ fn prompt_delimiter_default_callback(_: &Seg, _: &mut SymTable) -> Result<Ctr, T
pub fn static_stdlib_overwrites(syms: &mut SymTable) {
static_stdlib(syms);
window::add_window_lib_funcs(syms);
file::add_file_lib(syms);
syms.insert(
"call".to_string(),