Skip to content

Visual Studio Code, rebuilt in Rust

Rust
2.7 KB
Raw Blame
use std::path::Path;

use gpui::{TestAppContext, VisualTestContext};
use serde_json::{Value, json};

use crate::menus::MenuAction;
use crate::workbench::Workbench;

fn add(id: &str, label: &str) -> Value {
    json!({ "op": 0, "item": { "expand": 0, "item": { "extId": id, "label": label, "tags": [], "uri": { "scheme": "file", "path": "/math.test.js" } } } })
}

fn workbench(cx: &mut TestAppContext) -> (gpui::Entity<Workbench>, &mut VisualTestContext) {
    let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
    let theme = crate::theme::load_theme(&[exthost::scan::scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap()], "Dark Modern").unwrap();
    let (sender, _host) = std::sync::mpsc::channel();
    cx.add_window_view(|_, cx| {
        let mut workbench = Workbench::new(theme, app_root.clone(), None, crate::languages::Languages::new(&[]), crate::default_settings(), crate::host_sync::HostSync::new(sender), cx);
        workbench.testing.collection.apply(&[add("c", "c"), add("c\0math", "Math"), add("c\0math\0basic", "Basic"), add("c\0math\0basic\0add", "adds numbers"), add("c\0math\0basic\0sub", "subtracts"), add("c\0other", "Other")]);
        workbench
    })
}

fn checked(workbench: &Workbench) -> Vec<String> {
    let mut titles = Vec::new();
    for action in workbench.testing_more_menu_actions() {
        if let MenuAction::Command { title, checked: true, .. } = action {
            titles.push(title);
        }
    }
    titles
}

/// testExplorerActions.ts: View as List lists the root's leaves with their parents' labels (listProjection.ts), the
/// "..." checks the stored mode and sorting, and a file root below a single controller starts collapsed.
#[gpui::test]
fn view_as_list_shows_the_leaves_and_the_more_menu_checks_the_mode(cx: &mut TestAppContext) {
    let (workbench, cx) = workbench(cx);
    workbench.read_with(cx, |workbench, _| {
        assert_eq!(workbench.explorer_children(None), ["c\0math", "c\0other"]);
        assert!(!workbench.test_row_expanded("c\0math"), "a file root starts collapsed");
        assert!(workbench.test_row_expanded("c"));
        assert_eq!(checked(workbench), ["View as Tree", "Sort by Location"]);
    });
    workbench.update_in(cx, |workbench, window, cx| {
        drop(workbench.execute_command("testing.viewAsList", Vec::new(), window, cx));
        drop(workbench.execute_command("testing.sortByDuration", Vec::new(), window, cx));
    });
    workbench.read_with(cx, |workbench, _| {
        assert_eq!(workbench.explorer_children(None), ["c\0math\0basic\0add", "c\0other", "c\0math\0basic\0sub"]);
        assert_eq!(workbench.test_ancestor_labels("c\0math\0basic\0add"), "Math \u{203a} Basic");
        assert_eq!(checked(workbench), ["View as List", "Sort by Duration"]);
    });
}