#[cfg(test)]
mod tests {
use super::super::*;
use crate::notebook::{NotebookGUI, CellEditor, NotebookCell, CellType, Notebook};
use fltk::prelude::*;
#[test]
fn test_cell_editor_basic_functionality() {
let cell = NotebookCell::new_code("x + y".to_string());
let original_id = cell.id;
let editor = CellEditor::new(0, 0, 400, 200, &cell);
assert_eq!(editor.cell_id(), original_id);
assert_eq!(editor.cell_type(), &CellType::Code);
assert_eq!(editor.get_input_text(), "x + y");
assert!(!editor.is_selected());
}
#[test]
fn test_cell_editor_selection() {
let cell = NotebookCell::new_code("test".to_string());
let mut editor = CellEditor::new(0, 0, 400, 200, &cell);
assert!(!editor.is_selected());
editor.set_selected(true);
assert!(editor.is_selected());
editor.set_selected(false);
assert!(!editor.is_selected());
}
#[test]
fn test_cell_editor_type_conversion() {
let cell = NotebookCell::new_code("# 标题".to_string());
let mut editor = CellEditor::new(0, 0, 400, 200, &cell);
assert_eq!(editor.cell_type(), &CellType::Code);
editor.set_cell_type(CellType::Markdown);
assert_eq!(editor.cell_type(), &CellType::Markdown);
editor.set_cell_type(CellType::Text);
assert_eq!(editor.cell_type(), &CellType::Text);
}
#[test]
fn test_cell_editor_text_operations() {
let cell = NotebookCell::new_code("original text".to_string());
let mut editor = CellEditor::new(0, 0, 400, 200, &cell);
assert_eq!(editor.get_input_text(), "original text");
editor.set_input_text("new text");
assert_eq!(editor.get_input_text(), "new text");
editor.set_output_text("output result");
}
#[test]
fn test_notebook_gui_creation() {
if std::env::var("DISPLAY").is_err() && std::env::var("WAYLAND_DISPLAY").is_err() {
println!("跳过 GUI 测试:没有图形环境");
return;
}
match std::panic::catch_unwind(|| {
let gui = NotebookGUI::new();
assert!(gui.get_notebook().is_none());
}) {
Ok(_) => println!("GUI 创建测试通过"),
Err(_) => println!("GUI 创建测试跳过:无法初始化图形环境"),
}
}
#[test]
fn test_notebook_gui_set_notebook() {
if std::env::var("DISPLAY").is_err() && std::env::var("WAYLAND_DISPLAY").is_err() {
println!("跳过 GUI 测试:没有图形环境");
return;
}
let mut notebook = Notebook::with_title("测试笔记本".to_string());
notebook.add_cell(NotebookCell::new_code("x = 1".to_string()));
notebook.add_cell(NotebookCell::new_markdown("# 标题".to_string()));
match std::panic::catch_unwind(|| {
let mut gui = NotebookGUI::new();
let result = gui.set_notebook(notebook);
assert!(result.is_ok());
}) {
Ok(_) => println!("笔记本设置测试通过"),
Err(_) => println!("笔记本设置测试跳过:无法初始化图形环境"),
}
}
#[test]
fn test_shortcut_functionality() {
let ctrl_n_shortcut = fltk::enums::Shortcut::Ctrl | 'n';
assert_eq!(ctrl_n_shortcut.bits(), (fltk::enums::Shortcut::Ctrl | 'n').bits());
let ctrl_m_shortcut = fltk::enums::Shortcut::Ctrl | 'm';
assert_eq!(ctrl_m_shortcut.bits(), (fltk::enums::Shortcut::Ctrl | 'm').bits());
let ctrl_enter_shortcut = fltk::enums::Shortcut::Ctrl | fltk::enums::Shortcut::from_key(fltk::enums::Key::Enter);
assert!(ctrl_enter_shortcut.bits() != 0);
let shift_enter_shortcut = fltk::enums::Shortcut::Shift | fltk::enums::Shortcut::from_key(fltk::enums::Key::Enter);
assert!(shift_enter_shortcut.bits() != 0);
}
#[test]
fn test_syntax_highlighting_setup() {
let code_cell = NotebookCell::new_code("fn main() {}".to_string());
let mut code_editor = CellEditor::new(0, 0, 400, 200, &code_cell);
assert_eq!(code_editor.cell_type(), &CellType::Code);
let markdown_cell = NotebookCell::new_markdown("# 标题\n\n内容".to_string());
let mut markdown_editor = CellEditor::new(0, 0, 400, 200, &markdown_cell);
assert_eq!(markdown_editor.cell_type(), &CellType::Markdown);
code_editor.set_cell_type(CellType::Markdown);
assert_eq!(code_editor.cell_type(), &CellType::Markdown);
markdown_editor.set_cell_type(CellType::Code);
assert_eq!(markdown_editor.cell_type(), &CellType::Code);
}
#[test]
fn test_cell_container_functionality() {
let cell = NotebookCell::new_code("test".to_string());
let editor = CellEditor::new(10, 20, 400, 200, &cell);
let container = editor.container();
assert!(container.x() == 10);
assert!(container.y() == 20);
assert!(container.width() == 400);
assert!(container.height() == 200);
}
#[test]
fn test_user_interaction_flow() {
if std::env::var("DISPLAY").is_err() && std::env::var("WAYLAND_DISPLAY").is_err() {
println!("跳过集成测试:没有图形环境");
return;
}
match std::panic::catch_unwind(|| {
let mut notebook = Notebook::with_title("集成测试笔记本".to_string());
notebook.add_cell(NotebookCell::new_code("x = 2 + 3".to_string()));
notebook.add_cell(NotebookCell::new_markdown("## 计算结果".to_string()));
notebook.add_cell(NotebookCell::new_code("y = x * 2".to_string()));
let mut gui = NotebookGUI::new();
let result = gui.set_notebook(notebook);
assert!(result.is_ok());
assert!(gui.get_notebook().is_some());
println!("集成测试完成");
}) {
Ok(_) => println!("集成测试通过"),
Err(_) => println!("集成测试跳过:无法初始化图形环境"),
}
}
}
#[cfg(test)]
mod performance_tests {
use super::super::*;
use std::time::Instant;
#[test]
fn test_large_notebook_performance() {
let start = Instant::now();
let mut notebook = Notebook::with_title("性能测试笔记本".to_string());
for i in 0..100 {
notebook.add_cell(NotebookCell::new_code(format!("x{} = {}", i, i)));
}
let creation_time = start.elapsed();
println!("创建 100 个单元格耗时: {:?}", creation_time);
assert_eq!(notebook.cell_count(), 100);
assert!(creation_time.as_millis() < 10, "单元格创建性能不达标");
}
#[test]
fn test_cell_editor_creation_performance() {
let start = Instant::now();
let mut editors = Vec::new();
for i in 0..50 {
let cell = NotebookCell::new_code(format!("test_{}", i));
let editor = CellEditor::new(0, i * 160, 400, 150, &cell);
editors.push(editor);
}
let creation_time = start.elapsed();
println!("创建 50 个单元格编辑器耗时: {:?}", creation_time);
assert_eq!(editors.len(), 50);
assert!(creation_time.as_millis() < 100, "单元格编辑器创建性能不达标");
}
}
#[cfg(test)]
mod error_handling_tests {
use super::super::*;
#[test]
fn test_invalid_cell_type_conversion() {
let cell = NotebookCell::new_code("test".to_string());
let mut editor = CellEditor::new(0, 0, 400, 200, &cell);
editor.set_cell_type(CellType::Markdown);
assert_eq!(editor.cell_type(), &CellType::Markdown);
editor.set_cell_type(CellType::Text);
assert_eq!(editor.cell_type(), &CellType::Text);
editor.set_cell_type(CellType::Code);
assert_eq!(editor.cell_type(), &CellType::Code);
editor.set_cell_type(CellType::Output);
assert_eq!(editor.cell_type(), &CellType::Output);
}
#[test]
fn test_empty_text_handling() {
let cell = NotebookCell::new_code("".to_string());
let mut editor = CellEditor::new(0, 0, 400, 200, &cell);
assert_eq!(editor.get_input_text(), "");
editor.set_input_text("");
assert_eq!(editor.get_input_text(), "");
editor.set_input_text("some text");
assert_eq!(editor.get_input_text(), "some text");
editor.set_input_text("");
assert_eq!(editor.get_input_text(), "");
}
#[test]
fn test_long_text_handling() {
let long_text = "x = ".repeat(1000) + "1";
let cell = NotebookCell::new_code(long_text.clone());
let mut editor = CellEditor::new(0, 0, 400, 200, &cell);
assert_eq!(editor.get_input_text(), long_text);
let another_long_text = "y = ".repeat(500) + "2";
editor.set_input_text(&another_long_text);
assert_eq!(editor.get_input_text(), another_long_text);
}
}