Skip to content

Visual Studio Code, rebuilt in Rust

Rust
4.5 KB
Raw Blame
use serde_json::json;

use super::*;

fn add(id: &str, expand: u8) -> Value {
    json!({ "op": 0, "item": { "expand": expand, "item": { "extId": id, "label": id.rsplit('\0').next().unwrap(), "tags": [], "uri": { "scheme": "file", "path": "/w/a.test.js" }, "range": { "startLineNumber": 3, "startColumn": 1, "endLineNumber": 5, "endColumn": 1 } } } })
}

#[test]
fn diffs_build_the_tree_and_remove_subtrees() {
    let mut collection = TestCollection::default();
    let roots = collection.apply(&[add("ctrl", 1), add("ctrl\0b", 0), add("ctrl\0a", 1), add("ctrl\0a\0x", 0)]);
    assert_eq!(roots, vec!["ctrl".to_owned()], "a single root's resolvable child waits for the explorer to expand it");
    assert_eq!(collection.children_of("ctrl"), ["ctrl\0b".to_owned(), "ctrl\0a".to_owned()], "tests at one location keep the extension's order");
    let resolvable = collection.apply(&[json!({ "op": 1, "item": { "extId": "ctrl", "expand": 1 } }), json!({ "op": 1, "item": { "extId": "ctrl\0b", "expand": 1, "item": { "label": "renamed" } } })]);
    assert_eq!(resolvable, vec!["ctrl".to_owned()]);
    assert_eq!(collection.items["ctrl\0b"].label(), "renamed");
    collection.apply(&[json!({ "op": 3, "itemId": "ctrl\0a" })]);
    assert!(!collection.items.contains_key("ctrl\0a\0x"));
    assert_eq!(collection.children_of("ctrl"), ["ctrl\0b".to_owned()]);
    assert_eq!(collection.tests_in_file("/w/a.test.js").count(), 2);
}

/// testingExplorerView.ts `TreeSorter` by location: start line in one file, then sortText or label by `compareFileNames`.
#[test]
fn children_sort_by_line_then_by_file_name_order() {
    let test = |id: &str, path: &str, line: u32, sort_text: Option<&str>| {
        json!({ "op": 0, "item": { "expand": 0, "item": { "extId": id, "label": id.rsplit('\0').next().unwrap(), "tags": [], "sortText": sort_text, "uri": { "scheme": "file", "path": path }, "range": { "startLineNumber": line, "startColumn": 1, "endLineNumber": line, "endColumn": 1 } } } })
    };
    let mut collection = TestCollection::default();
    collection.apply(&[add("ctrl", 1), test("ctrl\0z", "/w/a.js", 9, None), test("ctrl\0y", "/w/a.js", 2, None), test("ctrl\0Test10", "/w/b.js", 1, None), test("ctrl\0test9", "/w/c.js", 1, None), test("ctrl\0k", "/w/d.js", 1, Some("Test95"))]);
    assert_eq!(collection.children_of("ctrl"), ["ctrl\0test9", "ctrl\0Test10", "ctrl\0k", "ctrl\0y", "ctrl\0z"]);
}

#[test]
fn terminal_states_do_not_go_back_and_parents_compute_the_worst() {
    let mut result = TestResult::new("r".into(), json!({ "targets": [{ "testIds": ["c\0a"] }] }));
    result.add_task(&json!({ "id": "t", "name": "task", "ctrlId": "c", "running": true }));
    result.add_chain(&[json!({ "extId": "c", "label": "c" }), json!({ "extId": "c\0a", "label": "a" })]);
    result.add_chain(&[json!({ "extId": "c", "label": "c" }), json!({ "extId": "c\0b", "label": "b" })]);
    result.update_state("c\0a", "t", ResultState::Failed, Some(3.));
    result.update_state("c\0a", "t", ResultState::Passed, None);
    result.update_state("c\0b", "t", ResultState::Running, None);
    assert_eq!(result.items["c\0a"].own_state, ResultState::Failed);
    assert_eq!(result.computed_state("c"), ResultState::Running);
    result.mark_complete(1);
    assert_eq!(result.items["c\0b"].own_state, ResultState::Skipped);
    assert_eq!(result.computed_state("c"), ResultState::Failed);
    assert_eq!(result.name(), "a");
}

#[test]
fn output_goes_to_the_task_and_marks_the_test() {
    let mut result = TestResult::new("r".into(), json!({}));
    result.add_task(&json!({ "id": "t", "name": "task", "ctrlId": "c" }));
    result.add_chain(&[json!({ "extId": "c", "label": "c" })]);
    result.append_output("t", "one\r\n", None, None);
    result.append_output("t", "two", None, Some("c"));
    assert_eq!(result.tasks[0].output, "one\r\ntwo");
    assert_eq!(result.items["c"].tasks[0].messages[0]["offset"], 5);
}

#[test]
fn default_profile_respects_tags() {
    let test = TestItem { id: "c\0a".into(), expand: ExpandState::NotExpandable, item: json!({ "tags": ["c\0slow"] }) };
    let profile = |id, tag: Option<&str>, is_default| Profile { controller_id: "c".into(), profile_id: id, label: String::new(), group: GROUP_RUN, is_default, tag: tag.map(str::to_owned), supports_continuous_run: false, has_configuration_handler: false };
    let profiles = [profile(1, Some("fast"), true), profile(2, Some("slow"), false), profile(3, None, false)];
    assert_eq!(default_profile(&profiles, GROUP_RUN, &test).map(|profile| profile.profile_id), Some(2));
    assert_eq!(default_profile(&profiles, GROUP_DEBUG, &test), None);
}