flesh/core/tests/test_lib_str.rs

314 lines
8.7 KiB
Rust
Raw Normal View History

2023-03-08 11:39:54 -08:00
mod str_lib_tests {
2024-02-06 22:39:08 +00:00
use flesh::ast::{eval, lex, SymTable};
use flesh::stdlib::static_stdlib;
#[test]
fn test_simple_concat() {
let document = "(concat \"test\")";
let result = "\"test\"";
2023-03-08 11:39:54 -08:00
let mut syms = SymTable::new();
static_stdlib(&mut syms);
2023-03-08 11:39:54 -08:00
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
2023-03-08 11:39:54 -08:00
#[test]
fn test_poly_concat() {
let document = "(concat \"test\" 1 2 3)";
let result = "\"test123\"";
2023-03-08 11:39:54 -08:00
let mut syms = SymTable::new();
static_stdlib(&mut syms);
2023-03-08 11:39:54 -08:00
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
2023-03-08 11:39:54 -08:00
#[test]
fn test_empty_concat() {
let document = "(concat)";
let result = "\"\"";
2023-03-08 11:39:54 -08:00
let mut syms = SymTable::new();
static_stdlib(&mut syms);
2023-03-08 11:39:54 -08:00
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
2023-03-08 11:39:54 -08:00
#[test]
fn test_strlen_str() {
let document = "(strlen \"test\")";
2023-03-08 11:39:54 -08:00
let result = 4;
let mut syms = SymTable::new();
static_stdlib(&mut syms);
2023-03-08 11:39:54 -08:00
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
#[test]
2023-03-08 11:39:54 -08:00
fn test_strlen_int() {
let document = "(strlen 1000)";
let result = 4;
let mut syms = SymTable::new();
static_stdlib(&mut syms);
2023-03-08 11:39:54 -08:00
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
2023-03-08 11:39:54 -08:00
#[test]
fn test_strlen_float() {
let document = "(strlen 10.2)";
let result = 4;
let mut syms = SymTable::new();
static_stdlib(&mut syms);
2023-03-08 11:39:54 -08:00
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
2023-03-08 11:39:54 -08:00
#[test]
fn test_strlen_seg() {
let document = "(strlen (1 2 3))";
let result = 7;
let mut syms = SymTable::new();
static_stdlib(&mut syms);
2023-03-08 11:39:54 -08:00
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
2023-03-08 11:39:54 -08:00
#[test]
fn test_strlen_bool() {
let document = "(strlen true)";
let result = 4;
let mut syms = SymTable::new();
static_stdlib(&mut syms);
2023-03-08 11:39:54 -08:00
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
#[test]
2023-03-08 11:39:54 -08:00
fn test_strcast_i() {
let document = "(string 4)";
let result = "\"4\"";
2023-03-08 11:39:54 -08:00
let mut syms = SymTable::new();
static_stdlib(&mut syms);
2023-03-08 11:39:54 -08:00
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
2023-03-08 11:39:54 -08:00
#[test]
fn test_strcast_seg() {
let document = "(string (1 2 3))";
let result = "\"(1 2 3)\"";
2023-03-08 11:39:54 -08:00
let mut syms = SymTable::new();
static_stdlib(&mut syms);
2023-03-08 11:39:54 -08:00
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
2023-03-08 11:39:54 -08:00
#[test]
fn test_contains() {
let document = "(substr? \"bigger\" \"ger\")";
2023-03-08 11:39:54 -08:00
let result = "true";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
2023-03-08 11:39:54 -08:00
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
2023-03-08 11:39:54 -08:00
#[test]
fn test_doesnt_contain() {
let document = "(substr? \"smaller\" \"ger\")";
2023-03-08 11:39:54 -08:00
let result = "false";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
2023-03-08 11:39:54 -08:00
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
2023-03-08 19:42:08 -08:00
#[test]
fn test_split() {
let document = "(split \"one.two.three\" \".\")";
let result = "(\"one\" \"two\" \"three\")";
2023-03-08 19:42:08 -08:00
let mut syms = SymTable::new();
static_stdlib(&mut syms);
2023-03-08 19:42:08 -08:00
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
#[test]
fn test_split_big_delim() {
let document = "(split \"one:d:two:d:three\" \":d:\")";
let result = "(\"one\" \"two\" \"three\")";
2023-03-08 19:42:08 -08:00
let mut syms = SymTable::new();
static_stdlib(&mut syms);
2023-03-08 19:42:08 -08:00
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
#[test]
fn test_splitnt() {
let document = "(split \"one.two.three\" \"-\")";
let result = "(\"one.two.three\")";
2023-03-08 19:42:08 -08:00
let mut syms = SymTable::new();
static_stdlib(&mut syms);
2023-03-08 19:42:08 -08:00
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
#[test]
fn test_substr_valid() {
let document = "(substr \"test\" 0 4)";
let result = "\"test\"";
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_substr_start_neg() {
let document = "(substr \"test\" -1 3)";
let result = "start index cannot be negative";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
let doc_res= eval(&lex(&document.to_string()).unwrap(), &mut syms);
if let Err(e) = doc_res {
assert_eq!(
e.0.first().unwrap().message,
result.to_string(),
);
} else {
assert!(false);
}
}
#[test]
fn test_substr_end_neg() {
let document = "(substr \"test\" 1 -3)";
let result = "end index cannot be negative";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
let doc_res= eval(&lex(&document.to_string()).unwrap(), &mut syms);
if let Err(e) = doc_res {
assert_eq!(
e.0.first().unwrap().message,
result.to_string(),
);
} else {
assert!(false);
}
}
#[test]
fn test_substr_start_out_of_bounds() {
let document = "(substr \"test\" 5 3)";
let result = "start index larger than source string";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
let doc_res= eval(&lex(&document.to_string()).unwrap(), &mut syms);
if let Err(e) = doc_res {
assert_eq!(
e.0.first().unwrap().message,
result.to_string(),
);
} else {
assert!(false);
}
}
#[test]
fn test_substr_end_out_of_bounds() {
let document = "(substr \"test\" 1 5)";
let result = "end index larger than source string";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
let doc_res= eval(&lex(&document.to_string()).unwrap(), &mut syms);
if let Err(e) = doc_res {
assert_eq!(
e.0.first().unwrap().message,
result.to_string(),
);
} else {
assert!(false);
}
}
#[test]
fn test_substr_index_order() {
let document = "(substr \"test\" 3 2)";
let result = "end index must be larger than start index";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
let doc_res= eval(&lex(&document.to_string()).unwrap(), &mut syms);
if let Err(e) = doc_res {
assert_eq!(
e.0.first().unwrap().message,
result.to_string(),
);
} else {
assert!(false);
}
}
}