close
Skip to content

Commit 22e7456

Browse files
authored
[Implement] isBuffer<T>(value: T) (#8)
* [Implement] isBuffer<T>(value: T) * [Fix] Null expectation, remove diff * [Cleanup] remove null check * [Cleanup] Update assemblyscript, fix null assertions, wait for as-pect bug fix in 2.2.0 * [Chore] update package versions * [Fix] update pass check
1 parent 05f31ee commit 22e7456

6 files changed

Lines changed: 46 additions & 62 deletions

File tree

‎assembly/buffer/index.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ export class Buffer extends Uint8Array {
2323
return result;
2424
}
2525

26+
public static isBuffer<T>(value: T): bool {
27+
return value instanceof Buffer;
28+
}
29+
2630
readUInt8(offset: i32 = 0): u8 {
2731
if(<u32>offset >= this.dataLength) throw new RangeError(E_INDEXOUTOFRANGE);
2832
return load<u8>(this.dataStart + usize(offset));
@@ -39,7 +43,7 @@ export class Buffer extends Uint8Array {
3943
store<i8>(this.dataStart + offset, value);
4044
return offset + 1;
4145
}
42-
46+
4347
readInt8(offset: i32 = 0): i8 {
4448
if(<u32>offset >= this.dataLength) throw new RangeError(E_INDEXOUTOFRANGE);
4549
return load<i8>(this.dataStart + usize(offset));

‎assembly/node.d.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ declare class Buffer extends Uint8Array {
33
static alloc(size: i32): Buffer;
44
/** This method allocates a new Buffer of indicated size. This is unsafe because the data is not zeroed. */
55
static allocUnsafe(size: i32): Buffer;
6+
/** This method asserts a value is a Buffer object via `value instanceof Buffer`. */
7+
static isBuffer<T>(value: T): bool;
68
/** Reads an unsigned integer at the designated offset. */
79
readUInt8(offset?: i32): u8;
810
/** Writes an inputted u8 value to the buffer, at the desired offset. */

‎package-lock.json‎

Lines changed: 9 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
"url": "https://github.com/AssemblyScript/node/issues"
1313
},
1414
"devDependencies": {
15-
"@as-pect/core": "^2.2.0",
16-
"assemblyscript": "github:assemblyscript/assemblyscript#7c775d1bccbe08fec5d820b9d53ae44ff6bd1e49",
17-
"diff": "^4.0.1",
15+
"@as-pect/core": "^2.3.1",
16+
"assemblyscript": "github:assemblyscript/assemblyscript",
1817
"glob": "^7.1.4",
1918
"wasi": "github:devsnek/node-wasi"
2019
},

‎tests/buffer.spec.ts‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe("buffer", () => {
2727
expect<Buffer>(Buffer.alloc(10)).toHaveLength(10);
2828
let buff = Buffer.alloc(100);
2929
for (let i = 0; i < buff.length; i++) expect<u8>(buff[i]).toBe(0);
30-
expect<ArrayBuffer>(buff.buffer).not.toBeNull();
30+
expect<ArrayBuffer | null>(buff.buffer).not.toBeNull();
3131
expect<u32>(buff.byteLength).toBe(100);
3232
// TODO: expectFn(() => { Buffer.alloc(-1); }).toThrow();
3333
// TODO: expectFn(() => { Buffer.alloc(BLOCK_MAXSIZE + 1); }).toThrow();
@@ -37,12 +37,27 @@ describe("buffer", () => {
3737
expect<Buffer>(Buffer.allocUnsafe(10)).toBeTruthy();
3838
expect<Buffer>(Buffer.allocUnsafe(10)).toHaveLength(10);
3939
let buff = Buffer.allocUnsafe(100);
40-
expect<ArrayBuffer>(buff.buffer).not.toBeNull();
40+
expect<ArrayBuffer | null>(buff.buffer).not.toBeNull();
4141
expect<u32>(buff.byteLength).toBe(100);
4242
// TODO: expectFn(() => { Buffer.allocUnsafe(-1); }).toThrow();
4343
// TODO: expectFn(() => { Buffer.allocUnsafe(BLOCK_MAXSIZE + 1); }).toThrow();
4444
});
4545

46+
test("#isBuffer", () => {
47+
let a = "";
48+
let b = new Uint8Array(0);
49+
let c = 0;
50+
let d = 1.1;
51+
let e = new Buffer(0);
52+
expect<bool>(Buffer.isBuffer<string>(a)).toBeFalsy();
53+
expect<bool>(Buffer.isBuffer<Uint8Array>(b)).toBeFalsy();
54+
expect<bool>(Buffer.isBuffer<i32>(c)).toBeFalsy();
55+
expect<bool>(Buffer.isBuffer<f64>(d)).toBeFalsy();
56+
expect<bool>(Buffer.isBuffer<Buffer>(e)).toBeTruthy();
57+
// null checks are done by the compiler explicitly at runtime
58+
expect<bool>(Buffer.isBuffer<Buffer | null>(null)).toBeFalsy();
59+
});
60+
4661
test("#readUInt8", () => {
4762
let buff = new Buffer(10);
4863
buff[0] = -2;
@@ -53,10 +68,10 @@ describe("buffer", () => {
5368
// Testing offset
5469
expect<u8>(buff.readUInt8(9)).toBe(47);
5570
// TODO:
56-
// expectFn(() => {
71+
// expectFn(() => {
5772
// let newBuff = new Buffer(1);
5873
// newBuff.readUInt8(5);
59-
// }).toThrow();
74+
// }).toThrow();
6075
});
6176

6277
test("#writeUInt8", () => {
@@ -65,7 +80,7 @@ describe("buffer", () => {
6580
expect<i32>(buff.writeUInt8(252,4)).toBe(5);
6681
expect<u8>(buff[0]).toBe(4);
6782
expect<u8>(buff[4]).toBe(252);
68-
});
83+
});
6984

7085
test("#writeInt8", () => {
7186
let buff = new Buffer(5);
@@ -84,7 +99,7 @@ describe("buffer", () => {
8499
// Testing offset, and casting between u8 and i8.
85100
expect<i8>(buff.readInt8(9)).toBe(-1);
86101
// TODO:
87-
// expectFn(() => {
102+
// expectFn(() => {
88103
// let newBuff = new Buffer(1);
89104
// newBuff.readInt8(5);
90105
// }).toThrow();

‎tests/node.js‎

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { TestContext, EmptyReporter } = require("@as-pect/core");
1+
const { TestContext, VerboseReporter } = require("@as-pect/core");
22
const { instantiateBuffer } = require("assemblyscript/lib/loader");
33
const glob = require("glob");
44
const { main } = require("assemblyscript/cli/asc");
@@ -7,7 +7,7 @@ const path = require("path");
77
const fs = require("fs");
88
const Wasi = require("wasi");
99
const wasi = new Wasi({});
10-
const diff = require("diff");
10+
let pass = true;
1111

1212
const options = parse(process.argv.slice(2), {
1313
"help": {
@@ -27,41 +27,7 @@ if (options.unknown.length > 1) {
2727
process.exit(1);
2828
}
2929

30-
class Reporter extends EmptyReporter {
31-
onGroupFinish(group) {
32-
if (group.name) {
33-
if (group.pass) process.stdout.write("Group : " + group.name + " -> ✔ PASS");
34-
else process.stdout.write("Group : " + group.name + " -> ❌ FAIL");
35-
process.stdout.write("\n");
36-
}
37-
}
38-
39-
onTestFinish(group, test) {
40-
if (test.pass) process.stdout.write("Test : " + group.name + " -> " + test.name + " ✔ PASS\n");
41-
else process.stdout.write("Test : " + group.name + " -> " + test.name + " ❌ FAIL\n");
42-
43-
if (!test.pass) {
44-
process.stdout.write("Actual : " + test.actual.message + "\n");
45-
process.stdout.write("Expected : " + test.expected.message + "\n");
46-
}
47-
48-
if (test.logs.length > 0) {
49-
test.logs.forEach((e, i) => {
50-
if (i > 0) process.stdout.write("\n");
51-
process.stdout.write("Log : " + e.value);
52-
});
53-
process.stdout.write("\n");
54-
}
55-
}
56-
onFinish(context) {
57-
const passed = context.testGroups.filter(e => !e.pass).length === 0;
58-
if (passed) process.stdout.write("Suite : ✔ PASS");
59-
else process.stdout.write("Suite : ❌ FAIL");
60-
process.stdout.write("\n");
61-
}
62-
}
63-
64-
const reporter = new Reporter();
30+
const reporter = new VerboseReporter();
6531

6632
function relativeFromCwd(location) {
6733
return path.relative(process.cwd(), location);
@@ -160,4 +126,8 @@ function runTest(file, type, binary, wat) {
160126
wasi.setMemory(wasm.memory);
161127
wasi.view = new DataView(wasm.memory.buffer);
162128
context.run(wasm);
129+
130+
if (!context.pass) pass = false;
163131
}
132+
133+
process.exit(pass ? 0 : 1);

0 commit comments

Comments
 (0)