-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy pathallThreeSuffix.test.ts
More file actions
98 lines (86 loc) · 3.7 KB
/
allThreeSuffix.test.ts
File metadata and controls
98 lines (86 loc) · 3.7 KB
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
// All three variants have non-empty suffix, i.e a suffix is required for any variant.
import { Java } from "../java";
import { getJava } from "../testHelpers";
import { describe, test, expect, beforeAll } from "vitest";
describe("allThreeSuffix", () => {
let java!: Java;
beforeAll(async () => {
java = await getJava({
syncSuffix: "Sync",
asyncSuffix: "Async",
promiseSuffix: "Promise",
});
});
test("api", () => {
const arrayList = java.newInstanceSync("java.util.ArrayList");
expect(arrayList).toBeDefined();
expect(java.instanceOf(arrayList, "java.util.ArrayList")).toBeTruthy();
expect(typeof arrayList.addSync !== "undefined", "Expected `addSync` to be present, but it is NOT.").toBeTruthy();
expect(typeof arrayList.addAsync !== "undefined", "Expected `addAsync` to be present, but it is NOT.").toBeTruthy();
expect(
typeof arrayList.addPromise !== "undefined",
"Expected `addPromise` to be present, but it is NOT."
).toBeTruthy();
expect(typeof arrayList.add === "undefined", "Expected `add` to NOT be present, but it is.").toBeTruthy();
});
test("importClass", () => {
// Note: java.import executes javascript code in src-node/nodeJavaBridge that makes sync calls to java classes.
const ArrayList = java.import("java.util.ArrayList");
expect(ArrayList).toBeTruthy();
const arrayList = new ArrayList();
expect(arrayList).toBeTruthy();
expect(arrayList.sizeSync()).toBe(0);
});
test("staticAPI", () => {
const String = java.import("java.lang.String");
expect(String).toBeTruthy();
const api = Object.keys(String).filter((key) => typeof String[key] === "function");
expect(api.includes("formatSync"), "Expected `formatSync` to be present, but it is NOT.").toBeTruthy();
expect(api.includes("formatAsync"), "Expected `formatAsync` to be present, but it is NOT.").toBeTruthy();
expect(api.includes("formatPromise"), "Expected `formatPromise` to be present, but it is NOT.").toBeTruthy();
expect(!api.includes("format"), "Expected `format` to NOT be present, but it is.").toBeTruthy();
expect(!api.includes("formatundefined"), "Expected `formatundefined` to NOT be present, but it is.").toBeTruthy();
});
test("syncCalls", () => {
const arrayList = java.newInstanceSync("java.util.ArrayList");
arrayList.addSync("hello");
arrayList.addSync("world");
expect(arrayList.sizeSync()).toBe(2);
});
test("staticSyncCalls", () => {
// Note: java.import executes javascript code in src-node/nodeJavaBridge that makes sync calls to java classes.
// Among other things, java.import creates Sync functions for static methods.
const String = java.import("java.lang.String");
expect(String.formatSync("%s--%s", "hello", "world")).toBe("hello--world");
});
test("asyncCalls", async () => {
const arrayList = java.newInstanceSync("java.util.ArrayList");
await new Promise<void>((resolve) => {
arrayList.addAsync("hello", (err: Error | undefined) => {
expect(err).toBeUndefined();
arrayList.addAsync("world", (err: Error | undefined) => {
expect(err).toBeUndefined();
arrayList.sizeAsync((err: Error | undefined, size: number) => {
expect(err).toBeUndefined();
expect(size).toBe(2);
resolve();
});
});
});
});
});
test("promiseCalls", async () => {
const arrayList = java.newInstanceSync("java.util.ArrayList");
await arrayList
.addPromise("hello")
.then(() => {
return arrayList.addPromise("world");
})
.then(() => {
return arrayList.sizePromise();
})
.then((size: number) => {
expect(size).toBe(2);
});
});
});