use crate::common::scenario::buffer_scenario::{
assert_buffer_scenario, BufferScenario, CursorExpect,
};
use fresh::test_api::Action;
#[test]
fn theorem_toggle_comment_rust_uses_double_slash_prefix() {
assert_buffer_scenario(BufferScenario {
language: Some("x.rs".into()),
description: "ToggleComment on a .rs file uses '// ' as the prefix".into(),
initial_text: "fn main() {\n println!(\"hello\");\n}".into(),
actions: vec![Action::ToggleComment],
expected_text: "// fn main() {\n println!(\"hello\");\n}".into(),
expected_primary: CursorExpect::at(3),
expected_extra_cursors: vec![],
expected_selection_text: None,
..Default::default()
});
}
#[test]
fn theorem_toggle_comment_python_uses_hash_prefix() {
assert_buffer_scenario(BufferScenario {
language: Some("x.py".into()),
description: "ToggleComment on a .py file uses '# ' as the prefix".into(),
initial_text: "def main():\n print(\"hello\")\n".into(),
actions: vec![Action::ToggleComment],
expected_text: "# def main():\n print(\"hello\")\n".into(),
expected_primary: CursorExpect::at(2),
expected_extra_cursors: vec![],
expected_selection_text: None,
..Default::default()
});
}
#[test]
fn theorem_toggle_comment_shell_uses_hash_prefix() {
assert_buffer_scenario(BufferScenario {
language: Some("x.sh".into()),
description: "ToggleComment on a .sh file uses '# ' as the prefix".into(),
initial_text: "echo hello\n".into(),
actions: vec![Action::ToggleComment],
expected_text: "# echo hello\n".into(),
expected_primary: CursorExpect::at(2),
expected_extra_cursors: vec![],
expected_selection_text: None,
..Default::default()
});
}
#[test]
fn theorem_toggle_comment_yaml_uses_hash_prefix() {
assert_buffer_scenario(BufferScenario {
language: Some("x.yaml".into()),
description: "ToggleComment on a .yaml file uses '# ' (issue #774)".into(),
initial_text: "key: value\nnested:\n child: 123".into(),
actions: vec![Action::ToggleComment],
expected_text: "# key: value\nnested:\n child: 123".into(),
expected_primary: CursorExpect::at(2),
expected_extra_cursors: vec![],
expected_selection_text: None,
..Default::default()
});
}
#[test]
fn theorem_toggle_comment_yml_uses_hash_prefix() {
assert_buffer_scenario(BufferScenario {
language: Some("x.yml".into()),
description: "ToggleComment on a .yml file uses '# ' (issue #774)".into(),
initial_text: "server:\n port: 8080".into(),
actions: vec![Action::ToggleComment],
expected_text: "# server:\n port: 8080".into(),
expected_primary: CursorExpect::at(2),
expected_extra_cursors: vec![],
expected_selection_text: None,
..Default::default()
});
}
#[test]
fn theorem_toggle_comment_preserves_selection() {
assert_buffer_scenario(BufferScenario {
language: Some("x.rs".into()),
description: "Comment-toggling preserves and grows the selection by 2*'// '".into(),
initial_text: "line1\nline2\nline3\nline4".into(),
actions: vec![
Action::SelectDown,
Action::SelectDown,
Action::ToggleComment,
],
expected_text: "// line1\n// line2\nline3\nline4".into(),
expected_primary: CursorExpect::range(0, 18),
expected_extra_cursors: vec![],
expected_selection_text: Some("// line1\n// line2\n".into()),
..Default::default()
});
}
#[test]
fn theorem_toggle_uncomment_preserves_selection() {
assert_buffer_scenario(BufferScenario {
language: Some("x.rs".into()),
description: "Comment-toggling on commented lines uncomments and preserves the selection"
.into(),
initial_text: "// line1\n// line2\n// line3\nline4".into(),
actions: vec![
Action::SelectDown,
Action::SelectDown,
Action::ToggleComment,
],
expected_text: "line1\nline2\n// line3\nline4".into(),
expected_primary: CursorExpect::range(0, 12),
expected_extra_cursors: vec![],
expected_selection_text: Some("line1\nline2\n".into()),
..Default::default()
});
}
#[test]
fn theorem_toggle_comment_roundtrip_with_selection_is_identity() {
assert_buffer_scenario(BufferScenario {
language: Some("x.rs".into()),
description: "SelectAll + Toggle + SelectAll + Toggle is the identity on text".into(),
initial_text: "line1\nline2\nline3".into(),
actions: vec![
Action::SelectAll,
Action::ToggleComment,
Action::SelectAll,
Action::ToggleComment,
],
expected_text: "line1\nline2\nline3".into(),
expected_primary: CursorExpect::range(0, 17),
expected_extra_cursors: vec![],
expected_selection_text: Some("line1\nline2\nline3".into()),
..Default::default()
});
}
#[test]
fn theorem_toggle_comment_single_line_no_newline() {
assert_buffer_scenario(BufferScenario {
language: Some("x.c".into()),
description: "ToggleComment on a single-line .c buffer with no trailing newline".into(),
initial_text: "int main() {}".into(),
actions: vec![Action::SelectAll, Action::ToggleComment],
expected_text: "// int main() {}".into(),
expected_primary: CursorExpect::range(0, 16),
expected_extra_cursors: vec![],
expected_selection_text: Some("// int main() {}".into()),
..Default::default()
});
}
#[test]
fn theorem_toggle_comment_selection_at_buffer_end() {
assert_buffer_scenario(BufferScenario {
language: Some("x.rs".into()),
description: "ToggleComment over a SelectAll that ends exactly at buffer length".into(),
initial_text: "fn foo() {}\nfn bar() {}".into(),
actions: vec![Action::SelectAll, Action::ToggleComment],
expected_text: "// fn foo() {}\n// fn bar() {}".into(),
expected_primary: CursorExpect::range(0, 29),
expected_extra_cursors: vec![],
expected_selection_text: Some("// fn foo() {}\n// fn bar() {}".into()),
..Default::default()
});
}