Spaces:
Paused

File size: 7,228 Bytes
4e7644b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
 * Tests for the RobotFramework Monaco language module:
 *   • registerRobotFramework wiring + double-registration guard
 *   • Monarch grammar smoke tests via a minimal rule-matching harness
 */

import { describe, expect, it, vi } from "vitest";
import {
  ROBOT_LANGUAGE_ID,
  conf,
  language,
  registerRobotFramework,
} from "./robotframework";

// ---------------------------------------------------------------------------
// Mock Monaco instance
// ---------------------------------------------------------------------------

function createMonacoMock(existingIds: string[] = []) {
  return {
    languages: {
      getLanguages: vi.fn(() => existingIds.map((id) => ({ id }))),
      register: vi.fn(),
      setLanguageConfiguration: vi.fn(),
      setMonarchTokensProvider: vi.fn(),
    },
  };
}

describe("registerRobotFramework", () => {
  it("registers language with .robot and .resource extensions", () => {
    const monaco = createMonacoMock();
    registerRobotFramework(monaco as never);

    expect(monaco.languages.register).toHaveBeenCalledWith(
      expect.objectContaining({
        id: ROBOT_LANGUAGE_ID,
        extensions: [".robot", ".resource"],
      }),
    );
    expect(monaco.languages.setLanguageConfiguration).toHaveBeenCalledWith(
      ROBOT_LANGUAGE_ID,
      conf,
    );
    expect(monaco.languages.setMonarchTokensProvider).toHaveBeenCalledWith(
      ROBOT_LANGUAGE_ID,
      language,
    );
  });

  it("skips registration when language already exists", () => {
    const monaco = createMonacoMock([ROBOT_LANGUAGE_ID]);
    registerRobotFramework(monaco as never);

    expect(monaco.languages.register).not.toHaveBeenCalled();
    expect(monaco.languages.setMonarchTokensProvider).not.toHaveBeenCalled();
  });
});

// ---------------------------------------------------------------------------
// Grammar smoke tests
//
// Minimal emulation of Monarch's first-match-wins scanning, including the
// state transitions used by the variable rules.
// ---------------------------------------------------------------------------

type MonarchAction = string | { token: string; next?: string };
type MonarchRule = [RegExp, MonarchAction];

const tokenizer = language.tokenizer as Record<string, MonarchRule[]>;

function tokenOf(action: MonarchAction): string {
  return typeof action === "string" ? action : action.token;
}

function tokenize(line: string): { token: string; text: string }[] {
  const out: { token: string; text: string }[] = [];
  const stateStack = ["root"];
  let pos = 0;
  while (pos < line.length) {
    let matched = false;
    const state = stateStack[stateStack.length - 1];
    for (const [pattern, action] of tokenizer[state]) {
      const re = new RegExp(pattern.source, "iy");
      re.lastIndex = pos;
      const m = re.exec(line);
      if (m && m[0].length > 0) {
        out.push({ token: tokenOf(action), text: m[0] });
        pos += m[0].length;
        if (typeof action !== "string" && action.next) {
          if (action.next === "@push") {
            stateStack.push(state);
          } else if (action.next === "@pop") {
            stateStack.pop();
          } else {
            stateStack.push(action.next.replace(/^@/, ""));
          }
        }
        matched = true;
        break;
      }
    }
    if (!matched) {
      out.push({ token: "", text: line[pos] });
      pos += 1;
    }
  }
  return out;
}

function tokensIn(line: string): string[] {
  return tokenize(line).map((t) => t.token);
}

describe("robotframework Monarch grammar", () => {
  it("highlights section headers case-insensitively", () => {
    expect(tokensIn("*** Test Cases ***")[0]).toBe("keyword.section");
    expect(tokensIn("*** settings ***")[0]).toBe("keyword.section");
    expect(tokensIn("*** Keywords ***")[0]).toBe("keyword.section");
    expect(tokensIn("*** Variables ***")[0]).toBe("keyword.section");
  });

  it("highlights comments", () => {
    expect(tokensIn("# top-level comment")[0]).toBe("comment");
    expect(tokensIn("    Log    hi    # trailing")).toContain("comment");
  });

  it("only treats hashes at cell boundaries as comments", () => {
    expect(tokensIn("    Log    https://example.org/#section")).not.toContain(
      "comment",
    );
    expect(tokensIn("    Log    value#literal")).not.toContain("comment");
    expect(tokensIn("    Log    value\t# trailing")).toContain("comment");
  });

  it("does not treat escaped hash as comment", () => {
    const tokens = tokenize("    Log    \\# not a comment");
    const escape = tokens.find((t) => t.text === "\\#");
    expect(escape?.token).toBe("string.escape");
  });

  it("highlights scalar / list / dict / env variables", () => {
    expect(tokensIn("${VAR}    value")).toContain("variable");
    expect(tokensIn("@{LIST}    a    b")).toContain("variable");
    expect(tokensIn("&{DICT}    k=v")).toContain("variable");
    expect(tokensIn("    Log    %{HOME}")).toContain("variable");
  });

  it("follows variable states and supports nested variables", () => {
    const tokens = tokenize("${outer_${inner}}    tail");
    const variableText = tokens
      .filter((token) => token.token === "variable")
      .map((token) => token.text)
      .join("");

    expect(variableText).toBe("${outer_${inner}}");
    expect(tokens.find((token) => token.text === "tail")?.token).toBe("");
  });

  it("highlights bracket settings like [Tags] and [Arguments]", () => {
    const tags = tokenize("    [Tags]    smoke");
    expect(tags.find((t) => t.text === "[Tags]")?.token).toBe("tag");
    const args = tokenize("    [Arguments]    ${a}");
    expect(args.find((t) => t.text === "[Arguments]")?.token).toBe("tag");
  });

  it("highlights control-flow structures and FOR separators", () => {
    const forLine = tokenize("    FOR    ${i}    IN RANGE    10");
    const controls = forLine.filter((t) => t.token === "keyword.control");
    expect(controls.map((t) => t.text.trim())).toEqual(["FOR", "IN RANGE"]);
    expect(forLine.find((t) => t.text === "10")?.token).toBe("number");
    expect(tokensIn("    END")).toContain("keyword.control");
  });

  it("accepts a single tab as a cell separator", () => {
    const forLine = tokenize("\tFOR\t${i}\tIN RANGE\t10");
    const controls = forLine.filter((t) => t.token === "keyword.control");

    expect(controls.map((t) => t.text.trim())).toEqual(["FOR", "IN RANGE"]);
    expect(tokensIn("Library\tCollections")[0]).toBe("keyword");
  });

  it("highlights settings-section directives at column 0", () => {
    expect(tokensIn("Library    Collections")[0]).toBe("keyword");
    expect(tokensIn("Suite Setup    Open Browser")[0]).toBe("keyword");
    expect(tokensIn("Resource    common.resource")[0]).toBe("keyword");
  });

  it("highlights test case / keyword names at column 0", () => {
    expect(tokensIn("My First Test")[0]).toBe("type");
    // A name merely starting with a directive word is still a name
    expect(tokensIn("Library helpers should load")[0]).toBe("type");
  });

  it("leaves indented keyword calls as plain text", () => {
    const tokens = tokenize("    Open Browser    https://example.org");
    expect(tokens.find((t) => t.text.includes("Open"))?.token).toBe("");
  });
});