Skip to content

Commit

Permalink
test: add some test
Browse files Browse the repository at this point in the history
  • Loading branch information
richerfu committed Oct 31, 2024
1 parent 808fb47 commit 42187ff
Show file tree
Hide file tree
Showing 12 changed files with 516 additions and 4 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,19 @@ find_package(node_addon_api_ohos REQUIRED)
target_link_libraries(entry PRIVATE node_addon_api_ohos)
```

## Environment

For node-addon-api-ohos, we acccepted the following environment variables:

| Name | Description | Default Value |
| --------------------------- | ----------------------------------- | ------------- |
| NAPI_CPP_EXCEPTIONS | Allow catch the cpp exception | true |
| NAPI_DISABLE_CPP_EXCEPTIONS | Disable catch the cpp exception | false |
| NAPI_NORMAL | Hidden some APIs or cases for Harmony | true |

## Build

You can release package locally. Just run command:
You can release package locally. Just run command:

```shell
bash ./scripts/release.sh
Expand All @@ -39,4 +49,4 @@ It will generate a `.har` package in current path. You can import it with `file`

## Community

- [ohos-rs](https://github.com/ohos-rs/ohos-rs)
- [ohos-rs](https://github.com/ohos-rs/ohos-rs)
157 changes: 157 additions & 0 deletions ohos_addon_example/entry/src/ohosTest/ets/test/Buffer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import { describe, it, DEFAULT } from './util/framework.test';
import binding from './util/binding';
import assert from './util/assert.test';
import { runGCTests } from './util/gc.test';
import { buffer } from '@kit.ArkTS';

declare class ArkTools {
static hintGC: () => void
}

export default function testCase() {
describe("Buffer", () => {
it("RunBuffer", DEFAULT, () => {
return runGCTests([
'Internal Buffer',
() => {
const test = binding.buffer.createBuffer();
binding.buffer.checkBuffer(test);
assert.ok(test instanceof ArrayBuffer);

const test2 = buffer.alloc(test.length);
test.copy(test2.buffer);
binding.buffer.checkBuffer(test2.buffer);
},

'Buffer copy',
() => {
const test = binding.buffer.createBufferCopy();
binding.buffer.checkBuffer(test);
assert.ok(test instanceof ArrayBuffer);
},

'External Buffer',
() => {
const test = binding.buffer.createExternalBuffer();
binding.buffer.checkBuffer(test);
assert.ok(test instanceof ArrayBuffer);
assert.strictEqual(0, binding.buffer.getFinalizeCount());
},
() => {
ArkTools.hintGC();
assert.strictEqual(0, binding.buffer.getFinalizeCount());
},

'External Buffer with finalizer',
() => {
const test = binding.buffer.createExternalBufferWithFinalize();
binding.buffer.checkBuffer(test);
assert.ok(test instanceof ArrayBuffer);
assert.strictEqual(0, binding.buffer.getFinalizeCount());
},
() => {
ArkTools.hintGC();
},
() => {
assert.strictEqual(1, binding.buffer.getFinalizeCount());
},

'External Buffer with finalizer hint',
() => {
const test = binding.buffer.createExternalBufferWithFinalizeHint();
binding.buffer.checkBuffer(test);
assert.ok(test instanceof ArrayBuffer);
assert.strictEqual(0, binding.buffer.getFinalizeCount());
},
() => {
ArkTools.hintGC();
},
() => {
assert.strictEqual(1, binding.buffer.getFinalizeCount());
},

'Create or Copy External Buffer',
() => {
const test = binding.buffer.createOrCopyExternalBuffer();
binding.buffer.checkBuffer(test);
assert.ok(test instanceof ArrayBuffer);
assert.strictEqual(0, binding.buffer.getFinalizeCount());
},
() => {
ArkTools.hintGC();
assert.strictEqual(0, binding.buffer.getFinalizeCount());
},

'Create or Copy External Buffer with finalizer',
() => {
const test = binding.buffer.createOrCopyExternalBufferWithFinalize();
binding.buffer.checkBuffer(test);
assert.ok(test instanceof ArrayBuffer);
assert.strictEqual(0, binding.buffer.getFinalizeCount());
},
() => {
ArkTools.hintGC();
},
() => {
assert.strictEqual(1, binding.buffer.getFinalizeCount());
},

'Create or Copy External Buffer with finalizer hint',
() => {
const test = binding.buffer.createOrCopyExternalBufferWithFinalizeHint();
binding.buffer.checkBuffer(test);
assert.ok(test instanceof ArrayBuffer);
assert.strictEqual(0, binding.buffer.getFinalizeCount());
},
() => {
ArkTools.hintGC();
},
() => {
assert.strictEqual(1, binding.buffer.getFinalizeCount());
},

'Create or Copy External Buffer when NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED defined',
() => {
const test = binding.bufferNoExternal.createOrCopyExternalBuffer();
binding.buffer.checkBuffer(test);
assert.ok(test instanceof ArrayBuffer);
assert.strictEqual(0, binding.buffer.getFinalizeCount());
},
() => {
ArkTools.hintGC();
assert.strictEqual(0, binding.buffer.getFinalizeCount());
},

'Create or Copy External Buffer with finalizer when NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED defined',
() => {
const test = binding.bufferNoExternal.createOrCopyExternalBufferWithFinalize();
binding.buffer.checkBuffer(test);
assert.ok(test instanceof ArrayBuffer);
// finalizer should have been called when the buffer was created.
assert.strictEqual(1, binding.buffer.getFinalizeCount());
},
() => {
ArkTools.hintGC();
},
() => {
assert.strictEqual(1, binding.buffer.getFinalizeCount());
},

'Create or Copy External Buffer with finalizer hint when NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED defined',
() => {
const test = binding.bufferNoExternal.createOrCopyExternalBufferWithFinalizeHint();
binding.buffer.checkBuffer(test);
assert.ok(test instanceof ArrayBuffer);
// finalizer should have been called when the buffer was created.
assert.strictEqual(1, binding.buffer.getFinalizeCount());
},
() => {
ArkTools.hintGC();
},
() => {
assert.strictEqual(1, binding.buffer.getFinalizeCount());
}
]);
})
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { describe, it, DEFAULT } from '../util/framework.test';
import binding from '../util/binding';
import assert from '../util/assert.test';

export default function testCase() {
describe("DataView", () => {
it("RunDataView", DEFAULT, () => {
function testDataViewCreation (factory, arrayBuffer, offset?, length?) {
const view = factory(arrayBuffer, offset, length);
offset = offset || 0;
assert.ok(dataview.getArrayBuffer(view) instanceof ArrayBuffer);
assert.strictEqual(dataview.getArrayBuffer(view), arrayBuffer);
assert.strictEqual(dataview.getByteOffset(view), offset);
assert.strictEqual(dataview.getByteLength(view),
length || arrayBuffer.byteLength - offset);
}

function testInvalidRange (factory, arrayBuffer, offset, length) {
assert.throws(() => {
factory(arrayBuffer, offset, length);
}, RangeError);
}

const dataview = binding.dataview;
const arrayBuffer = new ArrayBuffer(10);

testDataViewCreation(dataview.createDataView1, arrayBuffer);
testDataViewCreation(dataview.createDataView2, arrayBuffer, 2);
testDataViewCreation(dataview.createDataView2, arrayBuffer, 10);
testDataViewCreation(dataview.createDataView3, arrayBuffer, 2, 4);
testDataViewCreation(dataview.createDataView3, arrayBuffer, 10, 0);

// @ts-ignore
testInvalidRange(dataview.createDataView2, arrayBuffer, 11);
testInvalidRange(dataview.createDataView3, arrayBuffer, 11, 0);
testInvalidRange(dataview.createDataView3, arrayBuffer, 6, 5);
})
})
}
2 changes: 1 addition & 1 deletion ohos_addon_example/entry/src/ohosTest/ets/test/Example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describe, it, DEFAULT } from '../util/framework.test';
import binding from '../util/binding';
import assert from '../util/assert.test';

export default function basicTypeArray() {
export default function testCase() {
describe("basic_type_array_test", () => {
it("RunArray", DEFAULT, () => {
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { describe, it, DEFAULT } from '../util/framework.test';
import binding from '../util/binding';
import assert from '../util/assert.test';

export default function testCase() {
describe("GlobalObjectDeleteProperty", () => {
it("RunGlobalObjectDeleteProperty", DEFAULT, () => {
const KEY_TYPE = {
C_STR: 'KEY_AS_C_STRING',
CPP_STR: 'KEY_AS_CPP_STRING',
NAPI: 'KEY_AS_NAPI_VALUES',
INT_32: 'KEY_AS_INT_32_NUM'
};

function assertNotGlobalObjectHasNoProperty (key, keyType) {
switch (keyType) {
case KEY_TYPE.NAPI:
assert.notStrictEqual(binding.globalObject.hasPropertyWithNapiValue(key), true);
break;

case KEY_TYPE.C_STR:
assert.notStrictEqual(binding.globalObject.hasPropertyWithCStyleString(key), true);
break;

case KEY_TYPE.CPP_STR:
assert.notStrictEqual(binding.globalObject.hasPropertyWithCppStyleString(key), true);
break;

case KEY_TYPE.INT_32:
assert.notStrictEqual(binding.globalObject.hasPropertyWithInt32(key), true);
break;
}
}

function assertErrMessageIsThrown (propertyCheckExistenceFunction, errMsg) {
assert.throws(() => {
propertyCheckExistenceFunction(undefined);
}, errMsg);
}

binding.globalObject.createMockTestObject();

binding.globalObject.deletePropertyWithCStyleString('c_str_key');
binding.globalObject.deletePropertyWithCppStyleString('cpp_string_key');
binding.globalObject.deletePropertyWithCppStyleString('circular');
binding.globalObject.deletePropertyWithInt32(15);
binding.globalObject.deletePropertyWithNapiValue('2');

assertNotGlobalObjectHasNoProperty('c_str_key', KEY_TYPE.C_STR);
assertNotGlobalObjectHasNoProperty('cpp_string_key', KEY_TYPE.CPP_STR);
assertNotGlobalObjectHasNoProperty('circular', KEY_TYPE.CPP_STR);
assertNotGlobalObjectHasNoProperty(15, true);
assertNotGlobalObjectHasNoProperty('2', KEY_TYPE.NAPI);

assertErrMessageIsThrown(binding.globalObject.hasPropertyWithCppStyleString, 'Error: A string was expected');
assertErrMessageIsThrown(binding.globalObject.hasPropertyWithCStyleString, 'Error: A string was expected');
assertErrMessageIsThrown(binding.globalObject.hasPropertyWithInt32, 'Error: A number was expected');
})
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { describe, it, DEFAULT } from '../util/framework.test';
import binding from '../util/binding';
import assert from '../util/assert.test';

export default function testCase() {
describe("GlobalObjectGetProperty", () => {
it("RunGlobalObjectGetProperty", DEFAULT, () => {
const KEY_TYPE = {
C_STR: 'KEY_AS_C_STRING',
CPP_STR: 'KEY_AS_CPP_STRING',
NAPI: 'KEY_AS_NAPI_VALUES',
INT_32: 'KEY_AS_INT_32_NUM'
};

binding.globalObject.createMockTestObject();
function assertGlobalObjectPropertyIs (key, attribute, keyType) {
let napiObjectAttr;
switch (keyType) {
case KEY_TYPE.NAPI:
napiObjectAttr = binding.globalObject.getPropertyWithNapiValue(key);
assert.deepStrictEqual(attribute, napiObjectAttr);
break;

case KEY_TYPE.C_STR:
napiObjectAttr = binding.globalObject.getPropertyWithCString(key);
assert.deepStrictEqual(attribute, napiObjectAttr);
break;

case KEY_TYPE.CPP_STR:
napiObjectAttr = binding.globalObject.getPropertyWithCppString(key);
assert.deepStrictEqual(attribute, napiObjectAttr);
break;

case KEY_TYPE.INT_32:
napiObjectAttr = binding.globalObject.getPropertyWithInt32(key);
assert.deepStrictEqual(attribute, napiObjectAttr);
break;
}
}

function assertErrMessageIsThrown (propertyFetchFunction, errMsg) {
assert.throws(() => {
propertyFetchFunction(undefined);
}, errMsg);
}

// @ts-ignore
assertGlobalObjectPropertyIs('2', global['2'], KEY_TYPE.NAPI);
// @ts-ignore
assertGlobalObjectPropertyIs('c_str_key', global.c_str_key, KEY_TYPE.C_STR);
// @ts-ignore
assertGlobalObjectPropertyIs('cpp_string_key', global.cpp_string_key, KEY_TYPE.CPP_STR);
// @ts-ignore
assertGlobalObjectPropertyIs('circular', global.circular, KEY_TYPE.CPP_STR);
// @ts-ignore
assertGlobalObjectPropertyIs(15, global['15'], KEY_TYPE.INT_32);

assertErrMessageIsThrown(binding.globalObject.getPropertyWithCString, 'Error: A string was expected');
assertErrMessageIsThrown(binding.globalObject.getPropertyWithCppString, 'Error: A string was expected');
assertErrMessageIsThrown(binding.globalObject.getPropertyWithInt32, 'Error: A number was expected');
})
})
}
Loading

0 comments on commit 42187ff

Please sign in to comment.