Skip to content

Commit

Permalink
Redo tests to typescript
Browse files Browse the repository at this point in the history
commit-id:08bc84d8
  • Loading branch information
Draggu committed Dec 13, 2024
1 parent 813ee0a commit a050461
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 25 deletions.
2 changes: 1 addition & 1 deletion bin/gen-cairo-snippets.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const snippetsPath = path.join(
"..",
"..",
"test-support",
"cairo-snippets.gen.mjs",
"cairo-snippets.gen.ts",
);
await fs.writeFile(snippetsPath, snippetsFormatted, "utf8");

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@
"lint": "npm run lint-fmt && npm run lint-eslint",
"lint-eslint": "eslint .",
"lint-fmt": "prettier --check .",
"test": "node --test **/test/**/*.{cjs,mjs,js}",
"test": "node --test out/test/**/*.js",
"ui-test": "extest setup-and-run './out/ui-test/*.js' --code_version max --code_settings ui-test/settings.json --extensions_dir .test-extensions"
},
"engines": {
Expand Down
File renamed without changes.
21 changes: 7 additions & 14 deletions test-support/grammar.mjs → test-support/grammar.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
import * as fs from "node:fs/promises";
import { createRequire } from "node:module";
import * as path from "node:path";
import { fileURLToPath } from "node:url";
import oniguruma from "vscode-oniguruma";
import vsctm from "vscode-textmate";

const require = createRequire(import.meta.url);
import * as oniguruma from "vscode-oniguruma";
import * as vsctm from "vscode-textmate";

const wasmPath = require.resolve("vscode-oniguruma").replace(/main\.js$/, "onig.wasm");
const vscodeOnigurumaLib = fs
.readFile(wasmPath)
.then((wasmBin) => oniguruma.loadWASM(wasmBin.buffer))
.then(() => ({
createOnigScanner(patterns) {
createOnigScanner(patterns: string[]) {
return new oniguruma.OnigScanner(patterns);
},
createOnigString(s) {
createOnigString(s: string) {
return new oniguruma.OnigString(s);
},
}));

const cairoGrammar = fs
.readFile(
path.join(fileURLToPath(import.meta.url), "..", "..", "syntaxes", "cairo.tmLanguage.json"),
"utf-8",
)
.readFile(path.join(__filename, "..", "..", "..", "syntaxes", "cairo.tmLanguage.json"), "utf-8")
.then((grammar) => JSON.parse(grammar));

// Create a registry that can create a grammar from a scope name.
Expand All @@ -41,7 +34,7 @@ const registry = new vsctm.Registry({
/**
* Highlights Cairo code using the Cairo TextMate grammar.
*/
export async function highlightCairoCode(code) {
export async function highlightCairoCode(code: string) {
const grammar = await registry.loadGrammar("source.cairo");
if (!grammar) {
throw new Error("Could not load scope: source.cairo");
Expand Down Expand Up @@ -70,7 +63,7 @@ export async function highlightCairoCode(code) {
return output.join("\n");
}

function escapeWhitespace(str) {
function escapeWhitespace(str: string) {
return str.replace(/\s/g, (char) => {
switch (char) {
case "\n":
Expand Down
18 changes: 13 additions & 5 deletions test-support/snapshots.mjs → test-support/snapshots.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
import { createHash } from "node:crypto";
import * as fs from "node:fs/promises";
import * as path from "node:path";
import { fileURLToPath } from "node:url";

declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace NodeJS {
interface ProcessEnv {
CAIRO_FIX_TESTS: string;
}
}
}

const isFixMode = process.env.CAIRO_FIX_TESTS === "1";

export const snap = isFixMode ? fix : read;

async function read(key) {
async function read(key: string) {
const snapshotPath = getSnapshotPath(key);
return await fs.readFile(snapshotPath, "utf8");
}

async function fix(key, text) {
async function fix(key: string, text: string) {
const snapshotPath = getSnapshotPath(key);
await fs.mkdir(path.dirname(snapshotPath), { recursive: true });
await fs.writeFile(snapshotPath, text, "utf8");
return text;
}

function getSnapshotPath(key) {
function getSnapshotPath(key: string) {
const hash = createHash("sha256").update(key).digest("hex").slice(0, 10);
return path.join(fileURLToPath(import.meta.url), "..", "..", "test", "snapshots", `${hash}.txt`);
return path.join(__filename, "..", "..", "..", "test", "snapshots", `${hash}.txt`);
}
6 changes: 3 additions & 3 deletions test/cairo.tmLanguage.mjs → test/cairo.tmLanguage.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import assert from "assert/strict";
import test from "node:test";
import snippets from "../test-support/cairo-snippets.gen.mjs";
import { highlightCairoCode } from "../test-support/grammar.mjs";
import { snap } from "../test-support/snapshots.mjs";
import snippets from "../test-support/cairo-snippets.gen";
import { highlightCairoCode } from "../test-support/grammar";
import { snap } from "../test-support/snapshots";

test("highlighting cairo-lang-parser tests", async (t) => {
for (const code of snippets) {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"checkJs": false
},
"exclude": ["node_modules", ".test-extensions"],
"include": ["src", "test-support", "ui-test"]
"include": ["src", "test", "test-support", "ui-test"]
}

0 comments on commit a050461

Please sign in to comment.