use super::super::*;
#[test]
fn test_cursor_in_quote_empty() {
let state = EditorState::new();
assert!(!state.cursor_in_quote('"'));
assert!(!state.cursor_in_quote('\''));
}
#[test]
fn test_cursor_in_quote_unclosed() {
let mut state = EditorState::new();
state.buffer = r#""foo"#.to_string();
state.buffer_len = 4;
state.cursor_pos = 4;
assert!(state.cursor_in_quote('"'));
assert!(!state.cursor_in_quote('\''));
state.buffer = "'foo".to_string();
state.buffer_len = 4;
state.cursor_pos = 4;
assert!(state.cursor_in_quote('\''));
assert!(!state.cursor_in_quote('"'));
}
#[test]
fn test_cursor_in_quote_closed() {
let mut state = EditorState::new();
state.buffer = r#""foo""#.to_string();
state.buffer_len = 5;
state.cursor_pos = 5;
assert!(!state.cursor_in_quote('"'));
state.buffer = r#""foo" "#.to_string();
state.buffer_len = 6;
state.cursor_pos = 6;
assert!(!state.cursor_in_quote('"'));
}
#[test]
fn test_cursor_in_quote_escaped() {
let mut state = EditorState::new();
state.buffer = r#""foo\"bar"#.to_string();
state.buffer_len = 9;
state.cursor_pos = 9;
assert!(state.cursor_in_quote('"'));
state.buffer = r#""foo\"bar""#.to_string();
state.buffer_len = 10;
state.cursor_pos = 10;
assert!(!state.cursor_in_quote('"'));
}
#[test]
fn test_cursor_in_quote_uncertain() {
let mut state = EditorState::new();
state.buffer = r#""foo"#.to_string();
state.buffer_len = 4;
state.cursor_pos = 4;
state.uncertain = true;
assert!(!state.cursor_in_quote('"'));
}
#[test]
fn test_quote_auto_match_condition_not_in_string() {
let condition = CursorAtEndOrBeforeClosingAndNotInQuote::new('"');
let mut state = EditorState::new();
state.buffer = "foo".to_string();
state.buffer_len = 3;
state.cursor_pos = 3;
assert!(condition.check(&state));
}
#[test]
fn test_quote_auto_match_condition_in_unclosed_string() {
let condition = CursorAtEndOrBeforeClosingAndNotInQuote::new('"');
let mut state = EditorState::new();
state.buffer = r#""foo"#.to_string();
state.buffer_len = 4;
state.cursor_pos = 4;
assert!(!condition.check(&state));
}
#[test]
fn test_quote_auto_match_condition_after_closed_string() {
let condition = CursorAtEndOrBeforeClosingAndNotInQuote::new('"');
let mut state = EditorState::new();
state.buffer = r#""foo" "#.to_string();
state.buffer_len = 6;
state.cursor_pos = 6;
assert!(condition.check(&state));
}
#[test]
fn test_quote_auto_match_closes_string() {
let rules = create_auto_match_rules();
let quote_rule = &rules[3];
let mut state = EditorState::new();
state.buffer = r#""foo"#.to_string();
state.buffer_len = 4;
state.cursor_pos = 4;
assert!(!quote_rule.condition.check(&state));
match "e_rule.fallback_event {
ReedlineEvent::Edit(cmds) => {
assert_eq!(cmds.len(), 1);
assert!(matches!(&cmds[0], EditCommand::InsertChar('"')));
}
_ => panic!("Expected Edit event"),
}
}
#[test]
fn test_quote_auto_match_works_outside_string() {
let rules = create_auto_match_rules();
let quote_rule = &rules[3];
let mut state = EditorState::new();
state.buffer = "foo".to_string();
state.buffer_len = 3;
state.cursor_pos = 3;
assert!(quote_rule.condition.check(&state));
}
#[test]
fn test_single_quote_auto_match_in_string() {
let rules = create_auto_match_rules();
let quote_rule = &rules[4];
let mut state = EditorState::new();
state.buffer = "'foo".to_string();
state.buffer_len = 4;
state.cursor_pos = 4;
assert!(!quote_rule.condition.check(&state));
}
#[test]
fn test_backtick_auto_match_in_string() {
let rules = create_auto_match_rules();
let quote_rule = &rules[5];
let mut state = EditorState::new();
state.buffer = "`foo".to_string();
state.buffer_len = 4;
state.cursor_pos = 4;
assert!(!quote_rule.condition.check(&state));
}