Visual Studio Code, rebuilt in Rust
Rust
use serde_json::json;
use super::{Cell, FeatureData, feature_data, features};
fn rows(data: FeatureData) -> (Vec<&'static str>, Vec<Vec<Cell>>) {
match data {
FeatureData::Table { headers, rows } => (headers, rows),
FeatureData::Markdown(markdown) => panic!("a table, not markdown {markdown:?}"),
}
}
fn text(value: &str) -> Cell {
Cell::Text(value.to_owned())
}
fn markdown(value: &str) -> Cell {
Cell::Markdown(value.to_owned())
}
#[test]
fn the_features_are_sorted_by_label_after_the_runtime_status() {
let manifest = json!({ "main": "./out/extension.js", "activationEvents": ["onLanguage:yaml"], "contributes": { "commands": [], "colors": [], "iconThemes": [], "grammars": [] } });
let labels = |running| features(&manifest, running).into_iter().map(|(_, label)| label).collect::<Vec<_>>();
assert_eq!(labels(true), ["Runtime Status", "Activation Events", "Colors", "Commands", "Themes"]);
assert_eq!(labels(false), ["Activation Events", "Colors", "Commands", "Themes"]);
}
/// extensionFeaturesTab.ts `RuntimeStatusMarkdownRenderer`: the Uncaught Errors from `$onExtensionRuntimeError`
/// (errors.ts `getErrorMessage`) and the extension point Messages after the activation.
#[test]
fn the_runtime_status_lists_uncaught_errors_and_messages() {
let id = "Test.Runtime-Status";
crate::perf::extension_runtime_error(&[json!({ "value": id }), json!({ "message": "boom", "stack": "Error: boom\n at x" })]);
crate::perf::extension_runtime_error(&[json!({ "value": id }), json!({ "message": "", "stack": "TypeError: bad\n at y" })]);
crate::perf::extension_point_message(id, crate::notifications::Severity::Warning, "a warning".into());
crate::perf::extension_point_message(id, crate::notifications::Severity::Warning, "a warning".into());
let id = "test.runtime-status";
let markdown = super::runtime_status_markdown(None, &crate::perf::runtime_errors(id), &crate::perf::messages(id));
assert_eq!(markdown, "### Activation\n\nNot yet activated\n ### Uncaught Errors (2)\n$(error) boom\n\n$(error) TypeError: bad\n\n\n ### Messages (1)\n$(warning) a warning\n\n");
}
/// configurationExtensionPoint.ts collector messages of both points.
#[test]
fn invalid_configuration_is_an_extension_message() {
use crate::notifications::Severity::{Error, Warning};
let first = json!({ "contributes": {
"configuration": [
{ "title": 5, "properties": { "test.ok": { "type": "string" }, " ": {}, "[json]": {}, "editor.fontSize": {}, "test.both": { "policy": { "name": "A" }, "policyReference": "B" }, "test.number": 3, "test.agents": { "agentsWindow": { "default": 1 }, "agentHost": { "key": "x" } } } },
{ "properties": "none", "allOf": [{ "properties": { "test.ok": {}, "test.nested": { "type": "boolean" } } }] },
],
"configurationDefaults": { "editor.fontSize": 20, "chat.useAgentsMdFile": false, "update.mode": "none" },
} });
let registry = config::registry::Registry::with_extensions(&[("Test.Configuration", &first)]);
assert!(registry.property("test.nested").is_some(), "an allOf node's properties register");
let expected = |messages: &[(crate::notifications::Severity, &str)]| messages.iter().map(|(severity, message)| (*severity, message.to_string())).collect::<Vec<_>>();
assert_eq!(super::extension_messages("test.configuration", ®istry), expected(&[
(Error, "'configuration.title' must be a string"),
(Warning, "Cannot register an empty property"),
(Warning, "Cannot register '[json]'. This matches property pattern '\\[.*\\]$' for describing language specific editor settings. Use 'configurationDefaults' contribution."),
(Warning, "Cannot register 'editor.fontSize'. This property is already registered."),
(Warning, "Cannot register 'test.both'. A setting must not declare both 'policy' and 'policyReference'."),
(Error, "configuration.properties property 'test.number' must be an object"),
(Error, "Extension 'Test.Configuration' CANNOT use 'agentsWindow' property on configuration 'test.agents' without enabling the 'agentsWindowConfiguration' API proposal."),
(Error, "Extension 'Test.Configuration' CANNOT use the 'agentHost' property on configuration 'test.agents'."),
(Error, "'configuration.properties' must be an object"),
(Error, "'configuration.allOf' is deprecated and should no longer be used. Instead, pass multiple configuration sections as an array to the 'configuration' contribution point."),
(Warning, "Cannot register 'test.ok'. This property is already registered."),
(Warning, "Cannot register configuration defaults for 'chat.useAgentsMdFile'. This setting does not allow contributing configuration defaults."),
(Warning, "Cannot register configuration defaults for 'update.mode'. Only defaults for machine-overridable, window, resource and language overridable scoped settings are supported."),
]));
}
#[test]
fn commands_list_their_keybindings_and_menus_with_the_implicit_command_palette() {
let manifest = json!({ "contributes": {
"commands": [{ "command": "b.run", "title": "Run" }, { "command": "a.hidden", "title": { "value": "Hidden", "original": "Hidden" } }],
"menus": { "commandPalette": [{ "command": "a.hidden", "when": "false" }], "editor/title": [{ "command": "b.run" }, { "command": "c.menuOnly" }] },
"keybindings": [{ "command": "b.run", "key": "ctrl+r", "mac": "cmd+r", "win": "alt+r", "linux": "shift+r" }, { "command": "d.keyOnly", "key": "" }]
} });
let (headers, rows) = rows(feature_data(&manifest, "commands"));
assert_eq!(headers, ["ID", "Title", "Keyboard Shortcuts", "Menu Contexts"]);
let ids: Vec<&Cell> = rows.iter().map(|row| &row[0]).collect();
assert_eq!(ids, [&markdown("`a.hidden`"), &markdown("`b.run`"), &markdown("`c.menuOnly`")]);
assert_eq!(rows[0][1], text("Hidden"));
assert_eq!(rows[0][3], markdown(""));
assert_eq!(rows[1][3], markdown("`commandPalette` `editor/title`"));
let Cell::Keybindings(keybindings) = &rows[1][2] else { panic!("the keybindings cell") };
assert_eq!(keybindings.len(), 1);
let platform_key = match crate::keybindings::OS {
crate::keybindings::Os::Macintosh => "⌘",
crate::keybindings::Os::Windows => "Alt",
crate::keybindings::Os::Linux => crate::keybindings::ui_modifier_labels(crate::keybindings::OS).0[1],
};
assert!(keybindings[0][0].contains(&platform_key.to_owned()), "the platform's key wins: {keybindings:?}");
assert_eq!(rows[2][1], text(""));
}
#[test]
fn settings_show_every_node_by_key_with_the_type_default() {
let manifest = json!({ "contributes": { "configuration": [
{ "properties": { "z.flag": { "type": "boolean", "description": "Flag." } } },
{ "properties": { "a.list": { "type": ["array", "null"], "markdownDescription": "A *list*." } }, "allOf": [{ "properties": { "m.size": { "type": "number", "default": 4 } } }] }
] } });
let (headers, rows) = rows(feature_data(&manifest, "configuration"));
assert_eq!(headers, ["ID", "Description", "Default"]);
assert_eq!(rows[0], [markdown("`a.list`"), markdown("A *list*."), markdown("\n```json\n[]\n```\n")]);
assert_eq!(rows[1], [markdown("`m.size`"), text(""), markdown("\n```json\n4\n```\n")]);
assert_eq!(rows[2], [markdown("`z.flag`"), text("Flag."), markdown("\n```json\nfalse\n```\n")]);
}
#[test]
fn a_language_override_lists_each_overridden_setting_with_its_languages() {
let manifest = json!({ "contributes": { "configurationDefaults": { "[go][mod]": { "editor.tabSize": 4 }, "files.eol": "\n" } } });
let (_, rows) = rows(feature_data(&manifest, "configurationDefaults"));
assert_eq!(rows[0], [markdown("go, mod"), markdown("`editor.tabSize`"), markdown("\n```json\n4\n```\n")]);
assert_eq!(rows[1], [text(""), markdown("`files.eol`"), markdown("\n```json\n\"\\n\"\n```\n")]);
}
#[test]
fn a_grammar_or_snippets_of_an_undeclared_language_adds_its_row() {
let manifest = json!({ "contributes": {
"languages": [{ "id": "toml", "aliases": ["TOML"], "extensions": [".toml", ".tml"] }, { "id": 3 }],
"grammars": [{ "language": "toml" }, { "scopeName": "source.include" }],
"snippets": [{ "language": "ini" }]
} });
let (_, rows) = rows(feature_data(&manifest, "languages"));
assert_eq!(rows[0], [text("ini"), text("ini"), markdown(""), text("\u{2014}"), text("\u{2714}\u{fe0e}")]);
assert_eq!(rows[1], [text("toml"), text("TOML"), markdown("`.toml` `.tml`"), text("\u{2714}\u{fe0e}"), text("\u{2014}")]);
}
#[test]
fn a_color_default_is_a_swatch_for_a_hex_and_code_for_a_color_id() {
let manifest = json!({ "contributes": { "colors": [{ "id": "my.background", "description": "Background.", "defaults": { "dark": "#ff0000", "light": "editor.background", "highContrast": "#00ff00" } }] } });
let (_, rows) = rows(feature_data(&manifest, "colors"));
let Cell::Color(dark) = rows[0][2] else { panic!("a swatch") };
assert_eq!((dark.r, dark.g, dark.b), (1., 0., 0.));
assert_eq!(rows[0][3], markdown("`editor.background`"));
}
#[test]
fn themes_and_activation_events_render_as_markdown() {
let manifest = json!({ "activationEvents": ["onStartupFinished"], "contributes": { "themes": [{ "label": "Night" }], "productIconThemes": [{ "label": "Icons" }] } });
assert_eq!(feature_data(&manifest, "themes"), FeatureData::Markdown("### Color Themes\n\n- Night\n### Product Icon Themes\n\n- Icons\n".into()));
assert_eq!(feature_data(&manifest, "activationEvents"), FeatureData::Markdown("- `onStartupFinished`\n".into()));
}
#[test]
fn views_list_every_location_by_id() {
let manifest = json!({ "contributes": { "views": { "explorer": [{ "id": "z.tree", "name": "Tree" }], "test": [{ "id": "a.view", "name": "View" }] } } });
let (headers, rows) = rows(feature_data(&manifest, "views"));
assert_eq!(headers, ["ID", "Name", "Where"]);
assert_eq!(rows, [[text("a.view"), text("View"), text("test")], [text("z.tree"), text("Tree"), text("explorer")]]);
}