flowcode-core 0.4.1-alpha

Core execution engine for FlowCode data scripting language
Documentation
// flowcode-core/tests/error_tests.rs

// Note: For tests to access internal items of the crate (like suggest_command if it were not pub),
// you'd typically have `mod tests { use super::*; }` within the .rs files of the library itself,
// or ensure functions/structs are pub if they need to be accessed from an integration-style test in the /tests directory.
// Since FCError, Parser are public, and suggest_command is a private function within parser.rs, we can only directly test Parser::parse here for suggestion integration.
// To test suggest_command directly, it would need to be pub or we'd test it via the public interface that uses it (Parser::parse).

use flowcode_core::error::FCError;
use flowcode_core::parser::Parser;
use flowcode_core::util; // util::parse_numbers

#[test]
fn test_fcerror_display() {
    let error = FCError::new("E001", "Test error message".to_string());
    assert_eq!(format!("{}", error), "[E001] Test error message");
}

// To directly test suggest_command, it would need to be made public in parser.rs
// For now, we test its effect through Parser::parse.

#[test]
fn test_parser_parse_empty_input() {
    let parser = Parser::new();
    let result = parser.parse("");
    assert!(matches!(result, Err(FCError::ParseError(_))));
}

#[test]
fn test_parser_parse_known_command_valid() {
    let parser = Parser::new();
    let result = parser.parse("combine 1 2 3");
    assert!(result.is_ok());
    if let Ok(cmd) = result {
        assert_eq!(cmd.name, "combine");
        assert_eq!(cmd.args.len(), 3);
    }
}

#[test]
fn test_parser_unknown_command() {
    let parser = Parser::new();
    let result = parser.parse("unknown_command arg1 arg2");
    assert!(matches!(result, Err(FCError::UnknownCommand(_))));
}

#[test]
fn test_parser_known_command() {
    let parser = Parser::new();
    let result = parser.parse("combine arg1 arg2");
    assert!(result.is_ok());
    if let Ok(parsed) = result {
        assert_eq!(parsed.name, "combine");
        assert_eq!(parsed.args.len(), 2);
    }
}

#[test]
fn test_parser_parse_unknown_command_no_suggestion() {
    let parser = Parser::new();
    let result = parser.parse("unknown");
    assert!(matches!(result, Err(FCError::UnknownCommand(_))));
}

#[test]
fn test_parser_parse_unknown_command_with_suggestion() {
    let parser = Parser::new();
    let result = parser.parse("comb");
    assert!(matches!(result, Err(FCError::Suggestion(_, _))));
}

#[test]
fn test_parse_numbers_empty() {
    let args = vec![];
    let result = util::parse_numbers(&args);
    assert!(result.is_err());
    if let Err(e) = result {
        assert_eq!(e, FCError::InsufficientData("No numbers provided".to_string()));
    }
}

#[test]
fn test_parse_numbers_valid() {
    let args = vec!["1.0".to_string(), "2.0".to_string(), "3.0".to_string()];
    let result = util::parse_numbers(&args);
    assert!(result.is_ok());
    if let Ok(numbers) = result {
        assert_eq!(numbers, vec![1.0, 2.0, 3.0]);
    }
}

#[test]
fn test_parse_numbers_invalid() {
    let args = vec!["1.0".to_string(), "not_a_number".to_string()];
    let result = util::parse_numbers(&args);
    assert!(result.is_err());
    if let Err(e) = result {
        assert_eq!(e, FCError::ParseError("Invalid number format: not_a_number".to_string()));
    }
}