use super::*;
#[test]
fn define_and_expand() {
crate::test_utils::init_test_tracing();
let mut mgr = AbbrevManager::new();
mgr.define_abbrev("global-abbrev-table", "btw", "by the way");
let result = mgr.expand_abbrev("global-abbrev-table", "btw");
assert_eq!(result, Some("by the way".to_string()));
let tbl = mgr.get_table("global-abbrev-table").unwrap();
assert_eq!(tbl.abbrevs.get("btw").unwrap().count, 1);
let result = mgr.expand_abbrev("global-abbrev-table", "btw");
assert_eq!(result, Some("by the way".to_string()));
let tbl = mgr.get_table("global-abbrev-table").unwrap();
assert_eq!(tbl.abbrevs.get("btw").unwrap().count, 2);
}
#[test]
fn expand_nonexistent() {
crate::test_utils::init_test_tracing();
let mut mgr = AbbrevManager::new();
let result = mgr.expand_abbrev("global-abbrev-table", "nope");
assert!(result.is_none());
}
#[test]
fn case_insensitive_lookup() {
crate::test_utils::init_test_tracing();
let mut mgr = AbbrevManager::new();
mgr.define_abbrev("global-abbrev-table", "BTW", "by the way");
let result = mgr.expand_abbrev("global-abbrev-table", "btw");
assert_eq!(result, Some("by the way".to_string()));
let result = mgr.expand_abbrev("global-abbrev-table", "BTW");
assert_eq!(result, Some("BY THE WAY".to_string()));
}
#[test]
fn case_capitalized() {
crate::test_utils::init_test_tracing();
let mut mgr = AbbrevManager::new();
mgr.define_abbrev("global-abbrev-table", "btw", "by the way");
let result = mgr.expand_abbrev("global-abbrev-table", "Btw");
assert_eq!(result, Some("By the way".to_string()));
}
#[test]
fn case_fixed() {
crate::test_utils::init_test_tracing();
let mut mgr = AbbrevManager::new();
mgr.define_abbrev("global-abbrev-table", "btw", "by the way");
mgr.tables
.get_mut("global-abbrev-table")
.unwrap()
.case_fixed = true;
let result = mgr.expand_abbrev("global-abbrev-table", "BTW");
assert_eq!(result, Some("by the way".to_string()));
}
#[test]
fn table_inheritance() {
crate::test_utils::init_test_tracing();
let mut mgr = AbbrevManager::new();
mgr.define_abbrev("global-abbrev-table", "btw", "by the way");
let child = mgr.create_table("lisp-mode-abbrev-table");
child.parent = Some("global-abbrev-table".to_string());
mgr.define_abbrev("lisp-mode-abbrev-table", "df", "defun");
let result = mgr.expand_abbrev("lisp-mode-abbrev-table", "df");
assert_eq!(result, Some("defun".to_string()));
let result = mgr.expand_abbrev("lisp-mode-abbrev-table", "btw");
assert_eq!(result, Some("by the way".to_string()));
}
#[test]
fn fallback_to_global() {
crate::test_utils::init_test_tracing();
let mut mgr = AbbrevManager::new();
mgr.define_abbrev("global-abbrev-table", "teh", "the");
mgr.create_table("text-mode-abbrev-table");
let result = mgr.expand_abbrev("text-mode-abbrev-table", "teh");
assert_eq!(result, Some("the".to_string()));
}
#[test]
fn list_abbrevs_sorted() {
crate::test_utils::init_test_tracing();
let mut mgr = AbbrevManager::new();
mgr.define_abbrev("global-abbrev-table", "zz", "sleep");
mgr.define_abbrev("global-abbrev-table", "aa", "alpha");
mgr.define_abbrev("global-abbrev-table", "mm", "middle");
let list = mgr.list_abbrevs("global-abbrev-table");
assert_eq!(list.len(), 3);
assert_eq!(list[0], ("aa", "alpha"));
assert_eq!(list[1], ("mm", "middle"));
assert_eq!(list[2], ("zz", "sleep"));
}
#[test]
fn list_abbrevs_nonexistent_table() {
crate::test_utils::init_test_tracing();
let mgr = AbbrevManager::new();
let list = mgr.list_abbrevs("no-such-table");
assert!(list.is_empty());
}
#[test]
fn clear_table() {
crate::test_utils::init_test_tracing();
let mut mgr = AbbrevManager::new();
mgr.define_abbrev("global-abbrev-table", "a", "alpha");
mgr.define_abbrev("global-abbrev-table", "b", "beta");
assert_eq!(mgr.list_abbrevs("global-abbrev-table").len(), 2);
mgr.clear_table("global-abbrev-table");
assert_eq!(mgr.list_abbrevs("global-abbrev-table").len(), 0);
}
#[test]
fn enable_disable() {
crate::test_utils::init_test_tracing();
let mut mgr = AbbrevManager::new();
assert!(!mgr.is_enabled());
mgr.set_enabled(true);
assert!(mgr.is_enabled());
mgr.set_enabled(false);
assert!(!mgr.is_enabled());
}
#[test]
fn define_abbrev_full_with_hook_and_system() {
crate::test_utils::init_test_tracing();
let mut mgr = AbbrevManager::new();
mgr.define_abbrev_full(
"global-abbrev-table",
"hw",
"hello world",
Some("my-hook".to_string()),
true,
);
let tbl = mgr.get_table("global-abbrev-table").unwrap();
let ab = tbl.abbrevs.get("hw").unwrap();
assert_eq!(ab.expansion, "hello world");
assert_eq!(ab.hook.as_deref(), Some("my-hook"));
assert!(ab.system);
assert_eq!(ab.count, 0);
}
#[test]
fn all_table_names() {
crate::test_utils::init_test_tracing();
let mut mgr = AbbrevManager::new();
mgr.create_table("z-table");
mgr.create_table("a-table");
let names = mgr.all_table_names();
assert!(names.contains(&"a-table"));
assert!(names.contains(&"global-abbrev-table"));
assert!(names.contains(&"z-table"));
for i in 1..names.len() {
assert!(names[i - 1] <= names[i]);
}
}
#[test]
fn test_apply_case() {
crate::test_utils::init_test_tracing();
assert_eq!(apply_case("hello world", "hw", false), "hello world");
assert_eq!(apply_case("hello world", "Hw", false), "Hello world");
assert_eq!(apply_case("hello world", "HW", false), "HELLO WORLD");
assert_eq!(apply_case("hello world", "HW", true), "hello world");
assert_eq!(apply_case("", "HW", false), "");
assert_eq!(apply_case("hello", "", false), "hello");
}
#[test]
fn test_make_abbrev_table_and_predicate() {
crate::test_utils::init_test_tracing();
use super::super::eval::Context;
let mut eval = Context::new();
let table = builtin_make_abbrev_table(&mut eval, vec![]).unwrap();
assert!(table.is_vector());
let result = builtin_abbrev_table_p(&mut eval, vec![table]).unwrap();
assert!(result.is_truthy());
let result = builtin_abbrev_table_p(&mut eval, vec![Value::NIL]).unwrap();
assert!(result.is_nil());
let result = builtin_abbrev_table_p(&mut eval, vec![Value::fixnum(42)]).unwrap();
assert!(result.is_nil());
let plain_vec = Value::vector(vec![Value::fixnum(0); 10]);
let result = builtin_abbrev_table_p(&mut eval, vec![plain_vec]).unwrap();
assert!(result.is_nil());
}
#[test]
fn test_define_abbrev_and_lookup() {
crate::test_utils::init_test_tracing();
use super::super::eval::Context;
let mut eval = Context::new();
let table = builtin_make_abbrev_table(&mut eval, vec![]).unwrap();
let defined = builtin_define_abbrev(
&mut eval,
vec![table, Value::string("btw"), Value::string("by the way")],
)
.unwrap();
assert_eq!(defined.as_str(), Some("btw"));
let result = builtin_abbrev_expansion(&mut eval, vec![Value::string("btw"), table]).unwrap();
assert_eq!(result.as_str(), Some("by the way"));
let sym = builtin_abbrev_symbol(&mut eval, vec![Value::string("btw"), table]).unwrap();
assert!(sym.is_truthy());
assert_eq!(sym.as_symbol_name(), Some("btw"));
let result = builtin_abbrev_expansion(&mut eval, vec![Value::string("xyz"), table]).unwrap();
assert!(result.is_nil());
}
#[test]
fn test_clear_abbrev_table() {
crate::test_utils::init_test_tracing();
use super::super::eval::Context;
let mut eval = Context::new();
let table = builtin_make_abbrev_table(&mut eval, vec![]).unwrap();
builtin_define_abbrev(
&mut eval,
vec![table, Value::string("a"), Value::string("alpha")],
)
.unwrap();
builtin_define_abbrev(
&mut eval,
vec![table, Value::string("b"), Value::string("beta")],
)
.unwrap();
let result = builtin_abbrev_expansion(&mut eval, vec![Value::string("a"), table]).unwrap();
assert_eq!(result.as_str(), Some("alpha"));
builtin_clear_abbrev_table(&mut eval, vec![table]).unwrap();
let result = builtin_abbrev_expansion(&mut eval, vec![Value::string("a"), table]).unwrap();
assert!(result.is_nil());
let result = builtin_abbrev_table_p(&mut eval, vec![table]).unwrap();
assert!(result.is_truthy());
}
#[test]
fn test_abbrev_get_put() {
crate::test_utils::init_test_tracing();
use super::super::eval::Context;
let mut eval = Context::new();
let table = builtin_make_abbrev_table(&mut eval, vec![]).unwrap();
builtin_define_abbrev(
&mut eval,
vec![table, Value::string("hw"), Value::string("hello world")],
)
.unwrap();
let sym = builtin_abbrev_symbol(&mut eval, vec![Value::string("hw"), table]).unwrap();
}
#[test]
fn test_define_abbrev_table_and_lookup() {
crate::test_utils::init_test_tracing();
use super::super::eval::Context;
let mut eval = Context::new();
builtin_define_abbrev_table(&mut eval, vec![Value::symbol("test-table"), Value::NIL]).unwrap();
let table = eval.obarray().symbol_value("test-table").cloned().unwrap();
let result = builtin_abbrev_table_p(&mut eval, vec![table]).unwrap();
assert!(result.is_truthy());
}
#[test]
fn test_insert_abbrev_table_description_writes_buffer_text() {
crate::test_utils::init_test_tracing();
use super::super::eval::Context;
let mut eval = Context::new();
builtin_define_abbrev_table(&mut eval, vec![Value::symbol("test-table"), Value::NIL]).unwrap();
let table = eval
.obarray()
.symbol_value("test-table")
.cloned()
.expect("test-table value");
builtin_define_abbrev(
&mut eval,
vec![
table,
Value::string("btw"),
Value::string("by the way"),
Value::NIL,
Value::fixnum(7),
],
)
.unwrap();
builtin_insert_abbrev_table_description(&mut eval, vec![Value::symbol("test-table")]).unwrap();
let rendered = eval
.buffers
.current_buffer()
.expect("current buffer")
.buffer_string();
assert_eq!(
rendered,
"(define-abbrev-table 'test-table\n '(\n (\"btw\" \"by the way\" 7)\n ))\n\n"
);
}
#[test]
fn test_abbrev_tables_do_not_share_symbol_cells() {
crate::test_utils::init_test_tracing();
use super::super::eval::Context;
let mut eval = Context::new();
let table_a = builtin_make_abbrev_table(&mut eval, vec![]).unwrap();
let table_b = builtin_make_abbrev_table(&mut eval, vec![]).unwrap();
builtin_define_abbrev(
&mut eval,
vec![table_a, Value::string("dup"), Value::string("first table")],
)
.unwrap();
builtin_define_abbrev(
&mut eval,
vec![table_b, Value::string("dup"), Value::string("second table")],
)
.unwrap();
let a = builtin_abbrev_expansion(&mut eval, vec![Value::string("dup"), table_a]).unwrap();
let b = builtin_abbrev_expansion(&mut eval, vec![Value::string("dup"), table_b]).unwrap();
assert_eq!(a.as_str(), Some("first table"));
assert_eq!(b.as_str(), Some("second table"));
}
#[test]
fn test_abbrev_table_properties_are_table_local() {
crate::test_utils::init_test_tracing();
use super::super::eval::Context;
let mut eval = Context::new();
let table_a = builtin_make_abbrev_table(
&mut eval,
vec![Value::list(vec![Value::keyword(":case-fixed"), Value::T])],
)
.unwrap();
let table_b = builtin_make_abbrev_table(&mut eval, vec![]).unwrap();
let a =
builtin_abbrev_table_get(&mut eval, vec![table_a, Value::keyword(":case-fixed")]).unwrap();
let b =
builtin_abbrev_table_get(&mut eval, vec![table_b, Value::keyword(":case-fixed")]).unwrap();
assert!(a.is_truthy());
assert!(b.is_nil());
}
#[test]
fn test_wrong_arg_count() {
crate::test_utils::init_test_tracing();
use super::super::eval::Context;
let mut eval = Context::new();
let result = builtin_expand_abbrev(&mut eval, vec![Value::string("t")]);
assert!(result.is_err());
let result = builtin_expand_abbrev(&mut eval, vec![]);
assert!(result.is_ok());
assert!(result.unwrap().is_nil());
}