use super::super::*;
#[test]
fn test_cursor_at_end_or_before_closing_at_end() {
let condition = CursorAtEndOrBeforeClosing;
let mut state = EditorState::new();
assert!(condition.check(&state));
state.buffer = "foo".to_string();
state.buffer_len = 3;
state.cursor_pos = 3;
assert!(condition.check(&state));
}
#[test]
fn test_cursor_at_end_or_before_closing_before_closing_chars() {
let condition = CursorAtEndOrBeforeClosing;
let closing_chars = [')', ']', '}', '"', '\'', '`'];
for closing_char in closing_chars {
let mut state = EditorState::new();
state.buffer = format!("x{}", closing_char);
state.buffer_len = 2;
state.cursor_pos = 1;
assert!(
condition.check(&state),
"Expected condition to pass when cursor is before '{}'",
closing_char
);
}
}
#[test]
fn test_cursor_at_end_or_before_closing_before_regular_char() {
let condition = CursorAtEndOrBeforeClosing;
let mut state = EditorState::new();
state.buffer = "abc".to_string();
state.buffer_len = 3;
state.cursor_pos = 1;
assert!(!condition.check(&state));
}
#[test]
fn test_cursor_at_end_or_before_closing_uncertain() {
let condition = CursorAtEndOrBeforeClosing;
let mut state = EditorState::new();
state.buffer = "()".to_string();
state.buffer_len = 2;
state.cursor_pos = 1;
state.uncertain = true;
assert!(!condition.check(&state));
}
#[test]
fn test_auto_match_quote_inside_parens() {
let condition = CursorAtEndOrBeforeClosing;
let mut state = EditorState::new();
state.buffer = "()".to_string();
state.buffer_len = 2;
state.cursor_pos = 1;
assert!(condition.check(&state));
state.update_from_event(&ReedlineEvent::Edit(vec![
EditCommand::InsertString(r#""""#.to_string()),
EditCommand::MoveLeft { select: false },
]));
assert_eq!(state.buffer, r#"("")"#);
assert_eq!(state.cursor_pos, 2); }
#[test]
fn test_auto_match_parens_inside_quotes() {
let condition = CursorAtEndOrBeforeClosing;
let mut state = EditorState::new();
state.buffer = r#""""#.to_string();
state.buffer_len = 2;
state.cursor_pos = 1;
assert!(condition.check(&state));
state.update_from_event(&ReedlineEvent::Edit(vec![
EditCommand::InsertString("()".to_string()),
EditCommand::MoveLeft { select: false },
]));
assert_eq!(state.buffer, r#""()""#);
assert_eq!(state.cursor_pos, 2); }
#[test]
fn test_auto_match_braces_inside_brackets() {
let condition = CursorAtEndOrBeforeClosing;
let mut state = EditorState::new();
state.buffer = "[]".to_string();
state.buffer_len = 2;
state.cursor_pos = 1;
assert!(condition.check(&state));
state.update_from_event(&ReedlineEvent::Edit(vec![
EditCommand::InsertString("{}".to_string()),
EditCommand::MoveLeft { select: false },
]));
assert_eq!(state.buffer, "[{}]");
assert_eq!(state.cursor_pos, 2);
}
#[test]
fn test_auto_match_parens_inside_raw_string() {
let condition = CursorAtEndOrBeforeClosing;
let mut state = EditorState::new();
state.buffer = r#"r"""#.to_string();
state.buffer_len = 3;
state.cursor_pos = 2;
assert!(condition.check(&state));
state.update_from_event(&ReedlineEvent::Edit(vec![
EditCommand::InsertString("()".to_string()),
EditCommand::MoveLeft { select: false },
]));
assert_eq!(state.buffer, r#"r"()""#);
assert_eq!(state.cursor_pos, 3);
}
#[test]
fn test_auto_match_at_end_still_works() {
let condition = CursorAtEndOrBeforeClosing;
let mut state = EditorState::new();
state.buffer = "foo".to_string();
state.buffer_len = 3;
state.cursor_pos = 3;
assert!(condition.check(&state));
state.update_from_event(&ReedlineEvent::Edit(vec![
EditCommand::InsertString("()".to_string()),
EditCommand::MoveLeft { select: false },
]));
assert_eq!(state.buffer, "foo()");
assert_eq!(state.cursor_pos, 4);
}
#[test]
fn test_auto_match_blocked_in_middle() {
let condition = CursorAtEndOrBeforeClosing;
let mut state = EditorState::new();
state.buffer = "foobar".to_string();
state.buffer_len = 6;
state.cursor_pos = 3;
assert!(!condition.check(&state));
}
#[test]
fn test_auto_match_deeply_nested() {
let condition = CursorAtEndOrBeforeClosing;
let mut state = EditorState::new();
state.buffer = "()".to_string();
state.buffer_len = 2;
state.cursor_pos = 1;
assert!(condition.check(&state));
state.update_from_event(&ReedlineEvent::Edit(vec![
EditCommand::InsertString("[]".to_string()),
EditCommand::MoveLeft { select: false },
]));
assert_eq!(state.buffer, "([])");
assert_eq!(state.cursor_pos, 2);
assert!(condition.check(&state));
state.update_from_event(&ReedlineEvent::Edit(vec![
EditCommand::InsertString("{}".to_string()),
EditCommand::MoveLeft { select: false },
]));
assert_eq!(state.buffer, "([{}])");
assert_eq!(state.cursor_pos, 3);
assert!(condition.check(&state));
state.update_from_event(&ReedlineEvent::Edit(vec![
EditCommand::InsertString(r#""""#.to_string()),
EditCommand::MoveLeft { select: false },
]));
assert_eq!(state.buffer, r#"([{""}])"#);
assert_eq!(state.cursor_pos, 4);
}
#[test]
fn test_auto_match_rules_use_new_condition() {
let rules = create_auto_match_rules();
let paren_rule = &rules[0];
let mut state = EditorState::new();
state.buffer = "()".to_string();
state.buffer_len = 2;
state.cursor_pos = 1;
assert!(paren_rule.condition.check(&state));
state.buffer = "ab".to_string();
state.cursor_pos = 1;
assert!(!paren_rule.condition.check(&state));
}