Skip to content

Commit

Permalink
add ffipp tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vixalien committed Jan 21, 2024
1 parent 2f9ea9b commit f6c6a88
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.vscode/

deno.json
deno.jsonc
deno.lock
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"tasks": {
"test": "deno test --unstable --allow-ffi test/unit/base_utils/convert.ts"
"test": "deno test --unstable --allow-ffi --allow-read test/unit/base_utils/*"
}
}
87 changes: 87 additions & 0 deletions test/unit/base_utils/ffipp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import {
assert,
assertEquals,
} from "https://deno.land/[email protected]/assert/mod.ts";
import * as mod from "../../../src/base_utils/ffipp.js";

Deno.test("createType", () => {
const symbol = "test";
const size = 1;
const serilize = (arg_0: unknown) => arg_0;
const deserilize = (arg_0: unknown) => arg_0;

const result = mod.createType({
symbol,
size,
serilize,
deserilize,
});

assert(result, "result is null");
assertEquals(
typeof result,
"function",
"result is not a function",
);

const parameters = [1, 2, 3] as any;
const instance = result(parameters);

assert(instance, "instance is null");
assertEquals(typeof instance, "object", "instance is not an object");
assert(Object.hasOwn(instance, "result"), "instance has no result property");
assert(
Object.hasOwn(instance, "parameters"),
"instance has no parameters property",
);
assertEquals(
instance.parameters,
[parameters],
"instance.parameters is not the same as the original parameters",
);
assertEquals(
instance.result,
result,
"instance.result is not the same as the original result",
);
// @ts-ignore symbol is a private property
assertEquals(result.symbol, symbol, "result.symbol is not the same");
// @ts-ignore symbol is a private property
assertEquals(result.size, size, "result.size is not the same");
// @ts-ignore symbol is a private property
assertEquals(
// @ts-ignore symbol is a private property
result.serilize,
serilize,
"result.serilize is not the same",
);
assertEquals(
// @ts-ignore symbol is a private property
result.deserilize,
deserilize,
"result.deserilize is not the same",
);
});

// TODO: test openLib

Deno.test("libName", () => {
const name = "test";
const version = 1;
const result = mod.libName(name, version);

// TODO: fake check every OS
switch (Deno.build.os) {
case "darwin":
assertEquals(result, `lib${name}.${version}.dylib`);
break;
case "linux":
assertEquals(result, `lib${name}.so.${version}`);
break;
case "windows":
assertEquals(result, `lib${name}-${version}.dll`);
break;
default:
throw new Error("Unsupported OS");
}
});

0 comments on commit f6c6a88

Please sign in to comment.