Skip to content

Commit

Permalink
Add mcad tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DSchroer committed Mar 13, 2022
1 parent 3d988de commit a3bbe5e
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 29 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,18 @@ The project is an ES6 module. Simply import the module:
import OpenSCAD from "./openscad.js";
// OPTIONAL: add fonts to the FS
import { addFonts } from "./openscad.fonts.js";
// OPTIONAL: add MCAD liibrary to the FS
import { addMCAD } from "./openscad.mcad.js";

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

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

// OPTIONAL: add MCAD liibrary to the FS
addMCAD(instance);

// Write a file to the filesystem
instance.FS.writeFile("/input.scad", `cube(10);`);

Expand Down
48 changes: 47 additions & 1 deletion runtime/src/files.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,49 @@
export function fromHex(hex: string): Uint8Array {
import { FS } from "./openscad";

export function writeFile(
fs: FS,
filePath: string,
contents: string,
) {
ensureDirectoryExists(fs, filePath);
fs.writeFile(filePath, fromHex(contents));
}

export function writeFolder(
fs: FS,
base: string,
contents: Record<string, string>,
) {
for (const [file, data] of Object.entries(contents)) {
const fullPath = base + file;
ensureDirectoryExists(fs, fullPath);
fs.writeFile(fullPath, fromHex(data));
}
}

function fromHex(hex: string): Uint8Array {
if (hex.length == 0) {
return new Uint8Array(0);
}
return new Uint8Array(hex.match(/../g)!.map((h) => parseInt(h, 16)));
}

function ensureDirectoryExists(fs: FS, filePath: string) {
const dirIndex = filePath.lastIndexOf("/");
if (dirIndex != -1) {
const dirname = filePath.substring(0, dirIndex);
ensureDirectoryExists(fs, dirname);
if (dirname != "" && !exists(fs, dirname)) {
fs.mkdir(dirname);
}
}
}

function exists(fs: FS, path: string) {
try {
fs.stat(path);
return true;
} catch (e) {
return false;
}
}
7 changes: 6 additions & 1 deletion runtime/src/modules.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
declare module '../../res/*.conf' {
const data: string;
export default data;
}

declare module '../../res/*' {
const data: Record<string, string> | string;
const data: Record<string, string>;
export default data;
}
9 changes: 3 additions & 6 deletions runtime/src/openscad.fonts.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { OpenSCAD } from "./openscad";
import { fromHex } from "./files";
import { writeFile, writeFolder } from "./files";

import config from "../../res/fonts/fonts.conf";
import fonts from "../../res/liberation";

export function addFonts(openscad: OpenSCAD) {
openscad.FS.mkdir("/fonts");
openscad.FS.writeFile("/fonts/fonts.conf", fromHex(config as string));
for (const [file, data] of Object.entries(fonts as Record<string, string>)) {
openscad.FS.writeFile("/fonts/" + file, fromHex(data));
}
writeFile(openscad.FS, "/fonts/fonts.conf", config as string);
writeFolder(openscad.FS, "/fonts/", fonts as Record<string, string>);
}
9 changes: 2 additions & 7 deletions runtime/src/openscad.mcad.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { OpenSCAD } from "./openscad";
import { fromHex } from "./files";
import { writeFolder } from "./files";

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

export function addMCAD(openscad: OpenSCAD) {
openscad.FS.mkdir("/libraries");
openscad.FS.mkdir("/libraries/MCAD");

for (const [file, data] of Object.entries(mcad as Record<string, string>)) {
openscad.FS.writeFile("/libraries/MCAD/" + file, fromHex(data));
}
writeFolder(openscad.FS, "/libraries/MCAD/", mcad as Record<string, string>);
}
50 changes: 36 additions & 14 deletions tests/included.test.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,51 @@
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import OpenScad from "../build/openscad.js";
import { addFonts } from "../build/openscad.fonts.js";
import { join } from "https://deno.land/std/path/mod.ts";
import { loadTestFiles } from "./testing.ts";

for await (const entry of Deno.readDir(".")) {
if (!entry.isDirectory) {
continue;
}
import OpenScad, { OpenSCAD } from "../build/openscad.js";
import { addFonts } from "../build/openscad.fonts.js";
import { addMCAD } from "../build/openscad.mcad.js";

Deno.test({
name: entry.name,
fn: () => runTest(entry.name),
});
}
Deno.test("csg", async () => {
const instance = await OpenScad({ noInitialRun: true });
await runTest(instance, "./csg");
});

Deno.test("cube", async () => {
const instance = await OpenScad({ noInitialRun: true });
await runTest(instance, "./cube");
});

async function runTest(directory: string) {
Deno.test("cylinder", async () => {
const instance = await OpenScad({ noInitialRun: true });
await runTest(instance, "./cylinder");
});

Deno.test("lib", async () => {
const instance = await OpenScad({ noInitialRun: true });
await runTest(instance, "./lib");
});

Deno.test("mcad", async () => {
const instance = await OpenScad({ noInitialRun: true });
addMCAD(instance);
await runTest(instance, "./mcad");
});

Deno.test("text", async () => {
const instance = await OpenScad({ noInitialRun: true });
addFonts(instance);
await runTest(instance, "./text");
});

async function runTest(instance: OpenSCAD, directory: string) {
const __dirname = new URL('.', import.meta.url).pathname;

await loadTestFiles(instance, directory);
await loadTestFiles(instance, join(__dirname, directory));

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

const output = instance.FS.readFile("out.stl", { encoding: "binary" });
await Deno.writeFile(`${directory}/out.stl`, output);
await Deno.writeFile(join(__dirname, directory, "out.stl"), output);
}
3 changes: 3 additions & 0 deletions tests/mcad/test.scad
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
use <MCAD/involute_gears.scad>;

bevel_gear();

0 comments on commit a3bbe5e

Please sign in to comment.