дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-11-30 06:12:33 +02:00
The Variables view showed the Java debugger's text under its type: args = {String[]} String[0]@9, map = {HashMap} HashMap@10 size=1, and a map entry as 0 = {HashMap$Node} HashMap$Node@31 "a":"b". A JVM object now reads {String[0]@9} [] and {HashMap@10} size = 1, a map entry as "a" -> "b" without its index, and a String without its type. Inline values and the value tooltip use the same text (args: [], map: size = 1). Values from other debuggers keep their text and type.
175 рядки
7.0 KiB
TypeScript
175 рядки
7.0 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { EditorSelection, EditorState } from "@codemirror/state";
|
|
import { selectionExpression } from "../editor/valueTooltip";
|
|
import { classifyValue, identifiersIn, inlineValue, shownValue, sortVariables } from "../values";
|
|
import { inlineValuesFor } from "../editor/execution";
|
|
import { frameLabel, groupFrames, isLibraryFrame } from "../frames";
|
|
import type { StackFrame } from "@/platform/ipc";
|
|
|
|
describe("value classification", () => {
|
|
it.each([
|
|
["42", "number"],
|
|
["-3.5e10", "number"],
|
|
["0x1F", "number"],
|
|
["297.00", "number"],
|
|
["10u32", "number"],
|
|
['"hello"', "string"],
|
|
["'x'", "string"],
|
|
["b'bytes'", "string"],
|
|
["null", "keyword"],
|
|
["None", "keyword"],
|
|
["true", "keyword"],
|
|
["undefined", "keyword"],
|
|
["{CreateOrderRequest@4127}", "object"],
|
|
["CreateOrderRequest@4127", "object"],
|
|
["[1, 2, 3]", "object"],
|
|
["Vec(3)", "object"],
|
|
["<function main at 0x10>", "object"],
|
|
["some text", "plain"],
|
|
["", "plain"],
|
|
])("%s → %s", (value, kind) => {
|
|
expect(classifyValue(value)).toBe(kind);
|
|
});
|
|
|
|
it("uses the type name as a fallback for strings", () => {
|
|
expect(classifyValue("hello", "str")).toBe("string");
|
|
expect(classifyValue("hello", "Order")).toBe("plain");
|
|
});
|
|
|
|
it("flattens and truncates inline values", () => {
|
|
expect(inlineValue("a\n b")).toBe("a b");
|
|
expect(inlineValue("x".repeat(80), 10)).toBe("xxxxxxxxx…");
|
|
});
|
|
|
|
it("finds identifiers outside string literals", () => {
|
|
expect(identifiersIn('log.info("total " + total)')).toEqual(["log", "info", "total"]);
|
|
});
|
|
|
|
it("formats inline values for known locals on a line", () => {
|
|
const values = {
|
|
req: "CreateOrderRequest@4127",
|
|
total: "3",
|
|
unused: "1",
|
|
};
|
|
const locals = (v: Record<string, string>) =>
|
|
Object.fromEntries(
|
|
Object.entries(v).map(([name, value]) => [
|
|
name,
|
|
{ name, value, typeName: null, variablesReference: 0, evaluateName: name },
|
|
]),
|
|
);
|
|
expect(
|
|
inlineValuesFor("fun create(req: CreateOrderRequest): OrderDto {", locals(values))
|
|
?.map((p) => p.text)
|
|
.join(", "),
|
|
).toBe("req: CreateOrderRequest@4127");
|
|
expect(inlineValuesFor("return 1", locals(values))).toBeNull();
|
|
});
|
|
|
|
it.each(Object.getOwnPropertyNames(Object.prototype))("only displays an own local named %s", (name) => {
|
|
const variable = (value: string) => ({ name, value, typeName: null, variablesReference: 0, evaluateName: name });
|
|
expect(inlineValuesFor(name, Object.fromEntries([["count", variable("40")]]))).toBeNull();
|
|
expect(inlineValuesFor(name, Object.fromEntries([[name, variable("known")]]))?.[0]?.text).toBe(`${name}: known`);
|
|
expect(inlineValuesFor(name, Object.create(Object.fromEntries([[name, variable("inherited")]])))).toBeNull();
|
|
});
|
|
});
|
|
|
|
const frame = (id: number, path: string | null, hint: string | null = null, line = 9): StackFrame => ({
|
|
id,
|
|
name: `f${id}`,
|
|
path,
|
|
line,
|
|
column: 0,
|
|
presentationHint: hint,
|
|
});
|
|
|
|
describe("frames", () => {
|
|
const root = "/proj";
|
|
it("detects library frames", () => {
|
|
expect(isLibraryFrame(frame(1, "/proj/src/a.py"), root)).toBe(false);
|
|
expect(isLibraryFrame(frame(1, "/proj/node_modules/x/i.js"), root)).toBe(true);
|
|
expect(isLibraryFrame(frame(1, "/usr/lib/python3/x.py"), root)).toBe(true);
|
|
expect(isLibraryFrame(frame(1, null), root)).toBe(true);
|
|
expect(isLibraryFrame(frame(1, "/proj/a.py", "subtle"), root)).toBe(true);
|
|
expect(isLibraryFrame(frame(1, "/elsewhere/a.py"), root)).toBe(true);
|
|
});
|
|
|
|
it("collapses runs of library frames but keeps the top and selected frames", () => {
|
|
const frames = [
|
|
frame(1, "/proj/a.py"),
|
|
frame(2, null),
|
|
frame(3, null),
|
|
frame(4, null),
|
|
frame(5, "/proj/b.py"),
|
|
frame(6, null),
|
|
];
|
|
const items = groupFrames(frames, root, 1, false);
|
|
expect(items.map((i) => (i.kind === "hidden" ? `hidden${i.count}` : i.frame.id))).toEqual([1, "hidden3", 5, 6]);
|
|
const selected = groupFrames(frames, root, 3, false);
|
|
expect(selected.map((i) => (i.kind === "hidden" ? `hidden${i.count}` : i.frame.id))).toEqual([1, 2, 3, 4, 5, 6]);
|
|
expect(groupFrames(frames, root, 1, true)).toHaveLength(6);
|
|
});
|
|
|
|
it("labels frames with 1-based lines", () => {
|
|
expect(frameLabel(frame(1, "/proj/a.py", null, 21))).toBe("f1:22");
|
|
expect(frameLabel(frame(1, null))).toBe("f1");
|
|
});
|
|
});
|
|
|
|
describe("sortVariables", () => {
|
|
const names = (vs: { name: string }[]) => vs.map((v) => v.name);
|
|
it("sorts by name, case-insensitively and with numbers compared numerically", () => {
|
|
expect(names(sortVariables([{ name: "b" }, { name: "item10" }, { name: "A" }, { name: "item2" }]))).toEqual([
|
|
"A",
|
|
"b",
|
|
"item2",
|
|
"item10",
|
|
]);
|
|
});
|
|
it("keeps array elements in index order", () => {
|
|
const items = [{ name: "[0]" }, { name: "[1]" }, { name: "[10]" }, { name: "[2]" }];
|
|
expect(names(sortVariables(items))).toEqual(["[0]", "[1]", "[10]", "[2]"]);
|
|
});
|
|
});
|
|
|
|
describe("selectionExpression", () => {
|
|
const at = (doc: string, from: number, to: number) =>
|
|
selectionExpression(EditorState.create({ doc, selection: EditorSelection.single(from, to) }));
|
|
it("takes a one-line selection", () => {
|
|
expect(at("total += item.price", 9, 19)).toEqual({ from: 9, text: "item.price" });
|
|
});
|
|
it("ignores empty, blank, multi-line and multiple selections", () => {
|
|
expect(at("a + b", 2, 2)).toBeNull();
|
|
expect(at("a b", 1, 4)).toBeNull();
|
|
expect(at("a\nb", 0, 3)).toBeNull();
|
|
const multi = EditorState.create({
|
|
doc: "a + b",
|
|
selection: EditorSelection.create([EditorSelection.range(0, 1), EditorSelection.range(4, 5)]),
|
|
extensions: EditorState.allowMultipleSelections.of(true),
|
|
});
|
|
expect(selectionExpression(multi)).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("shown values", () => {
|
|
it.each([
|
|
["String[0]@9", "String[]", { label: "{String[0]@9}", text: "[]" }],
|
|
["int[3]@12", "int[]", { label: "{int[3]@12}", text: "" }],
|
|
["HashMap@10 size=1", "HashMap", { label: "{HashMap@10}", text: "size = 1" }],
|
|
["ArrayList@15 size=0", "java.util.ArrayList", { label: "{ArrayList@15}", text: "size = 0" }],
|
|
["Db@21", "Db", { label: "{Db@21}", text: "" }],
|
|
["com.example.Library@1", "Library", { label: "{Library@1}", text: "" }],
|
|
['HashMap$Node@31 "a":"b"', "HashMap$Node", { label: null, text: '"a" -> "b"', entry: true }],
|
|
['HashMap$Node@32 "a:b":"c"', "HashMap$Node", { label: null, text: '"a:b" -> "c"', entry: true }],
|
|
['TreeMap$Entry@33 1:"x"', "TreeMap$Entry", { label: null, text: '1 -> "x"', entry: true }],
|
|
['"hello"', "String", { label: null, text: '"hello"' }],
|
|
["42", "int", { label: "{int}", text: "42" }],
|
|
// Other debuggers' values keep their text and type.
|
|
["Foo@1", "Bar", { label: "{Bar}", text: "Foo@1" }],
|
|
["{'a': 1}", "dict", { label: "{dict}", text: "{'a': 1}" }],
|
|
["0x1000", null, { label: null, text: "0x1000" }],
|
|
])("%s {%s}", (value, type, shown) => {
|
|
expect(shownValue(value, type)).toEqual(shown);
|
|
});
|
|
});
|