Skip to content

Implement fixtures for Task & Project structs #1403

@stacksjb

Description

@stacksjb

CoPilot recommends several test cases (copy/paste below, have NOT vetted or reviewed)

However, they require implementing a "Default" for Task & Project Struct (which is not currently implemented) to test.


    #[test]
    fn test_content_priority_and_links() {
        let mut config = Config::default();
        let mut task = Task::default();
        task.content = "Test".to_string();

        // Priority::Low
        task.priority = priority::Priority::Low;
        assert_eq!(content(&task, &config), color::blue_string("Test"));

        // Priority::Medium
        task.priority = priority::Priority::Medium;
        assert_eq!(content(&task, &config), color::yellow_string("Test"));

        // Priority::High
        task.priority = priority::Priority::High;
        assert_eq!(content(&task, &config), color::red_string("Test"));

        // Priority::None
        task.priority = priority::Priority::None;
        assert_eq!(content(&task, &config), color::normal_string("Test"));

        // Hyperlinks disabled
        config.disable_links = true;
        assert_eq!(content(&task, &config), color::normal_string("Test"));
    }

    #[test]
    fn test_labels() {
        let mut task = Task::default();
        task.labels = vec!["foo".to_string(), "bar".to_string()];
        let result = labels(&task);
        assert!(result.contains("@"));
        assert!(result.contains("foo"));
        assert!(result.contains("bar"));
    }

    #[test]
    fn test_due_no_date() {
        let config = Config::default();
        let task = Task::default();
        assert_eq!(due(&task, &config, ""), "");
    }

    #[test]
    fn test_number_comments() {
        assert!(number_comments(1).contains("1 comment"));
        assert!(number_comments(2).contains("2 comments"));
    }

    #[tokio::test]
    async fn test_project_found_and_not_found() {
        use crate::projects::Project;

        // Mock config with a project
        let mut config = Config::default();
        let project_id = "123".to_string();
        config
            .set_projects(vec![Project {
                id: project_id.clone(),
                name: "TestProj".to_string(),
                ..Default::default()
            }])
            .await
            .unwrap();

        let mut task = Task::default();
        task.project_id = project_id.clone();

        // Project found
        let found = project(&task, &config, "  ").await.unwrap();
        assert!(found.contains("TestProj"));

        // Project not found
        task.project_id = "notfound".to_string();
        let not_found = project(&task, &config, "  ").await.unwrap();
        assert!(not_found.contains("Project not in config"));
    }
    #[test]
    fn test_due_various_branches() {
        let config = Config::default();
        let mut task = Task::default();

        // Mock DateTimeInfo::Date
        task.set_datetimeinfo(DateTimeInfo::Date {
            date: chrono::NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
            is_recurring: false,
            string: "every day".to_string(),
        });
        let out = due(&task, &config, "");
        assert!(out.contains("!"));

        // Mock DateTimeInfo::DateTime with duration
        task.set_datetimeinfo(DateTimeInfo::DateTime {
            datetime: chrono::NaiveDateTime::from_timestamp_opt(1_700_000_000, 0).unwrap(),
            is_recurring: true,
            string: "every week".to_string(),
        });
        task.duration = Some(Duration {
            amount: 2,
            unit: Unit::Day,
        });
        let out = due(&task, &config, "");
        assert!(out.contains("for 2 days"));
        assert!(out.contains("↻"));
    }

@alanvardy do you see any issues with that? Given tehse are autogenerated/recommended I don't know if it's necessary, maybe there is an easier method.

Metadata

Metadata

Assignees

No one assigned

    Labels

    first-issueA great issue for newcomers to fix

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions