Skip to content

Commit a3bbe5e

Browse files
committed
Add mcad tests
1 parent 3d988de commit a3bbe5e

File tree

7 files changed

+102
-29
lines changed

7 files changed

+102
-29
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,18 @@ The project is an ES6 module. Simply import the module:
6161
import OpenSCAD from "./openscad.js";
6262
// OPTIONAL: add fonts to the FS
6363
import { addFonts } from "./openscad.fonts.js";
64+
// OPTIONAL: add MCAD liibrary to the FS
65+
import { addMCAD } from "./openscad.mcad.js";
6466

6567
// Instantiate the application
6668
const instance = await OpenSCAD({ noInitialRun: true });
6769

6870
// OPTIONAL: add fonts to the FS
6971
addFonts(instance);
7072

73+
// OPTIONAL: add MCAD liibrary to the FS
74+
addMCAD(instance);
75+
7176
// Write a file to the filesystem
7277
instance.FS.writeFile("/input.scad", `cube(10);`);
7378

runtime/src/files.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,49 @@
1-
export function fromHex(hex: string): Uint8Array {
1+
import { FS } from "./openscad";
2+
3+
export function writeFile(
4+
fs: FS,
5+
filePath: string,
6+
contents: string,
7+
) {
8+
ensureDirectoryExists(fs, filePath);
9+
fs.writeFile(filePath, fromHex(contents));
10+
}
11+
12+
export function writeFolder(
13+
fs: FS,
14+
base: string,
15+
contents: Record<string, string>,
16+
) {
17+
for (const [file, data] of Object.entries(contents)) {
18+
const fullPath = base + file;
19+
ensureDirectoryExists(fs, fullPath);
20+
fs.writeFile(fullPath, fromHex(data));
21+
}
22+
}
23+
24+
function fromHex(hex: string): Uint8Array {
25+
if (hex.length == 0) {
26+
return new Uint8Array(0);
27+
}
228
return new Uint8Array(hex.match(/../g)!.map((h) => parseInt(h, 16)));
329
}
30+
31+
function ensureDirectoryExists(fs: FS, filePath: string) {
32+
const dirIndex = filePath.lastIndexOf("/");
33+
if (dirIndex != -1) {
34+
const dirname = filePath.substring(0, dirIndex);
35+
ensureDirectoryExists(fs, dirname);
36+
if (dirname != "" && !exists(fs, dirname)) {
37+
fs.mkdir(dirname);
38+
}
39+
}
40+
}
41+
42+
function exists(fs: FS, path: string) {
43+
try {
44+
fs.stat(path);
45+
return true;
46+
} catch (e) {
47+
return false;
48+
}
49+
}

runtime/src/modules.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
declare module '../../res/*.conf' {
2+
const data: string;
3+
export default data;
4+
}
5+
16
declare module '../../res/*' {
2-
const data: Record<string, string> | string;
7+
const data: Record<string, string>;
38
export default data;
49
}

runtime/src/openscad.fonts.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import { OpenSCAD } from "./openscad";
2-
import { fromHex } from "./files";
2+
import { writeFile, writeFolder } from "./files";
33

44
import config from "../../res/fonts/fonts.conf";
55
import fonts from "../../res/liberation";
66

77
export function addFonts(openscad: OpenSCAD) {
8-
openscad.FS.mkdir("/fonts");
9-
openscad.FS.writeFile("/fonts/fonts.conf", fromHex(config as string));
10-
for (const [file, data] of Object.entries(fonts as Record<string, string>)) {
11-
openscad.FS.writeFile("/fonts/" + file, fromHex(data));
12-
}
8+
writeFile(openscad.FS, "/fonts/fonts.conf", config as string);
9+
writeFolder(openscad.FS, "/fonts/", fonts as Record<string, string>);
1310
}

runtime/src/openscad.mcad.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
import { OpenSCAD } from "./openscad";
2-
import { fromHex } from "./files";
2+
import { writeFolder } from "./files";
33

44
import mcad from "../../res/MCAD";
55

66
export function addMCAD(openscad: OpenSCAD) {
7-
openscad.FS.mkdir("/libraries");
8-
openscad.FS.mkdir("/libraries/MCAD");
9-
10-
for (const [file, data] of Object.entries(mcad as Record<string, string>)) {
11-
openscad.FS.writeFile("/libraries/MCAD/" + file, fromHex(data));
12-
}
7+
writeFolder(openscad.FS, "/libraries/MCAD/", mcad as Record<string, string>);
138
}

tests/included.test.ts

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,51 @@
11
import { assertEquals } from "https://deno.land/std@0.125.0/testing/asserts.ts";
2-
import OpenScad from "../build/openscad.js";
3-
import { addFonts } from "../build/openscad.fonts.js";
2+
import { join } from "https://deno.land/std/path/mod.ts";
43
import { loadTestFiles } from "./testing.ts";
54

6-
for await (const entry of Deno.readDir(".")) {
7-
if (!entry.isDirectory) {
8-
continue;
9-
}
5+
import OpenScad, { OpenSCAD } from "../build/openscad.js";
6+
import { addFonts } from "../build/openscad.fonts.js";
7+
import { addMCAD } from "../build/openscad.mcad.js";
108

11-
Deno.test({
12-
name: entry.name,
13-
fn: () => runTest(entry.name),
14-
});
15-
}
9+
Deno.test("csg", async () => {
10+
const instance = await OpenScad({ noInitialRun: true });
11+
await runTest(instance, "./csg");
12+
});
13+
14+
Deno.test("cube", async () => {
15+
const instance = await OpenScad({ noInitialRun: true });
16+
await runTest(instance, "./cube");
17+
});
1618

17-
async function runTest(directory: string) {
19+
Deno.test("cylinder", async () => {
20+
const instance = await OpenScad({ noInitialRun: true });
21+
await runTest(instance, "./cylinder");
22+
});
23+
24+
Deno.test("lib", async () => {
25+
const instance = await OpenScad({ noInitialRun: true });
26+
await runTest(instance, "./lib");
27+
});
1828

29+
Deno.test("mcad", async () => {
30+
const instance = await OpenScad({ noInitialRun: true });
31+
addMCAD(instance);
32+
await runTest(instance, "./mcad");
33+
});
34+
35+
Deno.test("text", async () => {
1936
const instance = await OpenScad({ noInitialRun: true });
2037
addFonts(instance);
38+
await runTest(instance, "./text");
39+
});
40+
41+
async function runTest(instance: OpenSCAD, directory: string) {
42+
const __dirname = new URL('.', import.meta.url).pathname;
2143

22-
await loadTestFiles(instance, directory);
44+
await loadTestFiles(instance, join(__dirname, directory));
2345

2446
const code = instance.callMain([`/test.scad`, "-o", "out.stl"]);
2547
assertEquals(0, code);
2648

2749
const output = instance.FS.readFile("out.stl", { encoding: "binary" });
28-
await Deno.writeFile(`${directory}/out.stl`, output);
50+
await Deno.writeFile(join(__dirname, directory, "out.stl"), output);
2951
}

tests/mcad/test.scad

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
use <MCAD/involute_gears.scad>;
2+
3+
bevel_gear();

0 commit comments

Comments
 (0)